59
ADIVINA #include "stdafx.h" #include <stdlib.h> #include <time.h> #include <iostream> using std::cout; using std::cin; using std::endl; #include <math.h> using namespace std; int main() { srand(time(NULL)); int r,c,n,e; r= 1+rand() % 1000; do { cout <<"\nValor Pensado: "; cin>>n; if (n>r) cout << "Te pasaste. "; else if (n<r) cout << "Te falto"; c++; } while (n!=r); cout << "\nEl # generado es " << r; cout << "\nSe adivino en " << c<< " oportunidades"; cin >> e; return 0; } PROMEDIO #include "stdafx.h" #include <stdlib.h> #include <time.h> #include <iostream> using std::cout; using std::cin; using std::endl; #include <math.h> using namespace std; int main() { int n,i; float p,s,a[10]; cout <<"Cantidad de valores: "; cin>>n; for (i=1; i<=n; i++) {

Comandos C++.docx

Embed Size (px)

Citation preview

Page 1: Comandos C++.docx

ADIVINA#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

int main(){

srand(time(NULL));int r,c,n,e;r= 1+rand() % 1000;do {

cout <<"\nValor Pensado: ";cin>>n;if (n>r)

cout << "Te pasaste. ";else if (n<r)

cout << "Te falto";c++; } while (n!=r);cout << "\nEl # generado es " << r;cout << "\nSe adivino en " << c<< " oportunidades";cin >> e;return 0;

}PROMEDIO#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

int main(){

int n,i;float p,s,a[10];cout <<"Cantidad de valores: ";cin>>n;for (i=1; i<=n; i++) {

cout << "Dame el valor "<<i<< "; ";cin>> a[i];s += a[i]; }

p=s/n;cout << "El promedio es: " <<p;cin >> i;return 0;

}Mandar a Imprimir cual es tu edad:

Page 2: Comandos C++.docx

#include <iostream>using std::cout;using std::cin;using std::endl;

int main() {int edad;cout << "¿Cual es tu edad? " << endl;cin >> edad;cout << "Tienes " << edad << " anios" << endl;system("pause");return 1;

}

Mandar a Imprimir Hola Mundo

#include <iostream>using std::cout;using std::cin;

int main() {int pausa;cout <<"Hola Mundo!";cin >> pausa;}

Tirar dado (corregir)

#include <iostream>using std::cin;using std::cout;using std::endl;

#include <cstdlib>

#include <ctime>

int main() {srand((unsigned)time(0));int random;

random = (rand()%12)+1;if(random == 6) {

cout << "Ganaste! " << random << endl;} else if(random == 8) {

cout << "Ganaste! " << random <<endl;} else if(random == 10) {

cout << "Ganaste! " << random <<endl;} else {

cout << "LOOSER! " << random <<endl;}system("pause");return 1;

}

Page 3: Comandos C++.docx

Otra Opción del último ejerciciorandom = (rand()%12)+1;if(random == 6 || random ==8 || random || 10) {

cout << "Ganaste! " << random << endl;} else {

cout << "LOOSER! " << random <<endl;}system("pause");return 1;

}

Que un numero aleatorio del 0-100, te diga si es divisible entre 13,15,17 o no.

#include <iostream>using std::cin;using std::cout;using std::endl;

#include <cstdlib>

#include <ctime>

int main() {srand((unsigned)time(0));int random;

random = (rand()%1000);

if(random%13 == 0) {cout << "Es divisible entre 13, el numero es: " << random <<

endl;} else if(random%15 == 0) {

cout << "Es divisible entre 15, El numero es: " << random <<endl;

} else if(random%17 == 0) {cout << "Es divisible entre 17, El numero es: " << random

<<endl;} else {

cout << "No es divisible entre 13,15 o 17, el numero es: " << random <<endl;

}system("pause");return 1;

}

Page 4: Comandos C++.docx

Tragamonedas.

#include<iostream>using std::cin;using std::cout;using std::endl;

#include<cstdlib>

#include<ctime>

int main() {int valor1;int valor2;int valor3;

srand((unsigned)time(0));

valor1 = (rand() % 20);valor2 = (rand() % 20);valor3 = (rand() % 20);

if((valor1==valor2) && (valor2==valor3)) {cout << "JACKPOT! " << valor1 << " " << valor2 << " " <<

valor3 << endl;} else {

int mayor;int menor;int medio;if(valor1 > valor2) {

if(valor1 > valor3) {mayor = valor1;if(valor2 > valor3) {

medio = valor2;menor = valor3;

} else {medio = valor3;menor = valor2;

}} else {

mayor = valor3;medio = valor1;menor = valor2;

}} else if(valor1 > valor3) {

mayor = valor2;medio = valor1;menor = valor3;

} else {menor = valor1;if(valor2 > valor3) {

mayor = valor2;medio = valor3;

} else {mayor = valor3;medio = valor2;

Page 5: Comandos C++.docx

}}if ((mayor <= (medio + 3)) && (menor >= (medio - 3))) {

cout << "PREMIO! " << valor1 << " " << valor2 << " " << valor3 << endl;

} else if((mayor <= (medio + 5)) && (menor >= (medio - 5))) {cout << "OTRA OPORTUNIDAD! " << valor1 << " " << valor2

<< " " << valor3 << endl;} else {

cout << "QUE PENA!..." << valor1 << " " << valor2 << " " << valor3 << endl;

}}system("pause");

}

Tragamonedas Avanzado.

#include<iostream>using std::cin;using std::cout;using std::endl;

#include<cstdlib>

#include<ctime>

int main() {int valor1; //Primer valor de la maquinaint valor2; //Segundo valor de la maquinaint valor3; //Tercero valor de la maquinaint vRep; //Variable temporal para repetir cicloint veces; //Variable temporal para almacenar numero de

repeticiones

srand((unsigned)time(0));do {

cout << "Cuantas veces quieres jugar?" <<endl;cin >> veces;for(int i=0; i<veces; i++) {

valor1 = (rand() % 20);valor2 = (rand() % 20);valor3 = (rand() % 20);if((valor1==valor2) && (valor2==valor3)) {

cout << "JACKPOT! " << valor1 << " " << valor2 << " " << valor3 << endl;

} else {int mayor;int menor;int medio;if(valor1 > valor2) {

if(valor1 > valor3) {mayor = valor1;if(valor2 > valor3) {

medio = valor2;

Page 6: Comandos C++.docx

menor = valor3;} else {

medio = valor3;menor = valor2;

}} else {

mayor = valor3;medio = valor1;menor = valor2;

}} else if(valor1 > valor3) {

mayor = valor2;medio = valor1;menor = valor3;

} else {menor = valor1;if(valor2 > valor3) {

mayor = valor2;medio = valor3;

} else {mayor = valor3;medio = valor2;

}}if ((mayor <= (medio + 3)) && (menor >= (medio -

3))) {cout << "PREMIO! " << valor1 << " " <<

valor2 << " " << valor3 << endl;} else if((mayor <= (medio + 5)) && (menor >=

(medio - 5))) {cout << "OTRA OPORTUNIDAD! " << valor1 << "

" << valor2 << " " << valor3 << endl;} else {

cout << "QUE PENA!..." << valor1 << " " << valor2 << " " << valor3 << endl;

}}

}cout << "Quieres volver a jugar? \n(1)Si / (2)No" <<endl;cin >> vRep;

}while(vRep == 1);cout << "Gracias por jugar!" <<endl;system("pause");

}

Page 7: Comandos C++.docx

Programa para catálogo de MixUP.

#include<iostream>using std::cin;using std::cout;using std::endl;

#include<cstdlib>

#include<string>using std::string;

struct CatalogoCDs {    string titulo;    string artista;    float costo;    int anio;    string productora;};

CatalogoCDs catalogo[500];

int index = 0;                //Posicion del catalago disponible

int menuPrincipal();void capturaCD();void listaCDs();void listaCD(int);void buscaXTitulo(string);void buscaXArtista(string);

int main() {    int opcion = -1;    string searchStr;    do{        opcion = menuPrincipal();        switch(opcion) {            case 1: capturaCD(); break;            case 2: listaCDs(); break;            case 3:                cout << "Que titulo quieres? " << endl;                cin >> searchStr;                buscaXTitulo(searchStr);                break;            case 4:                cout << "Que artista quieres? " << endl;                cin >> searchStr;                buscaXArtista(searchStr);

Page 8: Comandos C++.docx

                break;        }    } while (opcion != 5);    cout << "Gracias por hacer uso del catalogo MixUP" << endl;    system("pause");    return 1;}

int menuPrincipal() {    int opcion = 0;    system("CLS");    do {        cout << "Sistema de control de catalogos MixUP" << endl;        cout << "Que quieres hacer? " << endl;        cout << " 1 - Capturar CD " << endl;        cout << " 2 - Listar CDs " << endl;        cout << " 3 - Buscar por titulo " << endl;        cout << " 4 - Buscar por artista " << endl;        cout << " 5 - Salir " << endl;

        cin >> opcion;        if(opcion < 1 || opcion > 5) {            cout << "Opcion invalida!" << endl;        }    } while(opcion < 1 || opcion > 5);    return opcion;}

