0% encontró este documento útil (0 votos)
17 vistas11 páginas

Practica 8

Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Está en la página 1/ 11

Universidad Nacional de Trujillo

CURSO:
ALGORITMOS Y PROGRAMACIÓN

PRÁCTICA SEMANA 8

ESTRUCTURAS SECUENCIALES,
CONDICIONALES Y REPETITIVAS

NOMBRES Y APELLIDOS: Ryan Alessandro Lazaro Urtecho


Función gotoxy()
La función gotoxy() se puede utilizar en entornos específicos, como Dev-C++, para realizar
posicionamiento de cursor en la pantalla.

Ejemplo de cómo implementar una función gotoxy() en Dev-C++ utilizando bibliotecas de


Windows:

#include<iostream>
#include<conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x,int y)
{ HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos); }

int main ()
{gotoxy (0,0);
cout<<"Hola";
getch();}

Práctica 1:
Escribir textos en diferentes posiciones de la pantalla

#include<iostream>
#include<conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x,int y)
{ HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos);
int main ()
{gotoxy (15,10);
cout<<"Ahora estoy en otro lugar";
getch();}
Práctica 2:
Utilizando la estructura for realizar un cuadrado.

Primero escribir las esquinas y luego las líneas verticales y horizontales.

#include<conio.h>
#include<iostream>
#include<windows.h>
using namespace std;

void gotoxy(int x,int y)


{ HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos); }

int main()
{ system ("color E1");
int x=10, y=5, x1=70, y1=13;

char esq1=201,esq2=187,esq3=200,esq4=188,v=186,h=205;
int i,j;
gotoxy(x,y); cout<<esq1;
gotoxy(x1,y); cout<<esq2;
gotoxy(x,y1); cout<<esq3;
gotoxy(x1,y1); cout<<esq4;

for(i=x+1;i<x1;i++)
{gotoxy(i,y); cout<<h; }

for(j=y+1;j<y1;j++)
{gotoxy(x,j); cout<<v;}

for(j=y+1;j<y1;j++)
{gotoxy(x1,j); cout<<v;}

for(i=x+1;i<x1;i++)
{gotoxy(i,y1); cout<<h; }

getch();
}
#include<conio.h>
#include<iostream>
#include<windows.h>
using namespace std;

void gotoxy(int x,int y)


{ HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos); }

int main()
{ system ("color E1");
int x=10, y=5, x1=40, y1=20;

char esq1=201,esq2=187,esq3=200,esq4=188,v=186,h=205;
int i,j;
gotoxy(x,y); cout<<esq1;
gotoxy(x1,y); cout<<esq2;
gotoxy(x,y1); cout<<esq3;
gotoxy(x1,y1); cout<<esq4;

for(i=x+1;i<x1;i++)
{gotoxy(i,y); cout<<h; }

for(j=y+1;j<y1;j++)
{gotoxy(x,j); cout<<v;}

for(j=y+1;j<y1;j++)
{gotoxy(x1,j); cout<<v;}

for(i=x+1;i<x1;i++)
{gotoxy(i,y1); cout<<h; }

getch();
}
Práctica 3:
Elaborar una pantalla con recuadros

JUEGO YAN KEN PO

PARTICIPANTES
Participante A
Participante B Ganador:

#include<conio.h> for(i=x+1;i<x1;i++)
#include<iostream> {gotoxy(i,y); cout<<h; }
#include<windows.h>

using namespace std; for(j=y+1;j<y1;j++)


void gotoxy(int x,int y) {gotoxy(x,j); cout<<v;}
{ HANDLE hcon;

hcon = GetStdHandle(STD_OUTPUT_HANDLE); for(j=y+1;j<y1;j++)


COORD dwPos; {gotoxy(x1,j); cout<<v;}
dwPos.X = x;

dwPos.Y= y; for(i=x+1;i<x1;i++)
SetConsoleCursorPosition(hcon,dwPos); } {gotoxy(i,y1); cout<<h; }
int main()

{ system ("color B5"); x=30; y=6; x1=55; y1=10;


int x=10, y=5, x1=80, y1=20; gotoxy(x,y); cout<<esq1;
char gotoxy(x1,y); cout<<esq2;
esq1=201,esq2=187,esq3=200,esq4=188,v=186,h=205;
gotoxy(x,y1); cout<<esq3;
int i,j;
gotoxy(x1,y1); cout<<esq4;
gotoxy(x,y); cout<<esq1;

gotoxy(x1,y); cout<<esq2;

gotoxy(x,y1); cout<<esq3;

gotoxy(x1,y1); cout<<esq4;
for(i=x+1;i<x1;i++)

{gotoxy(i,y); cout<<h; } gotoxy(23,13);

cout<<"PARTICIPANTES";

for(j=y+1;j<y1;j++) gotoxy(15,15);

{gotoxy(x,j); cout<<v;} cout<<"Participante A";

gotoxy(15,16);

for(j=y+1;j<y1;j++) cout<<"Participante B";

{gotoxy(x1,j); cout<<v;}

x=60; y=12; x1=75; y1=18;

for(i=x+1;i<x1;i++) gotoxy(x,y); cout<<esq1;

{gotoxy(i,y1); cout<<h; } gotoxy(x1,y); cout<<esq2;

gotoxy(x,y1); cout<<esq3;

gotoxy(35,8); gotoxy(x1,y1); cout<<esq4;

cout<<"JUEGO YAN KEN PO";

for(i=x+1;i<x1;i++)

x=12; y=12; x1=45; y1=18; {gotoxy(i,y); cout<<h; }

gotoxy(x,y); cout<<esq1;

gotoxy(x1,y); cout<<esq2; for(j=y+1;j<y1;j++)

gotoxy(x,y1); cout<<esq3; {gotoxy(x,j); cout<<v;}

gotoxy(x1,y1); cout<<esq4;

for(j=y+1;j<y1;j++)

for(i=x+1;i<x1;i++) {gotoxy(x1,j); cout<<v;}

{gotoxy(i,y); cout<<h; }

for(i=x+1;i<x1;i++)

for(j=y+1;j<y1;j++) {gotoxy(i,y1); cout<<h; }

{gotoxy(x,j); cout<<v;}

gotoxy(64,13);

for(j=y+1;j<y1;j++) cout<<"Ganador: ";

{gotoxy(x1,j); cout<<v;}

getch();

for(i=x+1;i<x1;i++) }

{gotoxy(i,y1); cout<<h; }
Práctica 4:
Elaborar un programa con dibujos animados utilizando for

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

void gotoxy(int x,int y)


{ HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos); }

