Nisheeth

Untitled

Aug 5th, 2011
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.43 KB | None | 0 0
  1. //
  2. //
  3. //Functions.cpp
  4. //
  5. //
  6. //
  7.  
  8.  
  9. #include "Functions.h"
  10.  
  11. void ClearScreen()
  12.   {
  13.   HANDLE                     hStdOut;
  14.   CONSOLE_SCREEN_BUFFER_INFO csbi;
  15.   DWORD                      count;
  16.   DWORD                      cellCount;
  17.   COORD                      homeCoords = { 0, 0 };
  18.  
  19.   hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  20.   if (hStdOut == INVALID_HANDLE_VALUE) return;
  21.  
  22.   /* Get the number of cells in the current buffer */
  23.   if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  24.   cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  25.  
  26.   /* Fill the entire buffer with spaces */
  27.   if (!FillConsoleOutputCharacter(
  28.     hStdOut,
  29.     (TCHAR) ' ',
  30.     cellCount,
  31.     homeCoords,
  32.     &count
  33.     )) return;
  34.  
  35.   /* Fill the entire buffer with the current colors and attributes */
  36.   if (!FillConsoleOutputAttribute(
  37.     hStdOut,
  38.     csbi.wAttributes,
  39.     cellCount,
  40.     homeCoords,
  41.     &count
  42.     )) return;
  43.  
  44.   /* Move the cursor home */
  45.   SetConsoleCursorPosition( hStdOut, homeCoords );
  46.   }
  47.  
  48. void Swap(int &a, int&b)
  49. {
  50.     a = a+b;
  51.     b = a-b;
  52.     a = a-b;
  53. }
  54.  
  55. void Swap(char &a, char &b)
  56. {
  57.     a ^= b ^= a ^= b;
  58. }
  59.  
  60. int PressAnyKey( const char *prompt )
  61.   {
  62.   DWORD        mode;
  63.   HANDLE       hstdin;
  64.   INPUT_RECORD inrec;
  65.   DWORD        count;
  66.   char         default_prompt[] = "Press a key to continue...";
  67.  
  68.   /* Set the console mode to no-echo, raw input, */
  69.   /* and no window or mouse events.              */
  70.   hstdin = GetStdHandle( STD_INPUT_HANDLE );
  71.   if (hstdin == INVALID_HANDLE_VALUE
  72.   || !GetConsoleMode( hstdin, &mode )
  73.   || !SetConsoleMode( hstdin, 0 ))
  74.     return 0;
  75.  
  76.   if (!prompt) prompt = default_prompt;
  77.  
  78.   /* Instruct the user */
  79.   WriteConsole(
  80.     GetStdHandle( STD_OUTPUT_HANDLE ),
  81.     prompt,
  82.     lstrlen( prompt ),
  83.     &count,
  84.     NULL
  85.     );
  86.  
  87.   FlushConsoleInputBuffer( hstdin );
  88.  
  89.   /* Get a single key RELEASE */
  90.   do ReadConsoleInput( hstdin, &inrec, 1, &count );
  91.   while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
  92.  
  93.   /* Restore the original console mode */
  94.   SetConsoleMode( hstdin, mode );
  95.  
  96.   return inrec.Event.KeyEvent.wVirtualKeyCode;
  97.   }
  98.  
  99. void DrawLine(string String, int Len)
  100. {
  101.     int x = 0;
  102.     cout << endl;
  103.     for (int i = 0; i <Len; i++)
  104.     {
  105.         cout << String[x];
  106.         if (x == (String.size()-1))
  107.             x = -1;
  108.         x++;
  109.     }
  110.     if (Len != 80)
  111.         cout << endl;
  112. }
  113.  
  114. int readkey()
  115.   {
  116.   HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );
  117.   DWORD  mode;
  118.   GetConsoleMode( hstdin, &mode );
  119.   SetConsoleMode( hstdin, 0 );
  120.  
  121.   INPUT_RECORD inrec;
  122.   DWORD        count;
  123.  
  124.   do ReadConsoleInput( hstdin, &inrec, 1, &count );
  125.   while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
  126.  
  127.   SetConsoleMode( hstdin, mode );
  128.  
  129.   return inrec.Event.KeyEvent.wVirtualKeyCode;
  130.   }
  131.  
  132. HANDLE Global::hStdin = GetStdHandle(STD_INPUT_HANDLE);
  133. HANDLE Global::hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  134.  
  135. void SetColor(int a)
  136. {
  137.     switch(a)
  138.     {
  139.         //
  140.         //White & Gray
  141.         case 0:
  142.             SetConsoleTextAttribute (
  143.             Global::hStdout,
  144.             FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN
  145.             );
  146.             break;
  147.         case 1:
  148.             SetConsoleTextAttribute (
  149.             Global::hStdout,
  150.             FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY
  151.             );
  152.             break;
  153.         //
  154.         //Red
  155.         case 2:
  156.             SetConsoleTextAttribute (
  157.             Global::hStdout,
  158.             FOREGROUND_RED
  159.             );
  160.             break;
  161.  
  162.         case 3:
  163.             SetConsoleTextAttribute (
  164.             Global::hStdout,
  165.             FOREGROUND_RED|FOREGROUND_INTENSITY
  166.             );
  167.             break;
  168.         //
  169.         //Blue
  170.         case 4:
  171.             SetConsoleTextAttribute (
  172.             Global::hStdout,
  173.             FOREGROUND_BLUE
  174.             );
  175.             break;
  176.  
  177.         case 5:
  178.             SetConsoleTextAttribute (
  179.             Global::hStdout,
  180.             FOREGROUND_BLUE|FOREGROUND_INTENSITY
  181.             );
  182.             break;
  183.         //
  184.         //Green
  185.         case 6:
  186.             SetConsoleTextAttribute (
  187.             Global::hStdout,
  188.             FOREGROUND_GREEN
  189.             );
  190.             break;
  191.  
  192.         case 7:
  193.             SetConsoleTextAttribute (
  194.             Global::hStdout,
  195.             FOREGROUND_GREEN|FOREGROUND_INTENSITY
  196.             );
  197.             break;
  198.         //
  199.         //Cyan
  200.         case 8:
  201.             SetConsoleTextAttribute (
  202.             Global::hStdout,
  203.             FOREGROUND_BLUE|FOREGROUND_GREEN
  204.             );
  205.             break;
  206.  
  207.         case 9:
  208.             SetConsoleTextAttribute (
  209.             Global::hStdout,
  210.             FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY
  211.             );
  212.             break;
  213.         //
  214.         //Magenta
  215.         case 10:
  216.             SetConsoleTextAttribute (
  217.             Global::hStdout,
  218.             FOREGROUND_BLUE|FOREGROUND_RED
  219.             );
  220.             break;
  221.  
  222.         case 11:
  223.             SetConsoleTextAttribute (
  224.             Global::hStdout,
  225.             FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY
  226.             );
  227.             break;
  228.         //
  229.         //YELLOW
  230.         case 12:
  231.             SetConsoleTextAttribute (
  232.             Global::hStdout,
  233.             FOREGROUND_RED|FOREGROUND_GREEN
  234.             );
  235.             break;
  236.  
  237.         case 13:
  238.             SetConsoleTextAttribute (
  239.             Global::hStdout,
  240.             FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY
  241.             );
  242.             break;
  243.     }
  244. }
  245.  
  246.  
  247. //
  248. //
  249. //Functions.h
  250. //
  251. //
  252. //
  253.  
  254. #ifndef _FUNCTIONS_H_INCLUDED_
  255. #define _FUNCTIONS_H_INCLUDED_
  256.  
  257. #include "default.h"
  258.  
  259. static HANDLE hStdout, hStdin;
  260.  
  261. void ClearScreen();
  262. void Swap(int&, int&);
  263. void Swap (char&, char&);
  264. int PressAnyKey(const char *prompt = "\n\nPress any key to continue . . .");
  265. void DrawLine(string String, int Len = 80);
  266. int readkey();
  267. void SetColor(int a = 1);
  268. struct Global
  269. {
  270.     static HANDLE hStdin;
  271.     static HANDLE hStdout;
  272. };
  273.  
  274. #endif
  275.  
  276.  
  277. //
  278. //
  279. //BANK.cpp
  280. //
  281. //
  282. //
  283.  
  284. #include "Functions.h"
  285. #include <Windows.h>
  286.  
  287. double Round(double a)
  288. {  
  289.     while (int(a)%10 != 0)
  290.     {
  291.         a -= 1;
  292.     }
  293.     return a;
  294. }
  295.  
  296. class Account_t
  297. {
  298. private:
  299.     string AccNumber;
  300.     long double iniBalance;
  301.     long double Balance;
  302.     long double initLoan;
  303.     long double Loan;
  304.     static int Interest;
  305. public:
  306.     Account_t();
  307.     Account_t Set(string accNumber, long int balance);
  308.     string rAccNumber() {return AccNumber;}
  309.     long double rBalance () {return Balance;}
  310.     Account_t Deposit(long int);
  311.     Account_t Withdraw(long int);
  312.     Account_t TakeLoan();
  313.     long double rIniLon() {return initLoan;}
  314.     long double rLoan() {return Loan;}
  315.     long double rInitialBalance() {return iniBalance;}
  316.     Account_t CalcLoan();
  317.     Account_t Repay();
  318. };
  319.  
  320. int Account_t::Interest = 5;
  321.  
  322. Account_t::Account_t()
  323. {
  324.     AccNumber = "N/A";
  325.     Balance = 0;
  326.     Loan = 0;
  327. }
  328.  
  329. Account_t Account_t::Set(string accNumber, long int balance)
  330. {
  331.     AccNumber = accNumber;
  332.     iniBalance = Balance = balance;
  333.     return *this;
  334. }
  335.  
  336. Account_t Account_t::Deposit(long int a)
  337. {
  338.     Balance += a;
  339.     return *this;
  340. }
  341.  
  342. Account_t Account_t::Withdraw(long int a)
  343. {
  344.     Balance -= a;
  345.     return *this;
  346. }
  347.  
  348. Account_t Account_t::TakeLoan()
  349. {
  350.     if (int(Loan) == 0)
  351.     {
  352.         int a,b,c,d,s;
  353.             a = rand()%7 * 1000;
  354.             b = rand()%10 * 100;
  355.             c = rand()%10 * 10;
  356.             d = rand()%10;
  357.             s = a+b+c+d;
  358.             if (s > Balance)
  359.                 s = Balance*0.1;
  360.        
  361.         Loan = initLoan = s;
  362.         Balance += initLoan;
  363.     }
  364.     return *this;
  365. }
  366.  
  367. Account_t Account_t::CalcLoan()
  368. {
  369.     Loan += ((Loan*Interest)/100);
  370.     long double ratio;
  371.     ratio = Loan/Balance;
  372.     ratio /= 10;
  373.     Loan -= ratio*Balance;
  374.     Balance -= ratio*Balance;
  375.     return *this;
  376. }
  377.  
  378. Account_t Account_t::Repay()
  379. {
  380.     int a,b,c,d,s = 0;
  381.     int x = 0;
  382.     double l = Loan;
  383.     if (Loan > 0)
  384.     {
  385.         if ( Loan > 1000)
  386.         {
  387.             x = Loan/1000;
  388.             a = rand()%x * 1000;
  389.             s+=a;
  390.         }
  391.         if ( Loan > 100)
  392.         {
  393.             x = (Loan/100)- Round(l*0.01);
  394.             if ( x != 0)
  395.             {
  396.                 b = rand()%x * 100;
  397.                 s+= b;
  398.             }
  399.         }
  400.         if ( Loan > 10)
  401.         {
  402.             x = Loan/10 - Round(l/10);
  403.             if ( x != 0)
  404.             {
  405.                 c = rand()%x * 10;
  406.                 s+=c;
  407.             }
  408.         }
  409.         d = rand()%10;
  410.         s+= d;
  411.         if (s > Loan)
  412.             s = Loan;
  413.     }
  414.     Loan -= s;
  415.     Balance -= s;
  416.     return *this;
  417. }
  418.  
  419. class Customer
  420. {
  421. public:
  422.     Account_t Account;
  423.     Customer();
  424.     Customer Set();
  425. };
  426.  
  427. Customer customer[10];
  428.  
  429. vector<int> Pre;
  430.  
  431. bool Used(long a)
  432. {
  433.     bool X = true;
  434.     int x;
  435.    
  436.     for (int i = 0; i < Pre.size(); i++)
  437.     {
  438.         if (a != Pre[i])
  439.             X = true;
  440.         else if (a == Pre[i])
  441.         {
  442.             X = false;
  443.             break;
  444.         }
  445.     }
  446.     if (X)
  447.         Pre.push_back(a);
  448.  
  449.     return X;
  450. }
  451.  
  452. Customer::Customer()
  453. {
  454.     long int a, b, c, d, s;
  455.     a = (rand()%10) * 1000;
  456.     b = (rand()%10) * 100;
  457.     c = (rand()%10) * 10;
  458.     d = (rand()%10);
  459.     s = a+b+c+d;
  460.    
  461.     long w,x,y,z, h;
  462.     w = (rand()%10) * 1000;
  463.     x = (rand()%10) * 100;
  464.     y = (rand()%10) * 10;
  465.     z = (rand()%10);
  466.     h = w+x+y+z;
  467.  
  468.     stringstream buffer;
  469.     buffer << h;
  470.    
  471.     Used(h);
  472.     Account.Set(buffer.str() , s);
  473. }
  474.  
  475. Customer Customer::Set()
  476. {
  477.     long int a, b, c, d, s;
  478.     a = (rand()%10) * 1000;
  479.     b = (rand()%10) * 100;
  480.     c = (rand()%10) * 10;
  481.     d = (rand()%10);
  482.     s = a+b+c+d;
  483.  
  484.     long w,x,y,z, h;
  485.     while (true)
  486.     {
  487.        
  488.         w = (rand()%10) * 1000 +1;
  489.         x = (rand()%10) * 100+1;
  490.         y = (rand()%10) * 10;
  491.         z = (rand()%10);
  492.         h = w+x+y+z;
  493.         if (Used(h))
  494.             break;
  495.     }
  496.  
  497.     string Str;
  498.     stringstream buffer;
  499.     buffer.setf(std::ios_base::hex, std::ios::basefield);
  500.     buffer << h;
  501.     Str = buffer.str();
  502.    
  503.     for (int i = 0; i < Str.size();i++)
  504.     {
  505.         if (isalpha(Str[i]))
  506.         {
  507.             Str[i] = toupper(Str[i]);
  508.         }
  509.     }
  510.  
  511.     Account.Set(Str , s);
  512.  
  513.     return *this;
  514. }
  515.  
  516. long  Amount(int i)
  517. {
  518.     long int a, b, c, d, s;
  519.     while(true)
  520.     {
  521.         a = (rand()%10) * 1000;
  522.         b = (rand()%10) * 100;
  523.         c = (rand()%10) * 10;
  524.         d = (rand()%10);
  525.         s = a+b+c+d;
  526.         if (s < customer[i].Account.rBalance() && (customer[i].Account.rBalance()-s) > 500)
  527.             break;
  528.         else if ((customer[i].Account.rBalance()-s) > 500)
  529.         {
  530.             s = 0;
  531.             break;
  532.         }
  533.     }
  534.  
  535.     return s;
  536. }
  537.  
  538. int main ()
  539. {
  540.     SetColor(1);
  541.     Pre.push_back(0);
  542.     srand(time(NULL));
  543.     int chance;
  544.     int Loan;
  545.     long  amount;
  546.     int person ;
  547.    
  548.     vector<double> balances;
  549.  
  550.     for (int i = 0; i < 10; i++)
  551.     {
  552.         customer[i].Set();
  553.     }
  554.        
  555.     for  ( int i = 0; i < 600; i++)
  556.     {
  557.         for (int j = 0; j < 4; j++)
  558.         {
  559.             Loan = rand()%100;
  560.             person = rand()%10;
  561.             chance = rand()%100;
  562.             amount = Amount(person);
  563.             int repay = rand()%100;
  564.            
  565.             if (chance <= 35)
  566.                 customer[person].Account.Withdraw(amount);
  567.                
  568.             else if (chance > 65)
  569.                 customer[person].Account.Deposit(amount);
  570.                
  571.             if (Loan >= 25)
  572.                 customer[person].Account.TakeLoan();
  573.        
  574.             if (customer[person].Account.rLoan() > 0 && i%15 == 0)
  575.                 customer[person].Account.CalcLoan();
  576.            
  577.             if (repay > 65)
  578.                 customer[person].Account.Repay();
  579.        
  580.             for (int i = 0; i < 10; i++)
  581.             {
  582.                 customer[i].Account.Deposit(customer[i].Account.rBalance()*0.01);
  583.             }
  584.         }
  585.         /*
  586.         for (int i = 0; i < 10; i++)
  587.         {
  588.             SetColor(2);
  589.             cout << endl << "Customer " << i+1;
  590.             DrawLine("-", 11);
  591.             SetColor(2);
  592.             cout << "\tAccount Number: " << customer[i].Account.rAccNumber() << endl;
  593.             cout << "\tInitial Balance: " << customer[i].Account.rInitialBalance() << endl;
  594.             cout << "\tBalance:  " << customer[i].Account.rBalance() << endl;
  595.             cout << "\tLoan: " << customer[i].Account.rLoan() << endl;
  596.             SetColor(3);
  597.             DrawLine("_");
  598.             PressAnyKey("");
  599.         }
  600.         */
  601.        
  602.         cout << i << "  ";      //To see at what iteration does the loop end.
  603.     }
  604.     ClearScreen();
  605.     long Bank_Balances = 0;
  606.     for (int i = 0; i < 10; i++)
  607.     {
  608.         balances.push_back(customer[i].Account.rBalance());
  609.     }
  610.     for (int i = 0; i <balances.size();i++)
  611.     {
  612.         Bank_Balances += balances[i];
  613.     }
  614.    
  615.     for (int i = 0; i < 10; i++)
  616.     {
  617.         SetColor(2);
  618.         cout << endl << "Customer " << i+1;
  619.         DrawLine("-", 11);
  620.         SetColor(2);
  621.         cout << "\tAccount Number: " << customer[i].Account.rAccNumber() << endl;
  622.         cout << "\tInitial Balance: " << customer[i].Account.rInitialBalance() << endl;
  623.         cout << "\tBalance:  " << customer[i].Account.rBalance() << endl;
  624.         cout << "\tLoan: " << customer[i].Account.rLoan() << endl;
  625.         PressAnyKey("");
  626.         SetColor(3);
  627.         DrawLine("_");
  628.     }
  629.     cout << "\n\nBank Balance:  " << Bank_Balances;
  630.  
  631.     PressAnyKey();
  632.     return 0;
  633. }
Advertisement
Add Comment
Please, Sign In to add comment