| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1434 人关注过本帖
标题:要1.将增加成绩改成随机插入成绩(要求按原来规律),2.统计不及格的科目; ...
只看楼主 加入收藏
Batters
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-6-27
结帖率:100%
收藏
 问题点数:0 回复次数:0 
要1.将增加成绩改成随机插入成绩(要求按原来规律),2.统计不及格的科目;有人能指导一下吗
#include "stdio.h"
#define SIZE 100000
#include"string.h"
#include"stdlib.h"
#include"conio.h"
struct student
{int n;
 int num;
 char name[10];
 int C;
 int Maths;
 int En;
 int sum;
}stu[SIZE];
/*录入数据*/
void finput()
{
    FILE *fp;//指向文件的指针
    int i;  
    fp=fopen("fstu","wb"); //打开或新建一个二进制文件                     
    for(i=0;i<SIZE;i+1)
    {
      printf("i=%d 输入序号i(当输入i=-1时,返回),i=",i++);
      scanf("%d",&i);
      if(i==-1)                                 
      {
          fclose(fp);//关闭文件      
          return;//返回菜单
      }
      else                                      
      {
          printf("请输入学号,姓名,C语言,数学,英语:");     
          scanf("%d%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].C,&stu[i].Maths,&stu[i].En);
          stu[i].sum=stu[i].C+stu[i].Maths+stu[i].En;//计算总分
          stu[i].n=i+1;
          fwrite(&stu[i],sizeof(struct student),1,fp);//二进制写入fp指向的文件
      }
    }
    fclose(fp);//关闭文件
}
/*输出数据*/
void flist()
{
    FILE *fp;
    int i;
    fp=fopen("fstu","rb"); //打开文件                     
    printf("-----------------------------------------------------------\n");
    printf("                       成绩列表\n");
    printf("-----------------------------------------------------------\n");
    printf("序号   学号   姓名    C语言    数学    英语      总分\n");
    printf("-----------------------------------------------------------\n");
    for(i=0;fread(&stu[i],sizeof(struct student),1,fp)==1;i++)
    {
printf("%d%8d%8s%8d%8d%8d%11d\n",stu[i].n,stu[i].num,stu[i].name,stu[i].C,stu[i].Maths,stu[i].En,stu[i].sum);
    }
    printf("-----------------------------------------------------------\n\n");
    printf("-----------------------------------------------------------\n");
    fclose(fp);//关闭文件
}
/*查找数据*/
void fsearch()
{
    FILE *fp;
    int i,num;
    fp=fopen("fstu","rb");//打开文件   
    rewind(fp);
    printf("请输入要查找的学号:");
    scanf("%d",&num);
    printf("-----------------------------------------------------------\n");
    printf("                      查询结果\n");
    printf("-----------------------------------------------------------\n");
    printf("序号   学号   姓名    C语言    数学    英语       总分\n");
    printf("-----------------------------------------------------------\n");
    for(i=0;i<=SIZE;i++)
    if(stu[i].num==num)
    {
printf("%d%8d%8s%8d%8d%8d%11d\n",stu[i].n,stu[i].num,stu[i].name,stu[i].C,stu[i].Maths,stu[i].En,stu[i].sum);
    }
    printf("-----------------------------------------------------------\n\n");
    printf("-----------------------------------------------------------\n\n\n");
    fclose(fp);//关闭文件
}
/*增加数据*/
void fadden()
{
    int i,s;
    FILE *fp;
    fp=fopen("fstu","ab+");//打开文件,加入数据   
    rewind(fp);
    printf("-----------------------------------------------------------\n");
    printf("                       成绩列表\n");
    printf("-----------------------------------------------------------\n");
    printf("序号   学号   姓名    C语言    数学    英语       总分\n");
    printf("-----------------------------------------------------------\n");
    for(i=0;fread(&stu[i],sizeof(struct student),1,fp)==1;i++)
    {
printf("%d%8d%8s%8d%8d%8d%11d\n",stu[i].n,stu[i].num,stu[i].name,stu[i].C,stu[i].Maths,stu[i].En,stu[i].sum);
    }
    printf("-----------------------------------------------------------\n");
    s=i;
    for(i=s;i<SIZE;i++)
    {
        printf("i=%d 输入序号i(当输入i=-1时,返回),i=",i+1);
        scanf("%d",&i);
        if(i==-1)
        {
            fclose(fp);
            return;
        }
        else
        {
            printf("请输入学号,姓名,C语言,数学,英语:");
            stu[i].n=i+1;
            scanf("%d%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].C,&stu[i].Maths,&stu[i].En);
            stu[i].sum=stu[i].C+stu[i].Maths+stu[i].En;
            fwrite(&stu[i],sizeof(struct student),1,fp);
        }
    }
    fclose(fp);//关闭文件
}
/*修改数据*/
void fchange()
{
    FILE *fp;
    int i,num,n;
    int flag=0;   
    printf("请输入要修改学生的学号:");
    scanf("%d",&num);
    for(i=0;i<=SIZE;i++)
    if(stu[i].num==num)
    {
        printf("-----------------------------------------------------------\n");
        printf("                       成绩列表\n");
        printf("-----------------------------------------------------------\n");
        printf("序号   学号   姓名    C语言    数学    英语       总分\n");
        printf("-----------------------------------------------------------\n");
        printf("%d%8d%8s%8d%8d%8d%11d\n",stu[i].n,stu[i].num,stu[i].name,stu[i].C,stu[i].Maths,stu[i].En,stu[i].sum);
        printf("-----------------------------------------------------------\n\n");
        printf("-----------------------------------------------------------\n");
        n=i;
        flag=1;
        break;
    }
    if(flag==0)
    {
        printf("-----------------------------------------------------------\n");
        printf("                        输入错误!\n");
        printf("                         请返回!\n");
        printf("-----------------------------------------------------------\n");
        return;
    }
    printf("\n\n\n");  
    fp=fopen("fstu","rb+");//打开文件
    fseek(fp,n*sizeof(struct student),0);
    printf("请输入新的学号,姓名,C成绩,数学成绩,英语成绩:");
    scanf("%d%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].C,&stu[i].Maths,&stu[i].En);
    stu[i].sum=stu[i].C+stu[i].Maths+stu[i].En;
    fwrite(&stu[i],sizeof(struct student),1,fp);//写入文件
    fclose(fp);//关闭文件
    fp=fopen("fstu","rb");//打开文件
    printf("-----------------------------------------------------------\n");
    printf("                       成绩列表\n");
    printf("-----------------------------------------------------------\n");
    printf("序号   学号   姓名    C语言    数学    英语       总分\n");
    printf("-----------------------------------------------------------\n");
    for(i=0;fread(&stu[i],sizeof(struct student),1,fp)==1;i++)
    {
printf("%d%8d%8s%8d%8d%8d%11d\n",stu[i].n,stu[i].num,stu[i].name,stu[i].C,stu[i].Maths,stu[i].En,stu[i].sum);
    }
    printf("-----------------------------------------------------------\n\n");
    printf("-----------------------------------------------------------\n");
    fclose(fp);//关闭文件
}
/*成绩排序*/
void ftaxis()
{
    int i,j,n;
    FILE *fp;
    struct student t;
    fp=fopen("fstu","rb");//打开文件
    printf("-----------------------------------------------------------\n");
    printf("                       成绩列表\n");
    printf("-----------------------------------------------------------\n");
    printf("序号   学号   姓名    C语言    数学    英语       总分\n");
    printf("-----------------------------------------------------------\n");
    for(i=0;fread(&stu[i],sizeof(struct student),1,fp)==1;i++)
    {
printf("%d%8d%8s%8d%8d%8d%11d\n",stu[i].n,stu[i].num,stu[i].name,stu[i].C,stu[i].Maths,stu[i].En,stu[i].sum);
    }
    printf("-----------------------------------------------------------\n\n");
    printf("-----------------------------------------------------------\n");
    fclose(fp);//关闭文件
    n=i;
    for(i=0;i<n;i++)
       for(j=i+1;j<n;j++)
       if(stu[i].sum<stu[j].sum)
       {
           t=stu[i];
           stu[i]=stu[j];
           stu[j]=t;
       }
    fp=fopen("fstus","wb");//打开文件
    printf("\n\n\n");
    printf("-----------------------------------------------------------\n");
    printf("                       成绩列表 \n");
    printf("-----------------------------------------------------------\n");
    printf("序号   学号   姓名    C成绩    数学    英语       总分\n");
    printf("-----------------------------------------------------------\n");
    for(i=0;i<n;i++)
    {
        fwrite(&stu[i],sizeof(struct student),1,fp);
 printf("%d%8d%8s%8d%8d%8d%11d\n",stu[i].n,stu[i].num,stu[i].name,stu[i].C,stu[i].Maths,stu[i].En,stu[i].sum);
    }
    printf("-----------------------------------------------------------\n");
    fclose(fp);//关闭文件
}
/*主函数*/
main()
{
    int n;
    for(;;)
    {
        printf("\n");
        printf("-----------------------------------------------------------\n");
        printf("                                                          \n");
        printf("                          主菜单                          \n");
        printf("                                                          \n");
        printf("                      ▲1.录入成绩▲                       \n");
        printf("                      ▲2.输出成绩▲                       \n");
        printf("                      ▲3.查找成绩▲                       \n");
        printf("                      ▲4.增加成绩▲                       \n");
        printf("                      ▲5.修改成绩▲                       \n");
        printf("                      ▲6.成绩排序▲                       \n");  
        printf("                      ▲7.退出系统▲                       \n");
        printf("                                                          \n");
        printf("-----------------------------------------------------------\n\n");
        printf("请输入选择项(1-7):");
        scanf("%d",&n);
        printf("\n\n\n\n");
        if(n>0&&n<8)
        {
            switch(n)
            {
               case 1:finput();break;
               case 2:flist();break;
               case 3:fsearch();break;
               case 4:fadden();break;
               case 5:fchange();break;
               case 6:ftaxis();break;  
               case 7:printf("--------------------------------------------\n");
                      printf("                                            \n");
                      printf("                  谢谢使用!                 \n");
                      printf("                    再见!                   \n");
                      printf("                                            \n");
                      printf("--------------------------------------------\n");
                      exit(0);  
            }
        }
        else
        {
            printf("---------------------------------------------\n");
            printf("                                            \n");
            printf("                  输入错误!                 \n");
            printf("                   请退出!                  \n");
            printf("                                            \n");
            printf("--------------------------------------------\n");
            break;
        }
    }
}
搜索更多相关主题的帖子: 成绩 stu num 文件 printf 
2018-06-29 18:24
快速回复:要1.将增加成绩改成随机插入成绩(要求按原来规律),2.统计不及格的科 ...
数据加载中...
 
   



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

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