下面是我发了一个小时写的一个简单的学生信息记录系统,由于时间仓促,没有写设计思路,这其实是我的一个作业,所以暂时没有时间了,忘谅!等没那么忙了,再来补上,另外这个虽说能编译,但是并有真正完整,还是有问题,星期四交作业,所以不急,明天再完善吧,嘿嘿,先放这........
/*下面我们用数组来实现增、删、改、查的函数功能,我写的输入不支持中文,目前没学*/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define Max_storage 20 /*能够存储的最大学生记录数*/
#define TURE 1 /*定义结果状态代码*/
#define FALSE 0
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define Max_storage 20 /*能够存储的最大学生记录数*/
#define TURE 1 /*定义结果状态代码*/
#define FALSE 0
typedef int Status; /*Status是函数类型,定义函数结果状态代码,返回如:OK*/
typedef struct student_type /*用结构体来定义学生信息存储结构*/
{
char num[12]; /*学号*/
char name[8]; /* 姓名*/
int age; /*年龄*/
char dep[32]; /*系*/
}Stud;
int count=0; /*定义一个计数器,用来记录存储学生信息的数目*/
typedef struct student_type /*用结构体来定义学生信息存储结构*/
{
char num[12]; /*学号*/
char name[8]; /* 姓名*/
int age; /*年龄*/
char dep[32]; /*系*/
}Stud;
int count=0; /*定义一个计数器,用来记录存储学生信息的数目*/
void add(Stud *a)
{ /*添加新纪录*/
int i;
printf("Please input added information:\n");
printf("(add)Age:\n");
scanf("%d",&a[count].age);
printf("(add)Number:\n");
scanf("%s",a[count].num);
printf("(add)Name:\n");
scanf("%s",a[count].name);
printf("(add)Department:\n");
scanf("%s",a[count].dep);
/*输出学生信息*/
printf("This is the %d student information:\n",count+1);
printf("a[%d].age=",count);
printf("%d",a[count].age);
printf("\n");
printf("a[%d].num[]=",count);
printf("%s",a[count].num);
printf("\n");
printf("a[%d].name[]=",count);
printf("%s",a[count].name);
printf("\n");
printf("a[%d].dep[]=",count);
printf("%s",a[count].dep);
printf("\n");
count++; /*学生记录数增加一个*/
}
void del(Stud *a)
{ /*以学号为关键字删除记录,stu为需要进行删除的学生记录*/
int i,j;
int count1; /*计数器二,用于查找到该学号,在进行删除操作*/
Stud del1;
Status sentence=TURE; /*判断删除操作是否成功!TURE or FALSE*/
printf("(delete)Please input the number you want to delete:\n");
scanf("%s",del1.num);
for(count1=0;count1<=count;count1++) /*在数组逐一查询*/
{
for(i=0;a[count1].num[i]==del1.num[i];i++) {
if(i==strlen(del1.num)) /*如果完全匹配*/
{
for(j=0;j<strlen(del1.num);j++)
a[count1].num[j]='\0';
for(j=0;j<strlen(a[count1].name);j++)
a[count1].name[j]='\0';
for(j=0;j<strlen(a[count1].dep);j++)
a[count1].dep[i]='\0';
a[count1].age=0;
count--; /*学生记录数少一个*/
printf("Delete Succeed!\n");
sentence=TURE;
}
else
sentence=FALSE;
}
}
if(sentence==FALSE)
printf("Delete Failed!\n");
}
void update(Stud *a){ /*以学号为关键字修改记录信息*/
int i,menu1;
int count2; /*计数器2,用来查找修改学生信息是否存在*/
Stud modify;
printf("(update)Please input the number you want to modify:\n");
scanf("%s",modify.num);
for(count2=0;count2<=count;count2++) /*在数组逐一查询*/
{
for(i=0;a[count2].num[i]==modify.num[i];i++)
{
if(i==strlen(modify.num)) /*如果完全匹配,即需要修改的学生信息存在*/
{
while(1){ /*用while结构实现子菜单选择功能*/
printf("1. Modify the name.\n");
printf("2. Modify the age.\n");
printf("3. Modify the department.\n");
printf("4. Quit the Modify.\n"); /*退出修改*/
printf("Please input the your choose:");
scanf("%d",&menu1);
switch(menu1)
{
case 1:
printf("Please input the name:");
scanf("%s",modify.name);
if(strlen(a[count2].name)<=strlen(modify.name)){
for(i=0;i<8;i++)
a[count2].name[i]=modify.name[i];
}
else{
for(i=strlen(modify.name);i<strlen(modify.name)-strlen(a[count2].name);i--)
modify.name[i]='\0';
}
break;
case 2:
printf("Please input the age:");
scanf("%d",&modify.age);
a[count2].age=modify.age;
break;
case 3:
printf("Please input the department:");
scanf("%s",modify.dep);
if(strlen(a[count2].dep)<=strlen(modify.dep)){
for(i=0;i<32;i++)
a[count2].dep[i]=modify.dep[i];
}
else{
for(i=strlen(modify.dep);i<strlen(modify.dep)-strlen(a[count2].dep);i--)
modify.dep[i]='\0';
}
break;
case 4:
goto in; /*跳转到下面的输出信息语句,in为行号*/
}
printf("\n");
}
}
}
}
/*goto的跳转语句在这里*/
in:printf("This is the %d student information after you modified::\n",count2+1);
printf("a[%d].age=",count2);
printf("%d",a[count2].age);
printf("\n");
printf("a[%d].num[]=",count2);
printf("%s",a[count2].num);
printf("\n");
printf("a[%d].name[]=",count2);
printf("%s",a[count2].name);
printf("\n");
printf("a[%d].dep[]=",count2);
printf("%s",a[count2].dep);
printf("\n");
}
Status query(Stud *a){ /*以学号或姓名为查询字段进行查询,*a为指向存储学生记录的数组的指针*/
Stud find;
int i,j,count;
Status sentence=TURE;
printf("(query)Please input the number you want to find:\n");
scanf("%s",find.num);
for(count=0;count<Max_storage;count++) /*在数组逐一查询*/
{
for(i=0;a[count].num[i]==find.num[i];i++)
{
if(i==strlen(find.num)) /*如果完全匹配,则输出查询结果*/
{
printf("This is the result you find out:\n"); /*输出查找结果*/
printf("a[%d].num[]=",count); /*输出所查找信息的学号*/
printf("%s\n",a[count].num);
printf("a[%d].name[]=",count); /*输出所查找信息的姓名*/
printf("%s\n",a[count].name);
printf("a[%d].age=",count); /*输出所查找信息的年龄*/
printf("%d\n",a[count].age);
printf("a[%d].dep=",count); /*输出所查找信息的系别*/
printf("%s\n",a[count].dep);
sentence=TURE;
printf("Find Succeed!\n"); /*查找成功*/
}
else
sentence=FALSE;
}
}
if(sentence==FALSE)
printf("Find Failed!\n"); /*没有查询内容*/
}
void main()
{
int menu;
Stud STU[Max_storage]; /*用来存储学生记录的数组*/
int i;
while(1)
{ /*用while结构定义主菜单结构,供用户选择操作*/
printf("------The Simple Student Information Management System v1.0------\n");
printf("1. Add the student information in STU.\n");
printf("2. Delete the student information in STU.\n");
printf("3. Modify the student information in STU.\n");
printf("4. Find the student information in STU.\n");
printf("5. Quit the system.\n");
printf("Please input your selection:");
scanf("%d", &menu);
if(menu==1||menu==2||menu==3||menu==4||menu==5)
{
printf("**We begin to operate now!\n");
switch(menu)
{
case 1:
add(STU);
break;
case 2:
del(STU);
break;
case 3:
update(STU);
break;
case 4:
query(STU);
break;
case 5:
exit(1);
break;
}
}
else
printf("Sorry! Your input does not match,again please!\n");
}
getch();
}
{ /*添加新纪录*/
int i;
printf("Please input added information:\n");
printf("(add)Age:\n");
scanf("%d",&a[count].age);
printf("(add)Number:\n");
scanf("%s",a[count].num);
printf("(add)Name:\n");
scanf("%s",a[count].name);
printf("(add)Department:\n");
scanf("%s",a[count].dep);
/*输出学生信息*/
printf("This is the %d student information:\n",count+1);
printf("a[%d].age=",count);
printf("%d",a[count].age);
printf("\n");
printf("a[%d].num[]=",count);
printf("%s",a[count].num);
printf("\n");
printf("a[%d].name[]=",count);
printf("%s",a[count].name);
printf("\n");
printf("a[%d].dep[]=",count);
printf("%s",a[count].dep);
printf("\n");
count++; /*学生记录数增加一个*/
}
void del(Stud *a)
{ /*以学号为关键字删除记录,stu为需要进行删除的学生记录*/
int i,j;
int count1; /*计数器二,用于查找到该学号,在进行删除操作*/
Stud del1;
Status sentence=TURE; /*判断删除操作是否成功!TURE or FALSE*/
printf("(delete)Please input the number you want to delete:\n");
scanf("%s",del1.num);
for(count1=0;count1<=count;count1++) /*在数组逐一查询*/
{
for(i=0;a[count1].num[i]==del1.num[i];i++) {
if(i==strlen(del1.num)) /*如果完全匹配*/
{
for(j=0;j<strlen(del1.num);j++)
a[count1].num[j]='\0';
for(j=0;j<strlen(a[count1].name);j++)
a[count1].name[j]='\0';
for(j=0;j<strlen(a[count1].dep);j++)
a[count1].dep[i]='\0';
a[count1].age=0;
count--; /*学生记录数少一个*/
printf("Delete Succeed!\n");
sentence=TURE;
}
else
sentence=FALSE;
}
}
if(sentence==FALSE)
printf("Delete Failed!\n");
}
void update(Stud *a){ /*以学号为关键字修改记录信息*/
int i,menu1;
int count2; /*计数器2,用来查找修改学生信息是否存在*/
Stud modify;
printf("(update)Please input the number you want to modify:\n");
scanf("%s",modify.num);
for(count2=0;count2<=count;count2++) /*在数组逐一查询*/
{
for(i=0;a[count2].num[i]==modify.num[i];i++)
{
if(i==strlen(modify.num)) /*如果完全匹配,即需要修改的学生信息存在*/
{
while(1){ /*用while结构实现子菜单选择功能*/
printf("1. Modify the name.\n");
printf("2. Modify the age.\n");
printf("3. Modify the department.\n");
printf("4. Quit the Modify.\n"); /*退出修改*/
printf("Please input the your choose:");
scanf("%d",&menu1);
switch(menu1)
{
case 1:
printf("Please input the name:");
scanf("%s",modify.name);
if(strlen(a[count2].name)<=strlen(modify.name)){
for(i=0;i<8;i++)
a[count2].name[i]=modify.name[i];
}
else{
for(i=strlen(modify.name);i<strlen(modify.name)-strlen(a[count2].name);i--)
modify.name[i]='\0';
}
break;
case 2:
printf("Please input the age:");
scanf("%d",&modify.age);
a[count2].age=modify.age;
break;
case 3:
printf("Please input the department:");
scanf("%s",modify.dep);
if(strlen(a[count2].dep)<=strlen(modify.dep)){
for(i=0;i<32;i++)
a[count2].dep[i]=modify.dep[i];
}
else{
for(i=strlen(modify.dep);i<strlen(modify.dep)-strlen(a[count2].dep);i--)
modify.dep[i]='\0';
}
break;
case 4:
goto in; /*跳转到下面的输出信息语句,in为行号*/
}
printf("\n");
}
}
}
}
/*goto的跳转语句在这里*/
in:printf("This is the %d student information after you modified::\n",count2+1);
printf("a[%d].age=",count2);
printf("%d",a[count2].age);
printf("\n");
printf("a[%d].num[]=",count2);
printf("%s",a[count2].num);
printf("\n");
printf("a[%d].name[]=",count2);
printf("%s",a[count2].name);
printf("\n");
printf("a[%d].dep[]=",count2);
printf("%s",a[count2].dep);
printf("\n");
}
Status query(Stud *a){ /*以学号或姓名为查询字段进行查询,*a为指向存储学生记录的数组的指针*/
Stud find;
int i,j,count;
Status sentence=TURE;
printf("(query)Please input the number you want to find:\n");
scanf("%s",find.num);
for(count=0;count<Max_storage;count++) /*在数组逐一查询*/
{
for(i=0;a[count].num[i]==find.num[i];i++)
{
if(i==strlen(find.num)) /*如果完全匹配,则输出查询结果*/
{
printf("This is the result you find out:\n"); /*输出查找结果*/
printf("a[%d].num[]=",count); /*输出所查找信息的学号*/
printf("%s\n",a[count].num);
printf("a[%d].name[]=",count); /*输出所查找信息的姓名*/
printf("%s\n",a[count].name);
printf("a[%d].age=",count); /*输出所查找信息的年龄*/
printf("%d\n",a[count].age);
printf("a[%d].dep=",count); /*输出所查找信息的系别*/
printf("%s\n",a[count].dep);
sentence=TURE;
printf("Find Succeed!\n"); /*查找成功*/
}
else
sentence=FALSE;
}
}
if(sentence==FALSE)
printf("Find Failed!\n"); /*没有查询内容*/
}
void main()
{
int menu;
Stud STU[Max_storage]; /*用来存储学生记录的数组*/
int i;
while(1)
{ /*用while结构定义主菜单结构,供用户选择操作*/
printf("------The Simple Student Information Management System v1.0------\n");
printf("1. Add the student information in STU.\n");
printf("2. Delete the student information in STU.\n");
printf("3. Modify the student information in STU.\n");
printf("4. Find the student information in STU.\n");
printf("5. Quit the system.\n");
printf("Please input your selection:");
scanf("%d", &menu);
if(menu==1||menu==2||menu==3||menu==4||menu==5)
{
printf("**We begin to operate now!\n");
switch(menu)
{
case 1:
add(STU);
break;
case 2:
del(STU);
break;
case 3:
update(STU);
break;
case 4:
query(STU);
break;
case 5:
exit(1);
break;
}
}
else
printf("Sorry! Your input does not match,again please!\n");
}
getch();
}
今天起了个早床,终于把基本的功能实现完整了,昨晚没睡好,因为一直在想这个问题,脑子里总是萦绕着这个东西,所以没睡好,现在好困啊,好在做出来了,付出有所回报!应该算完整了,再就只要实现非二进制流的文件输入和输出了,快了!
转载于:https://blog.51cto.com/lingchuan/201531