C语言程序设计 班级学生成绩管理系统
解决时间:2009-6-26 09:50
基本要求
1. 初始化成绩表:将原成绩表的信息清空;
2. 显示班级学生成绩信息:按要求显示全部或特定学生成绩信息;
3. 插入学生成绩:插入一个学生成绩信息;
4. 修改学生成绩:先查找要修改成绩的学生学号,如果存在该学生学号,则输入该学生的各科新成绩,替换相应课程的原成绩,如不存在该学生学号,则提示“没找到”;
5. 删除学生成绩:先查找要删除成绩的学生学号,如果存在该学生学号,则删除该学生的相关信息,如不存在该学生学号,则提示“没找到”;
6. 查找学生成绩:可分别按学号、姓名查找学生成绩;
7. 求班级学生人数:统计各个班级的学生总人数;
8. 求每个学生的平均成绩:求各个学生的平均成绩,并显示学号、姓名、数分,高代,C语言,马哲、平均成绩等信息。
高级要求
1. 插入学生成绩时,需保证学号是唯一的;
2. 输入学生成绩时,要保证课程成绩的范围在0-100之间;
3. 显示各科成绩前三名的学生信息(学号、姓名、性别、班级、课程名称(数分或高代或C语言或马哲)、名次。
**********************
#include
#include
#include
#include
#define ADDRFILE "c:\\addrlist.txt"
typedef struct tagLinkedList
{
char name[30];
char tel[30];
char addr[100];
struct tagLinkedList *prev;
struct tagLinkedLIst *next;
}ADDR;
ADDR *g_pAddrHead = NULL;
ADDR *g_pFind;
int g_bSaved = 1;
void get_addrlist(void);
int add_list(const ADDR *addr);
int find_list(const char *name);
void SetHeadPosition(void);
void SetTailPosition(void);
void Add_addr(void);
void Find_addr(void);
void Modify_addr(void);
void Delete_addr(void);
void Print_addr(void);
void Save_addr(void);
void Remove_addr(void);
void main(void)
{
int ch;
get_addrlist();
puts("通讯录程序 Version 1.0");
while(1)
{
printf("\n[1]新增 [2]搜索 [3]修改 [4]删除 [5]输出 [S]储存 [Q]退出");
ch = getch();
switch(ch)
{
case '1': Add_addr();break;
case '2': Find_addr();break;
case '3': Modify_addr();break;
case '4': Delete_addr();break;
case '5': Print_addr();break;
case 's':
case 'S': Save_addr();break;
case 'q':
case 'Q':
if(g_bSaved == 0)
{
printf("\n\n修改后的数据需要储存吗?(y/n)?");
ch = getchar();
if(ch == 'Y' || ch == 'y') Save_addr();
}
Remove_addr(); exit(1);
default : printf("\n\n 请按1~5或S/Q健.\n\n");break;
}
}
}
void get_addrlist(void)
{
ADDR addr;
FILE *fp;
fp = fopen(ADDRFILE,"rb");
if(fp == NULL)
{
perror("open file error;");
return;
}
while(!feof(fp))
{
fread(&addr,1,sizeof(ADDR),fp);
if(ferror(fp))
{
fclose(fp);
perror("file read error;");
return;
}
if(feof(fp))break;
if(add_list(&addr) == 0)
{
printf(".\n");
}
}
fclose(fp);
}
int add_list(const ADDR *addr)
{
ADDR *plocal,*pn = g_pAddrHead;
SetHeadPosition();
if(g_pAddrHead == NULL)
{
plocal = malloc(sizeof(ADDR));
memset(plocal,0,sizeof(ADDR));
if(plocal == NULL)
{
return 0;
}
g_pAddrHead = plocal;
g_pAddrHead->prev = NULL;
g_pAddrHead->next = NULL;
}
else
{
plocal = malloc(sizeof(ADDR));
memset(plocal,0,sizeof(ADDR));
if(plocal == NULL)
{
return 0;
}
while(pn->next)
{
pn = pn->next;
}
pn->next = plocal;
plocal->prev = pn;
plocal->next = NULL;
}
strcpy(plocal->name,addr->name);
strcpy(plocal->tel,addr->tel);
strcpy(plocal->addr,addr->addr);
return 1;
}
int find_list(const char *name)
{
ADDR *plocal;
SetHeadPosition();
plocal = g_pAddrHead;
while(plocal)
{
if(strstr(plocal->name,name))
{
g_pFind = plocal;
return 1;
}
plocal = plocal->next;
}
return 0;
}
void SetHeadPosition(void)
{
if(g_pAddrHead == NULL) return;
while(g_pAddrHead->prev)
{
g_pAddrHead = g_pAddrHead->prev;
}
}
void SetTailPosition(void)
{
if(g_pAddrHead == NULL) return;
while(g_pAddrHead->next)
{
g_pAddrHead = g_pAddrHead->next;
}
}
void Remove_addr(void)
{
ADDR *plocal;
if(g_pAddrHead == NULL) return;
SetHeadPosition();
while(g_pAddrHead)
{
plocal = g_pAddrHead->next;
free(g_pAddrHead);
g_pAddrHead = plocal;
}
g_pAddrHead = NULL;
}
void Add_addr(void)
{
ADDR addr;
memset(&addr,0,sizeof(ADDR));
printf("\n\nName: ");gets(addr.name);
if(strlen(addr.name) == 0) return;
printf("TEL: ");gets(addr.tel);
printf("ADDR: ");gets(addr.addr);
if(find_list(addr.name) == 1)
{
printf("\n已经存在的姓名.\n\n");
puts(g_pFind->name);
puts(g_pFind->tel);
puts(g_pFind->addr);
return;
}
if(add_list(&addr))
{
g_bSaved = 0;
printf("\n储存完毕.\n\n");
}
}
void Find_addr(void)
{
char buff[100] ={0,};
ADDR *plocal;
printf("\n\n请输入要查找的姓名/电话/地址.\n");
printf("姓名/电话/地址 :");gets(buff);
if(strlen(buff) == 0) return;
SetHeadPosition();
plocal = g_pAddrHead;
g_pFind = NULL;
while(plocal)
{
if(strstr(plocal->name,buff))
{
g_pFind = plocal;
break;
}
if(strstr(plocal->tel,buff))
{
g_pFind = plocal;
break;
}
if(strstr(plocal->addr,buff))
{
g_pFind = plocal;
break;
}
plocal = plocal->next;
}
if(g_pFind)
{
puts(g_pFind->name);
puts(g_pFind->tel);
puts(g_pFind->addr);
}
else
{
printf("\n\n搜索不到要找的%s.\n\n",buff);
}
}
void Modify_addr(void)
{
char name[100] = {0,};
ADDR addr;
while(1)
{
printf("\n\n要修改的姓名: ");gets(name);
if(strlen(name) == 0) return;
if(find_list(name) == 0)
{
puts("输入的姓名不存在.");
continue;
}
break;
}
printf("\n%s的通讯录如下:\n\n",name);
puts(g_pFind->name);
puts(g_pFind->tel);
puts(g_pFind->addr);
printf("\n输入要修改的姓名/电话/地址.\n\n");
printf("NAME: ");gets(addr.name);
printf("TEL : ");gets(addr.tel);
printf("ADDR: ");gets(addr.addr);
if(strlen(addr.name)==0) strcpy(addr.name,name);
strcpy(g_pFind->name,addr.name);
strcpy(g_pFind->tel,addr.tel);
strcpy(g_pFind->addr,addr.addr);
g_bSaved = 0;
printf("%s的通讯录修改成功.\n",name);
}
void Delete_addr(void)
{
char name[100] ={0,};
ADDR *plocal;
int ch;
while(1)
{
printf("\n\n要删除的姓名: ");gets(name);
if(strlen(name)==0) return;
if(find_list(name) == 0)
{
puts("要删除的通讯录不存在.");
continue;
}
break;
}
puts(g_pFind->name);
puts(g_pFind->tel);
puts(g_pFind->addr);
printf("确定要删除%s的通讯录吗(y/n)?",name);
ch = getch();
fflush(stdin);
if(ch == 'Y'||ch == 'y')
{
if(g_pFind->prev == NULL)
{
if(g_pFind->next == NULL)
{
free(g_pFind);
g_pAddrHead = NULL;
}
else
{
plocal = g_pFind->next;
free(g_pFind);
plocal->prev =NULL;
g_pAddrHead = plocal;
}
}
else if(g_pFind->next == NULL)
{
plocal = g_pFind->prev;
free(g_pFind);
plocal->next =NULL;
g_pAddrHead = plocal;
}
else
{
plocal = g_pFind->prev;
plocal->next = g_pFind->next;
plocal = g_pFind->next;
plocal->prev = g_pFind->prev;
free(g_pFind);
g_pAddrHead = plocal;
}
g_bSaved = 0;
printf("\n\n删除完毕.\n\n");
}
}
void Print_addr(void)
{
int count = 1;
ADDR *plocal;
SetHeadPosition();
if(g_pAddrHead == NULL)
{
printf("\n\n无任何数据\n");
return;
}
plocal = g_pAddrHead;
while(plocal->prev)
{
plocal=plocal->prev;
}
printf("\n\n");
while(plocal)
{
printf("NO.%d\n",count++);
puts(plocal->name);
puts(plocal->tel);
printf("%s\n\n",plocal->addr);
printf("请按任意一健(结束:q)\n\n");
getch();
if(getch() == 'q')return;
plocal=plocal->next;
}
}
void Save_addr(void)
{
ADDR *plocal, *pn;
FILE *fp;
if(g_pAddrHead == NULL) return;
fp = fopen(ADDRFILE,"w+b");
if(fp == NULL)
{
perror("file open error;");
return;
}
SetHeadPosition();
plocal = g_pAddrHead;
while(plocal)
{
pn = plocal->next;
plocal = pn;
}
printf("\n所有数据已经储存完毕.");
g_bSaved = 1;
fclose(fp);
} 嘿嘿,给分吧
用struct定义每个学生。 其中用if(.class=“班级”)判断。计算成绩总成绩回了吧