Ixiaofei423#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
#include <string.h>
#include <io.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long int num;
char name[20];
int age;
char sex[4];
char address[30];
long int tele_num;
char E_mail[20];
struct student *next;
};
int TOTAL_NUM = 0;
struct student *head = NULL;
void record();
void insert(struct student *stu);
void display(struct student *stu);
void displayAll();
void query();
void query_by_num();
void freeAll();
void devise(struct student *p);
//系統主菜單
void main()
{
int choice;
choice = -1;
do
{
printf("\n\n\n");
printf("\t\t\t--------------------------------\n");
printf("\t\t\t+ 學生信息管理系統 |\n");
printf("\t\t\t--------------------------------\n");
printf("\t\t\t+ [1]----錄入學生信息 |\n");
printf("\t\t\t+ [2]----浏覽學生信息 |\n");
printf("\t\t\t+ [3]----查詢學生信息 |\n");
printf("\t\t\t+ [0]----退出系統 |\n");
printf("\t\t\t+*·*·*·*·*·*·*·*·*·*·|\n");
printf("\t\t\t--------------------------------\n");
printf("請輸入您的選擇:");
scanf("%d", &choice);
switch(choice)
{
case 0:
freeAll();
break;
case 1:
record();
break;
case 2:
displayAll();
break;
case 3:
query();
break;
default:
printf("\n無效選項!");
break;
}
}
while(choice != 0);
}
//錄入學生信息
void record()
{
int i=0;
struct student *p0,*head;
char a;
do
{
p0 = (struct student *)malloc(LEN);
printf("請輸入學生的學號:");
scanf("%ld",&p0->num);
fflush(stdin);
printf("請輸入學生的姓名:");
scanf("%s",p0->name);
fflush(stdin);
printf("請輸入學生的年齡:");
scanf("%d",&p0->age);
fflush(stdin);
printf("請輸入學生的性別:");
scanf("%s",p0->sex);
fflush(stdin);
printf("請輸入學生的籍貫:");
scanf("%s",p0->address);
fflush(stdin);
printf("請輸入學生的電話:");
scanf("%ld",&p0->tele_num);
fflush(stdin);
printf("請輸入學生的E-mail:");
scanf("%s",p0->E_mail);
fflush(stdin);
printf("\n是否繼續輸入另外一個學生信息?(y/n)");
a=getchar();
i++;
}while(a=='y' && i<=50);
TOTAL_NUM=i;
head=p0;
}
void insert(struct student *stu)
{
struct student *p0, *p1, *p2;
p1 = head;
p0 = stu;
if(head == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while((p0->num > p1->num)&&(p1->next != NULL))
{
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;
}
}
TOTAL_NUM++;
}
void display(struct student *p)
{
printf("%ld\t%s\t%d\t%s\t%s\t%ld\t%s\n", p->num, p->name, p->age, p->sex,p->address, p->tele_num, p->E_mail);
}
//浏覽學生信息
void displayAll()
{
struct student *p;
printf("學生總數:%d\n", TOTAL_NUM);
if(head != NULL)
{
do
{
printf("%10d%10s%10d%10s%10s\n",p->num,p->name,p->age,p->sex,p->E_mail);
p=p->next;
} while(p!=NULL);
}
printf("\n");
}
//查詢學生信息
void query()
{
int choice;
choice = -1;
do
{
printf("\n");
printf("+--------------------+\n");
printf("| 按學號查詢 請按 1 |\n");
printf("| 取消 請按 0 |\n");
printf("+--------------------+\n");
printf("請輸入您的選擇:");
scanf("%d", &choice);
switch(choice)
{
case 0:
return;
case 1:
query_by_num();
break;
default:
printf("\n無效選項!");
break;
}
}
while(choice != 0);
}
//按學號查詢學生信息
void query_by_num()
{
int num;
struct student *p1;
printf("請輸入學生的學號:");
scanf("%ld", &num);
if(head==NULL)
{
printf("無學生記錄!\n");
return;
}
p1=head;
while(num!=p1->num && p1->next!=NULL)
p1=p1->next;
if(num == p1->num)
{
printf("\n學號\t姓名\t年齡\t性別\t出生年月\t地址\t電話\tE-mail\n");
printf("-------------------------------------------------------------------------------");
display(p1);
}
else
printf("沒有該學生記錄,請核對!");
}
void freeAll()
{
struct student *p1, *p2;
p1=p2=head;
while(p1)
{
p2=p1->next;
free(p1);
p1=p2;
}
}
上面是我的代碼和運行是的截圖,我明明已經讀入學生信息了,為什麼在顯示信息的時候只有學生數沒有學生信息啊??還有為什麼查詢也不行啊???誰能幫我改改??我改好長時間了都不行!!!!順便幫我把流程圖畫了!謝謝!!!
補充:抓緊啊
我趕時間用!!!!!
樓上的還是有點問題,在錄入函數裡面。
這是我修改之後的
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <io.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long int num;
char name[20];
int age;
char sex[4];
char address[30];
long int tele_num;
char E_mail[20];
struct student *next;
};
int TOTAL_NUM = 0;
struct student *head = NULL;
void record();
void insert(struct student *stu);
void display(struct student *stu);
void displayAll();
void query();
void query_by_num();
void freeAll();
void devise(struct student *p);
//系統主菜單
void main()
{
int choice;
choice = -1;
do
{
printf("\n\n\n");
printf("\t\t\t--------------------------------\n");
printf("\t\t\t+ 學生信息管理系統 |\n");
printf("\t\t\t--------------------------------\n");
printf("\t\t\t+ [1]----錄入學生信息 |\n");
printf("\t\t\t+ [2]----浏覽學生信息 |\n");
printf("\t\t\t+ [3]----查詢學生信息 |\n");
printf("\t\t\t+ [0]----退出系統 |\n");
printf("\t\t\t+*·*·*·*·*·*·*·*·*·*·|\n");
printf("\t\t\t--------------------------------\n");
printf("請輸入您的選擇:");
scanf("%d", &choice);
switch(choice)
{
case 0:
freeAll();
break;
case 1:
record();
break;
case 2:
displayAll();
break;
case 3:
query();
break;
default:
printf("\n無效選項!");
break;
}
}
while(choice != 0);
}
//錄入學生信息
void record()
{
int i=0;
struct student *p0,*p1;
char a;
do
{
p0 = (struct student *)malloc(LEN);
printf("請輸入學生的學號:");
scanf("%ld",&p0->num);
fflush(stdin);
printf("請輸入學生的姓名:");
scanf("%s",p0->name);
fflush(stdin);
printf("請輸入學生的年齡:");
scanf("%d",&p0->age);
fflush(stdin);
printf("請輸入學生的性別:");
scanf("%s",p0->sex);
fflush(stdin);
printf("請輸入學生的籍貫:");
scanf("%s",p0->address);
fflush(stdin);
printf("請輸入學生的電話:");
scanf("%ld",&p0->tele_num);
fflush(stdin);
printf("請輸入學生的E-mail:");
scanf("%s",p0->E_mail);
fflush(stdin);
if(i==0) head=p0;
else p1->next=p0;
p1=p0;
printf("\n是否繼續輸入另外一個學生信息?(y/n)");
a=getchar();
i++;
}while(a=='y' && i<=50);
TOTAL_NUM=i;
p1->next=NULL;
}
void insert(struct student *stu)
{
struct student *p0, *p1, *p2;
p1 = head;
p0 = stu;
if(head == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while((p0->num > p1->num)&&(p1->next != NULL))
{
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;
}
}
TOTAL_NUM++;
}
void display(struct student *p)
{
printf("\n%ld\t%s\t%d\t%s\t%s\t%ld\t%s\n", p->num, p->name, p->age, p->sex,p->address, p->tele_num, p->E_mail);
}
//浏覽學生信息
void displayAll()
{
struct student *p;
printf("學生總數:%d\n", TOTAL_NUM);
if((p = head) != NULL) //細心點撒
{
do
{
printf("%10d%10s%10d%10s%10s\n",p->num,p->name,p->age,p->sex,p->E_mail);
p=p->next;
} while(p!=NULL);
}
printf("\n");
}
//查詢學生信息
void query()
{
int choice;
choice = -1;
do
{
printf("\n");
printf("+--------------------+\n");
printf("| 按學號查詢 請按 1 |\n");
printf("| 取消 請按 0 |\n");
printf("+--------------------+\n");
printf("請輸入您的選擇:");
scanf("%d", &choice);
switch(choice)
{
case 0:
return;
case 1:
query_by_num();
break;
default:
printf("\n無效選項!");
break;
}
}
while(choice != 0);
}
//按學號查詢學生信息
void query_by_num()
{
int num;
struct student *p1;
printf("請輸入學生的學號:");
scanf("%ld", &num);
if(head==NULL)
{
printf("無學生記錄!\n");
return;
}
p1=head;
while(num!=p1->num && p1->next!=NULL)
p1=p1->next;
if(num == p1->num)
{
printf("\n學號\t姓名\t年齡\t性別\t出生年月\t地址\t電話\tE-mail\n");
printf("-------------------------------------------------------------------------------");
display(p1);
}
else
printf("沒有該學生記錄,請核對!");
}
void freeAll()
{
struct student *p1, *p2;
p1=p2=head;
while(p1)
{
p2=p1->next;
free(p1);
p1=p2;
}
}