Solución

Anuncio
Solución Práctica Calificada - Programación C++
Pág. 1
ÍNDICE
ÍNDICE.............................................................................................................................................................................. 1
1.
EJERCICIO 1: MINICALCULADORA ............................................................................................................. 1
2.
EJERCICIO 2: CONTROL DE GASTOS ........................................................................................................... 2
1. Ejercicio 1: Minicalculadora
#include <iostream.h>
#include <string.h>
#include <math.h>
double Calcula(char *str);
void main()
{
char str[30];
while(true){
cout << ">>"; cin.getline(str,30);
if (str[0]=='E') break;
double val = Calcula(str);
cout << "\tans = " << val << endl;
}
}
cout << "Fin de la MiniCalculadora" << endl;
double Calcula(char *str)
{
bool ope=false;
double val,dop1,dop2;
char op1[20],op2[20];
int cont=0;
char chope;
static double ans=0;
//si existe operador en la expresion
for (int i=0;i<strlen(str);i++){
if ( str[i] == '+' || str[i] == '-'||str[i] == '*'||str[i] == '/'){
ope=true;
op1[cont]=0;
//fin de cadena op1
cont=0;
//para segundo contador
chope=str[i];
//almacena qué operador es
}else{
if (!ope) op1[cont++]=str[i];
else op2[cont++]=str[i];
}
}
op2[cont]=0;
if (!ope) { //solo hay un operador
if (op1[0]=='a') return ans;
else return atof(op1);
}
if ( op1[0] == 'a' ) dop1 = ans;
else dop1 = atof(op1);
if ( op2[0] == 'a' ) dop2 = ans;
else dop2 = atof(op2);
Informática II - Fundamentos de Programación
Paul Bustamante
Solución Práctica Calificada - Programación C++
}
switch(chope){
case '+': val =
case '-': val =
case '*': val =
case '/': val =
default: val=0;
}
ans = val;
return val;
dop1
dop1
dop1
dop1
+
*
/
dop2;
dop2;
dop2;
dop2;
Pág. 2
break;
break;
break;
break;
2. Ejercicio 2: Control de gastos
#include <iostream.h>
#include <iostream.h>
#include <iomanip.h>
int Menu();
void AgregaGasto( double gas[][3], int cont[] );
void VerGasto( double gastos[][3], int cont[] );
void main()
{
double gastos[100][3];
int cont[3]={0,0,0};
while(true){
int opc = Menu();
if (opc==3) break;
if (opc==1){
AgregaGasto( gastos, cont );
}else if (opc==2){
VerGasto( gastos, cont );
}
}
}
int Menu()
{
int opc=0;
while(opc<1 || opc>3){
cout << "*** Menu ***" << endl;
cout << "1. Cargar Gasto" << endl;
cout << "2. Ver Gastos" << endl;
cout << "3. Salir" << endl;
cout << "Opc:?";
cin >> opc;
}
return opc;
}
void AgregaGasto( double gastos[][3], int cont[] )
{
int opc=0, num;
while(opc<1 || opc>3){
cout << "\t1. Alimentacion 2.Transporte 3.Vivienda:?";
cin >> opc;
}
opc--;
//comprobar que elige bien
num = cont[opc];
cout << "\tDar gasto:?"; cin >> gastos[num][opc];
Informática II - Fundamentos de Programación
Paul Bustamante
Solución Práctica Calificada - Programación C++
Pág. 3
cont[opc]++;
}
void VerGasto( double gastos[][3], int cont[] )
{
double total=0;
int max=cont[0];
cout << "--------- Reporte de gastos ---------" << endl;
for (int i=0;i<3;i++) if (cont[i]>max) max=cont[i];
//ver el mayor
cout << setw(15) << "Alimentacion" << setw(15) << "Transporte" << setw(15) <<
"Vivienda" << endl;
for (i=0;i<max;i++){
for (int j=0;j<3;j++){
//sumar gastos
if (cont[j] > i) {
cout << setw(15) << gastos[i][j];
total += gastos[i][j];
}else cout << setw(15) << " ";
}
cout << endl;
}
cout << "--------- Total:" << total <<" ---------"<< endl;
cout << endl;
}
Informática II - Fundamentos de Programación
Paul Bustamante
Descargar