分享
 
 
 

单向链表实现学生管理程序

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/* TC 2.01 测试通过 非 tc 请自行修改 */

/* 没有全面测试 自行解决 嘿嘿 by碎心竹 */

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <ctype.h>

#define NAME 16

#define SEX 6

/*

#define NO 6

#define AGE 3

#define CLAS 3

*/

#define ERROR 0

#define OK 1

/*

1、学生名单管理

任务:可以实现学生名单的增加、删除、修改、显示、排序;

姓名、 性别、 年龄、 学号、 班级、 基础课成绩、专业课程成绩,总成绩

name sex age no clas base pro sum

zhangwenhui n 24 123456 5 70 60 130

*/

typedef struct nod

{

int no;

char name[NAME];

char sex[SEX];

short int age;

short int clas;

int base;

int pro;

int sum;

struct nod *next;

}Nod;

int readfile(Nod *head,FILE *fp); /* 从文件读取数据 */

int writefile(FILE *fp,Nod *head); /* 数据写入文件 */

Nod * createnod(void); /* 创建空链表 */

int destroynod(Nod *head); /*销毁 链表 */

int getline(char *pch,int x); /* 字符输入 */

int getdig(int *i); /*数字输入*/

int insetnod (Nod *head); /* 顺序插入一个学员 */

int deletenod(Nod *head); /*删除一个学员 */

int sortnod(Nod *head); /*排序 列表 */

int printnod(Nod *head); /*显示所有 具有分屏功能*/

modifynod(Nod *head); /* 修改学员资料 */

void mainlist(int open,FILE *fp); /* 主菜单*/

main(void)

{

int open = 0; /* 文件打开 */

FILE *fp;

fp = fopen ("data.txt","r");

if (!fp) {

open = 1;

}

textbackground(BLUE);

textcolor(YELLOW);

clrscr();

printf(" \n\n\n\n\n\n\n\n\n\t\t\t *************************\n"

" \t\t\t * *\n"

" \t\t\t * *\n"

" \t\t\t * Welcom to this ! *\n"

" \t\t\t * *\n"

" \t\t\t * *\n"

" \t\t\t *************************\n"

"\n\n\n\n\n\n\n\n\n\nPlease any key to continue........\n");

getch();

mainlist(open,fp);

return 0;

}/* main() */

void mainlist(int open,FILE *fp)

{

int ch;

Nod *head = NULL;

if (fp){

head = createnod();

readfile(head,fp);

}

while(1){

textcolor(YELLOW);

clrscr();

printf("\n\n\n "

"\t\t\t*******************************\n"

"\t\t\t* *\n"

"\t\t\t* 1. Creat a list *\n"

"\t\t\t* 2. Add a student *\n"

"\t\t\t* 3. Del a student *\n"

"\t\t\t* 4. Print list *\n"

"\t\t\t* 5. Modify a student *\n"

"\t\t\t* 6. Sort list for No *\n"

"\t\t\t* 7. Load File * do't use! *\n"

"\t\t\t* 8. Wirte File for list *\n"

"\t\t\t* *\n"

"\t\t\t*******************************\n"

"\t\t\tOther is Exit !!! \n"

);

if(!open)

printf("\n\n\n\n FILE IS OPEN !!!!!!\n");

else

printf("\n\n\n\n FILE NOT OPEN Now is Write Mode !!\n");

ch=getch();

switch(ch){

case '1':if (!open){

textcolor(WHITE);

clrscr();

printf("\n\nFile Is Open Not Write \n\nPlease Input 7 \n\n\n Please any key to continue\n");

getch();

continue;

}

else if(head){

textcolor(WHITE);

clrscr();

printf("Lis Is have Now Not be Create \n\n\nPlease any key to continue\n");

getch();

continue;

}

head = createnod();

textcolor(WHITE);

clrscr();

printf("\nCreat List OK! \n\nPlease Input 2 for Add a studen \n\n\nPlease any key to continue\n");

getch();

break;

case '2':if (!head){

textcolor(WHITE);

clrscr();

printf("\n\nList is Empty\n\n Please Input 1\n\n\n\n Please any key to continue\n");

getch();

continue;

}

else{

clrscr();

insetnod (head);

}

break;

case '3':if (!head){

textcolor(WHITE);

clrscr();

printf("\n\nList is Empty \n\nPlease Input 1\n\n\n\n Please any key to continue\n");

getch();

continue;

}

else{

clrscr();

deletenod(head);

}

break;

case '4':if (!head){

textcolor(WHITE);

clrscr();

printf("\n\nList is Empty\n\n Please Input 1\n\n\n\n Please any key to continue\n");

getch();

continue;

}

else{

clrscr();

printnod(head);

}

break;

case '5':if (!head){

textcolor(WHITE);

clrscr();

printf("\n\nList is Empty Please Input 1\n\n\n Please any key to continue\n");

getch();

continue;

}

else{

textcolor(YELLOW);

clrscr();

modifynod(head);

printf("modif stdent OK\n\n\n Please any key to continue\n");

getch();

}

break;

case '6':if (!head){

textcolor(WHITE);

clrscr();

printf("List is Empty \n\nPlease Input 1\n\n\n Please any key to continue\n");

getch();

continue;

}

else{

textcolor(YELLOW);

clrscr();

sortnod(head);

printf("Sort stdent OK\n\n\n Please any key to continue\n");

getch();

}

break;

/*

case '7':if (open){

clrscr();

printf("File is Not Find Please Create a list to Write it\n\n\n Please any key to continue\n");

getch();

continue;

}

else if (head){

clrscr();

printf("List is being Don't Read file \n\n\n Please any key to continue\n");

getch();

continue;

}

else{

clrscr();

readfile(fp);

}

break;

*/

case '8': if (!head){

textcolor(WHITE);

clrscr();

printf("\n\nList is Empty \n\nPlease Input 1\n\n\n Please any key to continue\n");

getch();

continue;

}

else{

textcolor(YELLOW);

clrscr();

writefile(fp,head);

printf("File Write OK ! \n\n\n\nPlease any key to continue\n");

getch();

}

break;

default : destroynod(head); exit(0);

}

/*break; */

}

}/* mainlist() */

Nod * createnod(void)

{

Nod *head ;

head = (Nod *) malloc( sizeof(Nod) );

if (!head)

return NULL;

head->no = 0;

head->name[0] = '\0';

head->sex[0] = '\0';

head->age = 0;

head->clas = 0;

head->base = 0;

head->pro = 0;

head->sum = 0;

head->next = NULL;

return head;

}/* createnod() */

int insetnod (Nod *head)

{

int no;

Nod *curr, *front, *temp;

if(!head)

return ERROR;

front = curr = head;

textcolor(YELLOW);

clrscr();

printf("Please input this student No.\n"); /* 输入学号 */

getdig(&no);

while(NULL != curr && curr->next->no < no){ /* 寻找 插入位置 */

front = curr;

curr = curr->next;

}

temp = (Nod *) malloc( sizeof(Nod) );

if(!temp)

return ERROR;

temp->no = no;

/* printf("temp no:%d\n",temp->no); */

printf("Please Input this student Name:\n");

getline(temp->name,NAME);

/* printf("temp name :%s\n",temp->name);*/

printf("Please Input this student Sex:\n");

getline(&temp->sex,SEX);

printf("Please Input this student Age:\n");

getdig(&temp->age);

printf("Please Input this student class:\n");

getdig(&temp->clas);

printf("Please Input this student base:\n");

getdig(&temp->base);

printf("Please Input this student Pro:\n");

getdig(&temp->pro);

temp->sum = temp->base + temp->pro;

temp->next = front->next;

front->next = temp;

++head->sum; /* 链表总数+1 */

return OK;

}/* insetnod() */

int getdig(int *no)

{

int ch;

*no = 0;

while ( (ch = getchar()) != '\n' && ch != EOF){

if ( isdigit(ch) && (*no * 10 + (ch -'0')) > 0 ){

*no = *no * 10 + (ch - '0');

}

}

/* printf("no is : %d\n",*no);*/

return OK;

}/* getdig() */

int getline(char *pch,int x)

{

int i,ch;

x -= 1;

i = 0;

if (x < 0)

return ERROR;

while ( (ch = getchar()) != '\n' && ch != EOF){

if (i < x ){

pch[i] = ch;

/* printf("pch is : %c\n",pch[i]);*/

++i;

}

}

if (x > 1 ){

pch[i] = '\0';

/* printf(" \ 0 is input\n"); */

}

return OK;

}/* getline() */

int destroynod(Nod *head) /* 销毁 */

{

Nod *temp;

temp = head;

do{

head = temp;

temp = head->next;

printf("destroy %d\n",temp);

free(head);

}while (temp);

printf("See You \n");

return OK;

}/* destroynod() */

int deletenod(Nod *head) /* 删除 */

{

Nod *curr,*temp;

int no;

temp = curr = head;

printf("Please input this student No.\n"); /* 输入要删除学号 */

getdig(&no);

while(NULL != curr) { /* 寻找 位置 */

if (curr->no == no){

head->next = curr->next;

free(curr);

--temp->sum;

return OK;

}

head = curr;

curr = curr->next;

}

printf("Nod Find No.%d \n\n\n\nPlease any key to continue\n",no);

getch();

return ERROR;

} /* deletenod() */

int printnod(Nod *head)

{

if (!head)

return ERROR;

printf("all student is %d\n",head->sum);

head = head->next;

printf("%6s %10s %5s %5s %5s %5s %5d %5s\n",

"No","name","sex","age","class","base","pro", "sum");

while (head){

printf("%6d %10s %5s %5d %5d %5d %5d %5d\n",

head->no,head->name,head->sex,head->age,head->clas,

head->base,head->pro,head->sum);

head = head->next;

}

printf("Please any key to continue\n");

getch();

return OK;

}

int writefile(FILE *fp,Nod *head)

{

if (!head)

return ERROR;

fp = fopen ("data.txt","w");

head = head->next;

while(head != NULL){

fputs(itoa(head->no,"",10),fp);

fputc(' ',fp);

fputs(head->name,fp);

fputc(' ',fp);

fputs(head->sex,fp);

fputc(' ',fp);

fputs(itoa(head->age,"",10),fp);

fputc(' ',fp);

fputs(itoa(head->clas,"",10),fp);

fputc(' ',fp);

fputs(itoa(head->base,"",10),fp);

fputc(' ',fp);

fputs(itoa(head->pro,"",10),fp);

fputc(' ',fp);

fputs(itoa(head->sum,"",10),fp);

fputc('\n',fp);

head = head->next;

}

fclose(fp);

return OK;

}/* writefile() */

int readfile(Nod *head,FILE *fp)

{

Nod *temp, *suu;

char atmp[20];

if (!fp || !head)

return NULL;

suu = head;

while ( fscanf(fp,"%s",atmp) != EOF ){

/* fscanf(fp,"%s",atmp); */

temp =(Nod *) malloc( sizeof(Nod) );

temp->no = atoi(atmp);

fscanf(fp,"%s",temp->name) ;

fscanf(fp,"%s",temp->sex);

fscanf(fp,"%s",atmp);

temp->age = atoi( atmp );

fscanf(fp,"%s",atmp);

temp->clas = atoi( atmp );

fscanf(fp,"%s",atmp);

temp->base = atoi( atmp );

fscanf(fp,"%s",atmp);

temp->pro = atoi( atmp );

fscanf(fp,"%s",atmp);

temp->sum = atoi( atmp );

temp->next = NULL;

++suu->sum; /* */

head->next = temp;

head = head->next;

}

return OK;

}/* readfile() */

modifynod(Nod *head)/* 修改学员资料 */

{

Nod *temp;

int no;

temp = head;

printf("Please input this student No.\n"); /* 输入要修改学号 */

getdig(&no);

while(NULL != head) { /* 寻找 位置 */

if (temp->no == no){

printf("Please input this student No.\n"); /* 输入学号 */

getdig(&no);

temp->no = no;

printf("Please Input this student Name:\n");

getline(temp->name,NAME);

/* printf("temp name :%s\n",temp->name);*/

printf("Please Input this student Sex:\n");

getline(&temp->sex,SEX);

printf("Please Input this student Age:\n");

getdig(&temp->age);

printf("Please Input this student class:\n");

getdig(&temp->clas);

printf("Please Input this student base:\n");

getdig(&temp->base);

printf("Please Input this student Pro:\n");

getdig(&temp->pro);

temp->sum = temp->base + temp->pro;

return OK;

}

temp = temp->next;

}

printf("Nod Find No.%d \n\n\nPlease any key to continue\n",no);

getch();

return ERROR;

}/* modifynod() */

int sortnod(Nod *head)

{

Nod *list,*r, *temp, *hhead;

int max, i, l, ts;

hhead = head;

max = head -> sum-1;

printf("max: %d\n",max);

for (i = 0; i < max; i++){

head = hhead;

ts = 0; /* 测试 检查 */

for (l = 0; l < max;l++){

list = head -> next;

r = list -> next;

if(list->no > r->no){

temp = head->next;

head->next = list->next;

list -> next = r->next;

r->next = temp;

++ts;

}

head = head->next;

}

if (0 == ts)

return OK;

}

return OK;

}/* sortnod() */

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有