Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Sem 3 / Sem 3 / Sem 3

.cpp
Скачиваний:
0
Добавлен:
10.04.2019
Размер:
10.76 Кб
Скачать
яю// Sem 3.cpp: >?@545;O5B B>G:C 2E>40 4;O :>=A>;L=>3> ?@8;>65=8O.

//

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <string>



using namespace std;



class product

{

private:

	char* name;

	int quantity;

public:

	product() {};

	void Set(char* _name, int _quantity)

	{

		name = _name;

		quantity = _quantity;

	};

	char* GetName()

	{

		return name;

	}

	int GetQuantity()

	{

		return quantity;

	}

	void operator+(product p)

	{

		ofstream file;

		file.open("products.txt", ios::app);

		if (!file.is_open())

			cout << "File can not be opened!\n";

		else

		{

			file << p.name << "	" << p.quantity << "\n";

		}

		file.close();

	}

	void operator-(char* filename)

	{

		ifstream file(filename);

		ofstream file_temp("Temp.txt");

		if (!file.is_open())

			cout << "File can not be opened!\n";

		else

		{

			string str, str_temp;

			cin >> str;

			while (getline(file, str_temp))

			{

				if (str_temp != str)

					file_temp << str_temp;

			}

		}

		file.close();

		file_temp.close();

		remove(filename);

		rename("Temp.txt", filename);

	}

	~product() {};

};



int inic(product p [])							//D-F8O 8=8F80;870F88 <0AA820 >1J5:B>2

{

	ifstream prod("products.txt");

	char name [20], quantity;

	int i = 0;

	if (!prod.is_open())						// 5A;8 D09; =5 >B:@KB

		cout << "File can not be opened!\n"; // A>>1I8BL >1 MB><

	else

	{

		while (!prod.eof())

		{

			prod >> name;

			prod >> quantity;

			p[i].Set(name, quantity);

			i++;

		}

	}

	prod.close();

	return i;

}



int main()

{

	setlocale(LC_ALL, " ");

	product P[20];

	int len;

	len = inic(P);



	string name, s;								//02B>@870F8O ?>;L7>20B5;O

	cout << "Enter your username:\n";		//=502B>@87>20==><C ?>;L7>20B5;N 4>ABC?=> B>;L:> GB5=85 ?C;>2

	cin >> name;								//02B>@87>20==><C ?>;L7>20B5;N 4>ABC?=K GB5=85 8 @540:B8@>20=85

	ifstream users("users.txt");				//?C;>2 8 A?8A:0 ?>;L7>20B5;59

	int x = 0;									//A?8A>: ?>;L7>20B5;59 E@0=8BAO 2 D09;5 users.txt

	while (getline(users, s))

	{

		if (s == name)

		{

			cout << "User is found\n";

			x = 1;

			break;

		}

	}

	if (x == 0)

		cout << "User is not found\n" << "You come in as a guest\n";

	char a = 'y';

	while (a == 'y')

	{

		if (x == 1)

		{

			cout << "1. List the resources\n"

				<< "2. Edit resources list\n"

				<< "3. List users\n"

				<< "4. Exit\n";

			char c;

			cin >> c;

			switch (c)

			{

			case '1':

			{

				ifstream prod("products.txt");

				if (!prod.is_open())

					cout << "File can not be opened!\n";

				else

				{

					int i = 0;

					string str;

					while (getline(prod, str))

					{

						cout << str << endl;

					}

					cout << "----------------------------\n\n";

				}

				prod.close();

				break;

			}

			case '2':

			{

				cout << "Possible actions:\n"

					<< "+ <name resource> <quantity>	- adding a new resource\n"

					<< "- <name resource>	- deleting a resource\n";

				char act;

				cin >> act;

				if (act == '+')

				{

					ofstream file;

					file.open("products.txt", ios::app);

					if (!file.is_open())

						cout << "File can not be opened!\n";

					else

					{

						string str, str1;

						cin >> str >> str1;

						file << str << "	" << str1 << "\n";

					}

					file.close();

					inic(P);

				}

				else

				{

					if (act == '-')

					{

						ifstream file("products.txt");

						ofstream file_temp("Temp.txt");

						if (!file.is_open())

							cout << "File can not be opened!\n";

						else

						{

							string str, str_temp, quantity_temp;

							cin >> str;

							while (!file.eof())

							{

								file >> str_temp;

								file >> quantity_temp;

								if (str != str_temp)

								{

									file_temp << str_temp << " " << quantity_temp << endl;

								}

							}

						}

						file.close();

						file_temp.close();

						remove("products.txt");

						rename("Temp.txt", "products.txt");

					}

				}

				break;

			}

			case '3':

			{

				ifstream users("users.txt");

				if (!users.is_open())

					cout << "File can not be opened!\n";

				else

				{

					int i = 0;

					string str;

					while (getline(users, str))

					{

						cout << str << endl;

					}

					cout << "----------------------------\n" << endl;

				}

				users.close();

				break;

			}

			case '4':

			{

				a = 'n';

				break;

			}

			default:

				break;

			}

		}

		else

		{

			cout << "1. List the resources\n"

				<< "2. List users\n"

				<< "3. Exit\n";

			char b;

			cin >> b;

			switch (b)

			{

			case '1':

			{

				ifstream prod("products.txt");

				if (!prod.is_open())

					cout << "File can not be opened!\n";

				else

				{

					int i = 0;

					string str;

					while (getline(prod, str))

					{

						cout << str << endl;

					}

					cout << "----------------------------\n\n";

				}

				prod.close();

				break;

			}

			case '2':

			{

				ifstream users("users.txt");

				if (!users.is_open())

					cout << "File can not be opened!\n";

				else

				{

					int i = 0;

					string str;

					while (getline(users, str))

					{

						cout << str << endl;

					}

					cout << "----------------------------\n" << endl;

				}

				users.close();

				break;

			}

			case '3':

			{

				a = 'n';

				break;

			}

			default:

				break;

			}

		}

	}

    return 0;

}
Соседние файлы в папке Sem 3