📄 main.c
字号:
#include "stdio.h"
//#include "Conio.h"
#include "string.h"
#include "stdlib.h"
#include <stdio.h>
struct student {
char ID[10];
int BN;
char RN[10];
char NAME[10];
struct student *next;
};
typedef struct student stu;
stu* Head;
int menu()
{
printf("1:查看学生信息\n");
printf("2按学号排序:\n");
printf("3:查看宿舍的人员信息\n");
printf("0:退出系统\n");
printf("请选择:\n");
char c;
int ch;
int flag=1;
while (flag)
{
scanf("%c",&c);
ch=(int)c-48;
if (ch>=0&&ch<=3)
flag=0;
else
printf("输入错误,请重新输入:\n");
}
return ch;
}
void sort(stu *head)
{
int time=0;
char id[10];
int bn;
char rn[10];
char name[10];
stu* temp=head;
while (temp->next!=NULL)
{
temp=temp->next;
time++;
}
temp=head;
int i,j,last;
i=time-1;
while (i>0) {
for (j=0;j<i;j++)
{
if (temp->BN>temp->next->BN)
{
strcpy(id,temp->ID);
strcpy(rn,temp->RN);
strcpy(name,temp->NAME);
bn=temp->BN;
strcpy(temp->ID,temp->next->ID);
strcpy(temp->RN,temp->next->RN);
strcpy(temp->NAME,temp->next->NAME);
temp->BN=temp->next->BN;
strcpy(temp->next->ID,temp->ID);
strcpy(temp->next->RN,temp->RN);
strcpy(temp->next->NAME,temp->NAME);
temp->next->BN=temp->BN;
last=j;
}
i=last;
}
}
printf("排序成功!!!\n");
}
void List(stu *head)
{
stu *p;
p=head;
if (p==NULL)
printf("记录为空\n");
else
{
printf("记录如下:\n");
while (p!=NULL) {
printf("%s %d %s %s\n",p->ID,p->BN,p->RN,p->NAME);
p=p->next;
}
}
}
stu *InputNewRecord(stu *node)
{
printf("Input ID\n");
scanf("%s",&node->ID);
printf("Input BN\n");
scanf("%d",&node->BN);
printf("Input RN\n");
scanf("%s",&node->RN);
printf("Input NAME\n");
scanf("%d",&node->NAME);
return node;
}
int OkOrNot(char *name)
{
char c;
printf("请确认想进行此项操作(是请按y或者Y)");
scanf("%c",&c);
if (c=='y'||c=='Y')
return 1;
else
return 0;
}
void AppendNode(stu *head)/*在链表的末尾添加新的节点*/
{
stu *p,*newnode,*last;
if (!OkOrNot("Append")) return;
last=head;
p=head->next;
while (p!=NULL)
{
last=p;
p=p->next;
}
newnode=(stu*)malloc(sizeof(stu));
newnode->next=NULL;
p=InputNewRecord(newnode);
last->next=p;
}
void ShowRD(stu *head)
{
stu *p=head;
int flag=1;
printf("输入你要查看的房间号:\n");
char tp[10];
gets(tp);
for (;p!=NULL;p=p->next)
{
if (strcmp(p->RN,tp)==0)
{
//cout<<p->ID<<"**"<<p->BN<<"**"<<p->RN<<"**"<<p->NAME<<endl;
printf("%s %d %s %s\n",p->ID,p->BN,p->RN,p->NAME);
flag=0;
}
}
if (flag)
{
printf("找不到!\n");
}
}
int main()
{
stu a={"1002",1,"102","jack"};
stu *head;
head=&a;
head->next=NULL;
Head=head;
printf(" 欢迎进入宿舍信息管理系统 \n");
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
begin:
AppendNode(Head);
printf("还要输吗?\n");
char k;
scanf("%c",&k);
if (k=='Y'||k=='y')
goto begin;
int f3=1;
while (f3)
{
switch (menu())
{
case 1:
List(Head);
break;
case 2:
sort(Head);
break;
case 3:
ShowRD(Head);
break;
case 0:
f3=0;
break;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -