| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1319 人关注过本帖
标题:【求助大佬】这段程序编译没有错误,测试时功能1添加完后输出答案错误,不理 ...
取消只看楼主 加入收藏
zj121
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2020-4-15
结帖率:0
收藏
 问题点数:0 回复次数:1 
【求助大佬】这段程序编译没有错误,测试时功能1添加完后输出答案错误,不理解
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define N 100

char filename[20];
struct student
{
char id;
char name;
int chinese;
int math;
int english;
}stu[N];  
int menu_filename()
   {
      system("cls");
      printf("请输入你要操作的文件名:");
      scanf("\t%s",filename);
      return 1;
    }
int cd()                              //菜单选择系统函数
{
     char c;
      do{
        system("cls");
        printf("\t\t ╭═════════■□■□═══╮\n");
        printf("\t\t│         学生信息管理系统         │\n");
        printf("\t\t╰═══■□■□══════════╯\n");
        printf("\t\t   ┌─────────────—┐\n");
        printf("\t\t   │ 1. 添加记录    2. 显示记录 │\n");
        printf("\t\t                               │\n");
        printf("\t\t   │ 3. 信息查询    4. 修改记录 │\n");
        printf("\t\t                               │\n");
        printf("\t\t   │ 5. 删除记录    0. 退出程序 │\n");
        printf("\t\t                               │\n");
        printf("\t\t   └─────────────—┘\n");
        printf("\t\t请您选择(0-5):");
        c=getchar();
     }while(c<'0'||c>'5');
        return (c-'0');                                      //字符减去’0‘得到的是数字整型//
}


void lr()
{
    FILE *fp;
    int i=0,n=0;
    char sign='y';                              //首先定义sing='y'
    system("cls");                                //清屏函数  系统中调用
    while(sign!='n'&&sign!='N')                    //其实只有当输入N或n时才跳出循环
      {
        printf("             ----学生纪录添加----\n");
        printf("学号:\n");
        scanf("\t%s",&stu[i].id);
        printf("姓名:\n");
        scanf("\t%s",&stu[i].name);
        printf("语文:\n");
        scanf("\t%d",&stu[i].chinese);
        printf("数学:\n");
        scanf("\t%d",&stu[i].math);
        printf("英语:\n");
        scanf("\t%d",&stu[i].english);
        printf("是否继续添加?(y/n)");
        scanf("\t%c",&sign);
        i++;
        n=n+1;
        system( "cls ");
   }system("pause");                                     //等待命令
  if((fp=fopen(filename,"a"))==NULL)                    //以2进制形式进行文件尾追加//
      {
         printf("cannot open the files\n");
         system("pause");
         return;                                         //如果文件出现错误 就返回
       }
        for(i=0;i<n;i++)
        if(fwrite(&stu[i],sizeof(student),1,fp)!=1)      //以2进制形式写入文件中//一次写入一个数组//
        printf("file write error\n");
        fclose(fp);                                       //关闭文件
        system("pasue");
  }
   


void xs()
 
{
    char sign='y';
    FILE *fp;
    int i=0,n;
    system("cls");
      if((fp=fopen(filename,"r"))==NULL)             //以2进制方式读取文件信息//
      {
         printf("cannot open the files\n");
         system("pause");
         return;
       }
    while(!feof(fp))                              //一直读取知道文件尾结束
    {
        fread(&stu[i],sizeof(student),1,fp);  //以2进制方式将文件信息读到数组内存中//一次读取一个//
         i++;
     }
    fclose(fp);
    for(n=0;n<i-1;n++)                             //循环全部输出
     printf("学号:%s \n姓名:%s \n语文成绩:%d \n数学成绩:%d \n英语成绩:%d\n",stu[i].id,stu[i].name,stu[i].chinese,stu[i].math,stu[i].english);
     system("pause");
     system("cls");
  }


