Ejercicio 1 #include <iostream.h> #include <math.h> #include <iomanip.h> #include <stdlib.h> #define NUM 24 void Estadisticas(double temp[], double hum[]); void PideDatos(double temp[], double hum[], bool); double Media(double []); void MaxMin(double[], double&, int&, double[], double&,int&, bool); double DesvStd(double []); void MostrarDatos(double temp[], double hum[]); void main() { int opc; double temp[NUM],hum[NUM]; for (int i=0;i<NUM;i++) temp[i]=hum[i]=0; while(true){ cout << "\t** Menu **"<<endl; cout << "1. Introducir Todos los datos"<<endl; cout << "2. Introducir el dato de 1 hora"<<endl; cout << "3. Mostrar datos"<<endl; cout << "4. Datos Estadisticos"<<endl; cout << "5. Limpiar Pantalla"<<endl; cout << "6. Salir"<<endl; cout << "\tOpcion:?"; cin >> opc; switch(opc){ case 1: PideDatos(temp,hum,true); break; case 2: PideDatos(temp,hum,false); break; case 3: MostrarDatos(temp,hum); break; case 4: Estadisticas(temp,hum); break; case 5: system("cls"); break; case 6: return; } } } void Estadisticas(double temp[], double hum[]) { double mdt, mdh, mt, mh; int pmt, pmh; //pmt:posicion maximo temp. pmh: pos. max. humedad mdt = Media(temp); //media de Temp. mdh = Media(hum); //media de Hum, cout << "\n** Datos Estadisticos ***" << endl; cout.setf(ios::left|ios::fixed); cout <<setw(10)<< ""<<setw(15)<< "Temperatura" << setw(10) << "Humedad" << endl; cout.fill('.'); //media cout <<setw(10)<< "Media" << setw(15)<< setprecision(2) << mdt; cout.fill(' '); cout << setw(10) << setprecision(2) << mdh << endl; //maximos cout.fill('.'); MaxMin(temp,mt,pmt,hum,mh,pmh,true); //maximos cout <<setw(10)<< "Maximos" << setw(15)<< setprecision(2) << mt; cout.fill(' '); cout << setw(10) << setprecision(2) << mh << endl; //minimos MaxMin(temp,mt,pmt,hum,mh,pmh,false); //minimos cout.fill('.'); cout <<setw(10)<< "Minimos" << setw(15)<< setprecision(2) << mt; cout.fill(' '); cout << setw(10) << setprecision(2) << mh << endl; //desv. estandar double dsvt = DesvStd(temp); double dsvh = DesvStd(hum); cout.fill('.'); cout <<setw(10)<< "Desv.Std" << setw(15)<< setprecision(2) << dsvt; cout.fill(' '); cout << setw(10) << setprecision(2) << dsvh << endl << endl; } double DesvStd(double vec[]) { double ds=0; double md = Media(vec); for (int i=0;i<NUM;i++){ ds += pow(vec[i]-md,2.0); } ds = sqrt(ds/(NUM-1)); return ds; } void MaxMin(double temp[],double &mt,int &pmt,double hum[],double &mh,int &pmh,bool sw) { mt=temp[0]; pmt=0; mh=hum[0]; pmh=0; for (int i=1;i<NUM;i++){ if (sw==true){ //maximo if (mt < temp[i]) { mt=temp[i]; pmt=i;} if (mh < hum[i]) { mh=hum[i]; pmh=i;} } else{ //minimos if (mt > temp[i]) { mt=temp[i]; pmt=i;} if (mh > hum[i]) { mh=hum[i]; pmh=i;} } } } double Media(double vec[]) { double med=0; for (int i=0;i<NUM;i++){ med += vec[i]; } return (med/NUM); } void MostrarDatos(double temp[], double hum[]) { cout.setf(ios::left|ios::fixed); cout << setw(10) << "Hora" << setw(10)<<"Temp"<< setw(10) << "Humedad"<<endl; for (int i=0;i<NUM;i++){ cout.fill('.'); cout << setw(10) << i ; cout << setw(10) << setprecision(2) << temp[i] ; cout.fill(' '); cout << setw(10) << setprecision(2) << hum[i] << endl; } cout << endl; } void PideDatos(double temp[], double hum[], bool sw) { int h; if (sw){ //true es todos } for (int i=0;i<NUM;i++){ cout << i << ":00h (Temp y Hum):? "; cin >> temp[i] >> hum[i]; } }else{ //solo uno cout << "Hora?:"; cin >>h; cout << h << ":00h (Temp y Hum):? "; cin >> temp[h] >> hum[h]; } Ejercicio 2 #include <iostream.h> #include <string.h> #include <stdlib.h> class Asignatura { char *Name; double Nota; public: Asignatura(){Name=NULL;} ~Asignatura(){ delete [] Name;} void Asigna(char *nam="",double Not=0){ Name = new char[ strlen(nam) + 1]; strcpy(Name,nam); Nota=Not; } double GetNota(){ return Nota;} friend ostream& operator<<(ostream &co, Asignatura &as){ co << "\tAsig.:"<<as.Name << " Nota:" << as.Nota; return co; } }; class Persona { protected: long DNI; char *Name; //nombre y Apellidos public: Persona(){Name=NULL;} void Asigna(char *n,long d){ Name = new char[ strlen(n) + 1]; strcpy(Name,n); DNI = d; } ~Persona(){ delete [] Name; } }; class Alumno: public Persona { long carnet; int numAsig; Asignatura *asig; public: Alumno(){numAsig=0;} ~Alumno(){ if (numAsig>0) delete [] asig;} void Asigna(char *name="", long DNI=0, long car=0, int numA=0){ Persona::Asigna(name,DNI); carnet = car; numAsig = numA; asig = new Asignatura[numA]; //crear numA asignaturas char tmp[80]; double Not; for (int i=0;i<numAsig;i++){ cout << "\tAsig. " << i+1 << " (Nombre Nota):?"; cin >> tmp >> Not; asig[i].Asigna(tmp,Not); } } double NotaMedia() { double nm=0; for (int i=0;i<numAsig;i++){ nm += asig[i].GetNota(); } return nm/numAsig; } friend ostream& operator<<(ostream &co, Alumno *al){ co << "Nombre: " << al->Name; co << " Carnet: " << al->carnet ; co << " DNI: " << al->DNI << endl; for (int i=0;i<al->numAsig;i++){ co << al->asig[i]<<endl; } co << "\tNota Media:" << al->NotaMedia() << endl; return co; } friend istream& operator>>(istream &ci, Alumno *al){ char tmp[120]; long dni,carnet,numA; cout << "Nombre:"; ci.getline(tmp,120); cout << "DNI:"; cin >> dni; cout << "Carnet:"; cin >> carnet; cout << "Num.Asignaturas:"; cin >> numA; al->Asigna(tmp,dni,carnet,numA); return ci; } }; #define NUM 50 int Menu(); void main() { int opc,num=0; Alumno *lista[NUM]; while(true){ opc = Menu(); if (opc==1) { // liberar memoria asignada a la lista return; //terminar } else if (opc==2){ cin.ignore(1,'\n'); lista[num]=new Alumno; cin >> lista[num]; //pedir datos num++; } else if (opc==3){ //display for (int i=0;i<num;i++) cout << lista[i] << endl; } else if (opc==4){ // limpiar pantalla system("cls"); } } } int Menu() { int opc; while(true){ cout << "\t** Menu **"<<endl; cout << "1. Salir"<<endl; cout << "2. Introducir Datos"<<endl; cout << "3. Mostrar Datos por consola"<<endl; cout << "4. Limpiar Pantalla"<<endl; cout << "5. Grabar Datos a disco"<<endl; cout << "\tOpcion:?"; cin >> opc; if ( opc >0 && opc <6) return opc; } }