Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <stack>
- #include <stdio.h>
- using namespace std;
- struct Buyer {
- char firstName[15];
- char secondName[15];
- int cardNumber;
- char goodName[15];
- int cost;
- };
- int fileLoad(stack<Buyer> *st, char* filename) {
- ifstream fin(filename);
- if (fin.is_open()) {
- Buyer *temp;
- while (!fin.eof()) {
- temp = new Buyer;
- fin
- >> temp->firstName
- >> temp->secondName
- >> temp->cardNumber
- >> temp->goodName
- >> temp->cost;
- st->push(*temp);
- }
- fin.close();
- return 1;
- }
- else {
- cout << "No such file: " << filename << ".\n";
- return 0;
- }
- }
- int fileWrite(stack<Buyer> st, char* filename) {
- ofstream fout(filename);
- if (fout) {
- Buyer temp;
- while (st.size() > 0) {
- temp = st.top();
- st.pop();
- fout
- << temp.firstName << "\t"
- << temp.secondName << "\t"
- << temp.cardNumber << "\t"
- << temp.goodName << "\t"
- << temp.cost << "\n";
- }
- fout.close();
- return 1;
- }
- else {
- cout << "No such file \"" << filename << "\".\n";
- return 0;
- }
- }
- void print(stack<Buyer> st) {
- Buyer temp;
- printf("%15s%15s%15s%15s%15s\n---------------------------------------------------------------------------\n",
- "First name", "Second name", "Card number", "Good name", "Cost");
- while (st.size() > 0) {
- temp = st.top();
- st.pop();
- printf("%15s%15s%15d%15s%15d\n",
- temp.firstName,
- temp.secondName,
- temp.cardNumber,
- temp.goodName,
- temp.cost
- );
- }
- }
- void create(stack<Buyer> *st) {
- Buyer temp;
- cin
- >> temp.firstName
- >> temp.secondName
- >> temp.cardNumber
- >> temp.goodName
- >> temp.cost;
- st->push(temp);
- }
- void remove(stack<Buyer> *st) {
- st->pop();
- }
- void search(stack<Buyer> st, char* firstName) {
- int count = 0;
- while (st.size() > 0) {
- Buyer temp = st.top();
- st.pop();
- if (!strcmp(temp.firstName, firstName)) {
- count++;
- if (count == 1)
- printf("%15s%15s%15s%15s%15s\n---------------------------------------------------------------------------\n",
- "First name", "Second name", "Card number", "Good name", "Cost");
- printf("%15s%15s%15d%15s%15d\n",
- temp.firstName,
- temp.secondName,
- temp.cardNumber,
- temp.goodName,
- temp.cost
- );
- }
- }
- if (count == 0) {
- cout << "Buyers not found\n";
- }
- }
- void sort(stack<Buyer> *st) {
- const int COUNT = st->size();
- Buyer* arr = new Buyer[COUNT];
- int i = 0;
- while (st->size() > 0) {
- Buyer temp = st->top();
- st->pop();
- arr[i] = temp;
- i++;
- }
- for (i = 0; i < COUNT; i++) {
- int max = 0;
- int maxIndex = 0;
- for (int j = 0; j < COUNT; j++) {
- if (arr[j].cost > max) {
- max = arr[j].cost;
- maxIndex = j;
- }
- }
- st->push(arr[maxIndex]);
- arr[maxIndex].cost = 0;
- }
- }
- void task(stack<Buyer> st) {
- const int COUNT = st.size();
- Buyer* arr = new Buyer[COUNT];
- int i = 0, j = 0;
- while (st.size() > 0) {
- Buyer temp = st.top();
- st.pop();
- arr[i] = temp;
- i++;
- }
- for (i = 0; i < COUNT; i++)
- for (j = i + 1; j < COUNT; j++)
- if (
- !strcmp(arr[i].firstName, arr[j].firstName)
- &
- !strcmp(arr[i].secondName, arr[j].secondName)
- &
- arr[i].cardNumber == arr[j].cardNumber
- &
- !strcmp(arr[i].goodName, arr[j].goodName)
- ) {
- arr[i].cost += arr[j].cost;
- arr[j].cost = 0;
- }
- for (i = 0; i < COUNT; i++) {
- int max = 0;
- int maxIndex = 0;
- for (j = 0; j < COUNT; j++) {
- if (arr[j].cost > max) {
- max = arr[j].cost;
- maxIndex = j;
- }
- }
- if (arr[maxIndex].cost != 0)
- st.push(arr[maxIndex]);
- arr[maxIndex].cost = 0;
- }
- print(st);
- }
- int main(int test, char** args) {
- stack<Buyer> mainStack;
- cout << "Laba №3. Write \"help\" for getting command list.\n";
- char input[15];
- while (true) {
- cin >> input;
- if (!strcmp(input, "help")) {
- cout
- << "Commands:\n"
- << "help\n"
- << "quit\n"
- << "load [filename]\n"
- << "write [filename]\n"
- << "print\n"
- << "create [firstName] [secondName] [cardNumber] [goodName] [cost]\n"
- << "remove\n"
- << "sort\n"
- << "search [firstName]\n"
- << "task\n";
- }
- else if (!strcmp(input, "load")) {
- char filename[15];
- cin >> filename;
- fileLoad(&mainStack, filename);
- }
- else if (!strcmp(input, "write")) {
- char filename[15];
- cin >> filename;
- fileWrite(mainStack, filename);
- }
- else if (!strcmp(input, "print")) {
- print(mainStack);
- }
- else if (!strcmp(input, "create")) {
- create(&mainStack);
- }
- else if (!strcmp(input, "remove")) {
- remove(&mainStack);
- }
- else if (!strcmp(input, "sort")) {
- sort(&mainStack);
- }
- else if (!strcmp(input, "search")) {
- char firstName[15];
- cin >> firstName;
- search(mainStack, firstName);
- }
- else if (!strcmp(input, "task")) {
- task(mainStack);
- }
- else if (!strcmp(input, "exit") | !strcmp(input, "quit") | !strcmp(input, "q")) {
- return 0;
- }
- else {
- cout << "Unknown command.\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement