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", "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) {
- int thisYear = time(NULL) / 60 / 60 / 24 / 365 + 1970;
- int count = 0;
- while (st.size() > 0) {
- Buyer temp = st.top();
- st.pop();
- count++;
- if (count == 1) {
- printf("%15s%15s%15s%15s%15s\n", "First name", "Second name", "Card number", "Good name", "Cost");
- }
- int years = thisYear - temp.cost;
- if (
- (!strcmp(temp.goodName, "Кандидат_наук") & years < 35)
- |
- (!strcmp(temp.goodName, "Доктор_наук") & years < 45)
- ) {
- 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;
- }
- }
- int main(int test, char** args) {
- stack<Buyer> mainStack;
- cout << "laba3 shell\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";
- }
- 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")) {
- search(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