用C语言编写宿舍管理查询系统
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C语言编写宿舍管理查询系统相关的知识,希望对你有一定的参考价值。
A. 采用交互工作方式
B. 建立数据文件 ,数据文件按关键字(姓名、学号、房号)进行排序(冒泡、选择、插入排序等任选一种)
2) 查询菜单: (用二分查找实现以下操作)
A. 按姓名查询
B. 按学号查询
C. 按房号查询
3) 打印任一查询结果(可以连续操作)
要能在winTC下运行的 谢谢了
#include <conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct /*定义结构体数组*/
char num[10]; /*学号*/
char name[20]; /*姓名*/
int roomID; /*房号*/
Student;
Student stu[80]; /*结构体数组变量*/
int menu_select() /*菜单函数*/
char c;
do
system("cls"); /*运行前清屏*/
printf("\t\t************宿舍管理查询系统************\n"); /*菜单选择*/
printf("\t\t | 1. 输入信息 |\n");
printf("\t\t | 2. 显示所有信息 |\n");
printf("\t\t | 3. 排序 |\n");
printf("\t\t | 4. 查询信息 |\n");
printf("\t\t | 0. Quit |\n");
printf("\t\t****************************************\n");
printf("\t\t\t请输入处理种类(0-4):");
c=getchar(); /*读入选择*/
while(c<'0'||c>'4');
return(c-'0'); /*返回选择*/
int Input(Student stud[],int n) /*输入若干条记录*/
int i=0;
char sign,x[10]; /*x[10]为清除多余的数据所用*/
while(sign!='n'&&sign!='N') /*判断*/
printf("\t\t\t学号:"); /*交互输入*/
scanf("\t\t\t%s",stud[n+i].num);
printf("\t\t\t姓名:");
scanf("\t\t\t%s",stud[n+i].name);
printf("\t\t\t房号:");
scanf("\t\t\t%d",&stud[n+i].roomID);
gets(x); /*清除多余的输入*/
printf("\t\t\t还需要继续输入吗?(Y/N)");
scanf("\t\t\t%c",&sign); /*输入判断*/
i++;
return(n+i);
void Display(Student stud[],int n) /*显示所有记录*/
int i;
printf("\t\t\t-----------------------------------\n"); /*格式头*/
printf("\t\t\t学号\t\t姓名\t\t房号\n");
printf("\t\t\t-----------------------------------\n");
for(i=1;i<n+1;i++) /*循环输入*/
printf("\t\t\t%-16s%-15s%d\n",stud[i-1].num,stud[i-1].name,stud[i-1].roomID);
if(i>1&&i%10==0) /*每十个暂停*/
printf("\t\t\t-----------------------------------\n"); /*格式*/
printf("\t\t\t");
system("pause");
printf("\t\t\t-----------------------------------\n");
printf("\t\t\t");
system("pause");
void Sort_by_num(Student stud[],int n) /*按学号排序*/
int i,j,*p,*q,s;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].num,stud[j+1].num)>0)
strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(t,stud[j+1].name);
strcpy(stud[j+1].name,stud[j].name);
strcpy(stud[j].name,t);
p=&stud[j+1].roomID;
q=&stud[j].roomID;
s=*p;
*p=*q;
*q=s;
Display(stu,n);
void Sort_by_name(Student stud[],int n) /*按姓名排序*/
int i,j,*p,*q,s;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].name,stud[j+1].name)>0)
strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(t,stud[j+1].name);
strcpy(stud[j+1].name,stud[j].name);
strcpy(stud[j].name,t);
p=&stud[j+1].roomID;
q=&stud[j].roomID;
s=*p;
*p=*q;
*q=s;
Display(stu,n);
void Sort_by_room(Student stud[],int n) /*按房号排序*/
int i,j,*p,*q,s;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(stud[j].roomID>stud[j+1].roomID)
strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(t,stud[j+1].name);
strcpy(stud[j+1].name,stud[j].name);
strcpy(stud[j].name,t);
p=&stud[j+1].roomID;
q=&stud[j].roomID;
s=*p;
*p=*q;
*q=s;
Display(stu,n);
void Query_a_record_by_name(Student stud[],int n) /*查找并显示一个记录*/
char s[20];
int i=0;
printf("\t\t\t请输入姓名:"); /*交互式输入*/
scanf("\t\t\t%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判断*/
if(i==n)
printf("\t\t\t没有找到相关信息!\n"); /*输入失败信息*/
return;
printf("\t\t\this(her) number:%s\n",stud[i].name);/*输出该学生信息*/
printf("\t\t\this(her) number:%s\n",stud[i].num);
printf("\t\t\this(her) roomID:%d\n",stud[i].roomID);
void Query_a_record_by_num(Student stud[],int n) /*查找并显示一个记录*/
char s[20];
int i=0;
printf("\t\t\t请输入学号:"); /*交互式输入*/
scanf("\t\t\t%s",s);
while(strcmp(stud[i].num,s)!=0&&i<n) i++; /*查找判断*/
if(i==n)
printf("\t\t\t没有找到相关信息!\n"); /*输入失败信息*/
return;
printf("\t\t\this(her) number:%s\n",stud[i].name);/*输出该学生信息*/
printf("\t\t\this(her) number:%s\n",stud[i].num);
printf("\t\t\this(her) roomID:%d\n",stud[i].roomID);
void Query_a_record_by_room(Student stud[],int n) /*查找并显示一个记录*/
int s;;
int i=0;
printf("\t\t\t请输入房号:"); /*交互式输入*/
scanf("\t\t\t%d",&s);
while(stud[i].roomID==s) i++; /*查找判断*/
if(i==n)
printf("\t\t\t没有找到相关信息!\n"); /*输入失败信息*/
return;
printf("\t\t\this(her) number:%s\n",stud[i].name);/*输出该学生信息*/
printf("\t\t\this(her) number:%s\n",stud[i].num);
printf("\t\t\this(her) roomID:%d\n",stud[i].roomID);
void WritetoText(Student stud[],int n) /*将所有记录写入文件*/
int i=0;
FILE *fp; /*定义文件指针*/
if((fp=fopen("stu.txt","w"))==NULL) /*打开文件*/
printf("\t\t\t文件打开失败!!\n");
system("pause");
return;
fprintf(fp,"%d\n",n); /*循环写入数据*/
while(i<n)
fprintf(fp,"%-16s%-15s%d\n",stud[i].num,stud[i].name,stud[i].roomID);
i++;
fclose(fp); /*关闭文件*/
printf("文件保存完毕!\n"); /*返回成功信息*/
void main() /*主函数*/
int n=0,i;
for(;;)
switch(menu_select()) /*选择判断*/
case 1:
printf("\t\t\t输入相关情报\n"); /*输入若干条记录*/
n=Input(stu,n);
break;
case 2:
printf("\t\t\宿舍状况一览\n"); /*显示所有记录*/
Display(stu,n);
break;
case 3:
printf("\t\t\t请输入按什么进行排序(1.姓名、2.学号、3.房号)\n\t\t\t");
scanf("%d",&i);
while(i)
if(i<1||i>3)
printf("错误输入!!\n");
printf("\t\t\t请输入按什么进行排序(1.姓名、2.学号、3.房号)\n\t\t\t");
continue;
switch(i)
case 1:
Sort_by_name(stu,n);
break;
case 2:
Sort_by_num(stu,n);
break;
case 3:
Sort_by_room(stu,n);
break;
break;
printf("\t\t\t排序完毕!\n");
printf("\t\t\t");
WritetoText(stu,n);
system("pause");
break;
case 4:
printf("\t\t\t请输入按什么进行查询(1.姓名、2.学号、3.房号)\n\t\t\t");
scanf("%d",&i);
while(i)
if(i<1||i>3)
printf("错误输入!!\n");
printf("\t\t\t请输入按什么进行查询(1.姓名、2.学号、3.房号)\n\t\t\t");
continue;
switch(i)
case 1:
Query_a_record_by_name(stu,n);
break;
case 2:
Query_a_record_by_num(stu,n);
break;
case 3:
Query_a_record_by_room(stu,n);
break;
break;
printf("\t\t\t");
system("pause");
break;
case 0:
printf("\t\t\t欢迎使用!!\n"); /*结束程序*/
printf("\t\t\t");
system("pause");
exit(0);
参考技术A #include "stdio.h"
#include "stdlib.h" /*标准库函数*/
#include "ctype.h" /*字符操作函数*/
#include "string.h"
typedef struct students
char name[10];
int num;
int room;
ADDRESS;
/******以下是函数原型*******/
int enter(ADDRESS t[]); /*录入宿舍信息*/
void search1(ADDRESS t[],int n); /*按姓名查询宿舍信息*/
void search2(ADDRESS t[],int n); /*按学号查询宿舍信息*/
void search3(ADDRESS t[],int n); /*按房号查询宿舍信息*/
int delete(ADDRESS t[],int n); /*删除一个宿舍信息*/
int add(ADDRESS t[],int n); /*增加一个宿舍信息*/
void sort(ADDRESS t[],int n); /*按姓名降序排序*/
int find(ADDRESS t[],int n,char *s);
void main()
ADDRESS adr[40]; /*定义结构体数组*/<br> int length;<br> char n;<br>printf("********************菜单*********************\n\n");<br> printf(" 1. 录入宿舍信息\n");<br> printf(" 2. 删除宿舍信息\n");<br> printf(" 3. 按姓名查询宿舍信息\n");<br> printf(" 4. 按学号查询宿舍信息\n");<br> printf(" 5. 按房号查询宿舍信息\n");<br> printf(" 6. 退出\n");<br> printf("***********************************************\n");<br> scanf("%c",&n);/*录入选择*/<br> for(;;)/*无限循环*/<br> <br> switch(n) /*选择n作开关语句的条件*/<br> <br> <br> case 1:enter(adr);break; /*录入宿舍信息*/<br> case 2:length=delete(adr,length);break; /*删除*/<br> case 3:search1(adr,length);break; /*姓名查找*/<br> case 4:search2(adr,length);break; /*学号查找*/<br> case 5:search3(adr,length);break; /*房号查找*/<br> case 6:exit(0); /*如n为6则程序结束*/<br>
int enter(ADDRESS t[])
int i;
printf("请输入宿舍信息 \n"); /*提示输入记录*/
printf("name num room\n");
printf("------------------------------------------------\n");
for(i=0;i<40;i++)
scanf("%s%d%d",t[i].name,t[i].num,t[i].room); /*输入记录*/
printf("----------------------------------------------\n");
/*姓名查找宿舍信息*/
void search1(ADDRESS t[],int n)
char name[10]; /*保存待查找工号字符串*/
int i; /*保存查找到结点的序号*/
printf("请输入需要查找的姓名\n");
scanf("%s",name); /*输入待查找姓名*/
i=find(t,n,name);
printf("姓名:%s学号:%d房号:%d",t[i].name,t[i].num,t[i].room);
/*学号查找宿舍信息*/
void search2(ADDRESS t[],int n)
int a; /*保存待查找学号*/
int i,j; /*保存查找到结点的序号*/
printf("请输入需要查找的学号\n");
scanf("%d",a); /*输入待查找工号*/
for(i=0;i<40;i++)/*从第一条记录开始,直到最后一条*/
if(strcmp(a,t[i].num)==0)
/*记录中的学号和待比较的学号是否相等*/
j=i; /*相等,则返回该记录的下标号,程序提前结结束*/<br> <br> printf("姓名:%s学号:%d房号:%d",t[j].name,t[j].num,t[j].room); <br>
/*房号查找宿舍信息*/
void search1(ADDRESS t[],int n)
int b; /*保存待查找房号*/
int i,j; /*保存查找到结点的序号*/
printf("请输入需要查找的姓名\n");
scanf("%s",b); /*输入待查找房号*/
for(i=0;i<40;i++)/*从第一条记录开始,直到最后一条*/
if(strcmp(b,t[i].room)==0)
/*记录中的房号和待比较的房号是否相等*/
j=i; /*相等,则返回该记录的下标号,程序提前结结束*/<br> <br> printf("姓名:%s学号:%d房号:%d",t[j].name,t[j].num,t[j].room); <br>
int delete(ADDRESS t[],int n)
char s[10]; /*要删除记录的姓名*/
int ch=0;
int i,j;
printf("请输入需要删除的姓名\n"); /*提示信息*/
scanf("%s",s);/*输入姓名*/
i=find(t,n,s); /*调用find函数*/
if(i>n-1) /*如果i>n-1超过了数组的长度*/
printf("对不起未找到对应信息,删除不成功\n"); /*显示没找到要删除的记录*/
else
printf("姓名:%s学号:%d房号:%d\n",t[i].name,t[i].num,t[i].room); /*显示该条记录信息*/
printf("确定删除该职工的信息(1/0)\n"); /*确认是否要删除*/
scanf("%d",&ch); /*输入一个整数0或1*/
if(ch==1) /*如果确认删除整数为1*/
for(j=i+1;j<n;j++) /*删除该记录,实际后续记录前移*/
strcpy(t[j-1].name,t[j].name); /*将后一条记录的姓名拷贝到前一条*/
strcpy(t[j-1].num,t[j].num); /*将后一条记录的学号拷贝到前一条*/
strcpy(t[j-1].room,t[j].room); /*将后一条记录的房号拷贝到前一条*/
int find(ADDRESS t[],int n,char *s)
int i;
for(i=0;i<40;i++)/*从第一条记录开始,直到最后一条*/
if(strcmp(name,t[i].name)==0)
/*记录中的姓名和待比较的姓名是否相等*/
return i; /*相等,则返回该记录的下标号,程序提前结结束*/
return i; /*返回i值*/
参考技术B 定义结构或者类,类属性包括姓名、学号、房间号
怎么用C语言编写简单的班级通讯录系统的管理与实现
具有如下功能:
1、输入记录功能(从键盘输入:姓名,地址,电话,邮编,E-mail)
2、通讯录编辑功能(添加、删除、修改)。
3、按姓名查询功能。
3、显示记录功能。
4、排序功能(按年龄排序后,显示)
5、统计结果保存在文本文件中。
6、从文本文件中读取数据、显示。
要求:设计一个总菜单,分别调用各个子功能模块。撰写课程设计报告,并打印。
#include "stdlib.h"
#include "string.h"
#include "iostream.h"
#include "fstream.h"
typedef struct goal /*成绩*/
float course1;
float course2;
float course3;
float course4;
float course5;
goal;
typedef struct base /*基本信息*/
int num;
char name[20];
char sex[4];
int age;
long tel;
goal *brother;
base;
typedef struct student
int num;
base *firstchild;
struct student *firstbrother;
student;
student *creatroot(void)
student *root;
root=(student*)malloc(sizeof(student));
root->firstchild=NULL;
root->firstbrother=NULL;
root->num=9999;
return (root);
int number(student *root)
student *p=root;
int num=0;
while(p->firstbrother)
p=p->firstbrother;
num++;
printf("总共%d条记录\n",num);
return num;
int jiemian()
int c;
printf("\n\n**********学生信息管理*********\n\n");
printf("1 批量登记学生基本信息\n");
printf("2 修改学生基本信息\n");
printf("3 删除学生基本信息\n");
printf("4 新增学生基本信息\n");
printf("5 登记成绩(初始化全部成绩)\n");
printf("6 修改成绩\n");
printf("7 插入新成绩\n");
printf("8 浏览全部学生基本信息\n");
printf("9 查询\n");
printf("0 退出\n");
printf("\n*******************************\n");
printf("\n Enter you choice(0~9):\n");
scanf("%d",&c);
while(c<0||c>9)
printf("\nwrong input again:\n");
scanf("%d",&c);
return(c);
student *bublesort(student *root)
student *q,*p,*r;
r=NULL;
while(r!=root->firstbrother)
p=root->firstbrother;q=root;
while(p->firstbrother!=r)
if(p->num>p->firstbrother->num)
q->firstbrother=p->firstbrother;
p->firstbrother=q->firstbrother->firstbrother;
q->firstbrother->firstbrother=p;
q=q->firstbrother;p=q->firstbrother;
r=p;
return(root);
student *loadbase(student *root) /*基本信息管理*/
int i,n;
student *p1,*p2;
base *q1;
goal *t;
p2=root;
if(root->firstbrother!=NULL)
while(p2->firstbrother)
p2=p2->firstbrother;
printf("要登记多少条学生基本信息?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
p1=(student*)malloc(sizeof(student));
q1=(base*)malloc(sizeof(base));
t=(goal*)malloc(sizeof(goal));
p1->firstchild=q1;
q1->brother=t;
printf("学号\t姓名\t年龄\t性别\t电话\n");
scanf("%d",&q1->num);p1->num=q1->num;
scanf("%s",q1->name);
scanf("%d",&q1->age);
scanf("%s",q1->sex);
scanf("%ld",&q1->tel);
t->course1=t->course2=t->course3=t->course4=t->course5=0;
p2->firstbrother=p1;
p2=p1;
p1->firstbrother=NULL;
bublesort(root);
return(root);
student *modifybase(student *root)
student *p;
base *q;
int num,c;
printf("输入要修改的学号:\n");
scanf("%d",&num);
p=root->firstbrother;
while(p!=NULL&&(p->num!=num))
p=p->firstbrother;
if(p==NULL)
printf("学号不存在!\n");
else
q=p->firstchild;
printf("\n\n***********选择需要修改的记录*********\n\n");
printf("1 学号\n");
printf("2 姓名\n");
printf("3 性别\n");
printf("4 年龄\n");
printf("5 电话\n");
printf("6 返回\n");
printf("\n\n**************************************\n\n");
printf("\n Enter you choice(1~6):\n");
scanf("%d",&c);
while(c<0||c>6)
printf("\nwrong input again:\n");
scanf("%d",&c);
switch(c)
case 1: printf("原学号: %d\t输入新学号:\t",q->num);scanf("%d",&q->num);p->num=q->num;break;
case 2: printf("原姓名: %s\t输入新姓名:\t",q->name);scanf("%s",q->name);break;
case 3: printf("原性别: %s\t输入新性别:\t",q->sex);scanf("%s",q->sex);break;
case 4: printf("原年龄: %d\t输入新年龄:\t",q->age);scanf("%d",&q->age);break;
case 5: printf("原电话: %ld\t输入新电话:\t",q->num);scanf("%ld",&q->tel);break;
case 6: return NULL;
root=bublesort(root);
return(root);
student *delbase(student *root)
int num;
student *p,*q;
p=root->firstbrother;q=root;
printf("输入要删除的学号:\t");
scanf("%d",&num);
while(p!=NULL&&p->num!=num)
q=p;
p=p->firstbrother;
if(p==NULL)
printf("学号不存在!\n");
else
q->firstbrother=p->firstbrother;
free(p);
return(root);
student *insertbase(student *root)
int num;
student *p,*q;
base *t;
p=root;
printf("输入要插入的学号");
scanf("%d",&num);
while((p->firstbrother!=NULL)&&(num>p->firstbrother->num))
p=p->firstbrother;
q=(student*)malloc(sizeof(student));
t=(base*)malloc(sizeof(base));
q->firstchild=t;
t->brother=(goal*)malloc(sizeof(goal));
t->brother->course1=t->brother->course2=t->brother->course3=t->brother->course4=t->brother->course5=0;
printf("学号: %d\n",num);
printf("姓名\t年龄\t性别\t电话\n");
q->num=t->num=num;
scanf("%s",t->name);
scanf("%d",&t->age);
scanf("%s",t->sex);
scanf("%ld",&t->tel);
q->firstbrother=p->firstbrother;
p->firstbrother=q;
return(root);
student *loadgoal(student *root) /*成绩信息管理*/
student *p;
base *q;
goal *t;
if(root->firstbrother==NULL)
printf("请输入学生基本信息:\n");
loadbase(root);
else
p=root->firstbrother;
while(p!=NULL)
q=p->firstchild;
t=q->brother;
printf("学号:%d\n",q->num);
printf("课程1\t课程2\t课程3\t课程4\t课程5\n");
scanf("%f",&t->course1);
scanf("%f",&t->course2);
scanf("%f",&t->course3);
scanf("%f",&t->course4);
scanf("%f",&t->course5);
p=p->firstbrother;
return(root);
student *insertgoal(student *root)
student *p;
base *q;
goal *t;
int num;
printf("请输入学号\n");
scanf("%d",&num);
p=root->firstbrother;
while(p!=NULL&&p->num!=num)
p=p->firstbrother;
if(p==NULL)
printf("学号不存在!\n");
else
q=p->firstchild;
t=q->brother;
printf("学号:%d\n",q->num);
printf("课程1\t课程2\t课程3\t课程4\t课程5\n");
scanf("%f",&t->course1);
scanf("%f",&t->course2);
scanf("%f",&t->course3);
scanf("%f",&t->course4);
scanf("%f",&t->course5);
return(root);
student *modifygoal(student *root)
student *p;
base *q;
goal *t;
int num,c;
printf("输入要修改的学号:\n");
scanf("%d",&num);
p=root->firstbrother;
while(p!=NULL&&p->num!=num)
p=p->firstbrother;
if(!p)
printf("学号不存在!\n");
else
q=p->firstchild;
if(!q->brother)
printf("没有已登记的成绩!\n");
else
t=q->brother;
printf("\n\n***********选择需要修改的记录*********\n\n");
printf("1 课程1\n");
printf("2 课程2\n");
printf("3 课程3\n");
printf("4 课程4\n");
printf("5 课程5\n");
printf("6 返回\n");
printf("\n\n**************************************\n\n");
printf("\n Enter you choice(1~6):\n");
scanf("%d",&c);
while(c<0||c>6)
printf("\nwrong input again:\n");
scanf("%d",&c);
switch(c)
case 1: printf("原成绩: %f\t输入新成绩:\t",t->course1);scanf("%f",&t->course1);break;
case 2: printf("原成绩: %f\t输入新成绩:\t",t->course2);scanf("%f",&t->course2);break;
case 3: printf("原成绩: %f\t输入新成绩:\t",t->course3);scanf("%f",&t->course3);break;
case 4: printf("原成绩: %f\t输入新成绩:\t",t->course4);scanf("%f",&t->course4);break;
case 5: printf("原成绩: %f\t输入新成绩:\t",t->course5);scanf("%f",&t->course5);break;
case 6: return NULL;
return(root);
void find(student *root) /*查询*/
student *p=root->firstbrother;
base *q;
goal *t;
int c=1,num;
printf("输入学号\n");
scanf("%d",&num);
while(p!=NULL&&p->num!=num)
p=p->firstbrother;
if(p==NULL)
printf("学号不存在!\n");
else
q=p->firstchild;
printf("查询(基本信息/成绩)?(1/0)\n");
scanf("%d",&c);
if(c==1)
printf("学号\t姓名\t年龄\t性别\t电话\n");
printf("%d ",q->num);
printf("%s ",q->name);
printf("%d ",q->age);
printf("%s ",q->sex);
printf("%ld\n",q->tel);
else if((c==0))
if(q->brother==NULL)
printf("无成绩,请输入成绩\n");root=loadgoal(root);
t=q->brother;
printf("学号: %d\n",q->num);
printf("课程1\t课程2\t课程3\t课程4\t课程5\n");
printf("%f ",t->course1);
printf("%f ",t->course2);
printf("%f ",t->course3);
printf("%f ",t->course4);
printf("%f \n",t->course5);
void print(student *root) /*浏览*/
student *p=root->firstbrother;
base *q;
if(!p)
printf("无内容!\n");
while(p)
q=p->firstchild;
printf("num :%d\n",p->num);
printf("学号\t姓名\t年龄\t性别\t电话\n");
printf("%d ",q->num);
printf("%s ",q->name);
printf("%d ",q->age);
printf("%s ",q->sex);
printf("%ld\n",q->tel);
p=p->firstbrother;
student *insertsave(void) /*申请新结点*/
student *s1;
base *b1;
goal *g1;
s1=(student*)malloc(sizeof(student));
s1->num=0;
b1=(base*)malloc(sizeof(base));
b1->age=0;b1->num=0;b1->tel=0;strcpy(b1->name,"0");strcpy(b1->sex,"0");
g1=(goal*)malloc(sizeof(goal));
g1->course1=g1->course2=g1->course3=g1->course4=g1->course5=0;
s1->firstchild=b1;
s1->firstbrother=NULL;
b1->brother=g1;
return(s1);
void savenum(int num)
fstream in;
in.open("number.txt",ios::in|ios::out);
if(in.fail())
printf("文件不存在\n");
return;
in<<num;
in.close();
int loadnum(void)
int num=0;
fstream out;
out.open("number.txt",ios::in|ios::out);
if(out.fail())
printf("文件不存在\n");
return 0;
out>>num;
out.close();
return num;
void savetofile(student *root)
student *ps;
int i,num=number(root);
ps=root->firstbrother;
if(ps==NULL)
printf("现在没有学生信息,请先输入学生信息\n\n");
return;
fstream in;
in.open("student.txt",ios::in|ios::out);
if(in.fail())
printf("文件不存在\n");
return;
savenum(num);
for(i=0;i<num;i++)
in<<ps->firstchild->num;
in<<" ";
in<<ps->firstchild->name;
in<<" ";
in<<ps->firstchild->age;
in<<" ";
in<<ps->firstchild->sex;
in<<" ";
in<<ps->firstchild->tel;
in<<" ";
in<<ps->firstchild->brother->course1;
in<<" ";
in<<ps->firstchild->brother->course2;
in<<" ";
in<<ps->firstchild->brother->course3;
in<<" ";
in<<ps->firstchild->brother->course4;
in<<" ";
in<<ps->firstchild->brother->course5;
in<<" ";
ps=ps->firstbrother;
in.close();
student *loadfromfile(void)
student *p,*q,*root;
int i,num=0;
root=creatroot();
fstream out;
out.open("student.txt",ios::in|ios::out);
if(out.fail())
printf("文件不存在\n");
return root;
num=loadnum();
printf("记录数:%d",num);
q=root;
for(i=0;i<num;i++)
p=insertsave();
q->firstbrother=p;
q=p;
out>>q->firstchild->num;
out>>q->firstchild->name;
out>>q->firstchild->age;
out>>q->firstchild->sex;
out>>q->firstchild->tel;
out>>q->firstchild->brother->course1;
out>>q->firstchild->brother->course2;
out>>q->firstchild->brother->course3;
out>>q->firstchild->brother->course4;
out>>q->firstchild->brother->course5;
q->num=q->firstchild->num;
out.close();
return root;
void choice(student *root)
student *t;
for(;;)
loop: switch(jiemian())
case 1: root=loadbase(root);break; /*登记基本信息*/
case 2: t=modifybase(root);if(t==NULL)goto loop;else root=t;break; /*修改基本信息*/
case 3: root=delbase(root);break; /*删除基本信息*/
case 4: root=insertbase(root);break; /*插入基本信息*/
case 5: root=loadgoal(root);break; /*登记成绩*/
case 6: t=modifygoal(root);if(t==NULL)goto loop;else root=t;break; /*修改成绩*/
case 7: insertgoal(root);break; /*插入新成绩*/
case 8: print(root);break; /*浏览*/
case 9: find(root);break; /*查看单条*/
case 0: savetofile(root);free(root);exit(1); /*退出*/
void main()
student *root;
root=loadfromfile();
choice(root);
没有现成的,自己改改
以上是关于用C语言编写宿舍管理查询系统的主要内容,如果未能解决你的问题,请参考以下文章