Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- В един супермаркет се прави малка програма за работа със стоките в него.
- Всяка стока се описва с име, производител, цена и налично количество.
- Въведете число n (5..50).
- Въведете данните за n стоки. Използвайте структура и функции.
- Изведете информация за:
- 1. всички стоки в магазина;
- 2. стоките с производител „Elite“ с цена по-ниска от 5.25 лв;
- 3. стоките с максимална цена от наличните в повече от 100 единици;
- 4. за даден производител средната цена;
- 5. стоките, подредени по нарастваща цена, по метода на мехурчето;
- 6. стоките, подредени по намаляваща цена, по метода на пряката селекция.
- */
- #include "stdAfx.h" // при нужда сменете или премахнете този ред, в зависимост от компилатора
- #include <iostream>
- #include <string>
- using namespace std;
- const int MaxStocks = 50;
- // глобална дефиниция на структурата, за да е видима от функциите извън main()
- struct Stock {
- string Name;
- string Maker;
- float Price;
- float Amount;
- };
- Stock Stocks[MaxStocks + 1]; // правим и масива Stocks глобален, MaxStocks+1 елемента, за да работим с 1..MaxStocks
- void EnterNumberOfStocks(int &n, int aMax)
- {
- do {
- cout << "Enter the number of stocks (5.." << aMax << "): ";
- cin >> n;
- } while (n < 5 || n > aMax);
- };
- void EnterStock(Stock &aStock, int id) // въвеждане на един елемент от тип Stock
- {
- cout << "Enter information for stock " << id << endl;
- cout << " Name: ";
- cin.get(); // flush the cin's buffer
- getline(cin, aStock.Name);
- cout << " Maker: ";
- getline(cin, aStock.Maker);
- cout << " Price: ";
- cin >> aStock.Price;
- cout << " Amount: ";
- cin >> aStock.Amount;
- }
- void EnterAllStocks(Stock StocksArr[], const int size)
- {
- for (int i = 1; i <= size; i++)
- EnterStock(StocksArr[i], i);
- }
- void PrintStock(Stock aStock, const int id)
- {
- cout << " " << id
- << ": name: " << aStock.Name
- << ", maker: " << aStock.Maker
- << ", price: " << aStock.Price
- << ", amount: " << aStock.Amount << endl;
- }
- void PrintAllStocks(Stock StocksArr[], const int size)
- {
- for (int i = 1; i <= size; i++)
- PrintStock(StocksArr[i], i);
- }
- void PrintStocksByMakerAndPrice(Stock StocksArr[], const int size, string aMaker, float aPrice)
- {
- for (int i = 1; i <= size; i++)
- if (StocksArr[i].Maker == aMaker && StocksArr[i].Price < aPrice)
- PrintStock(StocksArr[i], i);
- }
- void PrintMaximalPriceOverAmount(Stock StocksArr[], const int size, float anAmount)
- {
- float maxPrice = 0.0;
- for (int i = 1; i <= size; i++)
- {
- if (StocksArr[i].Amount > anAmount)
- {
- // ако е 1-вата стока с по-голяма наличност от указаната - запомняме веднага
- if (maxPrice == 0)
- maxPrice = StocksArr[i].Price;
- // проверяваме цената дали е по-голяма от най-голямата до момента
- if (StocksArr[i].Price > maxPrice)
- maxPrice = StocksArr[i].Price;
- }
- }
- if (maxPrice == 0.0)
- cout << "No stocks with amount over " << anAmount << endl;
- else
- for (int i = 1; i <= size; i++)
- if (StocksArr[i].Amount > anAmount && StocksArr[i].Price == maxPrice)
- PrintStock(StocksArr[i], i);
- }
- float AveragePriceByMaker(Stock StocksArr[], const int size, string aMaker, int &count)
- {
- count = 0;
- float sum = 0.0;
- for (int i = 1; i <= size; i++)
- if (StocksArr[i].Maker == aMaker)
- {
- count++;
- sum += StocksArr[i].Price;
- }
- if (count > 0) return sum / count;
- else return 0;
- }
- void SortByPriceSelSort(Stock StocksArr[], const int size)
- {
- for (int i = 1; i <= size; i++)
- {
- int max = i;
- for (int j = i; j <= size; j++)
- if (StocksArr[j].Price > StocksArr[max].Price)
- max = j;
- swap(StocksArr[max], StocksArr[i]);
- }
- }
- void SortByPriceBubbleSort(Stock StocksArr[], const int size)
- {
- for (int i = 1; i <= size; i++)
- {
- for (int j = size; j > i; j--)
- if (StocksArr[j].Price < StocksArr[j-1].Price)
- swap(StocksArr[j], StocksArr[j-1]);
- }
- }
- int main()
- {
- int numStocks;
- EnterNumberOfStocks(numStocks, MaxStocks);
- EnterAllStocks(Stocks, numStocks);
- cout << "1. Information for all stocks: " << endl;
- PrintAllStocks(Stocks, numStocks);
- cout << "2. Stocks with maker Elite and price under 5.25:" << endl;
- PrintStocksByMakerAndPrice(Stocks, numStocks, "Elite", 5.25);
- cout << "3. Stocks with maximal price from these with amount over 100:" << endl;
- PrintMaximalPriceOverAmount(Stocks, numStocks, 100);
- string theMaker;
- cout << "4. Average price of stocks from maker (enter it): ";
- cin.get();
- getline(cin, theMaker);
- int count;
- float avg = AveragePriceByMaker(Stocks, numStocks, theMaker, count);
- if (count == 0)
- cout << " No stocks from the maker " << theMaker << endl;
- else
- cout << " Average price of stocks from maker " << theMaker << " is " << avg << endl;
- cout << "5. Stocks, sorted by Price min->max (BubbleSort: ";
- SortByPriceBubbleSort(Stocks, numStocks);
- PrintAllStocks(Stocks, numStocks);
- cout << "6. Stocks, sorted by Price max->min (SelectionSort: ";
- SortByPriceSelSort(Stocks, numStocks);
- PrintAllStocks(Stocks, numStocks);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement