Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- #include<string>
- using namespace std;
- int n = 0;
- struct AutoSalon
- {
- string brand;
- int singlePrice;
- };
- AutoSalon cars[30];
- string ArrBrands[30];
- void EnterDataForCars(string brand, int singlePrice, int valueOfAllCars, AutoSalon cars[], int &n);
- void OutputTheValueOfACarBrand(int valueOfAllCars);
- void OutputCarsInAscendingOrder(AutoSalon cars[]);
- void main()
- {
- int choice;
- string brand;
- int singlePrice = 0;
- int valueOfAllCars = 0;
- while (true)
- {
- cout << "\t \t Menu:" << endl;
- cout << "1. Input data for cars. /brand, price/" << endl;
- cout << "2. Output data for cars in increasing order." << endl;
- cout << "3. Output the value of all cars." << endl;
- cout << "4. Exit." << endl;
- do
- {
- cout << "\nYour choice: ";
- cin >> choice;
- } while (choice < 1 || choice > 4);
- switch (choice)
- {
- case 1:
- EnterDataForCars(brand, singlePrice, valueOfAllCars, cars, n);
- break;
- case 2:
- OutputCarsInAscendingOrder(cars);
- break;
- case 3:
- OutputTheValueOfACarBrand(valueOfAllCars);
- break;
- case 4:
- return;
- default: cout << "Such choice does not exist." << endl;
- break;
- }
- }
- }
- void EnterDataForCars(string brand, int singlePrice, int valueOfAllCars, AutoSalon cars[], int &n)
- {
- cout << "Number of cars: ";
- cin >> n;
- cout << "Enter car brand and price for " << n << " cars, please." << endl;
- for (int i = 0; i < n; i++)
- {
- cout << "Enter car brand: ";
- cin.ignore(2, '\n');
- getline(cin, cars[i].brand);
- ArrBrands[i] = cars[i].brand;
- cout << "Enter price: ";
- cin >> singlePrice;
- cars[i].singlePrice = singlePrice;
- }
- }
- void OutputTheValueOfACarBrand(int valueOfAllCarsWithSameBrand)
- {
- string makes[30];
- int makesIndex = 0;
- for (int i = 0; i < n; i++) //всички марки от масива със коли
- {
- bool alreadyAdded = false;
- for (int j = 0; j < makesIndex; j++) //обхожда вече съществуващите марки
- {
- if (makes[j] == cars[i].brand) //марката е вече добавена
- {
- alreadyAdded = true;
- break;
- }
- }
- if (!alreadyAdded)
- {
- makes[makesIndex] = cars[i].brand;
- makesIndex++;
- }
- }
- //oбщата стойност на всяка марка
- for (int i = 0; i < makesIndex; i++) //за всяка марка от масива за марки
- {
- double valueForCarBrand = 0;
- for (int j = 0; j < n; j++)
- {
- if (cars[j].brand == makes[i])
- {
- valueForCarBrand += cars[j].singlePrice;
- }
- }
- cout << "Value of the brand: " << makes[i] << " is " << valueForCarBrand << endl;
- }
- for (int i = 0; i < makesIndex; i++)
- {
- cout << makes[i] << endl;
- }
- for (int i = 0; i < n; i++)
- {
- int count = 1;
- for (int j = i + 1; j < n; j++)
- {
- if (ArrBrands[i] == ArrBrands[j])
- {
- count++;
- if (count > 1)
- {
- valueOfAllCarsWithSameBrand = cars[i].singlePrice + cars[j].singlePrice;
- cout << "The value of all cars " << ArrBrands[i] << " is: " << valueOfAllCarsWithSameBrand << " lv." << endl;
- }
- }
- else
- {
- cout << "The value of all cars " << ArrBrands[j] << " is: " << cars[j].singlePrice << " lv." << endl;
- }
- }
- return;
- }
- }
- void OutputCarsInAscendingOrder(AutoSalon cars[])
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 1; j < n - i; j++)
- {
- if (cars[j - 1].singlePrice > cars[j].singlePrice)
- {
- AutoSalon temp = cars[j];
- cars[j] = cars[j - 1];
- cars[j - 1] = temp;
- }
- }
- }
- for (int j = 0; j < n; j++)
- {
- cout << cars[j].brand << " " << cars[j].singlePrice << " lv." << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment