Phoenix_x

Untitled

Dec 11th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 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.  
  90. for (int i = 0; i < n; i++) //всички марки от масива със коли
  91. {
  92. bool alreadyAdded = false;
  93.  
  94. for (int j = 0; j < makesIndex; j++) //обхожда вече съществуващите марки
  95. {
  96. if (makes[j] == cars[i].brand) //марката е вече добавена
  97. {
  98. alreadyAdded = true;
  99. break;
  100. }
  101. }
  102.  
  103. if (!alreadyAdded)
  104. {
  105. makes[makesIndex] = cars[i].brand;
  106. makesIndex++;
  107. }
  108. }
  109.  
  110. //oбщата стойност на всяка марка
  111. for (int i = 0; i < makesIndex; i++) //за всяка марка от масива за марки
  112. {
  113. double valueForCarBrand = 0;
  114.  
  115. for (int j = 0; j < n; j++)
  116. {
  117. if (cars[j].brand == makes[i])
  118. {
  119. valueForCarBrand += cars[j].singlePrice;
  120. }
  121. }
  122.  
  123. cout << "Value of the brand: " << makes[i] << " is " << valueForCarBrand << endl;
  124. }
  125.  
  126.  
  127. for (int i = 0; i < makesIndex; i++)
  128. {
  129. cout << makes[i] << endl;
  130. }
  131.  
  132.  
  133.  
  134. for (int i = 0; i < n; i++)
  135. {
  136. int count = 1;
  137. for (int j = i + 1; j < n; j++)
  138. {
  139.  
  140. if (ArrBrands[i] == ArrBrands[j])
  141. {
  142. count++;
  143.  
  144. if (count > 1)
  145. {
  146. valueOfAllCarsWithSameBrand = cars[i].singlePrice + cars[j].singlePrice;
  147. cout << "The value of all cars " << ArrBrands[i] << " is: " << valueOfAllCarsWithSameBrand << " lv." << endl;
  148. }
  149. }
  150.  
  151. else
  152. {
  153. cout << "The value of all cars " << ArrBrands[j] << " is: " << cars[j].singlePrice << " lv." << endl;
  154. }
  155. }
  156. return;
  157. }
  158. }
  159.  
  160. void OutputCarsInAscendingOrder(AutoSalon cars[])
  161. {
  162.  
  163. for (int i = 0; i < n; i++)
  164. {
  165. for (int j = 1; j < n - i; j++)
  166. {
  167. if (cars[j - 1].singlePrice > cars[j].singlePrice)
  168. {
  169. AutoSalon temp = cars[j];
  170. cars[j] = cars[j - 1];
  171. cars[j - 1] = temp;
  172. }
  173. }
  174. }
  175.  
  176. for (int j = 0; j < n; j++)
  177. {
  178. cout << cars[j].brand << " " << cars[j].singlePrice << " lv." << endl;
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment