Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string> //стринг са няколко символа, пример: aVeryNice$tring
- using namespace std;
- struct account
- {
- string ime;
- double blv;
- double usd;
- double euro;
- }; //Структурата да е задължително над функциите.
- void writeAndViewContent(account accountsList[50], int &size);
- void printByName(account accountsList[50], int &size);
- void sort(account accountsList[50], int &size);
- void printRich(account accountsList[50], int &size);
- void main()
- {
- account accountsList[50]; //съхранява информация за всички продадени коли (max 50)
- int size = 0; //размера, който ще въведе потребителя (ще има &)
- int option = 0;
- while (true)
- {
- cout << "Izberete 1 za vavezdane na danni vav fajl i masiv i izvezdaneto im." << endl;
- cout << "Izberete 2 za tarsene na akaunt po ime" << endl;
- cout << "Izberete 3 za izvezdane na akaunti sas BLV nad zadadenoto." << endl;
- cout << "Izberete 4 za izvezdane na akaunti sortirani vazhodqshto po otnoshenie na USD" << endl;
- do //ще се изпълни поне веднъж и ще се въвежда избора(1, 2 или 3)
- {
- cout << "Your choice: ";
- cin >> option;
- } while (option < 1 || option > 4); //ако се окаже, че въведеното число е невалидно (< 1 или > 4),
- //тогава подкани потребителя да въвежда отново число, докато то не стане да е между 1 и 3.
- switch (option)
- {
- case 1:
- writeAndViewContent(accountsList, size); //функция за записване на данните върху масив и файл, както и извеждането им.
- break; //ако няма break няма да знае докъде да спре с изпълнението(ще изпълни и следващата функция)
- case 2:
- printByName(accountsList, size);
- break;
- case 3:
- printRich(accountsList, size);
- break;
- case 4:
- sort(accountsList, size);
- break;
- }
- }
- }
- void writeAndViewContent(account accountsList[50], int &size) //с амперсант за да може да му променим стойността
- {
- ofstream writeOnFile; //променлива, с която ще се пише по файла
- cout << "Broj akaunti: ";
- cin >> size;
- //Въвеждане на данните във файла:
- writeOnFile.open("аcoount.txt", ios::binary); //позволява да пишем по файла sales.txt
- //Oсвен това изтрива досегашното съдържание на файла, и ако не съществува такъв създава нов празен
- //ios::binary за да е двуичен файла
- //В този цикъл данните от потребителя се записват във файла и в масива sales
- for (int i = 0; i < size; i++) //цикъл за въвеждане на данните в масив и файл
- {
- cout << "Vavedi informaciq za akaunt " << i + 1 << endl; //i + 1, за да не изпише за първата кола Enter information about car 0
- cout << "Ime: "; //подкана потребителя да въведе автокъща
- cin >> accountsList[i].ime; // За елеменa, искам свойството ime, да е това, което е въведено от потребителя
- writeOnFile << accountsList[i].ime + " "; //записва името(въведена от потребителя) във файла и един интервал за прегледност
- cout << "BLV: "; //Make и Year се записват по същия начин, както dealership
- cin >> accountsList[i].blv;
- writeOnFile << to_string(accountsList[i].blv) + " ";//конвертиране на число към текст за да се пише върху файл
- cout << "USD: ";
- cin >> accountsList[i].usd;
- writeOnFile << to_string(accountsList[i].usd) + " ";
- cout << "Euro: ";
- cin >> accountsList[i].euro;
- writeOnFile << to_string(accountsList[i].euro) + " ";
- writeOnFile << "|"; //за да разделя информацията от един акаунит от друг.
- }
- writeOnFile.close(); //Ако не напишеш close, възникват проблеми
- cout << "* * * * * * *" << endl; //(разделител)
- //output array(or file) content
- for (int i = 0; i < size; i++) //обхожда се всеки акаунт
- {
- cout << accountsList[i].ime << " " << accountsList[i].blv << " " << accountsList[i].usd << " " << accountsList[i].euro << endl;
- }
- }
- void printByName(account accountsList[50], int &size)
- {
- string ime;
- cout << "Ime: ";
- cin >> ime;
- for (int i = 0; i < size; i++) //за всеки акаунт от масива с акаунти
- {
- if (accountsList[i].ime == ime)//ако akaunta е с ime, което потребителя иска
- {
- //принтирай ми данните за въпросния акаунт:
- cout << "BLV: " << accountsList[i].blv << endl;
- cout << "EURO: " << accountsList[i].euro << endl;
- cout << "USD: " << accountsList[i].usd << endl;
- }
- }
- //запитване за нова справка:
- string choice;
- cout << "Zelaete li nova spravka?(y/n)";
- cin >> choice;
- if (choice == "y") //значи потребителя иска да направи пак справка
- {
- printByName(accountsList, size); //изпълни функцията пак
- }
- //ако потребителя не е натиснал y, a нещо друго вместо това
- //тогава не прави нищо и ме върхи към менюто.
- }
- void sort(account accountsList[50], int &size)
- {
- account temp; //помощна променлива за размяне на две променливи
- //bubble sort
- for (int i = 0; i < size; i++)
- {
- for (int j = i + 1; j < size; j++)
- {
- if (accountsList[i].usd > accountsList[j].usd)
- {
- //swap
- temp = accountsList[i];
- accountsList[i] = accountsList[j];
- accountsList[j] = temp;
- }
- }
- }
- //принтирай ми вече сортираните акаунти:
- for (int i = 0; i < size; i++) //обхожда се всеки акаунт
- {
- //принтирай ми данните за акаунта:
- cout << "Akaunt: " << i + 1 << ":" << endl; //i + 1 за да не изпише акаунт 0
- cout << "Ime: " << accountsList[i].ime << endl;
- cout << "BLV: " << accountsList[i].blv << endl;
- cout << "EURO: " << accountsList[i].euro << endl;
- cout << "USD: " << accountsList[i].usd << endl;
- }
- }
- void printRich(account accountsList[50], int &size)
- {
- double suma;
- cout << "Obshta suma (BLV): ";
- cin >> suma;
- for (int i = 0; i < size; i++) //за всеки акаунт от масива с акаунти
- {
- if (accountsList[i].blv > suma)//ако влога с blv е над общата сума
- {
- //принтирай ми данните за акаунта:
- cout << "Ime: " << accountsList[i].ime << endl;
- cout << "BLV: " << accountsList[i].blv << endl;
- cout << "EURO: " << accountsList[i].euro << endl;
- cout << "USD: " << accountsList[i].usd << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement