首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

输入数据后显示不正确,请

2012-03-11 
输入数据后显示不正确,请高手指点!各位高手,以下一段程序,输入两组信息后,查询时,显示值与录入值不同,实在

输入数据后显示不正确,请高手指点!
各位高手,以下一段程序,输入两组信息后,查询时,显示值与录入值不同,实在不知是什么原因造成,希望得到各位高手指点!

#include   <stdio.h>
#include   <stdlib.h>
#include   <malloc.h>
#define   MAX   1
#define   NULL   0
#define   LEN   sizeof(struct   student)
struct   student
{
        int   num;
        char   name;
        char   sex;
        int   age;
        int   score[5];
        struct   student   *next;
};


int   n;

void   DisplayMenu();
int   choiceItem();
void   Init(struct   student   *head);
void   print_all(struct   student   *head);
struct   student   *input_aInfo(struct   student   *head);
struct   student   *input_allInfo(struct   student   *head);
struct   student   *Search(struct   student   *head);
struct   student   *Modify(struct   student   *head);

void   DisplayMenu(){       /*Display   Menu*/
printf( "\n=========   Main   Menu   ========= ");
printf( "\n   Please   input   more   student 's   information   .........   1 ");
printf( "\n   Please   input   a   student 's   information   ............   2 ");
printf( "\n   Output   student 's   information   ....................   3 ");
printf( "\n   System   recovery   .................................   4 ");
printf( "\n   Query   student 's   information   .....................   5 ");
printf( "\n   Modify   student 's   information   ....................   6 ");
printf( "\n   Add   new   student   .................................   7 ");
printf( "\n   Quit   ............................................   0 ");
printf( "\n   Please   Select: ");
}

int   choiceItem(){     /*Menu   Select*/
int   choice;
do{
DisplayMenu();
scanf( "%d ",&choice);
}while(choice <0||choice> 7);
return   choice;
}

void   Init(struct   student   *head){       /*Initial*/
        head=NULL;
}


void   print_all(struct   student   *head){         /*Output   all   students '   information*/
struct   student   *p;
        printf( "\n ");
        p=head;
        if(head!=NULL)
do{
printf( "ID:%d     Name:%s     Sex:%s     Base:%d     Courses:%d     Elective   Course:%d     Humanities   courses:%d     Experimental   course:%d ",p-> num,p-> name,p-> sex,p-> score[0],p-> score[1],p-> score[2],p-> score[3],p-> score[4]);
p=p-> next;
}while(p!=NULL);
}


struct   student   *input_allInfo(struct   student   *head){     /*Input   Student 's   information*/
struct   student   *p1,*p2;
        int   i;


p1=head;
for(i=0;i <MAX;i++){
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Courses: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective   Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities   courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental   course: ");
scanf( "%d ",&p1-> score[4]);
p2=p1;
p1=p1-> next;
}
}

struct   student   *Search(struct   student   *head){     /*Query*/
struct   student   *p1,*p2;
int   num;
clrscr();
printf( "\n\nPlease   input   ID: ");
scanf( "%d ",&num);
        p1=head;
        while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;
}
        if(num==p1-> num)
printf( "ID:%d     Name:%s     Sex:%s     Base:%d     Course:%d     Elective   Course:%d     Humanities   courses:%d     Experimental   course:%d ",p1-> num,p1-> name,p1-> sex,p1-> score[0],p1-> score[1],p1-> score[2],p1-> score[3],p1-> score[4]);
        else
                printf( "%d   No   Matched   Data\n ",num);
                return(head);
}

struct   student   *Modify(struct   student   *head){     /*Modify*/
struct   student   *p1,*p2;
int   num;
clrscr();
printf( "\nPlease   input   ID: ");
scanf( "%d ",&num);
if(head==NULL){
printf( "\nBlank   File\n ");
goto   end;
}
        p1=head;
        while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective   Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities   courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental   course: ");
scanf( "%d ",&p1-> score[4]);
}else
                  printf( "%d   No   Matched   Data\n ",num);
                  end:
                  return(head);


}


struct   student   *input_aInfo(struct   student   *head){             /*input   new   student 's   information*/
struct   student   *p1,*p2;
int   num;
printf( "\nPlease   input   ID:   ");
scanf( "%d ",&num);
if(head==NULL){
printf( "\nBlank   File\n ");
goto   end;
}
        p1=head;
        while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
printf( "ID: ");
scanf( "%d ",p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective   Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities   courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental   course: ");
scanf( "%d ",&p1-> score[4]);
}else
printf( "%d   No   Matched   Data\n ",num);
end:
return(head);
  }


struct   student     *creat(void){                     /*Create   Linkage   Table*/
struct   student   *head;
struct   student   *p1,*p2;
int   i;
n=MAX;
p1=p2=(struct   student*)malloc(LEN);
printf( "\n\nID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective   Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities   courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental   course: ");
scanf( "%d ",&p1-> score[4]);
head=NULL;
for(i=0;i <n;i++){
if(i==0)
head=p1;
else
p2-> next=p1;
p1=(struct   student*)malloc(LEN);
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective   Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities   courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental   course: ");
scanf( "%d ",&p1-> score[4]);


}
p2-> next=NULL;
return(head);

}


struct   student   *del(struct   student   *head){                           /*Delete*/
struct   student   *p1,*p2;
        int   num;
        printf( "Please   input   ID   you   want   to   delete   it: ");
        scanf( "%d ",&num);
if(head==NULL){
printf( "\nNo   ID   can   be   deleted\n ");
goto   end;
}
        p1=head;
        while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
if(p1==head)
head=p1-> next;
else
p2-> next=p1-> next;
printf( "Delete:%d\n ",num);
n=n-1;
}else
printf( "%d   No   Matched   Data\n ",num);
end:
return(head);
}


struct   student   *insert(struct   student   *head,struct   student   *stud){         /*Insert   linkage*/
struct   student   *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL){
head=p0;
p0-> next=NULL;
}else{
while(p0-> num> p1-> num){
p2=p1;
p1=p1-> next;
}
if(p0-> num <=p1-> num){
if(head==p1)
head=p0;
else
p2-> next=p0;
p0-> next=p1;
}else{
p1-> next=p0;
p0-> next=NULL;
}
}
n=n+1;
return(head);
}


void   main(){
struct   student   *head,*stud;
        int   choice;

        Init(head);
        creat();
        /*Init(head);*/
        do{
choice=choiceItem();
switch   (choice){
case   0:printf( "\nThank   you ");break;
case   1:input_allInfo(head);break;
case   2:input_aInfo(head);break;
case   3:print_all(head);break;
case   4:Init(head);break;
case   5:Search(head);break;
case   6:Modify(head);break;
case   7:insert(head,stud);break;
}
        }while(choice);
}

[解决办法]
问题比较多

我主要说一下在输入那一块的,也就是*input_allInfo函数部分


struct student *input_allInfo(struct student *head){ /*Input Student 's information*/
struct student *p1,*p2;
int i;

p1 = p2 = (struct student *)malloc (LEN); /* p1,p2应该在使用前分配空间 */

p1=head;

/* 先输入一组数据 */
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Courses: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);


printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);

for(i=0;i <MAX-1;i++){ /* 循环次数要减1 */
p2 = p1; /* 为p2赋值 */
p1 = (inform *) malloc (LEN); /* 给p1分配空间 */
printf( "ID: "); /* 接着输入下一组数据 */
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Courses: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
p2 -> next = p1; /* 为了让前一个指针指向下一个指针 */
}
p2-> next = NULL; /* 这儿一定不能忘了,链表最后一个值为NULL */

return head; /* 你还忘了加返值了 */
}
[解决办法]
问题真多,创建后返回主函数之前头结点已经释放了,打印的时候当然错了。。。把正确打印这个给你改好了,其余的你自己去改吧。。。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 1
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
char sex[3];
int age;
int score[5];
struct student *next;
};


int n;

void DisplayMenu();
int choiceItem();
void Init(struct student *head);
void print_all(struct student *head);
struct student *input_aInfo(struct student *head);
struct student *input_allInfo(struct student *head);
struct student *Search(struct student *head);
struct student *Modify(struct student *head);

void DisplayMenu(){ /*Display Menu*/
printf( "\n========= Main Menu ========= ");
printf( "\n Please input more student 's information ......... 1 ");
printf( "\n Please input a student 's information ............ 2 ");
printf( "\n Output student 's information .................... 3 ");
printf( "\n System recovery ................................. 4 ");
printf( "\n Query student 's information ..................... 5 ");
printf( "\n Modify student 's information .................... 6 ");
printf( "\n Add new student ................................. 7 ");
printf( "\n Quit ............................................ 0 ");
printf( "\n Please Select: ");
}

int choiceItem(){ /*Menu Select*/
int choice;
do{
DisplayMenu();
scanf( "%d ",&choice);
}while(choice <0||choice> 7);
return choice;
}

void Init(struct student *head){ /*Initial*/
head=NULL;
}


void print_all(struct student *head){ /*Output all students ' information*/
struct student *p;
printf( "\n ");
p=head;
if(head!=NULL)
do{
printf( "ID:%d Name:%s Sex:%s Base:%d Courses:%d Elective Course:%d Humanities courses:%d Experimental course:%d ",p-> num,p-> name,p-> sex,p-> score[0],p-> score[1],p-> score[2],p-> score[3],p-> score[4]);
p=p-> next;
}while(p!=NULL);
}


struct student *input_allInfo(struct student *head){ /*Input Student 's information*/
struct student *p1,*p2;
int i;


p1=head;
for(i=0;i <MAX;i++){
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Courses: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
p2=p1;
p1=p1-> next;
}

return head;
}

struct student *Search(struct student *head){ /*Query*/
struct student *p1,*p2;
int num;
clrscr();
printf( "\n\nPlease input ID: ");
scanf( "%d ",&num);
p1=head;
while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;
}
if(num==p1-> num)
printf( "ID:%d Name:%s Sex:%s Base:%d Course:%d Elective Course:%d Humanities courses:%d Experimental course:%d ",p1-> num,p1-> name,p1-> sex,p1-> score[0],p1-> score[1],p1-> score[2],p1-> score[3],p1-> score[4]);
else
printf( "%d No Matched Data\n ",num);
return(head);
}

struct student *Modify(struct student *head){ /*Modify*/
struct student *p1,*p2;
int num;
clrscr();
printf( "\nPlease input ID: ");
scanf( "%d ",&num);
if(head==NULL){
printf( "\nBlank File\n ");
goto end;
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
}else
printf( "%d No Matched Data\n ",num);
end:
return(head);
}


struct student *input_aInfo(struct student *head){ /*input new student 's information*/
struct student *p1,*p2;
int num;
printf( "\nPlease input ID: ");
scanf( "%d ",&num);
if(head==NULL){
printf( "\nBlank File\n ");
goto end;
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
printf( "ID: ");
scanf( "%d ",p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");


scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
}else
printf( "%d No Matched Data\n ",num);
end:
return(head);
}


struct student *creat(void){ /*Create Linkage Table*/
struct student *head;
struct student *p1,*p2;
int i;
n=MAX;
p1=p2=(struct student*)malloc(LEN);
printf( "\n\nID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
head = p2;
for(i=0;i <n;i++){
if(i==0)
p2 = p1;
else
p2-> next=p1;
p1=(struct student*)malloc(LEN);
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
}
p2-> next = p1;
p2 = p1;
p2-> next=NULL;

return(head);

}


struct student *del(struct student *head){ /*Delete*/
struct student *p1,*p2;
int num;
printf( "Please input ID you want to delete it: ");
scanf( "%d ",&num);
if(head==NULL){
printf( "\nNo ID can be deleted\n ");
goto end;
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
if(p1==head)
head=p1-> next;
else
p2-> next=p1-> next;
printf( "Delete:%d\n ",num);
n=n-1;
}else
printf( "%d No Matched Data\n ",num);
end:
return(head);
}


struct student *insert(struct student *head,struct student *stud){ /*Insert linkage*/
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL){
head=p0;
p0-> next=NULL;
}else{
while(p0-> num> p1-> num){
p2=p1;
p1=p1-> next;
}
if(p0-> num <=p1-> num){
if(head==p1)
head=p0;
else
p2-> next=p0;
p0-> next=p1;


}else{
p1-> next=p0;
p0-> next=NULL;
}
}
n=n+1;
return(head);
}


void main(){
struct student *head,*stud;
int choice;

head = (struct student *)malloc(LEN);
stud = (struct student *)malloc(LEN);

Init(head);
head = creat();
/*Init(head);*/
do{
choice=choiceItem();
switch (choice){
case 0:printf( "\nThank you ");break;
case 1:input_allInfo(head);break;
case 2:input_aInfo(head);break;
case 3:print_all(head);break;
case 4:Init(head);break;
case 5:Search(head);break;
case 6:Modify(head);break;
case 7:insert(head,stud);break;
}
}while(choice);
}

[解决办法]
好像无法这样定义,提示系统错误!

void Init(struct student *&head){ /*Initial*/


难道你的是纯C的编译器吗?那你改成这样:
void Init(struct student **head){
*head=NULL;
}
[解决办法]
没做内存的释放处理

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 1
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[10];
char sex[3];
int age;
int score[5];
struct student *next;
};


int n;

void DisplayMenu();
int choiceItem();
void Init(struct student *head,student *stud);
void print_all(struct student *head);
struct student *input_aInfo(struct student *head);
struct student *input_allInfo(struct student *head);
struct student *Search(struct student *head);
struct student *Modify(struct student *head);

void DisplayMenu(){ /*Display Menu*/
printf( "\n========= Main Menu ========= ");
printf( "\n Please input more student 's information ......... 1 ");
printf( "\n Please input a student 's information ............ 2 ");
printf( "\n Output student 's information .................... 3 ");
printf( "\n System recovery ................................. 4 ");
printf( "\n Query student 's information ..................... 5 ");
printf( "\n Modify student 's information .................... 6 ");
printf( "\n Add new student ................................. 7 ");
printf( "\n Quit ............................................ 0 ");
printf( "\n Please Select: ");
}

int choiceItem(){ /*Menu Select*/
int choice;
do{
DisplayMenu();
scanf( "%d ",&choice);
}while(choice <0||choice> 7);
return choice;
}

void Init(struct student *head,student *stud){ /*Initial*/

head=(student*)malloc(sizeof(student));
head-> next=NULL;
stud=(student*)malloc(sizeof(student));
stud-> next=NULL;

}


void print_all(struct student *head){ /*Output all students ' information*/
struct student *p;
printf( "\n ");
p=head;
if(head!=NULL)
do{
printf( "ID:%d Name:%s Sex:%s Base:%d Courses:%d Elective Course:%d Humanities courses:%d Experimental course:%d ",p-> num,p-> name,p-> sex,p-> score[0],p-> score[1],p-> score[2],p-> score[3],p-> score[4]);
p=p-> next;
}while(p!=NULL);
}


struct student *input_allInfo(struct student *head){ /*Input Student 's information*/


struct student *p1,*p2;
int i;
p1=head;
for(i=0;i <MAX;i++){
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Courses: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
p2=p1;
p1=p1-> next;
}
return p1;
}

struct student *Search(struct student *head){ /*Query*/
struct student *p1,*p2;
int num;
//clrscr();
system( "cls ");
printf( "\n\nPlease input ID: ");
scanf( "%d ",&num);
p1=head;
while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;
}
if(num==p1-> num)
printf( "ID:%d Name:%s Sex:%s Base:%d Course:%d Elective Course:%d Humanities courses:%d Experimental course:%d ",p1-> num,p1-> name,p1-> sex,p1-> score[0],p1-> score[1],p1-> score[2],p1-> score[3],p1-> score[4]);
else
printf( "%d No Matched Data\n ",num);
return(head);
}

struct student *Modify(struct student *head){ /*Modify*/
struct student *p1,*p2;
int num;
//clrscr();
system( "cls ");
printf( "\nPlease input ID: ");
scanf( "%d ",&num);
if(head==NULL){
printf( "\nBlank File\n ");
goto end;
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
}else
printf( "%d No Matched Data\n ",num);
end:
return(head);
}


struct student *input_aInfo(struct student *head){ /*input new student 's information*/
struct student *p1,*p2;
int num;
printf( "\nPlease input ID: ");
scanf( "%d ",&num);
if(head==NULL){
printf( "\nBlank File\n ");
goto end;
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
printf( "ID: ");
scanf( "%d ",p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);


printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
}else
printf( "%d No Matched Data\n ",num);
end:
return(head);
}


struct student *creat(void){ /*Create Linkage Table*/
struct student *head;
struct student *p1,*p2;
int i;
n=MAX;
p1=p2=(struct student*)malloc(LEN);
//p2=(struct student*)malloc(LEN);
printf( "\n\nID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);
head=p1;
for(i=0;i <n;i++){

p1=(struct student*)malloc(LEN);
//p2=(struct student*)malloc(LEN);
printf( "ID: ");
scanf( "%d ",&p1-> num);
printf( "Name: ");
scanf( "%s ",&p1-> name);
printf( "Sex: ");
scanf( "%s ",&p1-> sex);
printf( "Age: ");
scanf( "%d ",&p1-> age);
printf( "Base: ");
scanf( "%d ",&p1-> score[0]);
printf( "Course: ");
scanf( "%d ",&p1-> score[1]);
printf( "Elective Course: ");
scanf( "%d ",&p1-> score[2]);
printf( "Humanities courses: ");
scanf( "%d ",&p1-> score[3]);
printf( "Experimental course: ");
scanf( "%d ",&p1-> score[4]);

head-> next=p1;
}
p1-> next=NULL;
return(head);

}


struct student *del(struct student *head){ /*Delete*/
struct student *p1,*p2;
int num;
printf( "Please input ID you want to delete it: ");
scanf( "%d ",&num);
if(head==NULL){
printf( "\nNo ID can be deleted\n ");
goto end;
}
p1=head;
while(num!=p1-> num&&p1-> next!=NULL){
p2=p1;
p1=p1-> next;}
if(num==p1-> num){
if(p1==head)
head=p1-> next;
else
p2-> next=p1-> next;
printf( "Delete:%d\n ",num);
n=n-1;
}else
printf( "%d No Matched Data\n ",num);
end:
return(head);
}


struct student *insert(struct student *head,struct student *stud){ /*Insert linkage*/
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL){
head=p0;
p0-> next=NULL;
}else{
while(p0-> num> p1-> num){
p2=p1;
p1=p1-> next;


}
if(p0-> num <=p1-> num){
if(head==p1)
head=p0;
else
p2-> next=p0;
p0-> next=p1;
}else{
p1-> next=p0;
p0-> next=NULL;
}
}
n=n+1;
return(head);
}


void main(){
struct student *head=NULL,*stud=NULL;
int choice;


Init(head,stud);
head=creat();

/*Init(head);*/
do{
choice=choiceItem();
switch (choice){
case 0:printf( "\nThank you ");break;
case 1:input_allInfo(head);break;
case 2:input_aInfo(head);break;
case 3:print_all(head);break;
case 4:Init(head,stud);break;
case 5:Search(head);break;
case 6:Modify(head);break;
case 7:insert(head,stud);break;
}
}while(choice);
}

热点排行