skymog
2021-06-01 22:43
采纳率: 50%
浏览 45

自己写的一个学生信息管理程序,为什么排序功能用不了?

排序函数:

void sort_message_Desc()                       //降序排名
{
    struct Student *Back,*pointer,*head;
    struct Student  temp;
    head=Back=pointer=ReadData();
    while(Back!=NULL)
    {
        while(pointer->next!=NULL)
        {
            pointer=pointer->next;
            if ( strcmp( Back->grade,pointer->grade)>0  )
            {
                strcpy(temp.grade,Back->grade);
                strcpy(temp.name,Back->name);
 
                strcpy( Back->grade,pointer->grade);
                strcpy(Back->name,pointer->name);
 
                strcpy(pointer->grade,temp.grade);
                strcpy(pointer->name,temp.name);
            }
        }
        Back=Back->next;
        pointer=Back;
 
    }
    while(head!=NULL)
    {
        printf("%5s%4d",head->name,head->grade);
        head=head->next;
        printf("\n\n");
    }
    printf("\n\n");
}

全部代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<time.h>
# define LEN sizeof(struct Student)
struct Student
{
    char num[100];
    char name[200];
    int grade;
    char sex[100];
    int age;
    char phone[200];
    char address[200];
    char birthday[200];
    char mail[200];
    struct Student *next;
};
void menu();
void staet();
void welcome();
char filename[30];
struct Student *Creat(int n)
{
 void menu_print_in(void);
 struct Student *head;
 struct Student *p1, *p2;
 system("cls");
 for(int i=1;i<n+1;i++) {
 p1 = (struct Student*)malloc(LEN);
 menu_print_in();
 scanf("%s%s%d%s%d%s%s%s%s",p1->num,p1->name,&p1->grade,p1->sex,
 &p1->age,p1->phone,p1->address,p1->birthday,p1->mail);
 p1->next = NULL;
 if(i==1)
 {
  head = p2 = p1;
 }
 else {
  p2->next = p1;
  p2 = p1;
 }
 }
 return(head);
}
 
void WriteData_wb(struct Student *head) {
 FILE *fp;
 struct Student *p;
 if((fp = fopen(filename, "wb"))==NULL)
 printf("\a 文件打开错误");
 p = head;
 while(p!=NULL) {
 if(fwrite(p,LEN,1,fp)!=1) {
  printf("写入数据出错\n");
  fclose(fp);
  return;
 }
 p=p->next;
 }
 fclose(fp);
}
//数据存盘(ab追加)
void WriteData_ab(struct Student *head)
{
 FILE *fp;
 struct Student *p;
 if((fp = fopen(filename, "ab"))==NULL)
 printf("\a 文件打开错误!");
 p = head;
 while(p!=NULL) {
 if(fwrite(p,LEN,1,fp)!=1) {
  printf("写入数据出错\n");
  fclose(fp);
  return;
 }
 p=p->next;
 }
 fclose(fp);
}
struct Student *ReadData(void)
{
 struct Student *head = NULL;
 struct Student *p1, *p2;//s = p1;p = p2;
 FILE *fp;
 if((fp=fopen(filename,"rb+"))==NULL)
 {
 printf("打开文件出错\n");
 exit(0);
 }
 while(!feof(fp)) {
 if((p1=(struct Student*)malloc(LEN))==NULL){
  printf("内存申请出错\n");
  fclose(fp);
  exit(0);
 }
 if(fread(p1,LEN,1,fp)!=1){
  free(p1);
  break;
 }
 if(head==NULL)
 head=p2=p1;
 else{
  p2->next=p1;
  p2=p1;
 }
 }
 fclose(fp);
 return (head);
}
 
 void display()
{
 system("cls");
 void menu_print_out(void);
 struct Student *p;
 p = ReadData();
 menu_print_out();
 do
 {
 printf("%-10s%6s%4d%8s%4d%13s%11s %4s %4s",p->num,p->name,p->grade,p->sex,
 p->age,p->phone,p->address,p->birthday,p->mail);
 p = p->next;
 printf("\n\n");
 }while(p!=NULL);
 printf("\n\n");
}
     int query_num()             //学号查询
     {
        system("cls");
        void menu_print_out();
        struct Student *p;
        char str_num[10];
        printf("请输入你要查询的学号\n");
        scanf("%s",str_num);
        p=ReadData();
        menu_print_out();
        do
        {   if(strcmp(p->num,str_num)==0)
           {
            printf("%-10s%6s%4d%8s%4d%13s%11s%4s%4s",p->num,p->name,p->grade,p->sex,p->phone,p->address,p->birthday,p->mail);
            printf("\n\n");
            return 0;
           }
            p=p->next;
        }while(p!=NULL);
        printf("数据库中没有你要找的数据\n");
        printf("\n\n");
        return 0;
     }
     int query_name()             //姓名查询
     {
        system("cls");
        void menu_print_out(void);
        struct Student *p;
        char str_name[30];
        printf("请输入你要查询的姓名\n");
        scanf("%s",str_name);
        p=ReadData();
        menu_print_out();
        do
        {   if(strcmp(p->name,str_name)==0)
           {
            printf("%-10s%6s%4d%8s%4d%13s%11s%4s%4s",p->num,p->name,p->grade,p->sex,p->phone,p->address,p->birthday,p->mail);
            printf("\n\n");
            return 0;
           }
            p=p->next;
        }while(p!=NULL);
        printf("数据库中没有你要找的数据\n");
        printf("\n\n");
        return 0;
     }
 
    int delStudent()                             //修改数据之删除记录
    {
        struct Student *p1,*p2,*head;
        char str_num[20];
        printf("\n请输入你要删除的学号");
        scanf("%s",str_num);
        p1=ReadData();
        p2=p1->next;
        head=p1;
        while(p2!=NULL)
        {
            if(strcmp(p1->num,str_num)==0)
            {
                WriteData_wb(p2);
                return 0;
            }
            else if(strcmp(p2->num,str_num)==0)
            {
                p1->next=p2->next;
                WriteData_wb(head);
                return 0;
            }
            p2=p2->next;
            p1=p1->next;
 
        }
        if(p2==NULL)
            printf("数据库里没有储存你要删除的数据\n");
            printf("\n\n");
            return 0;
 
    }
    int change1()           //修改数据之修改记录
    {
        void menu_print_in(void);
        struct Student *p1,*p2,*head;
        char str_num[20];
        printf("请输入你要修改的学号信息\n");
        scanf("%s",str_num);
        p1=ReadData();
        p2=p1->next;
        head=p1;
        while(p2!=NULL)
        {
            if(strcmp(p1->name,str_num)==0){
            menu_print_in();
            scanf("%s%s%d%s%d%s%s%s%s",p1->num,p1->name,p1->grade,p1->sex,&p1->age,p1->phone,p1->address,p1->birthday,p1->mail);
            WriteData_wb(head);
            return 0;
 
            }
            else if(strcmp(p2->name,str_num)==0)
            {
                menu_print_in();
                scanf("%s%s%d%s%d%s%s%s%s",p1->num,p1->name,p1->grade,p1->sex,&p1->age,p1->phone,p1->address,p1->birthday,p1->mail);
                WriteData_wb(head);
                return 0;
            }
            p2=p2->next;
            p1=p1->next;
 
        }
        if(p2==NULL)
        printf("数据库中没有你要修改的数据\n");
        return 0;
 
    }
 
void sort_message_order()                       //升序排名
{
    struct Student *Back,*pointer,*head;
    struct Student  temp;
    head=Back=pointer=ReadData();
    while(Back!=NULL)
    {
        while(pointer->next!=NULL)
        {
            pointer=pointer->next;
            if ( strcmp( Back->grade,pointer->grade)<0  )
            {
                strcpy(temp.grade,Back->grade);
                strcpy(temp.name,Back->name);
 
                strcpy( Back->grade,pointer->grade);
                strcpy(Back->name,pointer->name);
 
                strcpy(pointer->grade,temp.grade);
                strcpy(pointer->name,temp.name);
            }
        }
        Back=Back->next;
        pointer=Back;
 
    }
    while(head!=NULL)
    {
        printf("%5s%4d",head->name,head->grade);
        head=head->next;
        printf("\n\n");
    }
    printf("\n\n");
}
 
void sort_message_Desc()                       //降序排名
{
    struct Student *Back,*pointer,*head;
    struct Student  temp;
    head=Back=pointer=ReadData();
    while(Back!=NULL)
    {
        while(pointer->next!=NULL)
        {
            pointer=pointer->next;
            if ( strcmp( Back->grade,pointer->grade)>0  )
            {
                strcpy(temp.grade,Back->grade);
                strcpy(temp.name,Back->name);
 
                strcpy( Back->grade,pointer->grade);
                strcpy(Back->name,pointer->name);
 
                strcpy(pointer->grade,temp.grade);
                strcpy(pointer->name,temp.name);
            }
        }
        Back=Back->next;
        pointer=Back;
 
    }
    while(head!=NULL)
    {
        printf("%5s%4d",head->name,head->grade);
        head=head->next;
        printf("\n\n");
    }
    printf("\n\n");
}
 
void start() //欢迎界面
{
 system("color 1E");
 printf("\n\n\n\n\n\n\n\n\n\t\t\t⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙\n");
 printf("\t\t\t⊙    ⊙\n");
 printf("\t\t\t⊙ 欢迎使用学生信息管理系统 ⊙\n");
 printf("\t\t\t⊙    ⊙\n");
 printf("\t\t\t⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙\n");
 printf("\n\t\t\t\t系统开始启动.........\n");
 printf("===============================================================================\r");
 for(int i=1;i<80;i++)
 {
  Sleep(40);
  printf(">");
 }
 system("CLS");
 welcome();
}
void welcome()//登录界面
{
 system("CLS");
 system("color 2");
 printf("\n\n\n\n\n\n\t\t\t★★★★★★★★★★★★★★★★\n");
 printf("\t\t\t★    ★\n");
 printf("\t\t\t★ 欢迎使用学生信息管理系统 ★\n");
 printf("\t\t\t★    ★\n");
 printf("\t\t\t★ 设计者:zed ★\n");
 printf("\t\t\t★    ★\n");
 printf("\t\t\t★ 日期:2021.5.31 ★\n");
 printf("\t\t\t★    ★\n");
 printf("\t\t\t★★★★★★★★★★★★★★★★\n");
 printf("\n\t\t\t按任意键继续...");
 getchar();
 system("CLS");
 menu();
}
//主菜单
void menu()
{
 void addStudent();
 void query();
 int File_name();
 void sort();
 void change();
 system("CLS");
 system("color D");
 printf("\t\t\t★★★★★★主菜单※※※※※\n");
 printf("\n\n\n\n\n\t\t\t※※※※※※※※※※※※※※\n");
 printf("\t\t\t※   ※\n");
 printf("\t\t\t※ 1.录入学生信息 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 2.查询学生信息 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 3.修改学生信息 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 4.排序学生成绩 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 5. 退出系统 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※※※※※※※※※※※※※※\n");
 printf("\n\t\t请输入功能前的序号选择服务项目:");
 int a = 0;
 a = getchar();
 while(a!='1'&&a!='2'&&a!='3'&&a!='4'&&a!='5') {
 printf("错误,请输入正确的号码!\n");
 putchar('\a');
 getchar();
 printf("\n\t\t请输入功能前的序号选择服务项目:");
 a = getchar();
 }
 switch(a) {
 case '1': File_name();addStudent();
 break;
 case '2': File_name();query();
 break;
 case '3': File_name();change();
 break;
 case '4': File_name();sort();
 break;
 case '5': exit(0);
 break;
 }
 getchar();
}
int File_name()
{
 printf("\n\t\t请输入您要打开的文件:");
 if(scanf("%s", filename)!=1)
 printf("\a error!");
 return 0;
}
//二级菜单之添加数据
void addStudent()
{
 int Creat_num();
 system("cls");
 getchar();
 system("CLS");
 system("color B");
 printf("\t\t\t★★★★★★添加数据※※※※※\n");
 printf("\n\n\n\n\n\t\t\t※※※※※※※※※※※※※\n");
 printf("\t\t\t※   ※\n");
 printf("\t\t\t※ 1. 新建文件 ※\n");
 printf("\t\t\t※----------------------※\n");
 printf("\t\t\t※ 2. 增添数据 ※\n");
 printf("\t\t\t※----------------------※\n");
 printf("\t\t\t※ 3. 返回菜单 ※\n");
 printf("\t\t\t※----------------------※\n");
 printf("\t\t\t※※※※※※※※※※※※※\n");
 printf("\n\t\t请输入功能前的序号选择服务项目:");
 int a = 0;
 a = getchar();
 while(a!='1'&&a!='2'&&a!='3')
 {
 printf("错误,请输入正确的号码!\n");
 putchar('\a');
 getchar();
 printf("\n\t\t请输入功能前的序号选择服务项目:");
 a = getchar();
 }
 switch(a) {
 case '1': WriteData_wb(Creat(Creat_num()));
  printf("\n新建文件成功且数据已成功保存\n");
  system("pause");
  system("cls");
  addStudent();
 break;
 case '2': WriteData_ab(Creat(Creat_num()));
  printf("\n数据已成功添加\n");
  system("pause");
  system("cls");
  addStudent();
 break;
 case '3': system("cls");
  getchar();
  menu();
 break;
 }
}
//二级菜单之查询数据
void query()
{
 system("cls");
 getchar();
 while(1)
 {
 system("CLS");
 system("color C");
 printf("\t\t\t★★★★★查询数据※※※※※\n");
 printf("\n\n\n\n\n\t\t\t※※※※※※※※※※※※※※\n");
 printf("\t\t\t※   ※\n");
 printf("\t\t\t※ 1.全部查询 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 2.学号查询 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 3.姓名查询 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 4.返回菜单 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※※※※※※※※※※※※※※\n");
 printf("\n\t\t请输入功能前的序号选择服务项目:");
 int a = 0;
 a = getchar();
 while(a!='1'&&a!='2'&&a!='3'&&a!='4')
 {
  printf("错误,请输入正确的号码!\n");
  putchar('\a');
  getchar();
  printf("\n\t\t请输入功能前的序号选择服务项目:");
  a = getchar();
 }
 switch(a) {
  case '1': display();system("pause");getchar();
  break;
  case '2': query_num();system("pause");getchar();
  break;
  case '3': query_name();system("pause");getchar();
  break;
  case '4': system("cls");getchar();menu();
  break;
 }
 }
}
int Creat_num() {
 printf("\n\t\t请输入您要添加的数据个数:");
 int n;
 if(scanf("%d", &n)!=1) {
 printf("\a error!");
 }
 return n;
}
//二级菜单之修改数据/
void change()
{
 system("cls");
 getchar();
 while(1)
 {
 system("CLS");
 system("color A");
 printf("\t\t\t★★★★★★修改数据※※※※※\n");
 printf("\n\n\n\n\n\t\t\t※※※※※※※※※※※※※※\n");
 printf("\t\t\t※   ※\n");
 printf("\t\t\t※ 1.删除记录 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 2.修改记录 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 3.返回菜单 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※※※※※※※※※※※※※※\n");
 printf("\n\t\t请输入功能前的序号选择服务项目:");
 int a = 0;
 a = getchar();
 while(a!='1'&&a!='2'&&a!='3')
 {
 printf("错误,请输入正确的号码!\n");
 putchar('\a');
 getchar();
 printf("\n\t\t请输入功能前的序号选择服务项目:");
 a = getchar();
 }
 switch(a) {
  case '1': delStudent();
  printf("\n\n已成功删除指定数据\n");
  system("pause");
  getchar();
  break;
  case '2': change1();
   printf("\n\n已成功修改指定数据\n");
  system("pause");
   getchar();
  break;
  case '3': system("cls");
  getchar();
  menu();
  break;
 }
 }
}
void sort()
{
 system("cls");
 getchar();
 while(1)
 {
 system("CLS");
 system("color D");
 printf("\t\t\t★★★★★排序成绩※※※※※\n");
 printf("\n\n\n\n\n\t\t\t※※※※※※※※※※※※※※\n");
 printf("\t\t\t※   ※\n");
 printf("\t\t\t※ 1.升序排名 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 2.降序排名 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※ 3.返回菜单 ※\n");
 printf("\t\t\t※------------------------※\n");
 printf("\t\t\t※※※※※※※※※※※※※※\n");
 printf("\n\t\t请输入功能前的序号选择服务项目:");
 int a = 0;
 a = getchar();
 while(a!='1'&&a!='2'&&a!='3')
 {
  printf("错误,请输入正确的号码!\n");
  putchar('\a');
  getchar();
  printf("\n\t\t请输入功能前的序号选择服务项目:");
  a = getchar();
 }
 switch(a) {
  case '1': sort_message_order();system("pause");getchar();
  break;
  case '2': sort_message_Desc();system("pause");getchar();
  break;
  case '3': system("cls");getchar();menu();
  break;
 }
 }
}
//输入输出提示栏
void menu_print_in() {
 printf("------------------------------------------------------------------------\n");
 printf("学号 姓名 成绩 性别 年龄 电话  地址 生日 邮件 \n");
 printf("------------------------------------------------------------------------\n");
}
void menu_print_out(void) {
 printf("--------------------------------------------------------------------------\n");
 printf("学号 姓名 成绩 性别 年龄 电话  地址 生日 邮件 \n");
 printf("--------------------------------------------------------------------------\n");
}
//主函数
int main()
{
 start();
 return 0;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • qzjhjxj 2021-06-01 23:51
    已采纳

    楼主的问题在结构体里定义 int  grade; ,两个排序函数里用strcmp() 和strcpy()  对int型操作,修改如下,供参考:

    void sort_message_order()                       //升序排名
    {
        struct Student *Back,*pointer,*head;
        struct Student  temp;
        head=Back=pointer=ReadData();
        while(Back!=NULL)
        {
            while(pointer->next!=NULL)
            {
                pointer=pointer->next;
                if(Back->grade < pointer->grade ) //if ( strcmp( Back->grade,pointer->grade)<0  )
                {
                    temp.grade = Back->grade;    //strcpy(temp.grade,Back->grade);
                    strcpy(temp.name,Back->name);
    
                    Back->grade = pointer->grade; //strcpy( Back->grade,pointer->grade);
                    strcpy(Back->name,pointer->name);
    
                    pointer->grade = temp.grade;  //strcpy(pointer->grade,temp.grade);
                    strcpy(pointer->name,temp.name);
                }
            }
            Back=Back->next;
            pointer=Back;
    
        }
        while(head!=NULL)
        {
            printf("%5s%4d",head->name,head->grade);
            head=head->next;
            printf("\n\n");
        }
        printf("\n\n");
    }
    
    
    
    
    void sort_message_Desc()                       //降序排名
    {
        struct Student *Back,*pointer,*head;
        struct Student  temp;
        head=Back=pointer=ReadData();
        while(Back!=NULL)
        {
            while(pointer->next!=NULL)
            {
                pointer=pointer->next;
                if(Back->grade>pointer->grade )//if ( strcmp( Back->grade,pointer->grade)>0  )
                {
    
                    temp.grade = Back->grade;  //strcpy(temp.grade,Back->grade);
                    strcpy(temp.name,Back->name);
    
                    Back->grade = pointer->grade;//strcpy( Back->grade,pointer->grade);
                    strcpy(Back->name,pointer->name);
    
                    pointer->grade = temp.grade;//strcpy(pointer->grade,temp.grade);
                    strcpy(pointer->name,temp.name);
                }
            }
            Back=Back->next;
            pointer=Back;
        }
        while(head!=NULL)
        {
            printf("%5s%4d",head->name,head->grade);
            head=head->next;
            printf("\n\n");
        }
        printf("\n\n");
    }
    点赞 评论
  • CSDN专家-文盲老顾 2021-06-01 23:19

    https://blog.csdn.net/yangping_zheng/article/details/20781071

    在你的程序里加上 try catch,抛出异常看看出错的原因。

    点赞 评论
  • 有问必答小助手 2021-06-02 10:12

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论

相关推荐 更多相似问题