Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #include <stack>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. struct Buyer {
  10.     char firstName[15];
  11.     char secondName[15];
  12.     int cardNumber;
  13.     char goodName[15];
  14.     int cost;
  15. };
  16.  
  17. int fileLoad(stack<Buyer> *st, char* filename) {
  18.     ifstream fin(filename);
  19.    
  20.     if (fin.is_open()) {
  21.         Buyer *temp;
  22.         while (!fin.eof()) {
  23.             temp = new Buyer;
  24.            
  25.             fin
  26.                 >> temp->firstName
  27.                 >> temp->secondName
  28.                 >> temp->cardNumber
  29.                 >> temp->goodName
  30.                 >> temp->cost;
  31.            
  32.             st->push(*temp);
  33.         }
  34.        
  35.         fin.close();
  36.         return 1;
  37.     }
  38.     else {
  39.         cout << "No such file: " << filename << ".\n";
  40.         return 0;
  41.     }
  42. }
  43.  
  44. int fileWrite(stack<Buyer> st, char* filename) {
  45.     ofstream fout(filename);
  46.    
  47.     if (fout) {
  48.         Buyer temp;
  49.        
  50.         while (st.size() > 0) {
  51.             temp = st.top();
  52.             st.pop();
  53.            
  54.             fout
  55.                 << temp.firstName << "\t"
  56.                 << temp.secondName << "\t"
  57.                 << temp.cardNumber << "\t"
  58.                 << temp.goodName << "\t"
  59.                 << temp.cost << "\n";
  60.         }
  61.        
  62.         fout.close();
  63.         return 1;
  64.     }
  65.     else {
  66.         cout << "No such file \"" << filename << "\".\n";
  67.         return 0;
  68.     }
  69. }
  70.  
  71. void print(stack<Buyer> st) {
  72.     Buyer temp;
  73.    
  74.     printf("%15s%15s%15s%15s%15s\n", "First name", "Second name", "Card number", "Good name", "Cost");
  75.    
  76.     while (st.size() > 0) {
  77.         temp = st.top();
  78.         st.pop();
  79.    
  80.         printf("%15s%15s%15d%15s%15d\n",
  81.             temp.firstName,
  82.             temp.secondName,
  83.             temp.cardNumber,
  84.             temp.goodName,
  85.             temp.cost
  86.         );
  87.     }
  88. }
  89.  
  90. void create(stack<Buyer> *st) {
  91.     Buyer temp;
  92.    
  93.     cin
  94.         >> temp.firstName
  95.         >> temp.secondName
  96.         >> temp.cardNumber
  97.         >> temp.goodName
  98.         >> temp.cost;
  99.    
  100.     st->push(temp);
  101. }
  102.  
  103. void remove(stack<Buyer> *st) {
  104.     st->pop();
  105. }
  106.  
  107. void search(stack<Buyer> st) {
  108.     int thisYear = time(NULL) / 60 / 60 / 24 / 365 + 1970;
  109.    
  110.     int count = 0;
  111.    
  112.     while (st.size() > 0) {
  113.         Buyer temp = st.top();
  114.         st.pop();
  115.        
  116.         count++;
  117.         if (count == 1) {
  118.             printf("%15s%15s%15s%15s%15s\n", "First name", "Second name", "Card number", "Good name", "Cost");
  119.         }
  120.        
  121.         int years = thisYear - temp.cost;
  122.         if (
  123.             (!strcmp(temp.goodName, "Кандидат_наук") & years < 35)
  124.             |
  125.             (!strcmp(temp.goodName, "Доктор_наук") & years < 45)
  126.         ) {
  127.             printf("%15s%15s%15d%15s%15d\n",
  128.                 temp.firstName,
  129.                 temp.secondName,
  130.                 temp.cardNumber,
  131.                 temp.goodName,
  132.                 temp.cost
  133.             );
  134.         }
  135.     }
  136.    
  137.     if (count == 0) {
  138.         cout << "Buyers not found\n";
  139.     }
  140. }
  141.  
  142. void sort(stack<Buyer> *st) {
  143.     const int COUNT = st->size();
  144.    
  145.     Buyer* arr = new Buyer[COUNT];
  146.    
  147.     int i = 0;
  148.     while (st->size() > 0) {
  149.         Buyer temp = st->top();
  150.         st->pop();
  151.        
  152.         arr[i] = temp;
  153.         i++;
  154.     }
  155.    
  156.     for (i = 0; i < COUNT; i++) {
  157.         int max = 0;
  158.         int maxIndex = 0;
  159.         for (int j = 0; j < COUNT; j++) {
  160.             if (arr[j].cost > max) {
  161.                 max = arr[j].cost;
  162.                 maxIndex = j;
  163.             }
  164.         }
  165.        
  166.         st->push(arr[maxIndex]);
  167.        
  168.         arr[maxIndex].cost = 0;
  169.     }
  170. }
  171.  
  172. int main(int test, char** args) {
  173.     stack<Buyer> mainStack;
  174.    
  175.     cout << "laba3 shell\n";
  176.     char input[15];
  177.    
  178.     while (true) {
  179.         cin >> input;
  180.        
  181.         if (!strcmp(input, "help")) {
  182.             cout
  183.                 << "Commands:\n"
  184.                 << "help\n"
  185.                 << "quit\n"
  186.                 << "load [filename]\n"
  187.                 << "write [filename]\n"
  188.                 << "print\n"
  189.                 << "create [firstName] [secondName] [cardNumber] [goodName] [cost]\n"
  190.                 << "remove\n"
  191.                 << "sort\n";
  192.         }
  193.         else if (!strcmp(input, "load")) {
  194.             char filename[15];
  195.             cin >> filename;
  196.            
  197.             fileLoad(&mainStack, filename);
  198.         }
  199.         else if (!strcmp(input, "write")) {
  200.             char filename[15];
  201.             cin >> filename;
  202.            
  203.             fileWrite(mainStack, filename);
  204.         }
  205.         else if (!strcmp(input, "print")) {
  206.             print(mainStack);
  207.         }
  208.         else if (!strcmp(input, "create")) {
  209.             create(&mainStack);
  210.         }
  211.         else if (!strcmp(input, "remove")) {
  212.             remove(&mainStack);
  213.         }
  214.         else if (!strcmp(input, "sort")) {
  215.             sort(&mainStack);
  216.         }
  217.         else if (!strcmp(input, "search")) {
  218.             search(mainStack);
  219.         }
  220.         else if (!strcmp(input, "exit") | !strcmp(input, "quit") | !strcmp(input, "q")) {
  221.             return 0;
  222.         }
  223.         else {
  224.             cout << "Unknown command.\n";
  225.         }
  226.     }
  227.    
  228.     return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement