Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Account{
- public:
- string employee;
- string class_room;
- string combined(){
- return class_room+employee;
- }
- };
- class myvector{
- private:
- public:
- Account *employees;
- int size;
- myvector(){
- size=0;
- employees=new Account[size];
- }
- ~myvector(){
- delete[] employees;
- }
- void push(string name, string room){
- size++;
- Account *array=new Account[size];
- for(int i=0; i<size-1; i++){
- array[i] = employees[i];
- }
- Account *newaccount=new Account;
- newaccount->employee=name;
- newaccount->class_room=room;
- array[size-1]= *newaccount;
- delete[] employees;
- employees = array;
- }
- Account at(int index){
- return employees[index];
- }
- int lenght(){
- return size;
- }
- int search(string name){
- for(int i=0; i<size; i++){
- if(employees[i].employee==name){
- return i;
- }
- }
- return -1;
- }
- void erase(string name){
- employees[search(name)]=employees[size-1];
- Account *array=new Account[size-1];
- for(int i=0; i<size-1; i++){
- if(employees[i].employee!=name){
- array[i] = employees[i];
- }
- }
- delete[] employees;
- employees = array;
- size--;
- }
- void clear(){
- delete[] employees;
- size=0;
- Account *employees=new Account[size];
- }
- void sort(){
- bool isUnsorted=true;
- int sequence_count=1;
- while(isUnsorted){
- isUnsorted=false;
- for(int i=0; i<size-sequence_count; i++){
- if((employees[i].combined().compare(employees[i+1].combined()))==1){
- isUnsorted=true;
- Account token=employees[i];
- employees[i]=employees[i+1];
- employees[i+1]=token;
- }
- }
- sequence_count++;
- }
- }
- };
- class AddressBook{
- private:
- myvector accounts;
- public:
- void add(string name, string room){
- if(accounts.search(name)!=-1){
- throw runtime_error("Account already exists");
- }
- accounts.push(name, room);
- }
- void move(string name, string room){
- if(accounts.search(name)==-1){
- throw runtime_error("Account does not exists");
- }
- accounts.erase(name);
- accounts.push(name, room);
- }
- void removal(string name){
- if(accounts.search(name)==-1){
- throw runtime_error("Account does not exists");
- }
- accounts.erase(name);
- }
- void find(string name){
- if(accounts.search(name)==-1){
- throw runtime_error("Account does not exists");
- }
- cout << accounts.at(accounts.search(name)).employee << " is in room " << accounts.at(accounts.search(name)).class_room << endl;
- }
- void list(){
- if(accounts.lenght()<1){
- throw runtime_error("List is empty");
- }
- accounts.sort();
- for(int i=0; i<accounts.lenght(); i++){
- cout << accounts.at(i).employee << " is in room " << accounts.at(i).class_room << endl;
- }
- }
- void clear(){
- accounts.clear();
- }
- };
- void room_check(string room){
- if(room.size()!=4){
- throw runtime_error("Invalid room code");
- }
- if(!isalpha(room[0])){
- throw runtime_error("Invalid room code");
- }
- for(int i=1; i<4; i++){
- if(!isdigit(room[i])){
- throw runtime_error("Invalid room code");
- }
- }
- }
- int main(){
- string command;
- string name;
- string room;
- AddressBook book;
- while (true){
- try{
- cin >> command;
- if (command=="add"||command=="move"||command=="remove"||command=="find"){
- cin >> name;
- if(command=="add"||command=="move"){
- cin >> room;
- }
- }
- if(command=="add"){
- room_check(room);
- book.add(name, room);
- }
- if(command=="move"){
- room_check(room);
- book.move(name, room);
- }
- if(command=="remove"){
- book.removal(name);
- }
- if(command=="find"){
- book.find(name);
- }
- if(command=="list"){
- book.list();
- }
- if(command=="clear"){
- book.clear();
- }
- if(command=="exit"){
- return 0;
- }
- }
- catch (runtime_error &e){
- cerr << "Error: " <<e.what() << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement