inset用法c语言,用C语言编写void inset(struct student *p)函数

匿名用户

1级

2006-06-21 回答

struct SStudent

{

unsigned number;

char name[11];

int score1, score2, score3;

struct SStudent * link;

};

struct SStudent * CreateTable();/*?建空?表*/

void AppendToTable(struct SStudent * * stu);/*初始化?表,?入?表中?个?点的数据*/

void InsertToTable(struct SStudent * * stu);/*插入新?点*/

void DeleteFromTable(struct SStudent * * stu,int num);/*?除指定序号的?点*/

void PrintTable(struct SStudent * stu);/*屏幕?示?出?表所有?点*/

void Help();/*?示?助提示*/

void main()

{

struct SStudent * student;

unsigned number;

char keyValue;

student = CreateTable();

//clrscr();

Help();

printf("\n= ");

while((keyValue = getche()) != 'q' && keyValue != 'Q' && keyValue != 27)

{

puts("");

switch(keyValue)

{

case 'l': case 'L':

PrintTable(student); break;

case 'a': case 'A':

AppendToTable(&student); break;

case 'i': case 'I':

InsertToTable(&student); break;

case 'd': case 'D':

printf("Please input the number you want delete: ");

scanf("%d", &number);

DeleteFromTable(&student, number);

break;

case 'c': case 'C':

//clrscr();

break;

case 'h': case 'H':

Help(); break;

default: puts("Error command!");

}

printf("\n= ");

}

}

struct SStudent* InputStudent()

{

struct SStudent* p = (struct SStudent*)malloc(sizeof(struct SStudent));

if(p != NULL)

{

printf("input number:");

scanf("%d",& p->number);

printf("input name:");

scanf("%s",& p->name);

printf("input score1:");

scanf("%d",& p->score1);

printf("input score2:");

scanf("%d",& p->score2);

printf("input score3:");

scanf("%d",& p->score3);

p->link = NULL;

}

return p;

}

struct SStudent * CreateTable()

{

/*?建空?表*/

return NULL;

//return InputStudent();

}

void AppendToTable(struct SStudent * * stu)

{

/*初始化?表,?入?表中?个?点的数据*/

struct SStudent * * q = stu;

while((*q) != NULL)

{

q = &(*q)->link;

}

(*q) = InputStudent();

}

void InsertToTable(struct SStudent * * stu)

{

/*插入新?点*/

struct SStudent* q = InputStudent();

if(q != NULL)

{

if((*stu) != NULL)

{

q->link = (*stu);

}

(*stu) = q;

}

}

void DeleteFromTable(struct SStudent * * stu,int num)

{

/*?除指定序号的?点*/

struct SStudent** p = stu,*q = *p;

/* p zhi xiang shan chu jie dian */

/* q zhi xiang shan chu jie dian de qian yi jie dian */

while((*p) != NULL && (*p)->number != num)

{

q = *p;

p = &(*p)->link;

}

if((*p) == NULL)

{

puts("No Find");

return;

}

q->link = (*p)->link;

free(p);

}

void PrintTable(struct SStudent * stu)

{

/*屏幕?示?出?表所有?点*/

struct SStudent * p = stu;

printf("*********************************************************************\n");

while(p != NULL)

{

printf("number = %d\tname = %s\tscore1 = %d\tscore2 = %d\tscore3 = %d\n",

p->number,p->name,p->score1,p->score2,p->score3);

p = p->link;

}

printf("*********************************************************************\n");

}

void Help()

{ puts(" *********************************************");

puts(" * Student System Command Help *");

puts(" *********************************************");

puts(" * L = List all records *");

puts(" * A = Append records *");

puts(" * I = Insert records *");

puts(" * H = Show this help message *");

puts(" * C = Clear screen *");

puts(" * Q = Quit System *");

puts(" *********************************************");

puts(" * Copyright (C) 1999.09.09 By Kcliu *");

puts(" *********************************************");

}

#include"stdio.h" #include"stdlib.h" #define NULL 0 struct student { long num; char name[20]; int score[6]; struct student *next; }; void show() { printf("\nthere is a cataloge as follow.\n"); printf("***************************************\n"); printf("* *\n"); printf("* 1. create *\n"); printf("* 2. Insert *\n"); printf("* 3. print *\n"); printf("* 4. delete *\n"); printf("* 5. modify *\n"); printf("* 6. save_to_file *\n"); printf("* 7. lode from file *\n"); printf("* 8. exit *\n"); printf("***************************************\n"); printf("please input 1--8 to choice what you want:\n"); } struct student *create() { struct student *head,*p,*last; int i; int temp2; char name[20]; p=head=(struct student *)malloc(sizeof(struct student)); head->next=NULL; last=p; while(1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); /*last->next=p; last=p;*/ p->next=NULL; printf("number:"); scanf("%ld",&p->num); if(p->num==0)break; getchar(); printf("name:"); scanf("%s",p->name); printf("score:"); for(i=0;iscore[i]=temp2; } printf("next student's information.\n"); } free(p); return head; } void Insert(struct student *head) { struct student *p,*q; int score; long num; int i; printf("\nEnter the student's information you want to insert.\n"); printf("number:"); scanf("%ld",&num); q->num=num; getchar(); printf("name:"); scanf("%s",q->name); printf("score:(chinese,math,english,biology,physics,chemistry)\n"); for(i=0;iscore[i]=score; } q->next=NULL; p=head; while(p->next->numnum&&p->next!=NULL) p=p->next; q->next=p->next; p->next=q; if(p->next==NULL) p->next=q; } void delete(struct student *head) { struct student *p,*q; long num; printf("enter the student's information you want to delete.\n"); printf("number:"); scanf("%ld",&num); getchar(); p=head; while(p->next!=NULL&&p->next->num!=num) p=p->next; q=p->next; p->next=p->next->next; free(q); } void print(struct student *head) { struct student *p; int i; p=head->next; if(p==NULL) { printf("\nthere is no information.\n"); exit(0); } printf("\nnumber\tnamme\tchinese\tmath\tenglish\tbiology\tphysics\tchemistry\n"); while(p!=NULL) { printf("\n%ld\t%s",p->num,p->name); for(i=0;iscore[i]); p=p->next; } } void modify(struct student *head) { struct student *p; int choice,i; long num; char name[20]; int score[6]; printf("\nEnter what student's information you want to modify.\n"); printf("number:"); scanf("%ld",&num); getchar(); printf("\nname:"); scanf("%s",name); printf("\n"); p=head->next; while(p->num!=num&&p->name[20]!=name[20]&&p!=NULL) p=p->next; printf("\nplease choice what you want to modify:1-number 2-name 3-score.\n"); scanf("%d",&choice); switch(choice) { case 1:printf("\nEnter the true number:"); scanf("%ld",&num); p->num=num; break; case 2:printf("\nEnter the true name:"); scanf("%s",p->name); break; case 3:printf("\nEnter the right score:"); for(i=0;iscore[i]=score[i]; } break; } } void save_in(struct student *head) { struct student *p; FILE *fp; char file_name[30]; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"w"))==NULL) { printf("can't open the file.\n"); exit(0); } p=head; while(p->next!=NULL) { fwrite((void*)p->next,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } struct student *load_from_file() { struct student *head,*p,*last; FILE *fp; char file_name[30]; head=(struct student *)malloc(sizeof(struct student)); last=head; head->next=NULL; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"r"))==NULL) { printf("can't open the file.\n"); exit(0); } p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; while(fp=fread((void *)p,sizeof(struct student),1,fp)==1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; } free(p); fclose(fp); return head; } void main() { struct student *la; int choice; /*char Yes_No;*/ la=(struct student *)malloc(sizeof(struct student)); la->next=NULL; while(1) { show(); scanf("%d",&choice); switch(choice) { case 1:la=create(); break; case 2:Insert(la); break; case 3:print(la); break; case 4:delete(la); break; case 5:modify(la); break; case 6:save_in(la); break; case 7:la=load_from_file(); break; case 8:exit(0); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值