// Solucion a la Practica Calificada de Programación C++ // Escuela Superior de Ingenieros - Tecnun // Dr. Paul Bustamante //Ejercicio 1 #include <iostream.h> #include <string.h> char* PedirFrase(); int ContarVocal( char *cad, char vocal); void main1() { char *frase; char voc[]={'a','e','i','o','u'}; char vocM[]={'A','E','I','O','U'}; frase=PedirFrase(); for (int i=0,n=0;i<5;i++){ n = ContarVocal(frase,voc[i]); n += ContarVocal(frase,vocM[i]); cout << "Num. de "<<voc[i]<<":" << n<<endl; } } char* PedirFrase() { char tmp[120]; char *frase; cout << "Dar Frase:"; cin.getline(tmp,120); frase = new char[ strlen(tmp) + 1]; strcpy(frase,tmp); return frase; } int ContarVocal( char *cad, char vocal) { int num=0; for (int i=0;i<strlen(cad);i++){ if ( cad[i] == vocal) num++; } return num; } //Ejercicio 2 #include <iostream.h> #include <math.h> #include <iomanip.h> void RegLineal(double *x, double *y, int n, double &a, double &b){ double pxy, sx, sy, sx2, sy2; pxy=sx=sy=sx2=sy2=0.0; for(int i=0; i<n; i++){ sx+=x[i]; sy+=y[i]; sx2+=x[i]*x[i]; sy2+=y[i]*y[i]; pxy+=x[i]*y[i]; } a=(n*pxy-sx*sy)/(n*sx2-sx*sx); b=(sy-a*sx)/n; } void main(void) { //Una fábrica de bebidas refrescantes observa sus ventas y la temperatura de la calle double *t=NULL, *v=NULL; double a=0,b=0,Temp,Vent; int opc,n=0,i; while(1){ cout << "\n *** Menu ***\n"; cout << "1. Pedir Datos\n"; cout << "2. Ver Datos ( Temp, Ventas y Ecuacion V=T*a+b )\n"; cout << "3. Dada una Temperatura calcular las Ventas)\n"; cout << "4. Salir\n\tOpc:?"; cin >> opc; switch(opc){ case 1: cout << "Dar N:?"; cin >> n; if (t!=NULL) { delete []t; delete [] v; } t = new double[n]; v=new double[n]; for ( i=0;i<n;i++) { cout << "Temp y Vent["<<i<<"]:?"; cin >> t[i] >> v[i]; } break; case 2: cout << setw(8) << "Temp" <<setw(8)<<"Ventas" << endl;; for ( i=0;i<n;i++) cout << setw(8) << t[i] <<" " <<setw(8)<<v[i] << endl; RegLineal(t,v,n,a,b); cout << "\nEcuacion: V = "<< a << " * T + " << b << endl; break; } } case 3: cout << "Dar Temp:?"; cin >> Temp; RegLineal(t,v,n,a,b); Vent = Temp * a + b; cout << "Para la Temp="<<Temp<< " las ventas="<<Vent<<endl; break; case 4: if (t!=NULL) { delete []t; delete [] v; } return; }