# 关于C++的一道题，各路大神请看看，求围观，求完善，求解答。。。。

0

1. 模拟实验室人员流动

I  1001

O  1001

P   1

#include <iostream>

using namespace std;

class Student{

public :

Student(int snum) {

num =snum;

cout<<"学号为"<<snum<<"同学进入实验室"<<endl;

}

~Student() {

cout<<"学号为"<<num<<"同学离开实验室"<<endl;

}

int getnum(){ return num; }

private :

int num;

};

public:

private:

Student *pstu;

};

Student *p = new Student(num);

plab->pstu = p;

return plab;

}

while(cur!=NULL) {

if (cur->pstu->getnum()==num)

{

if(pre==cur){ //删除第一个节点

cur=cur->next;

delete pre->pstu;delete pre;

return cur;

}

else  //删除中间节点

{

pre->next = cur->next ;

delete cur->pstu;delete cur;

}

}

else {

pre=cur;

cur=cur->next;

}

}

}

while(cur!=NULL)

{

cout<<"学号："<<cur->pstu->getnum()<<endl;

cur=cur->next;

}

}

int main(){

char action;

int num;

while(cin>>action>>num){

switch(action){

case 'I':

case 'O':

case 'P':

}

}

return 0;

}

0

#include <iostream>
using namespace std;
class Student{
public :
Student(int snum) {
num =snum;
cout<<"学号为"<<snum<<"同学进入实验室"<<endl;
}
~Student() {
cout<<"学号为"<<num<<"同学离开实验室"<<endl;
}
int getnum(){ return num; }
private :
int num;
};
public:
private:
Student *pstu;
};

while(cur!=NULL){
if(cur->pstu->getnum() == num){
cout<<"学号为"<<num<<"的学生已经在实验室"<<endl;
return ;
}
else{
cur = cur->next;
}
}
plab->pstu = new Student(num);
}
cout<<"实验室现在没有人"<<endl;
return ;
}
else if(head->pstu->getnum() == num){

Student *pstu = tmp->pstu;

tmp->pstu = NULL;

delete pstu;

delete tmp;

return ;

}
else{

while(cur!=NULL){
if(cur->pstu->getnum() == num){
Lablink * tmp = cur;
pre->next = cur->next;
Student *pstu = tmp->pstu;

tmp->pstu = NULL;

delete pstu;

delete tmp;
return ;
}
else{
pre = cur;
cur = cur->next;
}
}
}
cout<<"学号为"<<num<<"的学生不在实验室"<<endl;

}
cout<<"实验室现在没有人"<<endl;
return ;
}
cout<<"实验室现在有如下学生："<<endl;
{
}
}
int main(){
char action;
int num;
cout<<"请输入命令："<<endl;
while(cin>>action>>num){
switch(action){
case 'I':
break;
case 'O':
case 'P':
default:
cout<<"输入命令有误"<<endl;
}
cout<<endl<<"请输入命令："<<endl;
}
system("pause");
return 0;
}

如果在输入数据时增加进入实验室和离开实验的时间，能修改类的设计实现当离开实验室时显示该人在实验室的逗留时间吗。

I  1001   10:23:10

I  1002   10:35:20

O 1002   11:00:00

O 1001   11:15:10

如果在输入数据时增加进入实验室和离开实验的时间，能修改类的设计实现当离开实验室时显示该人在实验室的逗留时间吗。

I  1001   10:23:10

I  1002   10:35:20

O 1002   11:00:00

O 1001   11:15:10

@张慧慧: 可以，这个倒是简单，但你要确认这样一次性就能改到位，不需要其他的更改了

@凤凰涅磐欲重生: 好像还有个挺麻烦的，就是输入某一时刻，检查在这一时刻，有学号为多少的在实验室！这个可以不用做。大神，帮忙做出那个逗留时间就万分感谢啦！

@张慧慧: 知道了，早点休息吧，明早起来应该就能看到结果了

@凤凰涅磐欲重生: 嗯嗯。大神，是个好银。。。同样早点休息

@张慧慧:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Student{
public :
Student(int snum,string time) {
num =snum;
setComeTime(time);
cout<<"学生学号："<<snum<<endl;
cout<<"进入时间："<<comeTime<<endl;

}
~Student() {
cout<<"学生学号："<<num<<endl;
cout<<"进入时间："<<comeTime<<endl;
cout<<"离开时间："<<leaveTime<<endl;
cout<<"逗留时间："<<getStayTime()<<endl;
}
int getnum(){ return num; }
void setComeTime(string time);
string getComeTime();
void setLeaveTime(string time);
string getStayTime();
int getSecond(string time);

private :
int num;
string comeTime;
string leaveTime;
};

void Student::setComeTime(string time){
comeTime = time;
}

string Student::getComeTime(){
return comeTime;
}

void Student::setLeaveTime(string time){
leaveTime = time;
}

string Student::getStayTime(){
int come = getSecond(comeTime);
int leave = getSecond(leaveTime);
if(come> leave){
return "--:--:--";
}else{
int stay = leave - come;
string stayTime = "00:00:00";
stayTime[0] += (int)(stay/36000);
stay %= 36000;
stayTime[1] += (int)(stay/3600);
stay %= 3600;
stayTime[3] += (int)(stay/600);
stay %= 600;
stayTime[4] += (int)(stay/60);
stay %= 60;
stayTime[6] += (int)(stay/10);
stay %= 10;
stayTime[7] += stay;
return stayTime;
}
}

int Student::getSecond(string time){
return (time[0]-'0')*3600*10 + (time[1]-'0')*3600
+ (time[3]-'0')*60*10 + (time[4]-'0')*60
+ (time[6]-'0')*10 + (time[7]-'0');
}

public:
static void delstu(Lablink * &head,int num,string time);
private:
Student *pstu;
};

while(cur!=NULL){
if(cur->pstu->getnum() == num){
cout<<"学号为"<<num<<"的学生已经在实验室"<<endl;
return ;
}
else{
cur = cur->next;
}
}
plab->pstu = new Student(num,time);
}
cout<<"实验室现在没有人"<<endl;
return ;
}
else if(head->pstu->getnum() == num){

tmp->pstu->setLeaveTime(time);

Student *pstu = tmp->pstu;

tmp->pstu = NULL;

delete pstu;

delete tmp;

return ;

}
else{

while(cur!=NULL){
if(cur->pstu->getnum() == num){
Lablink * tmp = cur;

tmp->pstu->setLeaveTime(time);

pre->next = cur->next;
Student *pstu = tmp->pstu;

tmp->pstu = NULL;

delete pstu;

delete tmp;
return ;
}
else{
pre = cur;
cur = cur->next;
}
}
}
cout<<"学号为"<<num<<"的学生不在实验室"<<endl;

}
cout<<"实验室现在没有人"<<endl;
return ;
}
cout<<"实验室现在有如下学生："<<endl;
{
}
}
int main(){
char action;
int num;
string time;
cout<<"请输入命令："<<endl;
while(cin>>action>>num){
if(action == 'I' || action == 'O'){
cin>>time;
}
switch(action){
case 'I':
break;
case 'O':
case 'P':
default:
cout<<"输入命令有误"<<endl;
}
//cout<<time<<endl;
cout<<endl<<"请输入命令："<<endl;
}
system("pause");
return 0;
}

@凤凰涅磐欲重生: 首先，辛苦大神了。。。。谢谢大神的帮助!受益匪浅呢!感谢。。。我在好好理解一下，不懂得的地方，还请指教呐！

@张慧慧: O(∩_∩)O好的

@凤凰涅磐欲重生: 嗯嗯，用vc++运行了。很棒，鼓掌！很感谢啦。。。

@张慧慧: 不客气！

您需要登录以后才能回答，未注册用户请先注册