Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Для новичков > Деньги


Автор: agentjamesbolt007 7.9.2012, 09:35
Задача: написать программу подсчета суммы и разности денег старой английской денежной системы(фунты, шиллинги, пенсы записывается так: 5.3.2.), а также умножение ее на вещественное число.  В одном фунте 20 шиллингов, в шиллинге 12 пенсов. Но это не суть , алгоритм суммы, разности и  умножение на вещественное число я написал. Мне нужно как-то соеденить две части программы в одну. Пробовал с помощью if else и switch неполучилось.


Код

#include <iostream>
using namespace std;

int main()
{
double fun, shil, penc, fun2, shil2, penc2, res, res2, res3;
char znak, ch;
do {
   cout <<"Vvedite pervuyu summu, operaciyu(+,-)i vtoroyu summu" <<endl;
   cout <<"ili pervuyu summu, operaciyu(*)i chislo" <<endl;
   cin >>fun >>shil >>penc >>znak >>fun2 >>shil2 >>penc2;
   switch(znak) 
   {
   case'+':
    res =fun +fun2;
    res2 =shil +shil2;
    res3 =penc +penc2;
    if(res3 >11)
     { ++res2;
       res3 =res3 -12;
     }
    if(res2 >19)
     { ++res;
       res2 =res2 -20;
     } break;
   case'-':
    res =fun -fun2;
    res2 =shil -shil2;
    res3 =penc -penc2;
    if(res3 <0)
     {--res2;
      res3 = 12 +res3;
     }
    if(res2 <0)
     {--res;
      res2 = 20 +res2;
     } break;
   }
    cout <<res <<'.' <<res2 <<'.' <<res3;
    cout <<"\nProdolgit (vvedite 'y' ili 'n')?" <<endl;
    cin >>ch;
   } while (ch !='n');
return 0;
}


//===========================VTORAYA CHAST'======================
//===============EE NUGNO KAK-TO PRISAEDINIT' K PROGRAMME
   
Код

 cin >>fun >>shil >>penc >>d;
    res =fun *d;
    res2 =shil *d;
    res3 =penc *d;
    if(res3 >11)
     {++res2;
      res3 =static_cast<int>(res3); //otbrasivanie drobnoy chasti
      res3 =res3 -12;
     }
    if(res2 >19)
     { ++res;
       reszer =static_cast<int>(res2); //otbrasivanie drobnoy chasti
       fraction =res2 -reszer;       //poluchenaya drob'
       res3 =fraction /0.083 +res3;   //pribavlenie poluchenogo resultata
       res2 =res2 -20;
     }
    if(fun *d)
     { reszer2 =static_cast<int>(res); //otbrasivanie drobnoy chasti
       fraction2 =res -reszer2;         //drob'
       res2 =fraction2 /0.05 +res2;    //pribavlenie k shillingam
     }
    cout <<"proizvedenie ravno:" <<res <<'.' <<res2 <<'.' <<res3 <<endl;

Автор: math64 7.9.2012, 10:35
Делай проще: переводи всё в пенсы, делай операцию, а результат переводи обратно:
Код

int  toPence(int pounds, int shillings, int  pennies) { return (pounds * 20 shillings)*12 + pennies); }
void  fromPence(int total, int& pounds, int& shillings, int&  pennies) { 
  pennies = total % 12;
  total /= 12;
  shillings = total % 20;
  pounds = total / 20;
}

Автор: borisbn 7.9.2012, 10:53
прикольное имя переменной, отвечающий за пенсы  smile 

Автор: agentjamesbolt007 7.9.2012, 11:28
Цитата(math64 @ 7.9.2012,  10:35)
Делай проще: переводи всё в пенсы, делай операцию, а результат переводи обратно:
Код

int  toPence(int pounds, int shillings, int  pennies) { return (pounds * 20 shillings)*12 + pennies); }
void  fromPence(int total, int& pounds, int& shillings, int&  pennies) { 
  pennies = total % 12;
  total /= 12;
  shillings = total % 20;
  pounds = total / 20;
}

это хорошо конечно, но как мне обе части соединить, чтоб если знак + или - нажимаешь выполнялась одна часть, а если * то другая

Автор: borisbn 7.9.2012, 12:58
как-то так
Код

cout <<"Vvedite pervuyu summu, operaciyu(+,-)i vtoroyu summu" <<endl;
cout <<"ili pervuyu summu, operaciyu(*)i chislo" <<endl;
cin >>fun >>shil >>penc >>znak >>fun2 >>shil2 >>penc2;
int p1 = toPence( fun, shil, penc );
int p2 = toPence( fun2, shil2, penc2 );
int result = 0;
switch ( znak ) {
   case '+': result = p1 + p2; break;
   case '-': result = p1 - p2; break;
   case '*': result = p1 * fun2; break;
}
fromPence( result, fun, shil, penc );
cout << fun <<'.' << shil <<'.' << penc;

Автор: math64 7.9.2012, 18:25
Код

cout << "Enter money (pounds shillings pence), + or -  and money2 or" << endl
cout <<" money * number :";
readMoney(pounds1, shillings1, pennies1);
cin >> sign;
switch(sign) {
   case '+' :
     readMoney(pounds2, shillings2, pennies2); 
     result = toPense(pounds1, shillings1, pennies1) + toPence(pounds2, shillings2, pennies2);
     break;
   case '-' :
     readMoney(pounds2, shillings2, pennies2); 
     result = toPense(pounds1, shillings1, pennies1) + toPence(pounds2, shillings2, pennies2);
     break;
   case '*' :
     cin >> number; 
     result = toPense(pounds1, shillings1, pennies1) * number;
     break;
}
fromPence(result, pounds, shillings, pennies);
cout  << pounds << " pounds " << shillings << " shillings " <<  pennies << "p" << endl;

void readMoney(int& pounds, int& shillings, int& pennies) {
cin >> pounds >> shillings >> pennies;
}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)