| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 340 人关注过本帖
标题:有谁知道???啊
只看楼主 加入收藏
dyn123
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
有谁知道???啊
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

//**********************************结构体*************************************
struct score
{
 char class_num[10];
 char person_num[10];
 char name[15];
 char sex;
 int Math;
 int Chinese;
 int English;
 int Physics;
 int Chemistry;
}stu;
 
//***********************************输出*******************************************
void print1()                                               //输出科目函数  //可避免这样的函数命名,可能让其他人误解
{
 printf("班级      学号      姓名               性别   数学   语文   英语   物理   化学\n");
}
 

//************************************菜单***********************************************
int menu()                                                  //菜单函数     //每个菜单都在同一位置,做法很好
{
 char i;
 printf("\n\n\n\n");
    puts("\t\t**********************主菜单******************\t\n");
    puts("\t\t\t\t1.学生信息输入\n");
    puts("\t\t\t\t2.学生信息浏览\n");
    puts("\t\t\t\t3.学生信息检索\n");
    puts("\t\t\t\t4.学生信息修改\n");
    puts("\t\t\t\t5.学生信息删除\n");
 puts("\t\t\t\t6.学生成绩排序\n");
    puts("\t\t\t\t7.退出系统\n");
    puts("\t\t**********************************************\t\n");
 printf("\n请选择相应的序号:[ ]\b\b");
 i=getchar();
 system("cls");
 return i;
}
//**************************************** 输入函数*******************************************
void Enter()                                          
{
 
 
 FILE *fp;
 char ch;
 if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
 {
  printf("\nCannot open file!\n");
  getch();
  exit(1);
 }
 do
 {
  printf("Enter  class_num:");scanf("%s",&stu.class_num);getchar();    printf("\n");
  printf("Enter person_num:");gets(stu.person_num);                    printf("\n");
  printf("Enter name:");gets(stu.name);                                printf("\n");
  printf("Enter sex(m/f):");scanf("%c",&stu.sex);getchar();            printf("\n");
  printf("Enter math score:");scanf("%d",&stu.Math);                   printf("\n");
  printf("Enter chinese score:");scanf("%d",&stu.Chinese);    printf("\n");
  printf("Enter english score:");scanf("%d",&stu.English);    printf("\n");
  printf("Enter physics score:");scanf("%d",&stu.Physics);    printf("\n");
  printf("Enter chemistry score:");scanf("%d",&stu.Chemistry);printf("\n");getchar();
  fwrite(&stu,sizeof(stu),1,fp);
  printf("continue(y/n)?");
  ch=getchar();   getchar();
 }while(ch=='y'||ch=='Y');
 fclose(fp);
 system("cls");
}

//*********************************************浏览函数**********************************************
void Browse()                                
{
 FILE *fp;
 int total=0;
 if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
 {
    printf("\tCan not open the inform file!");
    getch();
    exit(1);
 }
 while(fread(&stu,sizeof(stu),1,fp)==1)
 {
    total++;
    if(total==1)
     print1();
    printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
    printf("%-8c",stu.sex);
    printf("%-4d   ",stu.Math);
    printf("%-4d   ",stu.Chinese);
    printf("%-4d   ",stu.English);
    printf("%-4d   ",stu.Physics);
    printf("%-4d   ",stu.Physics);
    printf("\n");
 }
 fclose(fp);
 printf("\n\n\tThere are %d record in all!\n",total);
 printf("\tpress any key to continue!");
 getchar();
 getchar();
 system("cls");
}
 
//**********************************查询函数*********************************************
void Query()
{
 
 FILE * fp;
    char num1[10];
    char ch;
 if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
 {
    printf("\tCan not open the inform file!");
    getch();
    exit(1);
 }
 do
 {
  getchar();
     rewind(fp);
     printf("Please input the student number you want to search:");
     gets(num1);
     while(fread(&stu,sizeof(stu),1,fp)==1)
      if(strcmp(num1,stu.person_num)==0)
      {
    print1();
          printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
          printf("%-8c",stu.sex);
          printf("%-4d   ",stu.Math);
          printf("%-4d   ",stu.Chinese);
          printf("%-4d   ",stu.English);
          printf("%-4d   ",stu.Physics);
          printf("%-4d   ",stu.Physics);
          printf("\n");
    break;
      }
    printf("Search any more?(Y/N):[ ]\b\b");
    ch=getchar(); getchar();
 }while(ch=='Y'||ch=='y');
 fclose(fp);
 system("cls");
}
 
//**********************************学生成绩修改函数***********************************
void Recopose()                                 
{
     FILE * fp1,* fp2;
     char num1[10];
  char ch;
     int j=0;
  getchar();
    do
 {
        if((fp1=fopen("f:\\zimou.txt","a+"))==NULL)
  {
      printf("\tCan not open the infom file!");
      getch();
      exit(1);
  }
        if((fp2=fopen("f:\\ziye.txt","a+"))==NULL)
  {
      printf("\tCan not creat the temp file!");
      getch();
      exit(1);
  }
        printf("Please input the num you want to modify:");
        gets(num1);
        while(fread(&stu, sizeof(stu),1,fp1)==1)
  {
    if(strcmp(num1,stu.person_num)==0)
    {
       print1();
       printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
    printf("%-8c",stu.sex);
    printf("%-4d   ",stu.Math);
    printf("%-4d   ",stu.Chinese);
    printf("%-4d   ",stu.English);
    printf("%-4d   ",stu.Physics);
    printf("%-4d   ",stu.Physics);
    printf("\n");
    printf("\n\nPlease input the new information:\n");
    printf("\t班级代号:");gets(stu.class_num);
    printf("\t学生学号:");gets(stu.person_num);
    printf("\t学生姓名:"); gets(stu.name);
    printf("\t学生性别:");gets(&stu.sex);
    printf("\t学生成绩(数学):");scanf("%d",&stu.Math);
    printf("\t学生成绩(英语):");scanf("%d",&stu.English);
    printf("\t学生成绩(语文):");scanf("%d",&stu.Chinese);
    printf("\t学生成绩(物理):");scanf("%d",&stu.Physics);
    printf("\t学生成绩(化学):");scanf("%d",&stu.Chemistry);
    getchar();
    }
    fwrite(&stu,sizeof(stu),1,fp2);
  }
    fclose(fp1);
    fclose(fp2);
    printf("Modify sucess!\n");
    remove("f:\\zimou.txt");
    rename("f:\\ziye.txt","f:\\zimou.txt");
    printf("Modify ang more?(Y/N):[ ]\b\b");
    ch=getchar(); getchar();
 }while(ch=='Y'||ch=='y');
 system("cls");
}
//***************************************删除函数******************************************
void Delete()
{
     FILE * fp1,* fp2;
     int flag;
     char num1[10];
  char ch;
  getchar();
     do
  {
        if((fp1=fopen("f:\\zimou.txt","a+"))==NULL)
  {
           printf("\tCan not open the inform file!");
           getch();
           exit(1);
  }
        if((fp2=fopen("f:\\ziye.txt","a+"))==NULL)
  {
           printf("\tCan not creat the temp file!");
           getch();
           exit(1);
  }
        printf("请输入你要删除的学生的学号(person_num):");
        gets(num1);
        flag=0;
        while(fread(&stu,sizeof(stu),1,fp1)==1)
  {
           if(strcmp(num1,stu.person_num)==0)
     {
              print1();
              printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
              printf("%-8c",stu.sex);
              printf("%-4d   ",stu.Math);
              printf("%-4d   ",stu.Chinese);
              printf("%-4d   ",stu.English);
              printf("%-4d   ",stu.Physics);
              printf("%-4d   ",stu.Physics);
              printf("\n");
              flag=1;
     }
     else
      fwrite(&stu,sizeof(stu),1,fp2);
  }
       fclose(fp1);
       fclose(fp2);
       if(flag==1)
    {
          remove("f:\\zimou.txt");                    //这是核心算法,可以看看
          rename("f:\\ziye.txt","f:\\zimou.txt");
    }
       else
          printf("can not find this record!\n");
        printf("Delete any more?(Y/N):[ ]\b\b");
        ch=getchar(); getchar();
  }while(ch=='Y'||ch=='y');
  system("cls");
}
//********************************排序*****************************
void Score_sort()
{
 struct score stud[100];
 FILE * fp;
 int total=0;
 int i=0;
 char ch;
 int a,b;
 struct score temp;
 if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
 {
    printf("\tCan not open file!");
    getch();
    exit(1);
 }
 do
 {
    rewind(fp);
     printf("请输入你所要排序的科目(1.数学2.语文3.英语4.物理5.化学):[ ]\b\b");
     ch=getchar();getchar();
     total=0;
     while(fread(&stu,sizeof(stu),1,fp)==1)
     {
        total++;                                   //计数从而使结构体数组起到作用
       stud[i]=stu;
       i++;
     }
     switch(ch)
     {
      case'1':
    for(a=0;a<total-1;++a)
     for(b=a+1;b<total;++b)
     {
      if(stud[a].Math>stud[b].Math)
      {
       temp=stud[a];
       stud[a]=stud[b];
       stud[b]=temp;
      }
     }
    break;
      case'2':
    for(a=0;a<total-1;++a)
     for(b=a+1;b<total;++b)
     {
      if(stud[a].Chinese>stud[b].Chinese)
      {
       temp=stud[a];
       stud[a]=stud[b];
       stud[b]=temp;
      }
     }   
    break;
      case'3':
    for(a=0;a<total-1;++a)
     for(b=a+1;b<total;++b)
     {
      if(stud[a].English>stud[b].English)
      {
       temp=stud[a];
       stud[a]=stud[b];
       stud[b]=temp;
      }
     }
    break;
      case'4':  
    for(a=0;a<total-1;++a)
     for(b=a+1;b<total;++b)
     {
      if(stud[a].Physics>stud[b].Physics)
      {
       temp=stud[a];
       stud[a]=stud[b];
       stud[b]=temp;
      }
     }
     break;
      case'5':   
    for(a=0;a<total-1;++a)
     for(b=a+1;b<total;++b)
     {
      if(stud[a].Chemistry>stud[b].Chemistry)
      {
       temp=stud[a];
       stud[a]=stud[b];
       stud[b]=temp;
      }
     }
         
         break;
     }
  print1();
     for(i=0;i<total;i++)
     {
          printf("%-10s%-10s%-20s",stud[i].class_num,stud[i].person_num,stud[i].name);
          printf("%-8c",stud[i].sex);
          printf("%-4d   ",stud[i].Math);
          printf("%-4d   ",stud[i].Chinese);
          printf("%-4d   ",stud[i].English);
          printf("%-4d   ",stud[i].Physics);
          printf("%-4d   ",stud[i].Physics);
          printf("\n");
     }
 
   
    printf("\n\n总共有 %d 笔数据满足条件!",total);
    printf("继续?(Y/N):[ ]\b\b");
    ch=getchar();getchar();
 }while(ch=='Y'||ch=='y');
 fclose(fp);
}
//***********************************出口**************************************
void Exit()
{
 exit(0);
}

//***********************************主函数****************************************
int main()
{
 while(1)
 switch(menu())
 {
  case '1':Enter();break;
  case '2':Browse();break;
  case '3':Query();break;
  case '4':Recopose();break;
  case '5':Delete();break;
  case '6':Score_sort();break;
  case '7':Exit();break;
 }
 return 0;
}
 这个程序 在添加一个求平均分 最高分最低分的  模块  怎么写!!????
搜索更多相关主题的帖子: English 结构体 Chinese 
2011-06-16 00:46
hjywyj
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:1114
专家分:2611
注 册:2010-4-14
收藏
得分:20 
在结构体中加一个 float ave;求平均分。
最高分和最低分楼主应该会吧。数组排序
2011-06-16 06:35
快速回复:有谁知道???啊
数据加载中...
 
   



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

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