#include #include #include #include using namespace std; struct ucet{ string cisloUctu; char kodBanky; float zustatek; }; float round(float f) { int i = f * 1000; int m = i%10; i = i-m; if (m >= 5) { return (float) (i+10) / 1000;} return (float) i / 1000; } void create(vector& ucty){ string cisloUctu; cin >> cisloUctu; for(unsigned i = 0; i< ucty.size(); i++){ if(ucty[i].cisloUctu == cisloUctu){ cout << "create: already exists" << endl; return; } } ucet u; u.cisloUctu = cisloUctu; u.kodBanky = u.cisloUctu[u.cisloUctu.length()-1]; u.zustatek = 0; ucty.push_back(u); cout << "create: ok" << endl; } void deposit(vector& ucty){ string cisloUctu; cin >> cisloUctu; float p; cin >> p; for(unsigned i = 0; i< ucty.size(); i++){ if(ucty[i].cisloUctu == cisloUctu){ ucty[i].zustatek += p; ucty[i].zustatek = round(ucty[i].zustatek); cout << "deposit "; printf("%.2f",p); cout << ": ok" << endl; return; } } cout << "deposit "; printf("%.2f",p); cout << ": no such account" << endl; } void withdraw(vector& ucty){ string cisloUctu; cin >> cisloUctu; float p; cin >> p; for(unsigned i = 0; i< ucty.size(); i++){ if(ucty[i].cisloUctu == cisloUctu){ if(p <= ucty[i].zustatek){ ucty[i].zustatek -= p; ucty[i].zustatek = round(ucty[i].zustatek); cout << "withdraw "; printf("%.2f",p); cout << ": ok" << endl; }else{ cout << "withdraw "; printf("%.2f",p); cout << ": insufficient funds" << endl; } return; } } cout << "withdraw "; printf("%.2f",p); cout << ": no such account" << endl; } void transfer(vector& ucty){ string src, dest; cin >> src; cin >> dest; ucet *srcUcet = NULL; ucet *destUcet = NULL; float p; cin >> p; if(src == dest){ cout << "transfer "; printf("%.2f",p); cout << ": same account" << endl; }else{ for(unsigned i = 0; i< ucty.size(); i++){ if(ucty[i].cisloUctu == src){ srcUcet = &ucty[i]; } if(ucty[i].cisloUctu == dest){ destUcet = &ucty[i]; } } if(!srcUcet || !destUcet){ cout << "transfer "; printf("%.2f",p); cout << ": no such account" << endl; return; } if(srcUcet->zustatek < p){ cout << "transfer "; printf("%.2f",p); cout << ": insufficient funds" << endl; return; } srcUcet->zustatek -= p; srcUcet->zustatek = round(srcUcet->zustatek); destUcet->zustatek += p; destUcet->zustatek = round (destUcet->zustatek); if(srcUcet->kodBanky == destUcet->kodBanky){ cout << "transfer "; printf("%.2f",p); cout << ": ok" << endl; }else{ cout << "transfer "; printf("%.2f",p); cout << ": interbank" << endl; } } } int main () { while(true){ // nacitani uctu int pocetUctu; string operace; vector ucty; // nacteni uctu cin >> pocetUctu; if( pocetUctu == 0 ) break; for(unsigned i = 0; i < pocetUctu; i++){ ucet u; cin >> u.cisloUctu; u.kodBanky = u.cisloUctu[u.cisloUctu.length()-1]; cin >> u.zustatek; ucty.push_back(u); } // operace while(true){ cin >> operace; if(operace == "end") break; if(operace == "create"){ create(ucty); }else if(operace == "deposit"){ deposit(ucty); }else if(operace == "withdraw"){ withdraw(ucty); }else if(operace == "transfer"){ transfer(ucty); } /* cout << "zustatek "; printf("%f",ucty[0].zustatek); cout << endl;*/ } cout << "end" << endl; cout << endl; } cout << "goodbye" << endl; return 0; }