лаб1

← разместить еще код

Расшаренный код:

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>
#include <locale>

#define NUMBER_USERS 6
#define NUMBER_FILES 4

using namespace std;

void print(int n_subj, int n_obj, string subjs[], string objs[], string matric[NUMBER_USERS][NUMBER_FILES]) {
	for (int i = 0; i < n_obj; i++) {
		cout << "[" << objs[i] << "] ";
	}
	cout << "\n";
	for (int i = 0; i < n_subj; i++) {
		cout << subjs[i] << ": ";
		for (int j = 0; j < n_obj; j++) {
			cout << " [" << matric[i][j] << "] ";
		}
		cout << "\n";
	}
}


void console(string subjs[], int n_subj, string objs[], int n_obj, string matric[NUMBER_USERS][NUMBER_FILES], int code_in_system) {
	string instruction;
	int file;
	cout << "Введите команду > ";
	cin >> instruction;
	if (instruction == "read") {
		cout << "Номер файла для чтения: ";
		cin >> file;
		if (file < 0 or file > NUMBER_FILES) {
			cout << "Неправильный файл\n";
			console(subjs, n_subj, objs, n_obj, matric, code_in_system);
		}
		else {
			string file_to_read = matric[code_in_system][file - 1];
			if (file_to_read == "read" or file_to_read == "read & write" or file_to_read == "read & grant" or file_to_read == "read & write & grant") {
				cout << "Команда выполнена успешно\n";
				console(subjs, n_subj, objs, n_obj, matric, code_in_system);
			}
			else {
				cout << "Команда не была выполнена\n";
				console(subjs, n_subj, objs, n_obj, matric, code_in_system);
			}
		}
	}
	else if (instruction == "write") {
		cout << "Введите имя файла для записи: ";
		cin >> file;
		if (file < 0 or file > NUMBER_FILES) {
			cout << "Неправильный файл\n";
			console(subjs, n_subj, objs, n_obj, matric, code_in_system);
		}
		else {
			string file_to_read = matric[code_in_system][file - 1];
			if (file_to_read == "write" or file_to_read == "read & write" or file_to_read == "write & grant" or file_to_read == "read & write & grant") {
				cout << "Команда выполнена успешно\n";
				console(subjs, n_subj, objs, n_obj, matric, code_in_system);
			}
			else {
				cout << "Команда не была выполнена\n";
				console(subjs, n_subj, objs, n_obj, matric, code_in_system);
			}
		}
	}
	else if (instruction == "grant") {
		
		cout << "Введите номер файла для передачи прав: ";
		cin >> file;
		string file_to_read = matric[code_in_system][file - 1];
		if (file < 0 or file > NUMBER_FILES) {
			cout << "Неправильный файл\n";
			console(subjs, n_subj, objs, n_obj, matric, code_in_system);
		}

		else if ((file_to_read != "grant") and (file_to_read != "read & grant") and (file_to_read != "write & grant") and (file_to_read != "read & write & grant")) {
			cout << "У вас нет такого права!\n";
			console(subjs, n_subj, objs, n_obj, matric, code_in_system);
		}
		else {
			string right_to;
			string person_to;
			cout << "Какое право вы хотите передать?: ";
			cin >> right_to;
			if (right_to == "read" or right_to == "write") {
				cout << "Введите имя человека, которому хотите передать права: ";
				cin >> person_to;
				int check = 0;
				for (int i = 0; i < n_subj; i++) {
					if (subjs[i] == person_to) {
						check = 1;
						string granted = matric[i][file - 1];
						if (granted == "deny") {
							matric[i][file - 1] = right_to;
						}
						else if (granted == "read") {
							if (right_to == "write") {
								matric[i][file - 1] = "read & write";
							}
						}
						else if (granted == "write") {
							if (right_to == "read") {
								matric[i][file - 1] = "read & write";
							}
						}
						else if (granted == "grant") {
							if (right_to == "read") {
								matric[i][file - 1] = "read & grant";
							}
							else if (right_to == "write") {
								matric[i][file - 1] = "write & grant";
							}
						}
						else if (granted == "read & grant") {
							if (right_to == "write") {
								matric[i][file - 1] = "read & write & grant";
							}
						}
						else if (granted == "write & grant") {
							if (right_to == "read") {
								matric[i][file - 1] = "read & write & grant";
							}
						}
						cout << "\nКоманда выполнена успешно\n";
						print(n_subj, n_obj, subjs, objs, matric);
						console(subjs, n_subj, objs, n_obj, matric, code_in_system);
					}
					//cout << endl;
				}
				if (check == 0) {
					cout << "Такого человека нет в базе данных.\n";
					console(subjs, n_subj, objs, n_obj, matric, code_in_system);
				}
			}
			else {
				cout << "Такого права не существует.\n";
				console(subjs, n_subj, objs, n_obj, matric, code_in_system);
			}
		}
	}
	else if (instruction == "quit")
		cout << "Вы вышли из программы.\n";
	else {
		cout << "Неправильная команда\n";
		console(subjs, n_subj, objs, n_obj, matric, code_in_system);
	}
}

void log_in(string subjs[], int n_subj, string objs[], int n_obj, string matric[NUMBER_USERS][NUMBER_FILES]) {
	string username;
	int code_in_system;
	cout << "Введите ваше имя: ";
	cin >> username;
	int check = 0;
	for (int i = 0; i < n_subj; i++) {
		if (subjs[i] == username){
			check = 1;
			code_in_system = i;
			cout << "\nДобро пожаловать! Вы вошли как " << subjs[code_in_system] << endl;
			cout << "Ваши права:\n";
			for (int j = 0; j < n_obj; j++) {
				cout << objs[j] << " -- " << matric[code_in_system][j] << endl;
			}
			console(subjs, n_subj, objs, n_obj, matric, code_in_system);
			break;
		}
	}
	if (check == 0) {
		cout << "Неправильное имя\n";
		log_in(subjs, n_subj, objs, n_obj, matric);
	}
}

int main()
{
	setlocale(LC_ALL, "russian");
	srand((unsigned)time(NULL));
	
	string subjs[] = { "Admin", "User1", "User2", "User3", "User4", "User5"};
	string objs[] = { "file_1", "file_2", "file_3", "file_4" };
	string rights[] = { "deny", "read", "write", "grant", "read & write", "read & grant", "write & grant", "read & write & grant" };
	string matric[NUMBER_USERS][NUMBER_FILES];
	for (int i = 0; i < NUMBER_USERS; i++) {
		for (int j = 0; j < NUMBER_FILES; j++) {
			if (subjs[i] == "Admin") {
				matric[i][j] = "read & write & grant";
			}
			else {
				int rand_num = rand() % 8;
				matric[i][j] = rights[rand_num];
			}
		}
	}
	print(NUMBER_USERS, NUMBER_FILES, subjs, objs, matric);
	cout << endl;
	log_in(subjs, NUMBER_USERS, objs, NUMBER_FILES, matric);
	cout << endl;
}