Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.43 KB | None | 0 0
  1. #include <cctype>
  2. #include <ctime>
  3. #include <functional>
  4. #include <iostream>
  5. #include <random>
  6. #include <string>
  7. #include <vector>
  8. #include <windows.h>
  9.  
  10.  
  11. void cls()
  12. {
  13.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  14.     COORD coordScreen = { 0, 0 };
  15.     DWORD cCharsWritten;
  16.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  17.     DWORD dwConSize;
  18.     if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
  19.         return;
  20.     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  21.     if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ) ||
  22.         !GetConsoleScreenBufferInfo( hConsole, &csbi ) ||
  23.         !FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
  24.         return;
  25.     SetConsoleCursorPosition( hConsole, coordScreen );
  26. }
  27.  
  28.  
  29. template <typename T>
  30. T read(const std::string& request)
  31. {
  32.     std::cout << request;
  33.     T value;
  34.     bool repeat = true;
  35.     while (repeat) {
  36.         repeat = false;
  37.         std::cin >> value;
  38.         if(!std::cin) {
  39.             std::cout << "Invalid input\n";
  40.             std::cin.clear();
  41.             repeat = true;
  42.             while(!isspace(std::cin.get()))
  43.                 ;
  44.         }
  45.     }
  46.     return value;
  47. }
  48.  
  49.  
  50. bool ask(const std::string& question)
  51. {
  52.     std::cout << question << '\n';
  53.     char c;
  54.     do {
  55.         c = tolower(read<char>(""));
  56.         if(c != 'y' && c != 'n')
  57.             std::cout << "Invalid answer. Only Y, y, N and n are allowed\n";
  58.     } while(c != 'y' && c != 'n');
  59.     return c == 'y';
  60. }
  61.  
  62.  
  63. void SuperMarket()
  64. {
  65.     static const std::vector<std::pair<std::string, double>> goods = { {"", -1.0},
  66.         {"Milk",   3.5}, {"Beef",      8.0}, {"Cereal", 3.0}, {"Youghurt", 4.5},
  67.         {"Snacks", 1.5}, {"Chocolate", 4.5}, {"Bacon",  8.5}, {"Oranges",  3.3},
  68.         {"Apples", 3.2}, {"Justin Bieber", 1},
  69.     };
  70.     double cost = 0;
  71.     std::cout << "There's the list of what you can buy\n";
  72.     bool buy = true;
  73.     while (buy) {
  74.         for(unsigned i = 1; i < goods.size(); ++i)
  75.             std::cout << i << " = " << goods[i].first << '\n';
  76.         unsigned item = read<unsigned>("");
  77.         if(item >= 1 && item < goods.size()) {
  78.             cls();
  79.             std::cout << "One pack of " << goods[item].first << " costs " <<
  80.                          goods[item].second << " $\n";
  81.             unsigned amount = read<unsigned>("How many do you want to buy?\n");
  82.             std::cout << "You bought " << amount << " so you have to pay " <<
  83.                          goods[item].second * amount <<" $\n";
  84.             cost += goods[item].second * amount;
  85.         } else
  86.             std::cout << "You haven't entered one valid number\n";
  87.         buy = ask("Do you want to buy another item?");
  88.         cls();
  89.     }
  90.     std::cout <<"The final sum is " << cost << " $\n";
  91. }
  92.  
  93.  
  94. void Lottery()
  95. {
  96.     int choice[6];
  97.     std::cout << "Welcome to Lottery!\nEnter six numbers(1 to 6)\n";
  98.     for(int i = 0; i < 6; ++i) {
  99.         int input;
  100.         do {
  101.             input = read<int>("");
  102.             if(input < 1 || 6 < input)
  103.                 std::cout << "Input should be in range 1-6\n";
  104.         } while(input < 1 || 6 < input);
  105.         choice[i] = input;
  106.     }
  107.     std::cout << "The winner numbers are:\n";
  108.  
  109.     static std::mt19937 rg(time(nullptr));
  110.     static std::uniform_int_distribution<> dist(1, 6);
  111.     for(int x =1; x<7 ; x++)
  112.         std::cout << dist(rg) << ' ';
  113.     std::cout << '\n';
  114.     std::cout << " Two numbers   =    50 $\n"
  115.                  " Three numbers =   150 $\n"
  116.                  " Four numbers  =   500 $\n"
  117.                  " Five numbers  =  2000 $\n"
  118.                  " Six numbers   = 10000 $\n";
  119. }
  120.  
  121.  
  122. void AgeCaluclator()
  123. {
  124.     int year = read<int>("Enter the year you were born\n");
  125.     int age = 2014 - year;
  126.     std::cout << "You were born in " << year << " so ... You now are " << age << " years old\n" <<
  127.                  (age < 30?"You are still young...\n":"You are getting old...\n");
  128. }
  129.  
  130.  
  131. void advanced_calculator_impl(std::function<int(int, int)> op)
  132. {
  133.     int current = read<int>("Enter first number\n");
  134.     for(int i = 1; i < 6; ++i) {
  135.         int next = read<int>("Enter next number\n");
  136.         std::cout << "Your sum is " << (current = op(current, next)) << '\n';
  137.     }
  138. }
  139.  
  140. void AdvencedCalculator()
  141. {
  142.     static const std::vector<std::function<int(int, int)>> op = { nullptr,
  143.         std::plus<int>(), std::minus<int>(), std::multiplies<int>(),
  144.     };
  145.     unsigned operation = read<unsigned>("Choose the type of operation\n"
  146.                               "1 for plus | 2 for minus | 3 for multi\n");
  147.     if(0 < operation && operation < op.size()) {
  148.         do {
  149.             cls();
  150.             advanced_calculator_impl(op[operation]);
  151.         } while(ask("Do you want to continue?"));
  152.     }
  153. }
  154.  
  155. void moneycalculator_impl()
  156. {
  157.     static const std::vector<std::pair<std::string, double>> conv = { {"", 0.0/0.0}, //Another dummy
  158.         {"Leu-Euro", 1./4.42}, {"Euro-Leu", 4.42}, {"Lire-Leu", 5.5671}, {"Leu-Lire", 1./5.5671},
  159.     };
  160.     int cur_money = read<int>("Write the money you have\n");
  161.     std::cout << "Choose what you want to convert\n";
  162.     for(unsigned i = 1; i < conv.size(); ++i)
  163.         std::cout << (i != 1?" | ":"") << i << " for " << conv[i].first;
  164.     std::cout << '\n';
  165.     unsigned choice = read<unsigned>("");
  166.     if(choice > 0 && choice < conv.size())
  167.         std::cout << "Your money (" << cur_money << ") converted (" <<
  168.                      conv[choice].first << ") is " << cur_money * conv[choice].second << '\n';
  169.     else
  170.         std::cout << "You haven't entered 1,2,3 or 4\n";
  171. }
  172.  
  173. void moneycalculator()
  174. {
  175.     do {
  176.         cls();
  177.         moneycalculator_impl();
  178.     } while(ask("Do you want to continue?"));
  179. }
  180.  
  181.  
  182. void basicCalculator_impl()
  183. {
  184.     static const std::vector<std::function<int(int, int)>> op = {
  185.         std::plus<int>(), std::minus<int>(), std::multiplies<int>(), std::divides<int>(),
  186.     };
  187.     int first = read<int>("Enter first number\n");
  188.     int second = read<int>("Enter second number\n");
  189.     int type = read<int>("Choose the type (0 = + | 1 = - | 2 = * | 3 = / |)\n");
  190.     if(type >= 0 && type <= 3)
  191.         std::cout << "Your number is: " << op[type](first, second) << '\n';
  192.     else
  193.         std::cout << "Please write 0 ,1, 2 or 3\n";
  194. }
  195.  
  196. void basicCalculator()
  197. {
  198.     do {
  199.         cls();
  200.         basicCalculator_impl();
  201.     } while(ask("Do you want to continue?"));
  202. }
  203.  
  204.  
  205. int main()
  206. {
  207.     const std::vector<std::pair<std::string, std::function<void(void)>>> menu = { {"", nullptr}, //Dummy to avoid index convertion
  208.         {"Lottery",         Lottery},          {"Money Converting",         moneycalculator},
  209.         {"Age Calculator",  AgeCaluclator},    {"Super Market",             SuperMarket},
  210.         {"Basic Calculator", basicCalculator}, {"Multi-Numbers Calculator", AdvencedCalculator},
  211.     };
  212.     std::cout << "Choose what do you want to do...\n\n";
  213.     for(unsigned i = 1; i < menu.size(); ++i) {
  214.         const int margin = 23;
  215.         const int item_margin = margin - menu[i].first.size() / 2;
  216.         std::cout << std::string(margin - 3/*<-{ length*/, ' ') << "<-{" << i << "}->\n" <<
  217.                      std::string(item_margin, ' ') << menu[i].first << "\n\n";
  218.     }
  219.     unsigned choice = read<int>("");
  220.     if(choice > 0 && choice < menu.size()) {
  221.         cls();
  222.         menu[choice].second(); //Call function stored in vector by index "choice"
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement