Phoenix_x

Untitled

Dec 11th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7. int n = 0;
  8.  
  9. struct AutoSalon
  10. {
  11.     string brand;
  12.     int singlePrice;
  13. };
  14.  
  15. AutoSalon cars[30];
  16. string ArrBrands[30];
  17.  
  18. void EnterDataForCars(string brand, int singlePrice, int valueOfAllCars, AutoSalon cars[], int &n);
  19. void OutputTheValueOfACarBrand(int valueOfAllCars);
  20. void OutputCarsInAscendingOrder(AutoSalon cars[]);
  21.  
  22. void main()
  23. {
  24.     int choice;
  25.     string brand;
  26.     int singlePrice = 0;
  27.     int valueOfAllCars = 0;
  28.  
  29.     while (true)
  30.     {
  31.         cout << "\t \t Menu:" << endl;
  32.         cout << "1. Input data for cars. /brand, price/" << endl;
  33.         cout << "2. Output data for cars in increasing order." << endl;
  34.         cout << "3. Output the value of all cars." << endl;
  35.         cout << "4. Exit." << endl;
  36.  
  37.         do
  38.         {
  39.             cout << "\nYour choice: ";
  40.             cin >> choice;
  41.         } while (choice < 1 || choice > 4);
  42.  
  43.         switch (choice)
  44.         {
  45.         case 1:
  46.             EnterDataForCars(brand, singlePrice, valueOfAllCars, cars, n);
  47.             break;
  48.  
  49.         case 2:
  50.             OutputCarsInAscendingOrder(cars);
  51.             break;
  52.  
  53.         case 3:
  54.             OutputTheValueOfACarBrand(valueOfAllCars);
  55.             break;
  56.  
  57.         case 4:
  58.             return;
  59.  
  60.         default: cout << "Such choice does not exist." << endl;
  61.             break;
  62.         }
  63.     }
  64. }
  65.  
  66. void EnterDataForCars(string brand, int singlePrice, int valueOfAllCars, AutoSalon cars[], int &n)
  67. {
  68.     cout << "Number of cars: ";
  69.     cin >> n;
  70.     cout << "Enter car brand and price for " << n << " cars, please." << endl;
  71.  
  72.     for (int i = 0; i < n; i++)
  73.     {
  74.         cout << "Enter car brand: ";
  75.         cin.ignore(2, '\n');
  76.         getline(cin, cars[i].brand);
  77.         ArrBrands[i] = cars[i].brand;
  78.         cout << "Enter price: ";
  79.         cin >> singlePrice;
  80.         cars[i].singlePrice = singlePrice;
  81.     }
  82. }
  83.  
  84.  
  85. void OutputTheValueOfACarBrand(int valueOfAllCarsWithSameBrand)
  86. {
  87.     string makes[30];
  88.     int makesIndex = 0;
  89.     bool alreadyAdded = false;
  90.  
  91.     for (int i = 0; i < n; i++) //всички марки от масива със коли
  92.     {
  93.         for (int j = 0; j < makesIndex; j++) //обхожда вече съществуващите марки
  94.         {
  95.             if (cars[j].brand == cars[i].brand) //марката е вече добавена
  96.             {
  97.                 alreadyAdded = true;
  98.             }
  99.         }
  100.  
  101.         if (!alreadyAdded)
  102.         {
  103.             makes[makesIndex] = cars[i].brand;
  104.             makesIndex++;
  105.         }
  106.     }
  107.  
  108.     cout << "****";
  109.     //oбщата стойност на всяка марка
  110.     for (int i = 0; i < makesIndex; i++) //за всяка марка от масива за марки
  111.     {
  112.         double valueForCarBrand = 0;
  113.  
  114.         for (int j = 0; j < n; j++)
  115.         {
  116.             if (cars[j].brand == makes[i])
  117.             {
  118.                 valueForCarBrand += cars[j].singlePrice;
  119.             }
  120.         }
  121.  
  122.         cout << "Value of the brand: " << makes[i] << " is  "<< valueForCarBrand << endl;
  123.     }
  124.  
  125.  
  126.     for (int i = 0; i < makesIndex; i++)
  127.     {
  128.         cout << makes[i] << endl;
  129.     }
  130.  
  131.  
  132.  
  133.     for (int i = 0; i < n; i++)
  134.     {
  135.         int count = 1;
  136.         for (int j = i + 1; j < n; j++)
  137.         {
  138.  
  139.             if (ArrBrands[i] == ArrBrands[j])
  140.             {
  141.                 count++;
  142.  
  143.                 if (count > 1)
  144.                 {
  145.                     valueOfAllCarsWithSameBrand = cars[i].singlePrice + cars[j].singlePrice;
  146.                     cout << "The value of all cars  " << ArrBrands[i] << " is: " << valueOfAllCarsWithSameBrand << " lv." << endl;
  147.                 }
  148.             }
  149.  
  150.             else
  151.             {
  152.                 cout << "The value of all cars " << ArrBrands[j] << " is: " << cars[j].singlePrice << " lv." << endl;
  153.             }
  154.         }
  155.         return;
  156.     }
  157. }
  158.  
  159. void OutputCarsInAscendingOrder(AutoSalon cars[])
  160. {
  161.  
  162.     for (int i = 0; i < n; i++)
  163.     {
  164.         for (int j = 1; j < n - i; j++)
  165.         {
  166.             if (cars[j - 1].singlePrice > cars[j].singlePrice)
  167.             {
  168.                 AutoSalon temp = cars[j];
  169.                 cars[j] = cars[j - 1];
  170.                 cars[j - 1] = temp;
  171.             }
  172.         }
  173.     }
  174.  
  175.     for (int j = 0; j < n; j++)
  176.     {
  177.         cout << cars[j].brand << " " << cars[j].singlePrice << " lv." << endl;
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment