首页 新闻 搜索 专区 学院

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

0
悬赏园豆:60 [已解决问题] 解决于 2014-04-13 17:42

1. 模拟实验室人员流动

I  1001  

表示学号为1001同学进入实验室

O  1001 

表示学号为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;

};

class Lablink {

  public:

  Lablink(){ pstu=NULL;next=NULL;}

  Lablink *addstu(Lablink *head,int num);

  Lablink *delstu(Lablink *head,int num);

  void printstu(Lablink *head);

  private:

  Student *pstu;

  Lablink *next;

};

Lablink* Lablink::addstu(Lablink *head,int num){

  Student *p = new Student(num); 

  Lablink *plab = new Lablink;

  plab->pstu = p;

  if (head!=NULL)

  plab->next = head;

  return plab;

}

Lablink* Lablink::delstu(Lablink *head,int num){

  Lablink *pre,*cur;

  pre=cur=head;

  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;

  return head; 

  }

  }

  else {

    pre=cur;

  cur=cur->next;

  }

  }

  return head;

}

void Lablink::printstu(Lablink *head){

  Lablink *cur=head;

  while(cur!=NULL)

  {

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

  cur=cur->next;

  }

}

int main(){

  char action;

  int num;

  Lablink *head=NULL;  //建立一个空链表

  while(cin>>action>>num){

  switch(action){

  case 'I':

  head=head->addstu(head,num);break;

  case 'O':

  head=head->delstu(head,num);break;

  case 'P':

  head->printstu(head);break;

  } 

  }

  return 0;

}

问题补充:

有人帮忙看看么?

张慧慧的主页 张慧慧 | 初学一级 | 园豆:149
提问于:2014-04-12 13:57
< >
分享
最佳答案
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;
};
class Lablink {
public:
Lablink(){ pstu=NULL;next=NULL;}
static void addstu(Lablink * &head,int num);
static void delstu(Lablink * &head,int num);
static void printstu(Lablink * head);
private:
Student *pstu;
Lablink *next;
};
void Lablink::addstu(Lablink* &head,int num){

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

Lablink * tmp = head;
head = head->next;

Student *pstu = tmp->pstu;

tmp->pstu = NULL;

delete pstu;

delete tmp;

return ;

}
else{

Lablink *pre = head, *cur = head->next;
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;

}
void Lablink::printstu(Lablink * head){
if(head == NULL){
cout<<"实验室现在没有人"<<endl;
return ;
}
cout<<"实验室现在有如下学生:"<<endl;
while(head!=NULL)
{
cout<<"学生学号:"<<head->pstu->getnum()<<endl;
head=head->next;
}
}
int main(){
char action;
int num;
Lablink *head=NULL; //建立一个空链表
cout<<"请输入命令:"<<endl;
while(cin>>action>>num){
switch(action){
case 'I':
Lablink::addstu(head,num);
break;
case 'O':
Lablink::delstu(head,num);break;
case 'P':
Lablink::printstu(head);break;
default:
cout<<"输入命令有误"<<endl;
}
cout<<endl<<"请输入命令:"<<endl;
}
system("pause");
return 0;
}

收获园豆:60
凤凰涅磐欲重生 | 菜鸟二级 |园豆:264 | 2014-04-12 22:15

大神,好牛掰,突然觉得程序好完整,很多小细节都有考虑到,但是以我能力勉强能看懂。能不能再追问一下。

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

例如

I  1001   10:23:10

I  1002   10:35:20

O 1002   11:00:00

O 1001   11:15:10

可以化成秒之后相减

张慧慧 | 园豆:149 (初学一级) | 2014-04-12 23:51

大神,好牛掰,突然觉得程序好完整,很多小细节都有考虑到,但是以我能力勉强能看懂。能不能再追问一下。

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

例如  输入

I  1001   10:23:10

I  1002   10:35:20

O 1002   11:00:00

O 1001   11:15:10

可以化成秒之后相减

 

张慧慧 | 园豆:149 (初学一级) | 2014-04-12 23:53

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

凤凰涅磐欲重生 | 园豆:264 (菜鸟二级) | 2014-04-13 00:02

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

张慧慧 | 园豆:149 (初学一级) | 2014-04-13 00:12

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

凤凰涅磐欲重生 | 园豆:264 (菜鸟二级) | 2014-04-13 00:15

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

张慧慧 | 园豆:149 (初学一级) | 2014-04-13 00:19

@张慧慧:

#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');
}

class Lablink {
public:
Lablink(){ pstu=NULL;next=NULL;}
static void addstu(Lablink * &head,int num,string time);
static void delstu(Lablink * &head,int num,string time);
static void printstu(Lablink * head);
private:
Student *pstu;
Lablink *next;
};
void Lablink::addstu(Lablink* &head,int num,string time){

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

Lablink * tmp = head;

tmp->pstu->setLeaveTime(time);

head = head->next;

Student *pstu = tmp->pstu;

tmp->pstu = NULL;

delete pstu;

delete tmp;

return ;

}
else{

Lablink *pre = head, *cur = head->next;
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;

}
void Lablink::printstu(Lablink * head){
if(head == NULL){
cout<<"实验室现在没有人"<<endl;
return ;
}
cout<<"实验室现在有如下学生:"<<endl;
while(head!=NULL)
{
cout<<"学生学号:"<<setw(6)<<head->pstu->getnum()<<" 进入时间:"<<head->pstu->getComeTime()<<endl;
head=head->next;
}
}
int main(){
char action;
int num;
string time;
Lablink *head=NULL; //建立一个空链表
cout<<"请输入命令:"<<endl;
while(cin>>action>>num){
if(action == 'I' || action == 'O'){
cin>>time;
}
switch(action){
case 'I':
Lablink::addstu(head,num,time);
break;
case 'O':
Lablink::delstu(head,num,time);break;
case 'P':
Lablink::printstu(head);break;
default:
cout<<"输入命令有误"<<endl;
}
//cout<<time<<endl;
cout<<endl<<"请输入命令:"<<endl;
}
system("pause");
return 0;
}

凤凰涅磐欲重生 | 园豆:264 (菜鸟二级) | 2014-04-13 01:50

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

张慧慧 | 园豆:149 (初学一级) | 2014-04-13 09:44

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

凤凰涅磐欲重生 | 园豆:264 (菜鸟二级) | 2014-04-13 11:38

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

张慧慧 | 园豆:149 (初学一级) | 2014-04-13 17:31

@张慧慧: 不客气!

凤凰涅磐欲重生 | 园豆:264 (菜鸟二级) | 2014-04-13 17:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册