作者在 2008-06-05 21:01:33 发布以下内容
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 101
typedef struct student{
char number[15];
char name[30];
int score;
}STUDENT;
/*******************************************************************************
* 以下是函数声明部分 *
********************************************************************************/
int intputInfo(STUDENT *stu,int length);
void outputInfo(STUDENT *stu,int length);
void queryInfo(STUDENT *stu,int length,char *stuNum);
int only(STUDENT *stu,int k,char *num);
void login(char *p);
int insert(STUDENT *stu,int length);
int del(STUDENT *stu,int length);
void paixu(STUDENT *stu,int length);
void save(STUDENT *stu,int length);
int open(STUDENT *stu);
void displayMenu();
/******************************************************************************
* 以下是主函数部分 *
*******************************************************************************/
main()
{
STUDENT student[N];
STUDENT *stu;
int length,choose;
char password[10],stuNum[15];
length=0;
stu=student;
login(password);
while(1)
{
displayMenu();
printf("请选择(1,2,3,4,5,6,7,8,9): ");
scanf("%d",&choose);
switch(choose)
{
case 1:
{
printf("当输入学号为0时结束输入信息\n");
length=intputInfo(stu,length);
printf("\n\n");
printf("共有%d个学生信息\n",length);
}
break;
case 2:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
outputInfo(stu,length);
printf("\n");
printf("共有%d个学生信息\n",length);
}
}break;
case 3:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
printf("请输入要查询学生的学号: ");
getchar();
gets(stuNum);
printf("\n");
queryInfo(stu,length,stuNum);
}
}break;
case 4:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
length=insert(stu,length);
printf("\n\n");
printf("共有%d个学生信息\n",length);
}
}break;
case 5:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
length=del(stu,length);
outputInfo(stu,length);
printf("\n");
printf("共有%d个学生信息\n",length);
}
}break;
case 6:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
paixu(stu,length);
outputInfo(stu,length);
printf("\n");
printf("共有%d个学生信息\n",length);
}
}break;
case 7:
{
if(length==0)
printf("\n学生信息为空,不能完成存储!\n\n");
else
{
save(stu,length);
printf("存储完毕!\n");
}
}break;
case 8:
{
if(!(stu+1)->number)
printf("\n无学生信息!\n\n");
else
{
printf("\n");
length=open(stu);
printf("\n读取完毕!\n");
}
}break;
case 9:exit(1);
}
}
}
/****************************************************************************************************
* 以下是函数的定义部分 *
*****************************************************************************************************/
void displayMenu()
{
printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("+ 学生成绩管理系统2.0 +\n");
printf("+ 1.输入信息 4.插入信息 7.保存到文件 +\n");
printf("+ 2.输出信息 5.按学号删除 8.从文件读 +\n");
printf("+ 3.按学号查 6.按成绩排序 9.程序结束 +\n");
printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
#include<stdlib.h>
#include<string.h>
#define N 101
typedef struct student{
char number[15];
char name[30];
int score;
}STUDENT;
/*******************************************************************************
* 以下是函数声明部分 *
********************************************************************************/
int intputInfo(STUDENT *stu,int length);
void outputInfo(STUDENT *stu,int length);
void queryInfo(STUDENT *stu,int length,char *stuNum);
int only(STUDENT *stu,int k,char *num);
void login(char *p);
int insert(STUDENT *stu,int length);
int del(STUDENT *stu,int length);
void paixu(STUDENT *stu,int length);
void save(STUDENT *stu,int length);
int open(STUDENT *stu);
void displayMenu();
/******************************************************************************
* 以下是主函数部分 *
*******************************************************************************/
main()
{
STUDENT student[N];
STUDENT *stu;
int length,choose;
char password[10],stuNum[15];
length=0;
stu=student;
login(password);
while(1)
{
displayMenu();
printf("请选择(1,2,3,4,5,6,7,8,9): ");
scanf("%d",&choose);
switch(choose)
{
case 1:
{
printf("当输入学号为0时结束输入信息\n");
length=intputInfo(stu,length);
printf("\n\n");
printf("共有%d个学生信息\n",length);
}
break;
case 2:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
outputInfo(stu,length);
printf("\n");
printf("共有%d个学生信息\n",length);
}
}break;
case 3:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
printf("请输入要查询学生的学号: ");
getchar();
gets(stuNum);
printf("\n");
queryInfo(stu,length,stuNum);
}
}break;
case 4:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
length=insert(stu,length);
printf("\n\n");
printf("共有%d个学生信息\n",length);
}
}break;
case 5:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
length=del(stu,length);
outputInfo(stu,length);
printf("\n");
printf("共有%d个学生信息\n",length);
}
}break;
case 6:
{
if(length==0)
printf("\n无学生信息!\n\n");
else
{
paixu(stu,length);
outputInfo(stu,length);
printf("\n");
printf("共有%d个学生信息\n",length);
}
}break;
case 7:
{
if(length==0)
printf("\n学生信息为空,不能完成存储!\n\n");
else
{
save(stu,length);
printf("存储完毕!\n");
}
}break;
case 8:
{
if(!(stu+1)->number)
printf("\n无学生信息!\n\n");
else
{
printf("\n");
length=open(stu);
printf("\n读取完毕!\n");
}
}break;
case 9:exit(1);
}
}
}
/****************************************************************************************************
* 以下是函数的定义部分 *
*****************************************************************************************************/
void displayMenu()
{
printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("+ 学生成绩管理系统2.0 +\n");
printf("+ 1.输入信息 4.插入信息 7.保存到文件 +\n");
printf("+ 2.输出信息 5.按学号删除 8.从文件读 +\n");
printf("+ 3.按学号查 6.按成绩排序 9.程序结束 +\n");
printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
/**********************************
* 登陆密码: *
***********************************/
void login(char *p)
{
char a[10]="123";
do
{
printf("输入口令:(输入123)");
gets(p);
}
while(strcmp(p,a)!=0);
}
/**************************************
* 输入信息 *
***************************************/
int intputInfo(STUDENT *stu,int length)
{
int i,flag;
i=length;
while(1)
{
printf("学号: ");
scanf("%s",&(stu+i)->number);
if(strcmp((stu+i)->number,"0")==0)
break;
if(i>0)
{
flag=only(stu,i,(stu+i)->number);
if(flag==1)
{
printf("输入学号重复,请重新输入!\n");
continue;
}
}
printf("姓名: ");
scanf("%s",&(stu+i)->name);
printf("成绩: ");
scanf("%d",&((stu+i)->score));
i++;
}
return i;
}
/*********************************************
* 输出信息 *
**********************************************/
void outputInfo(STUDENT *stu,int length)
{
int i;
printf("\n\n");
printf(" 学号 姓名 成绩\n");
for(i=0;i<length;i++)
printf("%-20s%-20s%-20d\n",(stu+i)->number,(stu+i)->name,(stu+i)->score);
printf("\n\n");
}
/********************************************
* 按学号查 *
*********************************************/
void queryInfo(STUDENT *stu,int length,char *stuNum)
{
int i,k;
k=0;
for(i=0;i<length;i++)
{
if(strcmp((stu+i)->number,stuNum)==0)
{
printf("学号: %s\n",(stu+i)->number);
printf("姓名: %s\n",(stu+i)->name);
printf("成绩: %d\n\n",(stu+i)->score);
}
else
k++;
}
if(k==length)
printf("无此学生信息!\n\n");
}
/**********************************************************
* 判断学号的唯一性 *
***********************************************************/
int only(STUDENT *stu,int k,char *num)
{
int i,n;
n=0;
for(i=0;i<k;i++)
{
if(strcmp((stu+i)->number,num)==0)
break;
else
n++;
}
if(n==k)
return 0;
else
return 1;
}
/*****************************************************
* 插入信息 *
******************************************************/
int insert(STUDENT *stu,int length)
{
int i,flag,j,place;
char num[15];
i=length;
length++;
printf("选择要插入的位置: ");
scanf("%d",&place);
if(place>length-1||place<=0)
{
printf("没有该位置!\n");
length--;
}
else
{
printf("学号: ");
scanf("%s",&num);
flag=only(stu,i,num);
if(flag==1)
{
printf("\n学号重复!");
length--;
}
else
{
for(j=length-1;j>=place;j--)
{
strcpy((stu+j)->number,(stu+j-1)->number);
strcpy((stu+j)->name,(stu+j-1)->name);
(stu+j)->score=(stu+j-1)->score;
}
strcpy((stu+place-1)->number,num);
printf("姓名: ");
scanf("%s",&(stu+place-1)->name);
printf("成绩: ");
scanf("%d",&(stu+place-1)->score);
}
}
return length;
}
/****************************************************
* 按学号删除 *
*****************************************************/
int del(STUDENT *stu,int length)
{
char num[15];
int i,j,k;
k=0;
printf("输入要删除的学号: ");
scanf("%s",&num);
for(i=0;i<length;i++)
if(strcmp((stu+i)->number,num)==0)
{ for(j=i;j<length;j++)
{
strcpy((stu+j)->number,(stu+j+1)->number);
strcpy((stu+j)->name,(stu+j+1)->name);
(stu+j)->score=(stu+j+1)->score;
}
k++;
}
if(k!=0)
length--;
else
printf("输入的学号有误!\n");
return length;
}
/******************************************************
* 按成绩排序 *
* (选择排序法:降序) *
*******************************************************/
void paixu(STUDENT *stu,int length)
{
int i,j,k,temp1;
char *temp2,*temp3;
temp2=stu->name;
temp3=stu->number;
for(i=0;i<length-1;i++)
{
k=i;
for(j=k+1;j<length;j++)
if((stu+k)->score<(stu+j)->score)
k=j;
temp1=(stu+i)->score;
temp2=(char *)malloc(sizeof(char));
temp3=(char *)malloc(sizeof(char));
(stu+i)->score=(stu+k)->score;
(stu+k)->score=temp1;
strcpy(temp2,(stu+i)->name);
strcpy((stu+i)->name,(stu+k)->name);
strcpy((stu+k)->name,temp2);
strcpy(temp3,(stu+i)->number);
strcpy((stu+i)->number,(stu+k)->number);
strcpy((stu+k)->number,temp3);
}
}
/*****************************************************
* 保存到文件 *
******************************************************/
void save(STUDENT *stu,int length)
{
FILE *fp;
int i;
fp=fopen("myfile.txt","w");
for(i=0;i<length;i++)
fprintf(fp,"%20s%20s%20d\n",(stu+i)->number,(stu+i)->name,(stu+i)->score);
fclose(fp);
}
int open(STUDENT *stu)
{
FILE *fp;
int i=0;
fp=fopen("myfile.txt","r");
if(fp==NULL)
printf("无学生信息!\n");
else
{
printf(" 学号 姓名 成绩\n");
while(feof(fp)==0)
{
fscanf(fp,"%20s%20s%20d\n",(stu+i)->number,(stu+i)->name,&(stu+i)->score);
printf("%-20s%-20s%-20d\n",(stu+i)->number,(stu+i)->name,(stu+i)->score);
i++;
}
fclose(fp);
}
return i;
}
/**********************************
* 登陆密码: *
***********************************/
void login(char *p)
{
char a[10]="123";
do
{
printf("输入口令:(输入123)");
gets(p);
}
while(strcmp(p,a)!=0);
}
/**************************************
* 输入信息 *
***************************************/
int intputInfo(STUDENT *stu,int length)
{
int i,flag;
i=length;
while(1)
{
printf("学号: ");
scanf("%s",&(stu+i)->number);
if(strcmp((stu+i)->number,"0")==0)
break;
if(i>0)
{
flag=only(stu,i,(stu+i)->number);
if(flag==1)
{
printf("输入学号重复,请重新输入!\n");
continue;
}
}
printf("姓名: ");
scanf("%s",&(stu+i)->name);
printf("成绩: ");
scanf("%d",&((stu+i)->score));
i++;
}
return i;
}
/*********************************************
* 输出信息 *
**********************************************/
void outputInfo(STUDENT *stu,int length)
{
int i;
printf("\n\n");
printf(" 学号 姓名 成绩\n");
for(i=0;i<length;i++)
printf("%-20s%-20s%-20d\n",(stu+i)->number,(stu+i)->name,(stu+i)->score);
printf("\n\n");
}
/********************************************
* 按学号查 *
*********************************************/
void queryInfo(STUDENT *stu,int length,char *stuNum)
{
int i,k;
k=0;
for(i=0;i<length;i++)
{
if(strcmp((stu+i)->number,stuNum)==0)
{
printf("学号: %s\n",(stu+i)->number);
printf("姓名: %s\n",(stu+i)->name);
printf("成绩: %d\n\n",(stu+i)->score);
}
else
k++;
}
if(k==length)
printf("无此学生信息!\n\n");
}
/**********************************************************
* 判断学号的唯一性 *
***********************************************************/
int only(STUDENT *stu,int k,char *num)
{
int i,n;
n=0;
for(i=0;i<k;i++)
{
if(strcmp((stu+i)->number,num)==0)
break;
else
n++;
}
if(n==k)
return 0;
else
return 1;
}
/*****************************************************
* 插入信息 *
******************************************************/
int insert(STUDENT *stu,int length)
{
int i,flag,j,place;
char num[15];
i=length;
length++;
printf("选择要插入的位置: ");
scanf("%d",&place);
if(place>length-1||place<=0)
{
printf("没有该位置!\n");
length--;
}
else
{
printf("学号: ");
scanf("%s",&num);
flag=only(stu,i,num);
if(flag==1)
{
printf("\n学号重复!");
length--;
}
else
{
for(j=length-1;j>=place;j--)
{
strcpy((stu+j)->number,(stu+j-1)->number);
strcpy((stu+j)->name,(stu+j-1)->name);
(stu+j)->score=(stu+j-1)->score;
}
strcpy((stu+place-1)->number,num);
printf("姓名: ");
scanf("%s",&(stu+place-1)->name);
printf("成绩: ");
scanf("%d",&(stu+place-1)->score);
}
}
return length;
}
/****************************************************
* 按学号删除 *
*****************************************************/
int del(STUDENT *stu,int length)
{
char num[15];
int i,j,k;
k=0;
printf("输入要删除的学号: ");
scanf("%s",&num);
for(i=0;i<length;i++)
if(strcmp((stu+i)->number,num)==0)
{ for(j=i;j<length;j++)
{
strcpy((stu+j)->number,(stu+j+1)->number);
strcpy((stu+j)->name,(stu+j+1)->name);
(stu+j)->score=(stu+j+1)->score;
}
k++;
}
if(k!=0)
length--;
else
printf("输入的学号有误!\n");
return length;
}
/******************************************************
* 按成绩排序 *
* (选择排序法:降序) *
*******************************************************/
void paixu(STUDENT *stu,int length)
{
int i,j,k,temp1;
char *temp2,*temp3;
temp2=stu->name;
temp3=stu->number;
for(i=0;i<length-1;i++)
{
k=i;
for(j=k+1;j<length;j++)
if((stu+k)->score<(stu+j)->score)
k=j;
temp1=(stu+i)->score;
temp2=(char *)malloc(sizeof(char));
temp3=(char *)malloc(sizeof(char));
(stu+i)->score=(stu+k)->score;
(stu+k)->score=temp1;
strcpy(temp2,(stu+i)->name);
strcpy((stu+i)->name,(stu+k)->name);
strcpy((stu+k)->name,temp2);
strcpy(temp3,(stu+i)->number);
strcpy((stu+i)->number,(stu+k)->number);
strcpy((stu+k)->number,temp3);
}
}
/*****************************************************
* 保存到文件 *
******************************************************/
void save(STUDENT *stu,int length)
{
FILE *fp;
int i;
fp=fopen("myfile.txt","w");
for(i=0;i<length;i++)
fprintf(fp,"%20s%20s%20d\n",(stu+i)->number,(stu+i)->name,(stu+i)->score);
fclose(fp);
}
int open(STUDENT *stu)
{
FILE *fp;
int i=0;
fp=fopen("myfile.txt","r");
if(fp==NULL)
printf("无学生信息!\n");
else
{
printf(" 学号 姓名 成绩\n");
while(feof(fp)==0)
{
fscanf(fp,"%20s%20s%20d\n",(stu+i)->number,(stu+i)->name,&(stu+i)->score);
printf("%-20s%-20s%-20d\n",(stu+i)->number,(stu+i)->name,(stu+i)->score);
i++;
}
fclose(fp);
}
return i;
}