新手求助,指针结构体程序中断
#include <stdio.h>#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct student{
long ID;
char name[20];
float score;
}stu[15];
struct student *p;
//函数原型声明
void get(struct student *p,int N,int i);
void print(struct student *p,int N,int i);
void search_name(int N);
void sort_ID(struct student *p,int N);
void sort_score(struct student *p,int N);
void sort_name(struct student *p,int N);
//主函数
void main(){
int N,i=0;
printf("Please enter the number of students(5~15):");
scanf("%d",&N);
printf("\n");
//子函数
get(p,N,i);//输入
p=stu;
print(p,N,i);
p=stu;
search_name(N);//按姓名查询
sort_ID(p,N);//按学号排序
print(p,N,i);
p=stu;
sort_score(p,N);//按成绩排序
print(p,N,i);
p=stu;
sort_name(p,N);//按姓名排序
print(p,N,i);
p=stu;
getch();
}
void get(struct student *p,int N,int i){
p=stu;
for(i;i<N;i++){
printf("NO.%d:Please enter the student's ID:\t",i+1);
scanf("%ld",&((p+i)->ID));
fflush(stdin);
printf(" Please enter the student's name:\t");
scanf("%s",&((p+i)->name));
fflush(stdin);
printf(" Please enter the student's score:\t");
scanf("%f",&((p+i)->score));
fflush(stdin);
}
}
void print(struct student *p,int N,int i){
printf(" The Score List\n");
printf("NO. ID Name Score\n");
for(i;i<N;i++){
printf("%-6d%-12d%-20s%-6f\n",i+1,(p+i)->ID,(p+i)->name,(p+i)->score);
}
}
void search_name(int N){
int i,count=0;
char name[20];
printf("Please enter the name you want to find:");
scanf("%s",name);
printf("\n");
for (i=0;i<N;i++){
if (strcmp(name,(p+i)->name) == 0){
printf("ID Name Score\n");
printf("%-6d%-12d%-6f\n",i+1,(p+i)->ID,(p+i)->score);
count++;
}
}
if(count==0)
printf("This guy is not exsist!\r\n");
}
void sort_ID(struct student *p,int N){
struct student *pt;
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N-i;j++){
if((p+j)->ID>(p+j+1)->ID){
*pt=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=*pt;
}
}
}
}
void sort_score(struct student *p,int N){
struct student *pt;
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N-i;j++){
if((p+j)->score>(p+j+1)->score){
*pt=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=*pt;
}
}
}
}
void sort_name(struct student *p,int N){
struct student *pt;
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N-i;j++){
if(strcmp((p+j)->name,(p+j+1)->name)>0){
*pt=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=*pt;
}
}
}
}
经过了断点测试,问题就是出在这三个函数中
void sort_ID(struct student *p,int N);
void sort_score(struct student *p,int N);
void sort_name(struct student *p,int N);
可是不会改啊……