/* valuta.cpp */ /* Program that reads a exchange rates from file currencies.dat: ** mk 1.0 ** $ 5.7 ** ECU 6.5 ** SKr 0.9 ** ** Vesa Lappalainen 12.5.1998 ======================================== 1. Plan the program: ======================================== Hello this ..... Give the amount and valuta >15 $[ret] 15 $ is 85.5 mk Give the amount and valuta >3 SKr[ret] 3 SKr is 2.1 mk Give the amount and valuta >6[ret] 6 SKr is 4.2 mk Give the amount and valuta >[ret] Good bu.... ======================================== 2. Plan the files you need: ======================================== currencies.dat: ------------ mk 1.0 $ 5.7 SKr 0.7 In this program valuta curses is edited by some text editor because the lack of time in examination! ======================================== 3. Plan the classes you need: ======================================== Objects: (classes) =================== cValuta - amount : double - valuta : string - cCurrencies - handles a collection of valuta - read the file valuta.dat - tells how many mk cInterface - make quiestions - read the answer to valuta - ask cCurrencies to find the sum - display result ======================================== 4. Draw the picture of data structures: ======================================== cInteface ---------- | $ | valuta_str | -------| || $ || valuta cCurrencies || 15 || |---------| ||------|| max_size | 4 | | | number_of | 3 |<--------+---o | currencies items | o | ---------- -----|----- | cValuta v ----------- valuta | mk | 0 amount | 1 | ----------- | $ | 1 | 5.7 | ----------- | Skr | 2 | 0.7 | ----------- | | 3 | | ----------- ======================================== 5. Write the program ======================================== */ #include #include #include #include #include #define MAX_VALUTAS 20 //------------------------------------------------------------------------------ string separate(string &st, char ch=' ',bool backwards=false) // Separates from the string two parts from choosen character // The first part is returned as a function value and the other // part is in st. The dircetion the search ch is chosen by the // flag backwards. // If there is no ch then the whole string is returned and st = "" // Example: let we have st and s; // 1) st = "123 456"; s = separate(st); => st == "456" s == "123" // 2) st = "123"; s = separate(st); => st == "" s == "123" // 3) st = "1 2 3"; // while ( st != "" ) cout << separate(st) << ","; => print 1,2,3, // { size_t p; if ( !backwards ) p = st.find(ch); else p = st.rfind(ch); string first = st.substr(0,p); if ( p == string::npos ) st = ""; else st.erase(0,p+1); return first; } //--------------------------------------------------------------------------- class cValuta { string valuta; double amount; public: // 0123456 int reset(const string &st) { // "mk 1.00" // if st = "mk 2.00" => valuta = "mk" amount = 2.00 , return 0 #1 // if st = "mk" => valuta = "mk" amount = 1.00 , return 1 #2 // if st = " mk" => valuta = "" amount = 1.00 , return 1 #3 // if st = " " => valuta = "" amount = 1.00 , return 1 #4 // if st = "mk 2.00 " => valuta = "mk" amount = 2.00 , return 0 #1 amount = 1.0; // char val[100]; // if ( sscanf(st.c_str(),"%s %lf",val,&amount) != 2 ) return 1; // valuta = val; string tmp = st; valuta = separate(tmp); if ( sscanf(tmp.c_str(),"%lf",&amount) != 1 ) return 1; return 0; } int reset(double d,const string &st) { amount = d; valuta = st; return 0; } cValuta(double d=1.0, const string &st = "mk") { reset(d,st); } cValuta(const string &st) { reset(st); } double getAmount() const { return amount; } const string &getValuta() const { return valuta; } ostream &print(ostream &os) const { // cStreamPre pre(os,2); os << amount << " " << valuta; return os; } int operator==(const cValuta &val) const { return valuta == val.valuta; } }; ostream &operator<<(ostream &os,const cValuta &valuta) { return valuta.print(os); } //--------------------------------------------------------------------------- class cCurrencies { int max_size; int number_of_items; cValuta *items; int reserve(int size_of) { number_of_items = 0; max_size = 0; if ( size_of <= 0 ) return 0; items = new cValuta[size_of]; if ( items != NULL ) max_size = size_of; return max_size == 0; } public: void free() { if ( max_size > 0 ) delete [] items; } cCurrencies(int size_of=0) { reserve(size_of); } cCurrencies(const cCurrencies ¤cies) { reserve(0); substitute(currencies); } ~cCurrencies() { free(); } int add(const cValuta &n) { if ( number_of_items >= max_size ) return 1; items[number_of_items++] = n; return 0; } cCurrencies &substitute(const cCurrencies ¤cies); cCurrencies &operator=(const cCurrencies ¤cies) { return substitute(currencies); } int find(const cValuta &item_to_find) const; int read(); const string &valuta(const string &val); // const; double multiplier(const cValuta &val) const; const string &basic_valuta_name() const { return items[0].getValuta(); } }; cCurrencies &cCurrencies::substitute(const cCurrencies ¤cies) { if ( this == ¤cies ) return *this; if ( max_size != currencies.max_size ) { free(); reserve(currencies.max_size); } number_of_items = 0; for (int i=0; i= 0 ) return items[i].getAmount(); return 1.0; } const string &cCurrencies::valuta(const string &val) // const { static string error("?$?"); int i = find(cValuta(1.0,val)); if ( i >= 0 ) return items[i].getValuta(); return error; } //--------------------------------------------------------------------------- class cIntreface { cCurrencies *currencies; cValuta valuta; string valuta_str; public: cIntreface(cCurrencies *val) : currencies(val) { valuta_str[0] = 0; } void print() { double basic_amount = currencies->multiplier(valuta) * valuta.getAmount(); cValuta basic_valuta(basic_amount,currencies->basic_valuta_name()); cout << valuta << " is " << basic_valuta << endl; } int ask(); }; int cIntreface::ask() { string str; double money; cout << "Give the amount and valuta >"; getline(cin,str); // 15 MK if ( str == "" ) return 1; if ( str == "quit" ) return 1; // sscanf(str.c_str(),"%lf%s",&money,valuta_str); string tmp = separate(str); // tmp = "15", str=MK sscanf(tmp.c_str(),"%lf",&money); if ( str != "" ) valuta_str = str; valuta.reset(money,currencies->valuta(valuta_str)); // 15 mk return 0; } //--------------------------------------------------------------------------- int main(void) { cCurrencies currencies(MAX_VALUTAS); if ( currencies.read() ) { cout << "No file!" << endl; return 1; } cIntreface display(¤cies); while ( display.ask() == 0 ) display.print(); cout << "Thank you!" << endl; return 0; }