编程题-成绩排名

成绩排名

题目总共包含如下两种格式的字符串命令:

1 LOD GRADE命令,其格式为:

LOD GRADE:NAME=XiaoMing,MATH=80,LANG=90;

(1)此命令用于导入学生成绩

(2)NAME字段表示学生姓名

(3)MATH字段表示学生数学成绩

(4)LANG字段表示学生语文成绩

(5)MATH字段和LANG字段顺序不一定是MATH在前,LANG在后

(6)相同的分数,名词相同,后面的名次孔雀:例如100,99,99,99,98,98,名次1,2,2,2,5,5

(7)此命令会连续执行,直到遇到第一个LST GRADE

 

2 LST GRADE命令,其格式为:

LST GRADE:NAME=XiaoHong;

(1)此命令用于查询学生成绩

(2)NAME字段表示学生姓名

(3)查询结果格式:姓名 数学 语文 总分 数学排名 语文排名 总排名

(4)每组用例,此命令仅执行一次

样例输入

LOD GRADE:NAME=XiaoMing,MATH=80,LANG=90;

LOD GRADE:NAME=XiaoHong,LANG=60,MATH=100;

LOD GRADE:NAME=XiaoMei,MATH=70,LANG=90;

LST GRADE:NAME=XiaoHong;

样例输出 XiaoHong 100 60 160 1 3 2

 

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

 

char *find(char a[], int len, char b[], char *c);

 

struct ln

{

         char *name;

         int  math;

         int  lang;

         int  sum;

}arr[100];

 

void main()

{

         int i,j,len,num=0; 

         char str[100];

         char *tmp;

         while(gets(str) && strlen(str)>0)

         {

                   len = strlen(str);

 

                   if (str[1]=='O')

                   {

                            char s1[] = "NAME";

                            arr[num].name = find(str,len,s1,arr[num].name);

 

                            char s2[] = "MATH";

                            char *ma;

                            ma = find(str,len,s2,ma);

                            arr[num].math = atoi(ma);

 

                            char s3[] = "LANG";

                            char *la;

                            la = find(str,len,s3,la);

                            arr[num].lang = atoi(la);

 

                            arr[num].sum = arr[num].math + arr[num].lang;

 

                            num++;

                   }

 

                   else if (str[1]=='S')

                   {

                            int p1=1,p2=1,p3=1;

                            char s1[] = "NAME";

                            tmp = find(str,len,s1,tmp);

 

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

                            {

                                     if (strcmp(arr[i].name, tmp)==0)  //两个指针判断相等,用strcmp函数,如果返回值为0则相等

                                     {

                                               for (j=0; j<num; j++)

                                               {

                                                        if(arr[j].math>arr[i].math)

                                                        {

                                                                 p1++;

                                                        }

                                                        if(arr[j].lang>arr[i].lang)

                                                        {

                                                                 p2++;

                                                        }

                                                        if(arr[j].sum>arr[i].sum)

                                                        {

                                                                 p3++;

                                                        }

                                               }

                                               printf("%s %d %d %d %d %d %d\n",arr[i].name, arr[i].math, arr[i].lang, arr[i].sum, p1, p2, p3);

                                               break;

                                     }       

                            }

                   }

         }

}//main

 

char *find(char a[], int len, char b[], char *c)

{

         int i,j,t=0;

         char temp[20]={'\0'}; 

        

         for (i=0;i<len-3;i++)

         {

                   if(a[i]==b[0] && a[i+1]==b[1] && a[i+2]==b[2] && a[i+3]==b[3])  //a[i+4]是'='

                   {

                            for (j=i+5; a[j]!=','&&a[j]!=';' ;j++)

                            {

                                     temp[t]=a[j];

                                     t++;

                            }

                            c = (char *)malloc(t);

                            strcpy(c,temp);

                            return c; 

                   }

         }

         return NULL;

}//find

 

posted on 2016-09-08 09:55  jh.ding  阅读(1097)  评论(0编辑  收藏  举报