| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 482 人关注过本帖
标题:求好人讲解C语言代码每一句话
取消只看楼主 加入收藏
lxwyyz
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-6-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
求好人讲解C语言代码每一句话
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
struct STUDENT{
char name[10];
char number[10];
char sex;
int age;
float english,C_program;
float total,average;
};
void display()
{
    printf("------------------------------------------\n");
    printf("              1.输入信息                  \n");
    printf("              2.输出信息                  \n");
    printf("              3.按年龄查询                \n");
    printf("              4.按性别查询                \n");
    printf("              5.按姓名查询                \n");
    printf("              6.按学号排序                \n");
    printf("              7.按总分排序                \n");
    printf("              8.查找总分最大的            \n");
    printf("              9.退出系统                  \n");
    printf("------------------------------------------\n");
}
void input(struct STUDENT *s,int length)
{
    int i;
    char sex;
//    int sex_flag;
//    char temp_char;
    getchar();
    //跨过回车符
    //printf("|姓名|学号|性别|年龄|英语|C语言|总分|平均分|\n");
    for(i=0;i<length;i++)
    {
    printf("请输入第%d个人姓名",i+1);
    gets(s->name);//scanf("%s",s->name);

    printf("请输入第%d个人学号",i+1);
    gets(s->number);//scanf("%s",s->number);
    printf("请输入第%d个人性别",i+1);
    /*while(sex_flag!=1)
        {   
            scanf("%c",&temp_char);
            if((temp_char=='f' )||( temp_char=='F')||(temp_char=='m')||(temp_char=='M'))
            {
                s->sex=temp_char;
                sex_flag=1;break;
            }
            else
            {
                printf("性别输入错误,请重新输入:");
            }
        }*/

    do
    {
        sex=getchar();
        while(getchar()!='\n');
        if(sex!='f'&&sex!='F'&&sex!='M'&&sex!='m')
            printf("请重新输入‘F’或‘M’或‘f’或‘m’!\n");
    }while(sex!='F'&&sex!='f'&&sex!='M'&&sex!='m');
    s->sex=sex;
    printf("请输入第%d个人年龄",i+1);
    scanf("%d",&s->age);
    printf("请输入第%d个人的英语、C语言",i+1);
    scanf("%f%f",&s->english,&s->C_program);
    s->total=s->english+s->C_program;
    s->average=s->total/2;
    getchar();//跨过回车键
    s++;//指针向后移
    }
}
void output(struct STUDENT *s,int length)
{
    int i;
        printf("********************************************\n");
        printf("|姓名|学号|性别|年龄|英语|C语言|总分|平均分|\n");
    for(i=0;i<length;i++)
    {
        
        printf("%4s%4s%4c%4d%6.1f%6.1f%6.1f%7.1f\n",s->name,s->number,s->sex,s->age,s->english,s->C_program,s->total,s->average);
        s++;
    }
}
//年龄查询并统计多少人符合条件
void query_for_age(struct STUDENT *s,int length)
{
    int age;
    int i,count=0;
    printf("请输入你要查询的年龄:");
    scanf("%d",&age);
    for(i=0;i<length;i++,s++)
    {
        if(s->age==age)
        {
            printf("%4s%2c%5.2f\n",s->name,s->sex,s->total);
            count++;
        }
    }
    if(count==0)
        printf("没找到符合条件!\n");
    else
        printf("共找到复合条件的%d个\n",count);   
}
//性别统计
void query_for_sex(struct STUDENT *s,int length)
{
    int i,count_f=0,count_m=0;
    int total=0;
    for(i=0;i<length;i++)
    {
        if(s->sex=='f'||s->sex=='F')
            count_f++;
        else
            count_m++;
        s++;
    }

    printf("男生总数为%d,女生总数为%d\n",count_m,count_f);
}
//按姓名查询并统计多少人符合条件
void query_for_name(struct STUDENT *s,int length)
{
    int i,count=0;
    char name[10];
    printf("请输入要查询的姓名\n");
    //gets(name);
    scanf("%s",name);
    for(i=0;i<length;i++,s++)
    {
        if(strcmp(name,s->name)==0)
        {
            printf("%s%2c%5.2f\n",s->name,s->sex,s->total);
            count++;
        }
    }
    if(count==0)
        printf("没找到符合条件!\n");
    else
        printf("共找到复合条件的%d个\n",count);
}
void sortBynumber(struct STUDENT  *s,int length)
{
    int max_id,i,j;
    struct STUDENT tmp;//交换的是某个学生的所有信息,所以要定义一个结构体变量
    for(i=0;i<length-1;i++)
    {
        max_id=i;
        for(j=i+1;j<length;j++)
        {
           if(strcmp((s+max_id)->number,(s+j)->number)>0)
                  max_id=j;   
        }
    tmp=*(s+max_id);*(s+max_id)=*(s+i);*(s+i)=tmp;//*(s+max_id)=stu[max_id]
    }
    output(s,length);
}
void sortBytotal(struct STUDENT  *s,int length)
{
    int i,j,max_id;
    struct STUDENT temp;
    for(i=0;i<length-1;i++)
    {        
        max_id=i;
        for(j=i+1;j<length;j++)
        {
            if((s+max_id)->total<(s+j)->total)
                max_id=j;
        }
    temp=*(s+max_id);*(s+max_id)=*(s+i);*(s+i)=temp;
    }
    output(s,length);
}
void searchForMaxbytotal(struct STUDENT  *s,int length)
{
    int max_id=0,i;
    for(i=1;i<length;i++)
    {
      if((s+max_id)->total<(s+i)->total)
          max_id=i;
    }
    printf("总分最高分是%f分,\n该同学的学号为:%s 姓名:%s\n",(s+max_id)->total,(s+max_id)->number,(s+max_id)->name);


}
main()
{  
    struct STUDENT stu[3];
    int choice;
    while(1){
        display();
        printf("请输入你的操作(1-9):\n");
        scanf("%d",&choice);
        switch(choice)
          {
            case 1://输入信息
                input(stu,3);break;
            case 2://输出信息
               output(stu,3);break;
            case 3://按年龄查询
                query_for_age(stu,3);break;
            case 4://按性别查询
                query_for_sex(stu,3);break;
            case 5://按姓名查询
                query_for_name(stu,3);break;
            case 6://按学号排序
                sortBynumber(stu,3);break;
            case 7://按总分排序
                sortBytotal(stu,3);break;
            case 8://查找总分最大
                searchForMaxbytotal(stu,3);break;
            case 9://退出系统
              exit(0);break;
               
        }
        getchar();
    }
   
}
搜索更多相关主题的帖子: C语言 好人 代码 讲解 
2010-06-01 20:50
快速回复:求好人讲解C语言代码每一句话
数据加载中...
 
   



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

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