void capturaCD() {    system("CLS");    string tempStr;    float tempFlt;    int tempInt;    CatalogoCDs tempCD;

    if(index < 10) {        tempStr = "";        cin.ignore(1000,'\n');        do {            cout << "Dame el titulo del CD: " ;            //cin >> tempStr;            getline(cin,tempStr);        } while(tempStr.compare("") == 0);

        tempCD.titulo = tempStr;

        tempStr = "";        do {

Page 9: Comandos C++.docx

            cout << "Dame el nombre del artista: ";            cin >> tempStr;        } while(tempStr.compare("") == 0);        tempCD.artista = tempStr;

        cout << "Cual es el costo del cd: ";        cin >> tempFlt;        tempCD.costo = tempFlt;

        cout << "En que año se publico: ";        cin >> tempInt;        tempCD.anio = tempInt;

        cin.ignore(1000,'\n');        tempStr = "";        do{            cout << "Que productora la publico: ";            //cin >> tempStr;            getline(cin,tempStr);        } while(tempStr.compare("") == 0);        tempCD.productora = tempStr;

        catalogo[index] = tempCD;        index++;    } else {        cout << "Catalogo lleno!";    }}

void listaCDs() {    system("CLS");

     int indice = -1;

    for (indice = 0; indice < 500; indice ++ )    {        if(catalogo[indice].titulo.compare("") != 0)        {            //imprimir los datos del cd los de catalogo[indice]            cout << " titulo: " << catalogo[indice].titulo << endl;            cout << " artista: " << catalogo[indice].artista << endl;            cout << " costo: " << catalogo[indice].costo << endl;            cout << " anio: " << catalogo[indice].anio << endl;            cout << " productora: " << catalogo[indice].productora << endl;            cout << endl;        }    }    system("PAUSE");

Page 10: Comandos C++.docx

}

void buscaXTitulo(string titulo) {    system("CLS");

     int indice = -1;

    for (indice = 0; indice < 500; indice ++ )    {        if(catalogo[indice].titulo.compare(titulo) == 0)        {            //imprimir los datos del cd los de catalogo[indice]            cout << " titulo: " << catalogo[indice].titulo << endl;            cout << " artista: " << catalogo[indice].artista << endl;            cout << " costo: " << catalogo[indice].costo << endl;            cout << " anio: " << catalogo[indice].anio << endl;            cout << " productora: " << catalogo[indice].productora << endl;            cout << endl;        }    }    system("PAUSE");}

void buscaXArtista(string artista) {    system("CLS");

    /*string titulo;    string artista;    float costo;    int anio;    string productora;*/

     int indice = -1;

    for (indice = 0; indice < 500; indice ++ )    {        if(catalogo[indice].artista.compare(artista) == 0)        {            //imprimir los datos del cd los de catalogo[indice]            cout << " titulo: " << catalogo[indice].titulo << endl;            cout << " artista: " << catalogo[indice].artista << endl;            cout << " costo: " << catalogo[indice].costo << endl;            cout << " anio: " << catalogo[indice].anio << endl;            cout << " productora: " << catalogo[indice].productora << endl;            cout << endl;        }    }

Page 11: Comandos C++.docx

    system("PAUSE");}

Catalogo Carros

#include<iostream>using std::cin;using std::cout;using std::endl;

#include<cstdlib>

#include<string>using std::string;

struct CatalogoCarros {string modelo;int anio;string fabricante;

};

CatalogoCarros catalogo[500];

int index = 0; //Posicion del catalago disponible

int menuPrincipal();void capturaCarros();void listaCarros();void listaCarro(int);

int main() {int opcion = -1;string searchStr;do{

opcion = menuPrincipal();switch(opcion) {

case 1: capturaCarros(); break;case 2: listaCarros(); break;

}} while (opcion != 3);cout << "Gracias por hacer uso del catalogo de Carros" << endl;system("pause");return 1;

}

int menuPrincipal() {int opcion = 0;system("CLS");do {

cout << "Catalogos de Carro" << endl;cout << "Que quieres hacer? " << endl;cout << " 1 - Capturar Carros " << endl;cout << " 2 - Listar Carros" << endl;cout << " 3 - Salir " << endl;

Page 12: Comandos C++.docx

cin >> opcion;if(opcion < 1 || opcion > 3) {

cout << "Opcion invalida!" << endl;}

} while(opcion < 1 || opcion > 3);return opcion;

}

void capturaCarros() {system("CLS");string tempStr;float tempFlt;int tempInt;CatalogoCarros tempCarro;

if(index < 10) {tempStr = "";do {

cout << "Dame el modelo del carro: ";cin >> tempStr;

} while(tempStr.compare("") == 0);tempCarro.modelo = tempStr;

cout << "De que anio es el carro: ";cin >> tempInt;tempCarro.anio = tempInt;

cin.ignore(1000,'\n');tempStr = "";do{

cout << "Cual es el fabricante del carro: ";//cin >> tempStr;getline(cin,tempStr);

} while(tempStr.compare("") == 0);tempCarro.fabricante = tempStr;

catalogo[index] = tempCarro;index++;

} else {cout << "Catalogo lleno!";

}}

void listaCarros() {system("CLS");

int indice = -1;

for (indice = 0; indice < 500; indice ++ ){

if(catalogo[indice].modelo.compare("") != 0){

//imprimir los datos del cd los de catalogo[indice]cout << " modelo: " << catalogo[indice].modelo << endl;cout << " anio: " << catalogo[indice].anio << endl;

Page 13: Comandos C++.docx

cout << " fabricante: " << catalogo[indice].fabricante << endl;

cout << endl;}

}system("PAUSE");

}

SUMA DE 5 NUMEROS

#include<iostream.h>main(){

int suma=0;int cantidad;int num, i;

cout<<"Ingrese cantidad de numeros que desea sumar: ";cin>>cantidad;for(i=1;i<=cantidad;i++){cout<<"Ingrese numero "<<i<<":";cin>>num;suma=suma+num;}cout<<"la suma es:"<<suma;}

Profe asi me quedo el codigo del programa de suma de numeros, la verdad no supe que modificarle pero si tuve muchos errores, espero me lo pueda corregir para estudiar antes del examen, muchisimas gracias!!

#include<iostream>

Page 14: Comandos C++.docx

using std::cin;using std::cout;using std::endl;

#include<cstdlib>

#include<string>using std::string;

struct CatalogoNumeros { int num1; int num2; int num3; int num4; int num5; };

CatalogoNumeros catalogo[500];

int index = 0; //Posicion del catalago disponible

int menuPrincipal();void capturaNumeros(int);

int main() { int opcion = -1; string searchStr; do{ opcion = menuPrincipal(); switch(opcion) { case 1: capturaNumeros(); break; } } while (opcion != 2); cout << "Gracias por hacer uso del programa" << endl; system("pause"); return 1;}

int menuPrincipal() { int opcion = 0; system("CLS"); do {

Page 15: Comandos C++.docx

cout << "Programa de Suma de Numeros" << endl; cout << "Que quieres hacer? " << endl; cout << " 1 - Capturar Numeros " << endl; cout << " 2 - Salir " << endl;

cin >> opcion; if(opcion < 1 || opcion > 3) { cout << "Opcion invalida!" << endl; } } while(opcion < 1 || opcion > 3); return opcion;}

void capturaNumeros() { system("CLS"); int tempInt; CatalogoNumeros tempNumero;

if(index < 10) { cout << "Cual es tu primer numero "; cin >> tempInt; tempNumero.num1 = tempInt;

cin.ignore(1000,'\n'); tempInt = ""; catalogo[index] = tempNumero; index++; do{ cout << "Cual es tu segundo numero "; cin >> tempInt; tempNumero.num2 = tempInt;

cin.ignore(1000,'\n'); tempInt = ""; catalogo[index] = tempNumero; index++; do{ cout << "Cual es tu tercer numero "; cin >> tempInt; tempNumero.num3 = tempInt;

cin.ignore(1000,'\n');

Page 16: Comandos C++.docx

tempInt = ""; catalogo[index] = tempNumero; index++; do{ cout << "Cual es tu cuarto numero "; cin >> tempInt; tempNumero.num4 = tempInt;

cin.ignore(1000,'\n'); tempInt = ""; catalogo[index] = tempNumero; index++; do{ cout << "Cual es tu quinto numero "; cin >> tempInt; tempNumero.num5 = tempInt;

cin.ignore(1000,'\n'); tempInt = ""; catalogo[index] = tempNumero; index++; } else { cout << "Catalogo lleno!"; } for(int i=0; i<n;i++) //Dame el numero cin >> numeros[i] int cont = 0for(int i=0;i<n;i++) cont += numeros[i]} system("PAUSE");}

int numeros[n];

#include<iostream>using std::cin;using std::cout;using std::endl;

#include<cstdlib>

Page 17: Comandos C++.docx

#include<string>using std::string;

struct CatalogoCalificaciones {string nombrealumno;int calcalculo;int calprogramacion;int caletica;int calfisica;int calintalaing;int promedio;

};

CatalogoCalificaciones catalogo [20];

int index = 0; //Posicion del catalago disponible

int menuPrincipal();void capturanombrealumno();void listanombrealumnos();

int main() {int opcion = -1;string searchStr;do{

opcion = menuPrincipal();switch(opcion) {

case 1: capturanombrealumno(); break;case 2: listanomrealumnos(); break;

}} while (opcion != 3);cout << "Gracias por usar el programa" << endl;system("pause");return 1;

}

int menuPrincipal() {int opcion = 0;system("CLS");do {

cout << "Catalogos de Calificaciones" << endl;cout << "Que quieres hacer? " << endl;cout << " 1 - Capturar Calificaciones " << endl;cout << " 2 - Listar Calificaciones" << endl;cout << " 3 - Salir " << endl;

cin >> opcion;if(opcion < 1 || opcion > 3) {

cout << "Opcion invalida!" << endl;}

} while(opcion < 1 || opcion > 3);return opcion;

}

void capturanombrealumno() { system("CLS");

Page 18: Comandos C++.docx

string tempStr;int tempInt;CatalogoCalificaciones tempnombrealumno;

if(index < 10) {tempStr = "";cin.ignore(1000,'\n');do {

cout << "Ingrese nombre del alumno: " ;//cin >> tempStr;getline(cin,tempStr);

} while(tempStr.compare("") == 0);

tempnombrealumno.nombre = tempStr;

tempStr = "";

cout << "Cual fue su calificacion en Calculo?: ";cin >> tempInt;tempnombrealumno.calcalculo = tempInt;

cout << "Cual fue su calificacion en Programacion?: ";cin >> tempInt;tempnombrealumno.calprogramacion = tempInt;

cout << "Cual fue su calificacion en Etica?: ";cin >> tempInt;tempnombrealumno.caletica = tempInt;

cout << "Cual fue su calificacion en Fisica?: ";cin >> tempInt;tempnombrealumno.calfisica = tempInt;

cout << "Cual fue su calificacion en Introduccion a la Ingenieria?: ";

cin >> tempInt;tempnombrealumno.calintalaing = tempInt;

listado[index] = tempnombrealumno;index++;

} else {cout << "Ya no se pueden mas”;

}}

void listanombrealumnos() {system("CLS");

int indice = -1;

for (indice = 0; indice < 20; indice ++ ) { if(listado[indice].nombrealumno.compare("") != 0) { cout << " Nombre: " << listado[indice].calnombre << endl; cout << " Calculo: " << listado[indice].calcalculo << endl;

Page 19: Comandos C++.docx

cout << " Programacion: " << listado[indice].calprogramacion << endl; cout << " Etica: " << listado[indice].caletica << endl;

cout << " Fisica: " << listado[indice].calfisica << endl; cout << " Introduccion a la Ingenieria: " << listado[indice].calintalaing << endl; cout << " Su promedio es:" << (listado[indice].calcalculo+listado[indice].calprogramacion+listado[indice].caletica+listado[indice].calfisica+listado[indice].calintalaing)/5 << endl;

cout << endl; } } system("PAUSE");

}

// Resolver Funcion Cuadratica.cpp: archivo de proyecto principal.

#include "stdafx.h"#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>

using namespace System;

int main() {int a;int b;int c;float x1;float x2;float raiz;

cout << "Introduce el valor de a ? " << endl;cin >> a;cout << "Introduce el valor de b ? " << endl;cin >> b;cout << "Introduce el valor de c ? " << endl;cin >> c;

if (a==0) cout<<"Error, division entre cero"<<endl; else { raiz=(b*b)-4*(a*c); if (raiz<0) cout<<"Error, raiz negativa (imaginaria)"<<endl; else cout<<"X1: "<<((-1*b)+sqrt(raiz))/(2*a)<<endl<<"X2: "<<((-1*b)-sqrt(raiz))/(2*a)<<endl; } system("PAUSE"); return 1;}

Page 20: Comandos C++.docx

Numeros sumados consecutivos

// blah.cpp: archivo de proyecto principal.#include "stdafx.h"#include <iostream>

using namespace std;

void main(){ int g;do {

int z;

cout << "Para que la serie llegue hasta el numero ´n´, introduce un 1";

cout << "\n";cout << "Para que la serie llegue a ´n´ numeros, introduce un 2 ";cout << "\n";

cin >> z;cout << "\n";

if (z==2) { int a,b,x,c,l; a=1; b=1; x=0; c=0;

cout<<"Introduce el limite de la serie: "; cin>>l; if (l>0) { if (l==1) cout<<"1"<<endl; else { cout<<endl<<a<<", "<<b<<", "; while (c<l-2) { c++; x=a+b; cout<<x<<", "; a=b; b=x; } } } system("PAUSE");} if (z==1){

int a,b,x,c,l; a=1; b=1; x=0; c=0;

cout<<"Introduce hasta que numero quieres que llegue la serie: "; cin>>l; if (l>0) { if (l==1) cout<<"1"<<endl; else {

Page 21: Comandos C++.docx

cout<<endl<<a<<", "<<b<<", "; while (c<l-2) { c++;

x=a+b; if (x<=l){

cout<<x<<", "; a=b; b=x; }

}

}

} cout<<"\n";} cout <<"\n"<< "Para detener el programa introduce un 0: ";

cin >> g;cout<<"\n";} while (g!=0);

}

Otra Opcion

#include "stdafx.h"#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

using namespace System;

int main(array<System::String ^> ^args){ int p, s ,t ,n ,i ,o;char r;do {

cout << "Forma de escribir la serie de Fibonaci:\n 1.-Hasta N\n 2.-N valores\nElija su opcion: ";

cin>> o;cout << "\nDame el valor de N: ";cin >> n;cout << "\n";p=0;s=1;if (o==1)

do {t=p+s;cout << s << ", ";p=s;s=t;} while (s<=n);

else

Page 22: Comandos C++.docx

for (i=1; i<=n; i++) {t=p+s;cout << s << ", ";p=s;s=t;};cout << "Quieres salir del programa(S/N) ? ";cin>>r; } while (r!='S');return 0;

}

Leer valo entero y convertirlo a romano

// Convertir a Numeros Romanos.cpp: archivo de proyecto principal.

#include "stdafx.h"#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

using namespace System;

int main(array<System::String ^> ^args){

int x;int r;do {cout<<"Ingrese un numero: "<<endl; cin>>x; if((x<1)||(x>999))

cout<<"Ingrese un numero entre 0-999"<<endl; else {

if(x>=900) {cout<<"CM" ;x=x-900; } if(x>=500) {cout<<"D" ;x=x-500; } if(x>=400) {cout<<"CD" ;x=x-400; } if(x>=300) {cout<<"C" ;x=x-100; } if(x>=200) {cout<<"C" ;x=x-100; } if(x>=100) {cout<<"C" ;x=x-100; } if(x>=90) {cout<<"XC" ;x=x-90; } if(x>=50) {cout<<"L" ;x=x-50; } if(x>=40) {cout<<"XL" ;x=x-40; } if(x>=30) {cout<<"X" ;x=x-10; } if(x>=20) {cout<<"X" ;x=x-10; } if(x>=10) {cout<<"X" ;x=x-10; } if(x>=9) {cout<<"IX" ;x=x-9; } if(x>=5) {cout<<"V" ;x=x-5; } if(x>=4) {cout<<"IV" ;x=x-4; } if(x>=3) {cout<<"III";x=x-3; } if(x>=2) {cout<<"II" ;x=x-2; } if(x>=1) {cout<<"I" ;x=x-1; }

} "\n"

Page 23: Comandos C++.docx

cout << "Quieres salir del programa(S/N) ? "<<endl;cin>>r; } while (r!='S');

return 0; system("PAUSE");

}

Adivina

#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

int main(){

srand(time(NULL));int r,c,n,e;r= 1+rand() % 1000;do {

cout <<"\nValor Pensado: ";cin>>n;if (n>r)

cout << "Te pasaste. ";else if (n<r)

cout << "Te falto";c++; } while (n!=r);cout << "\nEl # generado es " << r;cout << "\nSe adivino en " << c<< " oportunidades";cin >> e;return 0;

}

Sacar Promedio

// Estad 30 enero 2012.cpp: archivo de proyecto principal.

#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

int main(){

int n,i;

Page 24: Comandos C++.docx

float p,s,a[10];cout <<"Cantidad de valores: ";cin>>n;for (i=1; i<=n; i++) {

cout << "Dame el valor "<<i<< "; ";cin>> a[i];s += a[i]; }

p=s/n;cout << "El promedio es: " <<p;cin >> i;return 0;

}

Valor máximo y minimo introducidoModaMedianaValor mas cercano al promedio

8,13,1,24,72,28,45,3,17,23,15,69,23,48,90,35

#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

int num,d ;int main(){

{cout <<"\nEscribe un numero entero: ";cin>>num;while (num%d != 0);(d = d+1);if (d == num) {

cout << "\nEl numero es primo ";else{

cout << "\nEl numero no es primo ";}}return 0;}

Operaciones#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;

Page 25: Comandos C++.docx

using std::cin;using std::endl;#include <math.h>using namespace std;

int main(){

int n,i,j,c,cm;float t,p,s,a[100],max,min,moda;cout <<"Cantidad de valores: ";cin>>n;for (i=1; i<=n; i++) {

cout << "Dame el valor "<<i<< "; ";cin>> a[i];s += a[i]; }

p=s/n;cout << "El promedio es: " <<p;

for (i=1; i<n; i++) {for (j=i+1; j<=n; j++) {

if (a[i]>a[j]) {t=a[j];

a[j]=a[i];a[i]=t; }

}}for (i=1; i<=n; i++) {cout << "\n" <<a[i];}cout << "\nEl minimo es: " <<a[1];cout<< "\nEl maximo es: " << a[n];cout<< "\nLa mediana es: ";if (n%2==0) {

cout << (a[n/2]+a[(n/2)+1])/2;else

cout << a[(n/2)+1];c=1;

moda=a[1];for(i=2; i<=n; i++) {

if (a[i-1]==a[i])c++;else

if(c>cm) {cm=c;moda=a[i-1];c=1; }

}cout << "\nLa moda es: " <<moda;cin >>i;return 0;}

}

// Arreglos 2D.cpp: archivo de proyecto principal.

#include "stdafx.h"#include <iostream>using namespace std;

Page 26: Comandos C++.docx

int main(){

int i,j,n;int arreglo [2][3] = {{1,2,3},{4,5,6}};//int arreglo [2][3] = {{1,2,3,4,5}};//int arreglo [2][3] = {{1,2},{4}};cout <<"Los valores del arreglo por fila son: \n";for (i=0; i<2; i++) {

for (j=0; j<3; j++)cout << arreglo[i][j] << " ";

cout << "\n";}cin >> n;return 0;

}Ejercicios ExtraCalcular el número binario de un número decimal.

Encontrar el número de cinco cifras que al multiplicarse por cuatro queda invertido ej. 12345 x5= 54321Calcular la suma de matrices y multiplicación máximo 10x10Leer una fecha y decir si es correcta, incluir bisiesto, ej 30 febrero no existeIntroducir una fecha de nacimiento y calcular edad al diaDado un numero escribirlo con letra (hasta 1000)Escribir 1 hasta N renglones 121 12321 1234321 . . . 123….N…321

MCD de 2#’sMCM de 2 #’sTriangulo de pascal hasta n renglones

#include "stdafx.h"#include <iostream>using namespace std;using namespace System;

int main(array<System::String ^> ^args){

int d, m, a, nd, nm, na, edad;cout << "Dia: ";cin >> d;cout << "Mes: ";cin >> m;cout << "Anio: ";cin >> a;

Page 27: Comandos C++.docx

if(m = 1 && d > 31) {cout << "FECHA INEXISTENTE ";}

else if(m = 3 && d > 31) {cout << "FECHA INEXISTENTE ";}

else if(m = 4 && d > 31) {cout << "FECHA INEXISTENTE ";}

else if(m = 5 && d > 30) {cout << "FECHA INEXISTENTE ";}

else if(m = 6 && d > 31) {cout << "FECHA INEXISTENTE ";}

else if(m = 7 && d > 30) {cout << "FECHA INEXISTENTE ";}

else if(m = 8 && d > 31) {cout << "FECHA INEXISTENTE ";}

else if(m = 9 && d > 30) {cout << "FECHA INEXISTENTE ";}

else if(m = 10 && d > 31) {cout << "FECHA INEXISTENTE ";}

else if(m = 11 && d > 30) {cout << "FECHA INEXISTENTE ";}

else if(m = 12 && d > 31) {cout << "FECHA INEXISTENTE ";}

else if (d <= 0) {cout<< "\nFECHA INEXISTENTE ";}

else if (m <= 0) {cout<< "\nFECHA INEXISTENTE ";}

else if (a <= 0) {cout<< "\nFECHA INEXISTENTE ";}

else if (a > 2012) {cout<< "\nFECHA INEXISTENTE ";}

else if (m > 13) {cout<< "\nFECHA INEXISTENTE ";}

elsecout<< "\nFECHA VALIDA ";}

return 1;}

Formula de Numero de CombinacionesC= (N!) \ ((R!)(N-R)!))

Un medio mas un tercio mas un cuarto mas un quinto mas un sexto mas 1 octavo mas un noveno mas un decimo mas un doceavo mas un dieciochoavo mas un 24avo

Numero de Combinaciones

#include "stdafx.h"#include <iostream>using namespace std;

int factorial(int x) {int i, f;f=1;for (i=1; i<=x; i++)

f *= i;

Page 28: Comandos C++.docx

return f;}int main(){

int i,n,r,c;cout <<"Dame el numero de elementos total: ";cin >> n;cout << "Dame el numero de elementos a escoger a la vez: ";cin >> r;c = factorial(n) / (factorial(r) * factorial (n-r));cout <<"El numero de combinaciones es " << c << " \n";cin >> i;return 0;

}

Funcion para calcular factorial#include "stdafx.h"#include <iostream>using namespace std;

int factorial(int x) {int i, f;f=1;for (i=1; i<=x; i++)

f *= i;return f;

}int main(){

int i,n;cout <<"Dame el valor al que le quieres calcular el factorial: ";cin >> n;cout <<"El valor del factorial de " << n << "es " << factorial (n)

<< " \n";cin >> i;return 0;

}

// MCM MCD#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

int MCD (int x,int y) {int d;do {

d=x%y;x=y;

Page 29: Comandos C++.docx

y=d;} while (d!=0);return x;

}

int MCM (int x,int y) {int d=x;while (d % x != 0 || d % y != 0)

d++;return d;

}

int main(){ int a,b,c;cout <<"Dame los denominadores separados por espacio: ";cin >> a >> b;cout << "El MCD es: " <<MCD (a,b) << "\n";cout << "El MCM es: " <<MCM (a,b) << "\n";cin >> a;return 0;}

Suma de Fracciones// MCM MCD.cpp: archivo de proyecto principal.

#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

int MCD (int x,int y) {int d;do {

d=x%y;x=y;y=d;

} while (d!=0);return x;

}

int MCM (int x,int y) {int d=x;while (d % x != 0 || d % y != 0)

d++;return d;

}

int main() {const int cont=11;int n[cont]={1,1,1,1,1,1,1,1,1,1,1};int d[cont]={2,3,4,5,6,8,9,10,12,18,24};

Page 30: Comandos C++.docx

int i,a,b,c,e,m,sn,sd;a=n[0];b=d[0];for (i=1; i<cont; i++) {

m=MCM(b,d[i]);e=(m/b)*a+(m/d[i])*n[i];sn=e/MCD(e,m);sd=m/MCD(e,m);a=sn;b=sd; }

cout << "El resultado es: " << a << "/" <<b;/* cout <<"Dame los denominadores separados por espacio: ";cin >> a >> b;cout << "El MCD es: " <<MCD (a,b) << "\n";cout << "El MCM es: " <<MCM (a,b) << "\n"; */cin >> a;return 0;

}

5 marzo 2012#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

bool primo(int x) {int i;bool p=true;for (i=2; i<x; i++)

if (x%i == 0) p=false;return p;

}

int sig_primo(int x) {int i;i=x+1;while (primo(i) == false)

i++; return i;

}

int MCD (int x,int y) {int d;do {

d=x%y;x=y;y=d;

} while (d!=0);return x;

}

Page 31: Comandos C++.docx

int MCM (int x,int y) {int d=x;while (d % x != 0 || d % y != 0)

d++;return d;

}

int main() {const int cont=11;int n[cont]={1,1,1,1,1,1,1,1,1,1,1};int d[cont]={2,3,4,5,6,8,9,10,12,18,24};int dc[cont]={2,3,4,5,6,8,9,10,12,18,24};int i,a,b,c,e,m,sn,sd,mcm;mcm=1;sd=2;do {

b=0;

do {c=0;for (i=1; i<=cont; i++) {

if (d[i]%sd==0) {c++;d[i]/=sd;

}b+=d[i];

}} while (c!=0);

sd=sig_primo(sd);}while (b!=11);for (i=0; i<cont; i++){

sn+=(mcm/dc[i])*n[i];cout << sn/MCD(sn,mcm) << "/" << mcm/MCD(sn,mcm);

/*a=n[0];b=d[0];for (i=1; i<cont; i++) {m=MCM(b,d[i]);e=(m/b)*a+(m/d[i])*n[i];sn=e/MCD(e,m);sd=m/MCD(e,m);a=sn;b=sd; }cout << "El resultado es: " << a << "/" <<b;cout <<"Dame los denominadores separados por espacio: ";cin >> a >> b;cout << "El MCD es: " <<MCD (a,b) << "\n";cout << "El MCM es: " <<MCM (a,b) << "\n"; */cin >> a;/*cout << sig_primo(a);cin >> a; */return 0;

}}

Multiplicacion de Matrices

Page 32: Comandos C++.docx

#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;#include <iomanip>//#include <curses.h>

const int dim=10;void LeerMatriz(int f, int c, float x[][dim]){

int i,j;for (i=0; i<f; i++){

for (j=0;j<c;j++){cout <<"Valor" <<i+1<<","<<j+1<<",";cin>>x[i][j];

}}

}void ImprimirMatriz(int f, int c, float x[][dim]){

int i,j;cout<<fixed<<setprecision(2);for(i=0;i<f;i++){

for (j=0;j<c;j++){cout<<setw(10)<<x[i][j];

}cout << "\n";

}}void MultiplicarMatrices(int f, int cf, int c, float x[][dim], float y[][dim], float z[][dim]){

int i,j,k;for (i=0;i<f;i++){

for(j=0;j<c;j++){z[i][j]=0;for (k=0;k<cf;k++){

z[i][j]+=x[i][k]*y[k][j];}

}}

}int main(){

int f1,c1,f2,c2;//system("clear");float m1[dim][dim],m2[dim][dim],m3[dim][dim];cout <<"Numero de filas 1:";cin >>f1;cout <<"Numero de columnas 1: ";cin >>c1;LeerMatriz(f1,c1,m1);cout << "Numero de filas 2: ";cin >>f2;

Page 33: Comandos C++.docx

cout << "Numero de columnas 2: ";cin >>c2;LeerMatriz(f2,c2,m2);MultiplicarMatrices(f1,c1,c2,m1,m2,m3);cout << "Matriz 1\n";ImprimirMatriz (f1,c1,m1);cout << "Matriz 2\n";ImprimirMatriz(f2,c2,m2);cout << "Matriz Multiplicacion\n";ImprimirMatriz(f1,c2,m3);cin >> f2;return 0;

}

Serie Fibonaci y Factorial de N (recursion)

#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

long Factorial(int n) {if (n<=1)

return 1;else

return n * Factorial (n-1);}

long Fib (int n) {if (n<=1)

return 1;else

return Fib (n-1) + Fib (n-2);}int main(){ int n;cout << "Dame el valor: ";cin >> n;cout << "El factorial de " << n << " es " << Factorial (n) << "\n";cout << "El termino " << n << " de la serie de Fibonaci es " << Fib (n) << "n";cin >> n;return 0;}

Torres de Hanoi, RECURSION#include "stdafx.h"#include <stdlib.h>#include <time.h>

Page 34: Comandos C++.docx

#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;

void Hanoi (int n,int i,int f,int aux) {//n = numero de discos// i = palito inicial// f = palito destino o final/*int aux;if (i+f==3) {

aux =3; }else{ if (i+f==4)

aux=2;else

aux=1; } */if (n==1){ cout << "De " << i << " a " << f << "\n"; }else {

Hanoi (n-1,i,aux, f);cout << "De " << i << " a " << f << "\n";Hanoi (n-1 ,aux ,f ,i );

}}

int main(){ int n;cout << "Dame el valor: ";cin >> n;Hanoi (n, 1, 3, 2);cin >> n;return 0;}

Invertir un Número, Recursión#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;#include <iomanip>

int invertir(int n, int i){ return n == 0 ? i : invertir(n / 10, i * 10 + n % 10); }

int main (){

Page 35: Comandos C++.docx

int num; cout << "Ingrese un numero: "; cin >> num; cout << "Numero Invertido: " << invertir(num, 0) << endl; system ("pause"); return 0;}

Pirámide de Pascallong Pascal (int f, int c) {

if (f==c || c==0)return 1;

elsereturn Pascal(f-1,c) + Pascal (f-1,c-1);

int main(){int i, j, n;cout << "Dame la fila: ";cin >> i;cout << "Dame la columna: ";cin >> j;cout << "El término de pascal " << i << "," << j << " es " << Pascal;cin >> n;return 0;}

UIsar la recursion en el metodo de euclides el maximo comun divisor de dos numeros

Si un cuadrado es magico o no, cuando todas sus filas todas sus columnas y diagonales principales suman lo mismo. El cuadrado del tamaño que quieras.

Llenar la pantalla con el abecedario.

Contestar determinantes

Codigos de repaso 2do parcial:

Abecedario:#include "stdafx.h"#include <iostream>using namespace std;

int main(array<System::String ^> ^args){ int c, a;for (c =1; c<=200; c++){

cout << "abcdefghijklmnopqrstuvwxyz";}

Page 36: Comandos C++.docx

cin >> a;

}

Cuadrado mágico:

#include "stdafx.h"#include <iostream>

using namespace std;

int main(array<System::String ^> ^args){ int n, r, c, j, s, m, co, d1, d2;

cout << "dame el valor de n"; cin >> n;

int cm [10][10]; int hor[100]; int ver[100];

cout << "introduce los valores del cuadrado";

for (r = 1; r <= n; r++) {for (c = 1; c <= n; c++){cout << "\nintroduce el valor: " << r << "," << c << ": ";cin >> cm[r][c];}}

for (r = 1; r <= n; r++) {

for (c = 1; c <= n; c++){cout << cm[r][c] << " ";}cout << "\n";}

for (r = 1; r <= n; r++){for (c = 1; c <= n; c++){

s = s + cm [r][c]; }

cout << s << " ";hor[r] = s;

Page 37: Comandos C++.docx

s = 0;}

for (c = 1; c <= n; c++){for (r = 1; r <= n; r++){s = s + cm [r][c]; }cout << s << " ";ver[c] = s;s = 0; }

r = 1;c = 1;s = 0;

while (r <= n) {s = s + cm[r][c];c = c + 1;r = r + 1;}cout << s << " ";d1 = s;

s = 0;c = 1;r = 1;

for (r = n; r > 0; r--){s = s + cm[r][c];c = c + 1;}cout << s << " ";d2 = s;

if ((hor[1] == ver[1]) && (ver[1] == d1) && (d1 == d2)){

cout << "es un cuadrado magico";}

else { cout << "no es un cuadrado magico";}

cin >> j;}

Page 38: Comandos C++.docx

método de eukrides:

#include "stdafx.h"#include <iostream>

int eukrides (int a,int b, int r, int c){r = a%b;if (r == 0) {c = b;return c;}else {a = b;b = r;return eukrides (a, b, r, c);}}

using namespace std;

int main(array<System::String ^> ^args){ int a, b, d, c ,r, h ;cout << "dame el numero mas grande";cin >> a;cout << "dame el numero mas chico";cin >> b;

h = eukrides(a,b,r,c);cout << h;

cin >> d; }

Potencia a la n por recursión:

#include "stdafx.h"#include <iostream>

using namespace std;

Page 39: Comandos C++.docx

int potencia (int x, int y, int c, int n){n = n*x;c = c + 1;if (c < y){

return potencia (x, y, c, n);}else {

return n;}}

int main(array<System::String ^> ^args){ int x, y, c, j, n;

cout << "dame el valor de X";cin >> x;cout << "dame el valor de Y";cin >> y;

n = 1;

cout << potencia (x, y, c, n);

cin >> j; }

determinantes:

#include <iostream>#include <iomanip>#include <math.h>

const int dim = 10;using namespace std;long Det(int n, double a[dim][dim]){     int i, j, k, g, s=0, c;    double b[dim][dim];    if(n==2){ return (a[1][1]*a[2][2])-(a[1][2]*a[2][1]);}          else{        while(a[1][1]==0){            s++;            for(i=1;i<=n;i++){                g = a[1][i];

Page 40: Comandos C++.docx

                a[1][i] = a[s][i];                a[s][i] = g;            }        }        if(a[1][1]!=1){            g = a[1][1];            for(k=1;k<=n;k++){                a[1][k] = a[1][k] * 1/g;            }}            for(k=2;k<=n;k++){                i=1;                c = a[k][i];                for(i=1;i<=n;i++){                    a[k][i] = a[k][i] - a[1][i] * c;                }            }                for(j=2;j<=n;j++){                for(i=2;i<=n;i++){                        b[i-1][j-1] = a[i][j];                }            }            return (pow(-1,s))*(g)*Det(n-1,b);        }}    int main (){    int n, j, i;    cout << "Introduce el tamaño del cuadro (nxn): ";    cin >> n;    double a[dim][dim];    for(i=1; i<=n; i++){        for(j=1; j<=n; j++){            cout << "Valor para " << i << ", " << j << ": ";            cin >> a[i][j];        }    }    for(i=1; i<=n; i++){        cout << "[ ";        for(j=1; j<=n; j++){            cout << a[i][j] << " ";        }        cout << "]\n";}        cout << "|A| = " << Det(n, a);

Page 41: Comandos C++.docx

    cin >> i;}

Suma N dgitos

#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;using namespace System;

int main()//con puntero{ int i,j,k,x;

char n1[50], n2[50], *p;p=n1;

j=0;i=0;

cout << "Dame los valores separados por espacios; ";cin >> n1 >> n2;do {

i++;p++;

} while (*p!='\0');

/* do {i++;

} while (n1[i]!='\0'); *///ciclo

/*do {i++;k++;

} while (n1[k]!='\0'); */

p=n2;do {

j++;p++;

} while (*p!='\0');x=n1[0]/*-48*/;//aqui el codigo char lo convierte a int por

medio del codigo ASCII para volver al valor original le restas el valor de la tabla//ejemplo: si pones 956 y 13 agarra el primer valor osea el 9 y le convierte a entero por ASCII para volver al original restas 48

cout << i << " " << j << " " << x;cin >> j;return 0;

}//mayuscula sumo 32 minuscula//minuscula sumo mayuscula convierte en mayuscula

Page 42: Comandos C++.docx

//leer dos arreglos de digitos de num grandes y sumarlos

///////////////////////////////////////////////////////////////////////

#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;using namespace System;

int main()//con puntero{ int i,j,k,x, max;char n1[50], n2[50], n3[50],*p;p=n1;j=0;i=0;cout << "Dame los valores separados por espacios; ";cin >> n1 >> n2;do {

i++;p++;

} while (*p!='\0');

/* do {i++;} while (n1[i]!='\0'); */

for (i=0; n1[i]!='\0'; i++){}for (i=0; n1[j]!='\0'; j++){}//ciclo /*do {i++;k++;} while (n1[k]!='\0'); */

/* p=n2;do {j++;p++;} while (*p!='\0');x=n1[0]/*-48;*///aqui el codigo char lo convierte a int por medio del codigo ASCII para volver al valor original le restas el valor de la tabla//ejemplo: si pones 956 y 13 agarra el primer valor osea el 9 y le convierte a entero por ASCII para volver al original restas 48if (i>j) {

k=i-j;max=i;for (x=0; x<k; x++) n3[x] = '0';for (x=0; x<j; x++) n3[x+k] = n2[x];for (x=0; x<max; x++) n2[x] = n3[x];

Page 43: Comandos C++.docx

n2[max]= '\0'; }

else {k=j-i;max=j;for (x=0; x<k; x++) n3[x] = '0';for (x=0; x<i; x++) n3[x+k] = n1[x];for (x=0; x<max; x++) n1[x] = n3[x];n1[max]= '\0'; }

cout << i << " " << j << " " << max << " " << n1 << "\n" ;

k=0;for (x=max; x>0; x--) {

i=n1[x-1]-48;j=n2[x-1]-48;n3[x]=((i+j+k) % 10)+48;k=(i+j) / 10; }n3[0] =k+48;n3[max+1] = '\0';

cout << n1 << " " << n2 << "=" << n3;;cin >> x;return 0;

}//mayuscula sumo 32 minuscula//minuscula sumo mayuscula convierte en mayuscula//leer dos arreglos de digitos de num grandes y sumarlos// convertir un numero binario en decimal// convertir un hexadecimal a decimal y de regreso, cuando tienes numero mayor a 16 ejemplo 16=0

// Binario Decimal.cpp: archivo de proyecto principal.

#include "stdafx.h"#include <iostream>using namespace std;

int main(){ char binario [50];int i,j,n,p,d;cout << "Dame el valor binario: ";cin >> binario;for (i=0; binario[i]!='\0'; i++); //cuenta cuantos digitos tienen=i;p=1;for (i=n; i>0; i--) {

d += p * (binario [i-1] - 48);p *=2; }

/* if (binario[i-1]=='1') {d +=p;p *= 2; } */

cout << "el valor decimal de; " << binario << " es " << d << "\n";cin >> j;

Page 44: Comandos C++.docx

return 0;}

//Hexadecimal decimal#include "stdafx.h"#include <iostream>using namespace std;

int main(){ char Hexa [50];int i,j,n,p,d;cout << "Dame el valor Hexadecimal: ";cin >> Hexa;for (i=0; Hexa[i]!='\0'; i++); //cuenta cuantos digitos tienen=i;p=1;for (i=n; i>0; i--) {

if (Hexa [i-1] <= 57)d += p * (Hexa [i-1] - 48);

elsed += p* (Hexa[i-1] - 55) ;p *=16; }

/* if (binario[i-1]=='1') {d +=p;p *= 2; } */

cout << "el valor decimal de; " << Hexa << " es " << d << "\n";cin >> j;return 0;}

// Strings.cpp: archivo de proyecto principal.#include "stdafx.h"#include <iostream>#include <cstring>using namespace std;#include <stdlib.h>

char*mayusc(char*s){int i;for(i=0;i<strlen(s);i++)

if (s[i]>=97&&s[i]<=122)s[i] -=32;

return s;

}

Page 45: Comandos C++.docx

int main() {int r,t;char s1[50],s2[50],c;cout <<"Dame el primer string (sin espacios): ";cin>>s1;cout<<"Dame el segundo string: ";cin >> s2;cout <<"Dame el caracter: ";cin >>c;cout <<"La longitud de la cadena es: " <<strlen(s1)<<"\n";cout << "strchr(s1,c)=" <<strchr(s1,c)<<"\n";cout << "* strchr(s1,c)=" <<* (strchr(s1,c)) <<"\n";cout << "La posicion del caracter dentro del string es =" <<

(strchr(s1,c))-s1 <<"\n";cout << "El string todo en mayusculas es: " << mayusc(s1);cin >> r;return r;

}

STRING EMBUDO PIRAMIDE Y CONTAR NUMERO DE LETRAS

#include "stdafx.h"#include <iostream>using namespace std;

int main (){char s[50],letra;int i,j,suma;cout <<"Dame el string: ";cin >>s;

for (i=0; i<strlen(s);i++)cout << (s+i) << "\n";

for(i=0; i<=strlen(s); i++){for (j=1;j<=i;j++) cout << " ";cout << (s+i) << "\n";

}for (i=0; i<=strlen(s)/2; i++){

for (j=1; j<=i;j++) cout << " ";for (j=i; j<strlen(s)-i;j++) cout << s[j];cout << "\n";

}for (letra=65;letra<=90; letra++){

suma=0;for (j=0; j<strlen(s);j++) suma+=s[j]==letra;cout << letra << "=" << suma << "\n";}

cin >> s;return 0;

}

BARAJEO#include "stdafx.h"#include <iostream>#include <ctime>

Page 46: Comandos C++.docx

// cstdlib para el random#include <cstdlib>

using namespace std;using namespace System;//estructura especie de arreglo puede contener tipos diferentes de variables//struct carta {char *p;char *v;};

char *palo [4]={"Corazones", "Treboles", "Diamantes", "Espadas"};char *valor [13]={"As", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete", "Ocho", "Nueve", "Diez", "Jota", "Quina", "Rey"};

int main(array<System::String ^> ^args){ carta baraja[52]; int i, j, c;

carta temp; c=0; for(i=0; i<4; i++){

for(j=0; j<13; j++){ baraja[c].p=palo[i]; baraja[c].v=valor[j]; // cout<< baraja[c].v << "de" <<baraja[c].p << ".\n"; c++; }

} srand(time(0)); for(i=0; i<52; i++){

j=rand() % 52; temp=baraja[j]; baraja[j]=baraja[i]; baraja[i]=temp;

} for(c=0; c<52; c++) cout << baraja[c].v << " de " << baraja[c].p << ".\n"; cin >>i; return 0;}

21#include "stdafx.h"#include <iostream>#include <ctime>#include <cstdlib>

using namespace std;using namespace System;

struct carta {

Page 47: Comandos C++.docx

char *p;char *v;};

struct Jugador {carta c1,c2;int juego;

};

char *palo [4]={"Corazones", "Treboles", "Diamantes", "Espadas"};char *valor [13]={"As", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete", "Ocho", "Nueve", "Diez", "Jota", "Qüina", "Rey"};

int cartavalor (carta x) {int i,vale;for (i=0;i<13;i++) if (strcmp(x.v, valor[i])==0) vale=i+1;if (vale>10)

return 10;else

if (vale==1)return 11;

elsereturn vale;

}

int main(array<System::String ^> ^args){ carta baraja[52]; Jugador jugadores[11]; int i, j, c, n;

carta temp; c=0; for(i=0; i<4; i++){

for(j=0; j<13; j++){ baraja[c].p=palo[i]; baraja[c].v=valor[j]; cout<< baraja[c].v << "de" <<baraja[c].p << ".\n"; c++; }

} srand(time(0)); for(i=0; i<52; i++){

j=rand() % 52; temp=baraja[j]; baraja[j]=baraja[i]; baraja[i]=temp;

} /* for(c=0; c<52; c++) cout << baraja[c].v << " de " << baraja[c].p << ".\n";} */ cout << "Numero de Jugadores: "; cin >> n; c=0;

for (j=0;j<=n;j++) { jugadores[j].c1=baraja[c]; c++;

}

Page 48: Comandos C++.docx

for (j=0; j<=n;j++) { jugadores[j].c2=baraja[c]; c++; jugadores [j].juego = cartavalor (jugadores[j].c1) + cartavalor

(jugadores [j].c2); } for (i=1;i<=n;i++) {

cout << "Jugador " << i << "\n"; cout << "Carta 1: " << jugadores[i].c1.v << " de " <<

jugadores[i].c1.p << ".\n"; cout << "Carta 2: " << jugadores[i].c2.v << " de " <<

jugadores[i].c2.p << ".\n"; cout << "Valor del juego = " << jugadores[i].juego << "\n"; cout << "\n";

} cout << "Banca\n"; cout << "Carta 1: " << jugadores[0].c1.v << " de " << jugadores[0].c1.p << ".\n"; cout << "Carta 2: " << jugadores[0].c2.v << " de " << jugadores[0].c2.p << ".\n"; cout << "\n";

cin >> j; return 0;}48-57 codigo ascii

SERIE DE ULAM#include "stdafx.h"#include "stdlib.h"#include "stdio.h"#include "time.h"#include <iostream>#include <cstdlib>using namespace std;using namespace System;using namespace std;

int main(){

int vRep; int num,i,j,x;

cout << "SERIE DE ULAM" << endl;cout << "Introduce un Numero: ";cin >> num;{ cout << num << ",";while (num>1){

j= num%2;if (j==0)

x=(num/2);else

x=(num*3)+1;cout<<x<<", ";

Page 49: Comandos C++.docx

num=x;} }

system("pause");}

Funcion sin#include "stdafx.h"#include <stdlib.h>#include <time.h>#include <iostream>using std::cout;using std::cin;using std::endl;#include <math.h>using namespace std;#include <cstdlib>using namespace std;using namespace System;

int main (){ int i, j;double a, h, x;

for (i=0; i <= 18; i++){a = i*5;x = a*(3.14159/180);h = (x - sin(x));cout << "angulo = " << a << " x: " << x <<" resultado " << h << "\

n";}

system("pause");cin >> j;

}