int main(){
int mov = 55;
for(int pos = 0; pos < mov; pos++){
system("cls");
gotoxy(60-pos,5); cout<<" .----.";
gotoxy(60-pos,6); cout<<" _.'__ `.";
gotoxy(60-pos,7); cout<<" .--(#)(##)---/#\ ";
gotoxy(60-pos,8); cout<<" .' @ /###\ ";
gotoxy(60-pos,9); cout<<" : , ##### ";
gotoxy(60-pos,10);cout<<" `-..__.-' _.-\###/ ";
gotoxy(60-pos,11);cout<<" `;_: `- ";
gotoxy(60-pos,12); cout<<" .'°°°°°°' " ;
gotoxy(60-pos,13); cout<<" /, JOE ,\ ";
gotoxy(60-pos,14); cout<<" `-._______.-' ";
gotoxy(60-pos,15); cout<<" // COOL! \\";
gotoxy(60-pos,16); cout<<" ___`. | .'__ ";
gotoxy(60-pos,17); cout<<" (______|______) ";
}
getch(); }
Práctica 5:
Modifica el programar para que snoopy se mueva de izquierda a derecha

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x,int y)
{ HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos); }
int main(){
int mov = 55;
for(int pos = 0; pos < mov; pos++){
system("cls");
gotoxy(60-pos,5); cout<<" .----.";
gotoxy(60-pos,6); cout<<" _.'__ `.";
gotoxy(60-pos,7); cout<<" .--(#)(##)---/#\ ";
gotoxy(60-pos,8); cout<<" .' @ /###\ ";
gotoxy(60-pos,9); cout<<" : , ##### ";
gotoxy(60-pos,10);cout<<" `-..__.-' _.-\###/ ";
gotoxy(60-pos,11);cout<<" `;_: `- ";
gotoxy(60-pos,12); cout<<" .'°°°°°°' " ;
gotoxy(60-pos,13); cout<<" /, JOE ,\ ";
gotoxy(60-pos,14); cout<<" `-._______.-' ";
gotoxy(60-pos,15); cout<<" // COOL! \\";
gotoxy(60-pos,16); cout<<" ___`. | .'__ ";
gotoxy(60-pos,17); cout<<" (______|______) "; }
int mov1=55;
for(int pos = 0; pos < mov; pos++){
system("cls");
gotoxy(6+pos,5); cout<<" .----.";
gotoxy(6+pos,6); cout<<" _.'__ `.";
gotoxy(6+pos,7); cout<<" .--(#)(##)---/#\ ";
gotoxy(6+pos,8); cout<<" .' @ /###\ ";
gotoxy(6+pos,9); cout<<" : , ##### ";
gotoxy(6+pos,10);cout<<" `-..__.-' _.-\###/ ";
gotoxy(6+pos,11);cout<<" `;_: `- ";
gotoxy(6+pos,12); cout<<" .'°°°°°°' " ;
gotoxy(6+pos,13); cout<<" /, JOE ,\ ";

gotoxy(6+pos,14); cout<<" `-._______.-' ";

gotoxy(6+pos,15); cout<<" // COOL! \\";

gotoxy(6+pos,16); cout<<" ___`. | .'__ ";

gotoxy(6+pos,17); cout<<" (______|______) ";

getch(); }
Práctica 6:
Elaborar un programa con otro dibujo animado
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x,int y)
{ HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos); }
int main(){
int mov = 55;
for(int pos = 0; pos < mov; pos++){
system("cls");
gotoxy(60-pos,5); cout<<" _.oo.";
gotoxy(60-pos,6); cout<<" _. u[[/;:,. .odMMMMMM'";
gotoxy(60-pos,7); cout<<" .o888UU[[[/;:-. .o@P^ MMM^";
gotoxy(60-pos,8); cout<<" oN88888UU[[[/;::-. dP^";
gotoxy(60-pos,9); cout<<" dNMMNN888UU[[[/;:--. .o@P^";
gotoxy(60-pos,10);cout<<" ,MMMMMMN888UU[[/;::-. o@^";
gotoxy(60-pos,11);cout<<" NNMMMNN888[[[/~.o@P^";
gotoxy(60-pos,12); cout<<" 888888888UU [[[/o@^-..";
gotoxy(60-pos,13); cout<<" 888888888UU [[[/o@^-..";
gotoxy(60-pos,14); cout<<" oI8888UU[[[/o@P^:--..";
gotoxy(60-pos,15); cout<<" oMP ^/o@P^;:::---..";
gotoxy(60-pos,16); cout<<" .dMMM .o@^ ^;::---...";
gotoxy(60-pos,17); cout<<" dMMMMMMM@^` `^^^^";
gotoxy(60-pos,18); cout<<"YMMMUP^";
gotoxy(60-pos,19); cout<<" ^^";
}
int mov1 = 55;
for(int pos = 0; pos < mov1; pos++){
system("cls");
gotoxy(6+pos,5); cout<<" _.oo.";
gotoxy(6+pos,6); cout<<" _. u[[/;:,. .odMMMMMM'";
gotoxy(6+pos,7); cout<<" .o888UU[[[/;:-. .o@P^ MMM^";
gotoxy(6+pos,8); cout<<" oN88888UU[[[/;::-. dP^";
gotoxy(6+pos,9); cout<<" dNMMNN888UU[[[/;:--. .o@P^";
gotoxy(6+pos,10);cout<<" ,MMMMMMN888UU[[/;::-. o@^";
gotoxy(6+pos,11);cout<<" NNMMMNN888[[[/~.o@P^";
gotoxy(6+pos,12); cout<<" 888888888UU [[[/o@^-..";
gotoxy(6+pos,13); cout<<" 888888888UU [[[/o@^-..";
gotoxy(6+pos,14); cout<<" oI8888UU[[[/o@P^:--..";
gotoxy(6+pos,15); cout<<" oMP ^/o@P^;:::---..";
gotoxy(6+pos,16); cout<<" .dMMM .o@^ ^;::---...";
gotoxy(6+pos,17); cout<<" dMMMMMMM@^` `^^^^";
gotoxy(6+pos,18); cout<<"YMMMUP^";
gotoxy(6+pos,19); cout<<" ^^";

}
getch(); }
Práctica 7: Elaborar un programa del juego YAN KEN PO. Estudiar e investigar las funciones
nuevas
#include<iostream>
#include<conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x,int y)
{ HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos); }
int main ()
{cout<<"ROCA: r/R PAPEL: p/P TIJERA: t/T"<<endl;
int c=0,a=0,b=0;
char x, y;
while(1)
{c++;
gotoxy(10,6);cout<<" ";
gotoxy(10,8);cout<<" ";
gotoxy (5,5); cout<<"Jugada "<<c;
gotoxy(10,6);cout<<"Participante A: ";
x = getch(); cout << "*";
gotoxy(10,8);cout<<"Participante B: ";
y = getch(); cout << "*";
if(x=='T'||x=='t')
switch (y)
{case 'P':
case 'p': a++; break;
case 'R':
case 'r': b++; break;}
if(x=='P'||x=='p')
switch (y)
{case 'T':
case 't': b++; break;
case 'R':
case 'r': a++; break;}
if(x=='R'||x=='r')
switch (y)
{case 'P':
case 'p': b++; break;
case 'T':
case 't': a++; break;}

gotoxy (40,2);cout<<"Puntos JUGADOR A: "<<a;


gotoxy (40,3); cout<<"Puntos JUGADOR B: "<<b;
gotoxy (5,12); cout<<"Presione una tecla para continuar siguiente jugada"<<endl; getch();

if(a==3)
{ gotoxy (15,14);cout<<"GANADOR JUGADOR A";
gotoxy (15,15);cout<<"Fueron "<<c<<" jugadas";
break;}
if(b==3)
{ gotoxy (15,14);cout<<"GANADOR JUGADOR B";
gotoxy (15,15);cout<<"Fueron "<<c<<" jugadas";
break;}
}
getch(); }
Práctica 8:
Modificar el programa del juego YAN KEN PO para que el jugador B sea la máquina.
#include<iostream> if (x == 'P' || x == 'p')
#include<conio.h> switch (y)
#include<windows.h> {
#include<ctime> case 'T':
using namespace std; case 't':
void gotoxy(int x, int y) b++;
{ break;
HANDLE hcon; case 'R':
hcon = GetStdHandle(STD_OUTPUT_HANDLE); case 'r':
COORD dwPos; a++;
dwPos.X = x; break; }
dwPos.Y = y; if (x == 'R' || x == 'r')
SetConsoleCursorPosition(hcon, dwPos); switch (y)
} {
int main() case 'P':
{ case 'p':
cout << "ROCA: r/R PAPEL: p/P TIJERA: t/T" << endl; b++;
int c = 0, a = 0, b = 0; break;
char x, y; case 'T':
srand(time(NULL)); case 't':
while (1) a++;
{ break;
c++; }
gotoxy(10, 6);
cout << " "; gotoxy(40, 2);
gotoxy(10, 8); cout << "Puntos JUGADOR A: " << a;
cout << " "; gotoxy(40, 3);
gotoxy(5, 5); cout << "Puntos JUGADOR B: " << b;
cout << "Jugada " << c; gotoxy(5, 12);
gotoxy(10, 6); cout << "Presione una tecla para continuar siguiente jugada"
cout << "Participante A: "; << endl;
x = getch(); getch();
cout << "*";
gotoxy(10, 8); if (a == 3)
cout << "Participante B: "; {
int num = rand() % 3;
gotoxy(15, 14);
if (num == 0)
cout << "GANADOR JUGADOR A";
y = 'R';
gotoxy(15, 15);
else if (num == 1)
cout << "Fueron " << c << " jugadas";
y = 'P';
else break;}
y = 'T'; if (b == 3)
cout << y; { gotoxy(15, 14);
cout << "GANADOR JUGADOR B";
if (x == 'T' || x == 't')
switch (y) gotoxy(15, 15);
{
case 'P': cout << "Fueron " << c << " jugadas";
case 'p':
break; }
a++;
break; }getch();
case 'R':
case 'r': return 0;
b++;
break; }
}

También podría gustarte