Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 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---------------------------------------------------------------------------\n",
  75.         "First name", "Second name", "Card number", "Good name", "Cost");
  76.    
  77.     while (st.size() > 0) {
  78.         temp = st.top();
  79.         st.pop();
  80.    
  81.         printf("%15s%15s%15d%15s%15d\n",
  82.             temp.firstName,
  83.             temp.secondName,
  84.             temp.cardNumber,
  85.             temp.goodName,
  86.             temp.cost
  87.         );
  88.     }
  89. }
  90.  
  91. void create(stack<Buyer> *st) {
  92.     Buyer temp;
  93.    
  94.     cin
  95.         >> temp.firstName
  96.         >> temp.secondName
  97.         >> temp.cardNumber
  98.         >> temp.goodName
  99.         >> temp.cost;
  100.    
  101.     st->push(temp);
  102. }
  103.  
  104. void remove(stack<Buyer> *st) {
  105.     st->pop();
  106. }
  107.  
  108. void search(stack<Buyer> st, char* firstName) {
  109.     int count = 0;
  110.    
  111.     while (st.size() > 0) {
  112.         Buyer temp = st.top();
  113.         st.pop();
  114.        
  115.         if (!strcmp(temp.firstName, firstName)) {
  116.             count++;
  117.            
  118.             if (count == 1)
  119.                 printf("%15s%15s%15s%15s%15s\n---------------------------------------------------------------------------\n",
  120.                     "First name", "Second name", "Card number", "Good name", "Cost");
  121.        
  122.             printf("%15s%15s%15d%15s%15d\n",
  123.                 temp.firstName,
  124.                 temp.secondName,
  125.                 temp.cardNumber,
  126.                 temp.goodName,
  127.                 temp.cost
  128.             );
  129.         }
  130.     }
  131.    
  132.     if (count == 0) {
  133.         cout << "Buyers not found\n";
  134.     }
  135. }
  136.  
  137. void sort(stack<Buyer> *st) {
  138.     const int COUNT = st->size();
  139.    
  140.     Buyer* arr = new Buyer[COUNT];
  141.    
  142.     int i = 0;
  143.     while (st->size() > 0) {
  144.         Buyer temp = st->top();
  145.         st->pop();
  146.        
  147.         arr[i] = temp;
  148.         i++;
  149.     }
  150.    
  151.     for (i = 0; i < COUNT; i++) {
  152.         int max = 0;
  153.         int maxIndex = 0;
  154.         for (int j = 0; j < COUNT; j++) {
  155.             if (arr[j].cost > max) {
  156.                 max = arr[j].cost;
  157.                 maxIndex = j;
  158.             }
  159.         }
  160.        
  161.         st->push(arr[maxIndex]);
  162.        
  163.         arr[maxIndex].cost = 0;
  164.     }
  165. }
  166.  
  167. void task(stack<Buyer> st) {
  168.     const int COUNT = st.size();
  169.    
  170.     Buyer* arr = new Buyer[COUNT];
  171.    
  172.     int i = 0, j = 0;
  173.     while (st.size() > 0) {
  174.         Buyer temp = st.top();
  175.         st.pop();
  176.        
  177.         arr[i] = temp;
  178.         i++;
  179.     }
  180.    
  181.     for (i = 0; i < COUNT; i++)
  182.         for (j = i + 1; j < COUNT; j++)
  183.             if (
  184.                 !strcmp(arr[i].firstName, arr[j].firstName)
  185.                 &
  186.                 !strcmp(arr[i].secondName, arr[j].secondName)
  187.                 &
  188.                 arr[i].cardNumber == arr[j].cardNumber
  189.                 &
  190.                 !strcmp(arr[i].goodName, arr[j].goodName)
  191.             ) {
  192.                 arr[i].cost += arr[j].cost;
  193.                 arr[j].cost = 0;
  194.             }
  195.    
  196.     for (i = 0; i < COUNT; i++) {
  197.         int max = 0;
  198.         int maxIndex = 0;
  199.         for (j = 0; j < COUNT; j++) {
  200.             if (arr[j].cost > max) {
  201.                 max = arr[j].cost;
  202.                 maxIndex = j;
  203.             }
  204.         }
  205.        
  206.         if (arr[maxIndex].cost != 0)
  207.             st.push(arr[maxIndex]);
  208.        
  209.         arr[maxIndex].cost = 0;
  210.     }
  211.    
  212.     print(st);
  213. }
  214.  
  215. int main(int test, char** args) {
  216.     stack<Buyer> mainStack;
  217.    
  218.     cout << "Laba №3. Write \"help\" for getting command list.\n";
  219.     char input[15];
  220.    
  221.     while (true) {
  222.         cin >> input;
  223.        
  224.         if (!strcmp(input, "help")) {
  225.             cout
  226.                 << "Commands:\n"
  227.                 << "help\n"
  228.                 << "quit\n"
  229.                 << "load [filename]\n"
  230.                 << "write [filename]\n"
  231.                 << "print\n"
  232.                 << "create [firstName] [secondName] [cardNumber] [goodName] [cost]\n"
  233.                 << "remove\n"
  234.                 << "sort\n"
  235.                 << "search [firstName]\n"
  236.                 << "task\n";
  237.         }
  238.         else if (!strcmp(input, "load")) {
  239.             char filename[15];
  240.             cin >> filename;
  241.            
  242.             fileLoad(&mainStack, filename);
  243.         }
  244.         else if (!strcmp(input, "write")) {
  245.             char filename[15];
  246.             cin >> filename;
  247.            
  248.             fileWrite(mainStack, filename);
  249.         }
  250.         else if (!strcmp(input, "print")) {
  251.             print(mainStack);
  252.         }
  253.         else if (!strcmp(input, "create")) {
  254.             create(&mainStack);
  255.         }
  256.         else if (!strcmp(input, "remove")) {
  257.             remove(&mainStack);
  258.         }
  259.         else if (!strcmp(input, "sort")) {
  260.             sort(&mainStack);
  261.         }
  262.         else if (!strcmp(input, "search")) {
  263.             char firstName[15];
  264.             cin >> firstName;
  265.            
  266.             search(mainStack, firstName);
  267.         }
  268.         else if (!strcmp(input, "task")) {
  269.             task(mainStack);
  270.         }
  271.         else if (!strcmp(input, "exit") | !strcmp(input, "quit") | !strcmp(input, "q")) {
  272.             return 0;
  273.         }
  274.         else {
  275.             cout << "Unknown command.\n";
  276.         }
  277.     }
  278.    
  279.     return 0;
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement