C语言课程设计,删除模块程序求解
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 10 struct student { char name[20]; /*姓名*/ char num[20]; /*学号*/ double dchinese; /*语文*/ double dmath; /*数学*/ double denglish; /*英语*/ double daverage; /*平均成绩*/ }; void Del(); int i,s,j,n=0,x,y; int main () { FILE *fp; char name[20],num[10]; int dchinese,dmath,denglish; int choice1=1,choice2; do { //主界面 printf("\t===============欢迎进入学生管理系统=================\n"); printf("\t| |\n"); printf("\t| *输入序号选择功能 |\n"); printf("\t| |\n"); printf("\t| 1、信息编辑 |\n"); printf("\t| |\n"); printf("\t| 2、信息查询 |\n"); printf("\t| |\n"); printf("\t| 3、显示全部信息 |\n"); printf("\t| |\n"); printf("\t| 4、成绩排序 |\n"); printf("\t| |\n"); printf("\t| 5、程序结束 |\n"); printf("\t| |\n"); printf("\t====================================================\n"); scanf("%d", &choice1); system("cls"); switch (choice1) { case 1: printf("\t====================================================\n"); printf("\t| 欢迎进入信息维护系统 |\n"); printf("\t| |\n"); printf("\t| 1、增加学生信息 |\n"); printf("\t| |\n"); printf("\t| 2、删除学生信息 |\n"); printf("\t| |\n"); printf("\t| 3、插入学生信息 |\n"); printf("\t| |\n"); printf("\t| 请输入序号选择功能 |\n"); printf("\t====================================================\n"); scanf("%d", &choice2); system("cls"); switch (choice2) { case 1: //增加学生信息; printf("\t================================================\n"); printf("\t| 欢迎进入增加学生信息界面 |\n"); printf("\t| |\n"); printf("\t| 姓名 学号 语文 数学 英语 |\n"); printf("\t| |\n"); printf("\t| 如:xxx 01 90 96 98 |\n"); printf("\t================================================\n"); scanf ("%d",&choice1); scanf ("%s %s %d %d %d",name,num,&dchinese,&dmath,&denglish); system("cls"); fp = fopen("student_1.txt","a"); fprintf(fp,"%s\t%s\t%d\t%d\t%d\n",name,num,dchinese,dmath,denglish); fclose (fp); fp = fopen ("student_2.txt","w"); fscanf(fp,"%d",&n); fclose(fp); fp=fopen ("student_2.txt","w"); fprintf(fp,"%d",n+1); fclose(fp); printf("\t*****************************************\n"); printf("\t************* 信息增加成功 **************\n"); break; case 2: //删除学生信息 system("cls"); printf("\t====================================================\n"); printf("\t| 欢迎进入删除学生信息界面 |\n"); printf("\t| |\n"); printf("\t| 输入您要删除学生的姓名 |\n"); printf("\t====================================================\n"); Del(); break; case 3: //插入学生信息 printf("\t================================================\n"); printf("\t*** 欢迎进入插入学生信息界面 ****\n"); printf("\t* *\n"); printf("\t* 1、按学号高低插入信息 *\n"); printf("\t* *\n"); printf("\t* 2、按平均成绩插入高低信息 *\n"); printf("\t* *\n"); printf("\t* 请输入序号选择功能 *\n"); printf("\t================================================\n"); scanf ("%d",&choice1); system("cls"); break; } printf("\t================================================\n"); printf("\t* 返回上级请按 0 *\n"); printf("\t* *\n"); printf("\t* 退出程序请按 5 *\n"); printf("\t================================================\n"); scanf("%d", &choice1); break; case 2: printf("\t================================================\n"); printf("\t* 欢迎进入成绩查询系统 *\n"); printf("\t| |\n"); printf("\t*** 1、按学号查询信息 ***\n"); printf("\t| |\n"); printf("\t***** 2、按姓名查询信息 ******\n"); printf("\t| |\n"); printf("\t******** 输入序号 **********\n"); printf("\t================================================\n"); scanf("%d", &choice2); system("cls"); switch (choice2) { case 1: //按学号查询 printf("\t================================================\n"); printf("\t** **\n"); printf("\t** 欢迎进入成绩查询系统 **\n"); printf("\t** **\n"); printf("\t**vv**** 按学号查询信息 ****vv****\n"); printf("\t** **\n"); printf("\t** 请输入学生学号 **\n"); printf("\t** **\n"); printf("\t================================================\n"); system("cls"); break; case 2: //按照姓名查询 printf("\t================================================\n"); printf("\t* 欢迎进入成绩查询系统 *\n"); printf("\t* *\n"); printf("\t**vv**** 按姓名查询信息 *vv*****\n"); printf("\t* *\n"); printf("\t* 请输入学生姓名 *\n"); printf("\t================================================\n"); system("cls"); break; } printf("\t*****************************************\n"); printf("\t* 返回上级菜单请按 0 *\n"); printf("\t* *\n"); printf("\t* 退出程序请按 5 *\n"); printf("\t*****************************************\n"); scanf("%d", &choice1); break; case 3: /*编写输出显示所有同学信息程序*/ break; case 4://成绩排序 printf("\t================================================\n"); printf("\t *** 欢迎进入学生成绩排序系统 *** \n"); printf("\t *** *** \n"); printf("\t *** * 1、按学生学号排序 * *** \n"); printf("\t \n"); printf("\t *** * 2、按平均成绩排序 * *** \n"); printf("\t \n"); printf("\t *** * 输入序号选择功能 * *** \n"); printf("\t================================================\n"); scanf("%d", &choice2); system("cls"); printf("\tXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf("\t** 返回上级菜单请按 0 **\n"); printf("\t*****************************************\n"); printf("\t*** 退出程序请按 5 ****\n"); printf("\tXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); scanf ("%d",&choice1); break; } system("cls"); }while (choice1 != 5); printf("\t \n"); printf("\t********************** 程序结束 **********************\n"); printf("\t \n"); return 0; } void Del() //删除函数 { struct student *p; char name[20]; FILE *fp; fp = fopen ("student_2.txt","r+"); fscanf(fp,"%d",&n); fclose (fp); fp = fopen ("student_1.txt","r"); p = (struct student *)malloc (n * sizeof(struct student)); for (i=0;i<n;i++) { fscanf (fp,"%s",p[i].name); fscanf (fp,"%s",p[i].num); fscanf (fp,"%d",&p[i].dchinese); fscanf (fp,"%d",&p[i].dmath); fscanf (fp,"%d",&p[i].denglish); } fclose (fp); scanf("%s",name); fp = fopen ("student_1.txt","w"); for (i=0;i<n;i++) { if(strcmp(name,p[i].name) == 0 ) { s=i; break; for (i=i;i<n;i++) { strcpy(p[i].name,p[i+1].name); strcpy(p[i].num,p[i+1].num); p[i].dchinese = p[i+1].dchinese; p[i].dmath = p[i+1].dmath; p[i].denglish = p[i+1].denglish; } } } for(i=0;i<n-1;i++) { if(i==s) { continue; } fprintf (fp,"%s\t%s\t%d\t%d\t%d\n",p[i].name,p[i].num,p[i].dchinese,p[i].dmath,p[i].denglish); } fclose(fp); fp=fopen ("student_2.txt","w"); fprintf(fp,"%d",n-1); free(p); fclose(fp); printf("\t********* 信息删除成功 ************\n"); } 后面的删除程序可以运行,但是我的本意是删除我输入的相关内容,而程序则是删除了所有数据, 各位大神能告诉我错在哪儿吗?