void cx()
{
FILE *fp;
    int i=0,n=0;
    int m;
    system("cls");
    if((fp=fopen(filename,"r"))==NULL)                 //以2进制方式读取文件信息//
      {
         printf("cannot open the files\n");
         system("pause");
         return;
       }
 
    while(!feof(fp))
    {
        fread(&stu[i],sizeof(student),1,fp);                      //以2进制方式将文件信息读到数组内存中//一次读取一个//
        i++;n=n+1;
     }
    {
          char s[15];
          int i=0;
          printf("请输入想查询的学号:");
          scanf("\t%s",s);
         while(strcmp(&stu[i].id,s)!=0&&i<n)i++;                       //比较per[i].name与所输入学号是否相同//
            if(i==n)
            {
                printf("\t查无此人\n");
                system("pause");
                return;
            }
            printf("\t学号: %s\n",stu[i].id);
            printf("\t姓名: %s\n",stu[i].name);
            printf("\t年龄: %d\n",stu[i].chinese);
            printf("\t英语: %d\n",stu[i].math);
            printf("\t计算机: %d\n",stu[i].english);
          }
          fclose(fp);
          system("pause");
  }
   
void xg()
{

    struct student temp;

    int k=-1,x,i;

    char c1='y',c2;

    while(c1=='y')

    {

       c2='y';

        printf("\n请输入要修改的同学的学号:");

        scanf("%d",&x);

        for(i=0;i<N;i++)

           if(x==stu[i].id)

           {

               k=i;/*找到要修改的同学学号,并用k记下其下标*/

               break;

           }

        if(k<0)/*k<0表示在数组中没有找到输入学号对应的学生*/

            printf("\n输入学号有误,请重新输入!");

        else

       {

            printf("请输入要修改学生修改后的的信息\n");

            printf("学号\t姓名、t语文\t数学\t英语\n");

            scanf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t\n",&temp.id,stu[i].name,temp.chinese,&temp.math,&temp.english);      

            stu[k]=temp;/*修改该学生信息*/

            printf("\n修改成功\n");

           break;

        }

        printf("\n继续修改操作吗?");

        printf("\n继续修改请按y;不进行修改请按n\n");

       c1=getchar();

        getchar();

    }/*结束while*/

    printf("\n按任意键继续!\n");

    getchar();

}

void sc()
  {
    FILE *fp;
    char s[10];
    int i=0,n=0;
      if((fp=fopen(filename,"r"))==NULL)                  //以2进制方式读取文件信息//
      {
         printf("cannot open the files\n");
         system("pause");
         return;
       }
 
    while(!feof(fp))
    {
      fread(&stu[i],sizeof(student),1,fp);               //将文件信息读取到数组内存中//
      i++;n=n+1;
     }
    fclose(fp);
     if((fp=fopen(filename,"w"))==NULL)                 //以2进制写入方式打开文件//
      {
         printf("cannot open the files\n");
         system("pause");
         return;
       }
    printf("学生学号:");
    scanf("%s",s);
    for(i=0;i<n-1;i++)
     if(!(strcmp(&stu[i].id,s)==0))                    //找到与其匹配的信息进行操作//   如果条件符合就写入否者跳过这组数据此数据就是所需删除的
      fwrite(&stu[i],sizeof(student),1,fp)!=1;             //以2进制方式将信息从内存中写入文件磁盘//
     fclose(fp);
     printf("数据删除成功");
     system("pause");
}
void main()
  {
      int n=0;
      menu_filename();                                         //引用全局变量文件名
      for(;;)                                                 //循环使用主界面直到遇到结束命令
      {
          switch(cd())                               // 函数的调用//
          {
          case 1:
              printf("\t\t *^_^*学生记录添加 *^_^*\n");
              lr();
              break;
          case 2:
              printf("\t\t *^_^*学生信息浏览 *^_^*\n");
              xs();
              break;
          case 3:
              printf("\t\t *^_^*学生信息查询 *^_^*\n");
              cx();
              break;
          case 4:
              printf("\t\t *^_^*数据修改 *^_^*\n");
              xg();
              break;
          case 5:
               printf("\t\t *^_^*数据删除 *^_^*\n");
               sc();
               break;
          case 0:
                printf("\n\t\t谢谢使用,再见!\n");
                printf("\n\t\t");
                system("pause");
                exit(0);
          }
      }
      system("pause");
  }

搜索更多相关主题的帖子: stu int system printf 文件 
2020-06-11 16:15
zj121
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2020-4-15
收藏
得分:0 
回复 2楼 rjsp
谢谢大佬
2020-06-11 16:53
快速回复:【求助大佬】这段程序编译没有错误,测试时功能1添加完后输出答案错误 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018870 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved