Write a computer program that could be used to track, by lab, which user is logged into whichcomputer:
Lab Number | Computer Station Numbers |
1 | 1-5 |
2 | 2-6 |
3 | 1-4 |
4 | 1-3 |
➢You run four computer labs. Each lab contains computer stations that are numbered as the above table.
➢Each user has a unique ID number. The ID starting with three characters (for example, SWE or DMT), and followed by three digits (like, 001).
➢Whenever a user logs in, the user’s ID, lab number, and the computer station number are transmitted to your system. For example, if user SWE001 logs into station 2 in lab 3, then your system receives (SWE001, 2, 3) as input data. Similarly, when a user SWE001 logs off a station, then your system receives the user id SWE001.
➢If a user who is already logged into a computer attempts to log into a second computer, display "invalid login".If a user attempts to log into a computer whichis already occupied, display "invalid login".If a user who is not included in the database attempts to log out, display "invalid logoff".
输入格式
If user SWE001 is logged into station 2 in lab 3 and user DMT001 is logged into station 1 of lab 4, use + for loggingin,-for loggingoff, and = for end of input:+ SWE001 2 3+ DMT001 1 4-SWE001=
输出格式
The status of all labs (who is logged into which computer). Otherwise, display invalid login or invalid logoff.You need to display the status of all labs evenwhen the input is invalid.
样例输入
样例输出
#include<iostream>
#include<cstring>
using namespace std;
class ComputerLab
{
int Lab_num,Sta_num;
int judge;
public:
ComputerLab(){}
ComputerLab(int x1,int y1)
{
Lab_num = x1;
Sta_num = y1;
judge = 0;
}
void fun(int x)
{
judge = x;
}
int Get_Lab_num() const
{return Lab_num;}
int Get_Sta_num() const
{return Sta_num;}
int Judge() const
{return judge;}
};
class User
{
char user_name[7];
public:
User(){strcpy(user_name,"empty");}
User(char name[])
{
strcpy(user_name,name);
}
char* Get_user_name()
{
return user_name;
}
};
int main()
{
//初始化数组类
int index = 0;
ComputerLab Lab[18];
User user[18];
for(int i = 1;i<=4;i++)
for(int j = 1;j<=6;j++)
{
if(i==1&&j==6) break;
if(i==3&&j==5) break;
if(i==4&&j==4) break;
Lab[index++] = ComputerLab(i,j);
}
char c;
char user_Name[7];
int line,row;
while(cin>>c,c!='=')
{
cin>>user_Name;
int flag = 0;
//上机
if(c=='+')
{
cin>>line>>row;
for(int i = 0;i<18;i++)
{
if(strcmp(user_Name,user[i].Get_user_name())==0)
{
flag = 1;
break;
}
}
int temp=0;
for(int i = 0;i<18;i++)
{
if(Lab[i].Get_Lab_num()==line&&Lab[i].Get_Sta_num()==row)
{
temp = i + 1;
}
if(temp>=1&&!Lab[temp-1].Judge()&&!flag)
{
user[temp-1] = User(user_Name);
Lab[temp-1].fun(1);
flag = 2;
break;
}
}
if(flag!=2)
cout<<"invalid login"<<endl;
}
//下机
if(c=='-')
{
int Index = 0;
for(int i = 0;i<18;i++)
{
if(strcmp(user_Name,user[i].Get_user_name())==0)
{
flag = 1;
Index = i+1;
break;
}
}
if(!flag)
cout<<"invalid logoff"<<endl;
else
{
if(Index>=1)
{
user[Index-1] = User();
Lab[Index-1].fun(0);
}
}
}
//输出所有计算机情况
for(int i = 0;i<18;i++)
{
if(i==0||i==5||i==11||i==15)
{
cout<<Lab[i].Get_Lab_num()<<" ";
}
cout<<Lab[i].Get_Sta_num()<<":"<<" "<<user[i].Get_user_name()<<" ";
if(i==4||i==10||i==14||i==17)
cout<<endl;
}
cout<<endl;
}
return 0;
}