Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cctype>
- #include <ctime>
- #include <functional>
- #include <iostream>
- #include <random>
- #include <string>
- #include <vector>
- #include <windows.h>
- void cls()
- {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- COORD coordScreen = { 0, 0 };
- DWORD cCharsWritten;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- DWORD dwConSize;
- if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
- return;
- dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
- if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ) ||
- !GetConsoleScreenBufferInfo( hConsole, &csbi ) ||
- !FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
- return;
- SetConsoleCursorPosition( hConsole, coordScreen );
- }
- template <typename T>
- T read(const std::string& request)
- {
- std::cout << request;
- T value;
- bool repeat = true;
- while (repeat) {
- repeat = false;
- std::cin >> value;
- if(!std::cin) {
- std::cout << "Invalid input\n";
- std::cin.clear();
- repeat = true;
- while(!isspace(std::cin.get()))
- ;
- }
- }
- return value;
- }
- bool ask(const std::string& question)
- {
- std::cout << question << '\n';
- char c;
- do {
- c = tolower(read<char>(""));
- if(c != 'y' && c != 'n')
- std::cout << "Invalid answer. Only Y, y, N and n are allowed\n";
- } while(c != 'y' && c != 'n');
- return c == 'y';
- }
- void SuperMarket()
- {
- static const std::vector<std::pair<std::string, double>> goods = { {"", -1.0},
- {"Milk", 3.5}, {"Beef", 8.0}, {"Cereal", 3.0}, {"Youghurt", 4.5},
- {"Snacks", 1.5}, {"Chocolate", 4.5}, {"Bacon", 8.5}, {"Oranges", 3.3},
- {"Apples", 3.2}, {"Justin Bieber", 1},
- };
- double cost = 0;
- std::cout << "There's the list of what you can buy\n";
- bool buy = true;
- while (buy) {
- for(unsigned i = 1; i < goods.size(); ++i)
- std::cout << i << " = " << goods[i].first << '\n';
- unsigned item = read<unsigned>("");
- if(item >= 1 && item < goods.size()) {
- cls();
- std::cout << "One pack of " << goods[item].first << " costs " <<
- goods[item].second << " $\n";
- unsigned amount = read<unsigned>("How many do you want to buy?\n");
- std::cout << "You bought " << amount << " so you have to pay " <<
- goods[item].second * amount <<" $\n";
- cost += goods[item].second * amount;
- } else
- std::cout << "You haven't entered one valid number\n";
- buy = ask("Do you want to buy another item?");
- cls();
- }
- std::cout <<"The final sum is " << cost << " $\n";
- }
- void Lottery()
- {
- int choice[6];
- std::cout << "Welcome to Lottery!\nEnter six numbers(1 to 6)\n";
- for(int i = 0; i < 6; ++i) {
- int input;
- do {
- input = read<int>("");
- if(input < 1 || 6 < input)
- std::cout << "Input should be in range 1-6\n";
- } while(input < 1 || 6 < input);
- choice[i] = input;
- }
- std::cout << "The winner numbers are:\n";
- static std::mt19937 rg(time(nullptr));
- static std::uniform_int_distribution<> dist(1, 6);
- for(int x =1; x<7 ; x++)
- std::cout << dist(rg) << ' ';
- std::cout << '\n';
- std::cout << " Two numbers = 50 $\n"
- " Three numbers = 150 $\n"
- " Four numbers = 500 $\n"
- " Five numbers = 2000 $\n"
- " Six numbers = 10000 $\n";
- }
- void AgeCaluclator()
- {
- int year = read<int>("Enter the year you were born\n");
- int age = 2014 - year;
- std::cout << "You were born in " << year << " so ... You now are " << age << " years old\n" <<
- (age < 30?"You are still young...\n":"You are getting old...\n");
- }
- void advanced_calculator_impl(std::function<int(int, int)> op)
- {
- int current = read<int>("Enter first number\n");
- for(int i = 1; i < 6; ++i) {
- int next = read<int>("Enter next number\n");
- std::cout << "Your sum is " << (current = op(current, next)) << '\n';
- }
- }
- void AdvencedCalculator()
- {
- static const std::vector<std::function<int(int, int)>> op = { nullptr,
- std::plus<int>(), std::minus<int>(), std::multiplies<int>(),
- };
- unsigned operation = read<unsigned>("Choose the type of operation\n"
- "1 for plus | 2 for minus | 3 for multi\n");
- if(0 < operation && operation < op.size()) {
- do {
- cls();
- advanced_calculator_impl(op[operation]);
- } while(ask("Do you want to continue?"));
- }
- }
- void moneycalculator_impl()
- {
- static const std::vector<std::pair<std::string, double>> conv = { {"", 0.0/0.0}, //Another dummy
- {"Leu-Euro", 1./4.42}, {"Euro-Leu", 4.42}, {"Lire-Leu", 5.5671}, {"Leu-Lire", 1./5.5671},
- };
- int cur_money = read<int>("Write the money you have\n");
- std::cout << "Choose what you want to convert\n";
- for(unsigned i = 1; i < conv.size(); ++i)
- std::cout << (i != 1?" | ":"") << i << " for " << conv[i].first;
- std::cout << '\n';
- unsigned choice = read<unsigned>("");
- if(choice > 0 && choice < conv.size())
- std::cout << "Your money (" << cur_money << ") converted (" <<
- conv[choice].first << ") is " << cur_money * conv[choice].second << '\n';
- else
- std::cout << "You haven't entered 1,2,3 or 4\n";
- }
- void moneycalculator()
- {
- do {
- cls();
- moneycalculator_impl();
- } while(ask("Do you want to continue?"));
- }
- void basicCalculator_impl()
- {
- static const std::vector<std::function<int(int, int)>> op = {
- std::plus<int>(), std::minus<int>(), std::multiplies<int>(), std::divides<int>(),
- };
- int first = read<int>("Enter first number\n");
- int second = read<int>("Enter second number\n");
- int type = read<int>("Choose the type (0 = + | 1 = - | 2 = * | 3 = / |)\n");
- if(type >= 0 && type <= 3)
- std::cout << "Your number is: " << op[type](first, second) << '\n';
- else
- std::cout << "Please write 0 ,1, 2 or 3\n";
- }
- void basicCalculator()
- {
- do {
- cls();
- basicCalculator_impl();
- } while(ask("Do you want to continue?"));
- }
- int main()
- {
- const std::vector<std::pair<std::string, std::function<void(void)>>> menu = { {"", nullptr}, //Dummy to avoid index convertion
- {"Lottery", Lottery}, {"Money Converting", moneycalculator},
- {"Age Calculator", AgeCaluclator}, {"Super Market", SuperMarket},
- {"Basic Calculator", basicCalculator}, {"Multi-Numbers Calculator", AdvencedCalculator},
- };
- std::cout << "Choose what do you want to do...\n\n";
- for(unsigned i = 1; i < menu.size(); ++i) {
- const int margin = 23;
- const int item_margin = margin - menu[i].first.size() / 2;
- std::cout << std::string(margin - 3/*<-{ length*/, ' ') << "<-{" << i << "}->\n" <<
- std::string(item_margin, ' ') << menu[i].first << "\n\n";
- }
- unsigned choice = read<int>("");
- if(choice > 0 && choice < menu.size()) {
- cls();
- menu[choice].second(); //Call function stored in vector by index "choice"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement