1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <sstream>
using namespace std;
class Money
{
public:
Money(){}
Money(string s):amount(s){}
Money (double d)
{
stringstream ss;
ss.setf(ios::fixed);
ss.setf(ios::showpoint);
ss.precision(2);
ss<<d;
ss>>amount;
//reset ss
ss.clear();
int location=amount.find_first_of('.');
int distance;
for(int i=0;i<amount.length();i++)
{
if(i==0)
//putchar('$');
ss<<"$";
//putchar(amount[i]);
ss<<amount[i];
distance=location-i-1;//from end of array
if(distance>0 && distance%3==0)
//putchar(',');
ss<<",";
}
ss>>amount; //put the formatted number into amount
}
friend istream& operator>>(istream& in, Money& m);
friend ostream& operator<<(ostream& out, Money& m);
Money operator+(Money& m){
return Money(double(Money(amount))+double(m));
}
Money operator-(Money& m){
return Money(double(Money(amount))-double(m));
}
Money operator/(Money& m){
return Money(double(Money(amount))/double(m));
}
Money operator/(double d){
return Money(double(Money(amount))/d);
}
Money operator*(double d){
return Money(double(Money(amount))*d);
}
operator double()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int i, j, count=0;
char sol[36];
for(i=0,j=0;i<amount.length();i++)
{
if(isdigit(amount[i]) || amount[i]=='.')
sol[j++]=amount[i];
if(amount[i]=='.' || amount[i]==',')
count++;
}
sol[amount.length()-count+1]='\0';
return (atof(sol));
}
private:
string amount;
};
int main()
{
char choice;
Money m1("$23,435.23"), m2("$5,245.11");
do{
//cin>>m1;
//cin>>m2;
//cout<<endl;
cout<<"First amount is: "<<m1;
cout<<"Second amount is: "<<m2<<endl<<endl;
double scalar=3.14;
double one=m1;
double two=m2;
cout<<"Addition: ";
Money addition=m1+m2;
cout<<addition<<endl;
cout<<endl;
cout<<"Subtraction: ";
Money subtraction=m1-m2;
cout<<subtraction<<endl;
cout<<endl;
cout<<"Division: ";
Money division=m1/m2;
cout<<division<<endl;
cout<<endl;
cout<<"Amount one multiplied by "<<scalar<<" is: ";
Money mult_scalar=m1*scalar;
cout<<mult_scalar<<endl;
cout<<endl;
cout<<"Amount one divided by "<<scalar<<" is: ";
Money div_scalar=m1/scalar;
cout<<div_scalar<<endl;
cout<<endl;
double divi=m1/m2;
cout<<"\nDouble m1/m2 is: "<<divi<<endl;
double multi=m1*m2;
cout<<"Double m1/m2 is: "<<multi<<endl;
cout<<"Again (y/n): ";
cin>>choice;
}while(choice=='y' || choice=='Y');
cout<<"Have a nice day: ";
exit(1);
_getch();
return 0;
}
ostream& operator<<(ostream& out, Money& m){
out<<m.amount<<endl;
return out;
}
istream& operator>>(istream& in, Money& m){
cout<<"Enter an amount of money: ";
getline(in,m.amount);
return in;
}
|