一道C++题目,要的急,谢谢强人了!

chc880 2006-06-02 06:05:27
宿舍管理系统:
1、宿舍有四个房间,每个房间可住学生<=4人,要求使用如下的链表存储结构:学号、姓名、房间号、后续指针(NEXT);
2、整个链表按房间号有序的排列,实现学生的入住、退房和查询;
3、按给定学号、姓名、房号查询。

以下是一个人写的(狂错。。。),要我帮忙,可是程序实在太。。。我能力有限,而他后天就要交了,请大虾们帮忙啊~~~如果实在对以下程序不堪入目,希望大虾能重新写一个。谢谢了



#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define SN sizeof(struct student)
struct student
{
int number[4];
char name[20];
int room_number[4];
struct student *next;
};
int n;
struct student *creat(void)
{
int i,n=0;
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student*)malloc(SN);
for(i=0;i<4;i++)
{
scanf("%d,%s,%d",&p1->number[i],&p1->name[i],&p1->room_number[i]);
}
head=NULL;
while(p1->number[0]!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(SN);
for(i=0;i<4;i++)
{
scanf("%d%s%d",&p1->number[i],&p1->name[i],&p1->room_number[i]);
}
}
p2->next=NULL;
return(head);
}
struct student *inputroom(struct student *head)
{
int i;
struct student *p1,*p2;
int number[4];
char name[20];
int room_number[4];
p1=head;
while(head!=NULL)
{
for(i=0;i<4;i++)
if(p1->number[i]!=0)
{scanf("%ld,%s,%d",&p1->number[i],&p1->name[i],&p1->room_number[i]);
head=p1;
return(head);
}
p1=p1->next;

}
printf("There is no space");
return(head);
}
struct student *del(struct student *head)
{
int number[4];
int i;
struct student *p1,*p2;
p1=head;
for(i=0;i<4;i++)
{
while(number[i]!=p1->number[i]&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(number[i]==p1->number[i])
printf("delete :%ld\n",number[i]);
else printf("%ld not been found!\n",number[i]);
return(head);
}
}
struct student *xhfine(struct student *head)
{
struct student *p1,*p2;
int number[4]; int i;
printf("input the fine number");
scanf("%ld",&number);
p1=head;
while(head!=NULL)
{
for(i=0;i<4;i++)
{
if(number[i]=p1->number[i])
{printf("%ld,%s,%d",p1->number[i],p1->name[i],p1->room_number[i]);}
p1=p1->next;
}
}
printf("There is no person");
return (head);
}
struct student *xmfine(struct student *head)
{
struct student *p1;
char name[20]; int i;
printf("input the fine name");
scanf("%s",&name);
p1=head;
while(head!=NULL)
{
for(i=0;i<4;i++)
{
if(name[i]=p1->name[i])
{printf("%ld,%s,%d",p1->number[i],p1->name[i],p1->room_number[i]);}
p1=p1->next;
}
}
printf("There is no person");
return(head);
}
struct student *fjfine(struct student *head)
{
struct student *p1;
int room_number[4]; int i;
printf("input the fine room_number");
scanf("%d",&room_number);
p1=head;
while(head!=NULL)
{
for(i=0;i<4;i++)
{
if(room_number[i]=p1->room_number[i])
{printf("%ld,%s,%d",p1->number[i],p1->name[i],p1->room_number[i]);}
p1=p1->next;
}
}
printf("There is no person");
return(head);
}
main()
{
struct student *head;
int number[1],number[2], number[3];
char name1,name2,name3;
int room_number[1],room_number[2], room_number[3];
head=creat();
scanf("%d,%s,%d",&number[1],name1,&room_number[1]);
if(number[1]!=0)
head=inputroom(head,number[1],name2,room_number[1]);
scanf(%d,%s,%d",&number[2],name2,room_number[2]);
if(number[2]!=0)
head=del(head,number[2]);
scanf("%d,%s,%d",&number[3], name3,room_number[3]);
head=fine(head,number);
}
...全文
330 点赞 收藏 18
写回复
18 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
塘外人 2006-06-03
多谢楼上的两位仁兄指点,由于昨晚看完超女快12点了
时间仓促,没怎么调试,所以难免有不足之处

多谢指出不足!
回复
z7swf 2006-06-03
BS求作业的人,作业中哪有问题就单独把问题拿出来问.直接那么多行代码往上一丢~
而且还提出让别人重写一份的要求~~
搞学术研究的人怎么能这样?>
回复
ywchen2000 2006-06-03
楼上的用
string x;
getline(cin,x,'\n');
回复
rabbit729 2006-06-03
to yhmhappy2006(Nathan) ( ) 信誉:100
看了你的代码,顶一下!
不过这里指出一个小瑕疵:输入字符串时用cin不太正确,因为遇到类似这样的输入时
"xiao ming"容易出错。
回复
oosky2004 2006-06-03
强,你可以去楼主那里领钱了。

回复
塘外人 2006-06-03
写完睡觉,
晚安csdn!
回复
塘外人 2006-06-03
学生宿舍管理系统(按楼主的要求完成,无时间写注释):

#include <iostream>

using namespace std;

const int ROOM_NUM = 4;
const int STU_NUM_EACH_ROOM = 4;

enum OP{RESIDEIN=1, OUT, QUERY, QUIT};
enum QUER{BYNO=1, BYNAME, BYROOMNO, ALL, BACK};//查询方式

typedef struct Room{
int roomNo;
int sNum; //该房间现住人数
int sNo[STU_NUM_EACH_ROOM]; //房间里住的学生的学号
struct Room* next;
}Room, *PRoom;

typedef struct Student{
int sNo;
char sName[20];
int roomNo;
struct Student* next;
}Student, *PStudent;

void printMessage(const char* mess)
{
cout << mess;
}
void makeRooms(PRoom& prs)
{
prs = new Room[ROOM_NUM];
if(prs)
{
for(int i=0; i<ROOM_NUM; ++i)
{
prs[i].roomNo = i+1;
prs[i].sNum = 0;
memset(prs[i].sNo,0,sizeof(prs[i].sNo));
prs[i].next = NULL;
}
}
else
{
printMessage("宿舍没盖成功,可能是资金的问题!\n");
exit(1);
}
}
int getRoomNo(PRoom& prs)
{
for(int i=0; i<ROOM_NUM; ++i)
{
if(prs[i].sNum < STU_NUM_EACH_ROOM)
return prs[i].roomNo;
}
return 0;
}
PStudent checkSno(PStudent& pslh, int sno)
{
for(PStudent ps=pslh->next; ps; ps=ps->next)
{
if(ps->sNo == sno)
return ps;
}

return NULL;
}
void resideIn(PStudent& pslh, PRoom& prs)
{
cin.clear();
int roomNo = getRoomNo(prs);
if(roomNo != 0)
{
Student* psNew = new Student;
printMessage("输入学号,姓名: ");
if(cin >> psNew->sNo >> psNew->sName)
{
if(!checkSno(pslh, psNew->sNo))
{
psNew->roomNo = roomNo;
psNew->next = NULL;

PStudent pst = pslh;
while(pst->next)
pst = pst->next;
pst->next = psNew;

for(int i=0; i<STU_NUM_EACH_ROOM; ++i)
if(prs[roomNo-1].sNo[i] == 0)
{
prs[roomNo-1].sNo[i] = psNew->sNo;
break;
}

prs[roomNo-1].sNum++;

pslh->sNo ++;//保存总人数
cout << "入住手续办完,旅途愉快,您的房间是: " << roomNo <<endl;
}
else
{
printMessage("该生学号不正确!\n");
delete psNew;
}
}
else
{
cin.clear();
delete psNew;
}
}
else
{
printMessage("对不起,客满\n");
}
}
void resideOut(PStudent& pslh, PRoom& prs)
{
printMessage("输入退房者学号: ");
int sno;
cin >> sno;
PStudent pst = checkSno(pslh,sno);
if(pst)
{
PStudent ps = pslh;
while(ps->next != pst)
ps = ps->next;
ps->next = pst->next;

int roomNo = pst->roomNo;

for(int i=0; i<STU_NUM_EACH_ROOM; ++i)
if(prs[roomNo-1].sNo[i] == pst->sNo)
{
prs[roomNo-1].sNo[i] =0;
break;
}

prs[roomNo-1].sNum--;
pslh->sNo --;

delete pst;
printMessage("退房成功!\n");
}
else
{
printMessage("该生不存在!\n");
}
}
void queryByNo(PStudent& pslh, PRoom& prs)
{
printMessage(" 输入学号: ");
int sno;
cin >> sno;
PStudent pst = checkSno(pslh,sno);
cout << " 查询结果:" << endl;
if(pst)
{

cout << " 学号:" << sno << endl;
cout << " 姓名:" << pst->sName << endl;
cout << " 房间:" << pst->roomNo << endl;
cout << " ===============" << endl;
}
else
{
printMessage(" 该生不存在!\n");
}
}
void queryByName(PStudent& pslh, PRoom& prs)
{
printMessage(" 输入姓名: ");
char sname[20];
cin >> sname;
bool found=false;
cout << " 查询结果:" << endl;
for(PStudent ps = pslh->next; ps; ps = ps->next)
{
if(strcmp(ps->sName, sname) == 0)
{
cout << " 学号:" << ps->sNo << endl;
cout << " 姓名:" << ps->sName << endl;
cout << " 房间:" << ps->roomNo << endl;
cout << " ===============" << endl;
found = true;
}
}

if(!found)
printMessage(" 该生不存在!\n");
}
void queryByRoomNo(PStudent& pslh, PRoom& prs)
{
printMessage(" 输入房间号: ");
int roomNo;
cin >> roomNo;
cout << " 查询结果:" << endl;
if(roomNo >= ROOM_NUM)
{
printMessage(" 无此房间!\n");
}
else
{

cout << " 房间号:" << roomNo << endl;
cout << " 学生数:" << prs[roomNo-1].sNum <<endl;
for(int i=0; i<STU_NUM_EACH_ROOM; ++i)
if(prs[roomNo-1].sNo[i] != 0)
{
cout << " 学号:" << prs[roomNo-1].sNo[i] << endl;
}
}
}
void queryAll(PStudent& pslh, PRoom& prs)
{
bool found = false;
cout << " 查询结果:" << endl;
for(PStudent ps = pslh->next; ps; ps = ps->next)
{
cout << " 学号:" << ps->sNo << endl;
cout << " 姓名:" << ps->sName << endl;
cout << " 房间:" << ps->roomNo << endl;
cout << " ===============" << endl;
found = true;
}
if(!found)
printMessage(" 无记录\n");
}
void query(PStudent& pslh, PRoom& prs)
{
int queryBy;
printMessage(" 选择查询方式: ");
cin.clear();
while(cin >> queryBy)
{
if(queryBy == BACK)
break;

switch(queryBy)
{
case BYNO:
queryByNo(pslh, prs);
break;
case BYNAME:
queryByName(pslh, prs);
break;
case BYROOMNO:
queryByRoomNo(pslh, prs);
break;
case ALL:
queryAll(pslh, prs);
break;
default:
printMessage(" 选择方式错误,请重新选择!\n");
break;
}
printMessage(" 选择查询方式: ");
}
cin.clear();
}
int main()
{
PRoom pRooms = NULL;
makeRooms(pRooms);

PStudent pStuLinkHeader = new Student;
pStuLinkHeader->sNo = 0;
pStuLinkHeader->next = NULL;

int op;
printMessage("1.入住\n");
printMessage("2.退房\n");
printMessage("3.查询\n");
printMessage("4.退出程序\n");
printMessage("选择服务: ");
while(cin >> op)
{
if(op == QUIT)
break;
switch(op)
{
case RESIDEIN:
resideIn(pStuLinkHeader, pRooms);
break;
case OUT:
resideOut(pStuLinkHeader,pRooms);
break;
case QUERY:
printMessage(" 1.按学号\n");
printMessage(" 2.按名字\n");
printMessage(" 3.按房间号\n");
printMessage(" 4.列出所有\n");
printMessage(" 5.返回上层\n");
query(pStuLinkHeader, pRooms);
break;
default:
printMessage("选择错误,请重新选择!\n");
break;
}
printMessage("选择服务: ");
cin.clear();
}
return 0;
}
回复
chc880 2006-06-03
"BS求作业的人,作业中哪有问题就单独把问题拿出来问.直接那么多行代码往上一丢~
而且还提出让别人重写一份的要求~~
搞学术研究的人怎么能这样?>
"
谢谢你的忠告,其实我昨天晚上自己也写了一份,后来自己分析了一下,把思路理清楚了,很快就写玩了,谢谢CSDN里的朋友了。

谢谢yhmhappy2006(Nathan),分是一定会给的。谢谢了!
回复
chenhu_doc 2006-06-02
还是把思路理清一下再做!!!
这样用一天就可以搞定!!
回复
jxfengzi 2006-06-02
把注释加上,否则你的东西没人想看。

除非你是BS
回复
chc880 2006-06-02
...天啊 ,为什么啊,的确貌似不难,但我快没时间了,这两天,安排满了,那人什么也不会,叫我怎么办啊。
回复
yuanchuang 2006-06-02
题目貌似不难……
回复
塘外人 2006-06-02
看完超女再说
回复
chc880 2006-06-02
兄弟们,帮帮忙啊!!!
回复
chc880 2006-06-02
hehe
回复
jixingzhong 2006-06-02
重新写闲麻烦的说 ...
回复
chc880 2006-06-02
一个程序设计的实习题目。。。
他用C做的。。。
回复
51365133 2006-06-02
请问下你发的这是啥子哦?
回复
相关推荐
发帖
C++ 语言
创建于2007-09-28

6.0w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2006-06-02 06:05
社区公告
暂无公告