编程题:
一批学员需要参加A、B、C三项考试;考试后,按照一定比例和分数的限制,评选成绩优胜者、获得一等奖、二等奖。请编写程序处理这些学员的成绩、根据要求找出优胜者。
以下是对优胜者的条件限制以及获奖比例说明。
1、优胜者的成绩要求:
1.1一等奖要求优胜者的每项成绩均需要75分以上(含75分);二等奖要求优胜者的每项成绩均需要60分以上(含60分)。
2、优胜者的获奖比例要求:
2.1原则上,参加考试人员(三项考试均缺考的人不算参考人员)按三项总成绩高低分数排列,成绩符合条件的前10%的人获得一等奖,成绩符合条件的前30%获得二等奖,并且一等奖与二等奖不兼得;具体获奖人数按照四舍五入原则的计算得出。
2.2对于成绩相同的情况,如果1人获奖,则与之成绩相同的人也同时获奖,并且本等级(一等奖、二等奖)获奖人数不受前面原则限制,但影响后面等级(指二等奖)的人数----前一奖项多几人、后一奖项则少几人。
2.3因成绩不符合要求,获奖人数比率有可能低于原则规定人数。
对成绩单的说明。
-
每次输入参加一项考试的学员成绩。
-
每个成绩单最多有100个学员。
-
三次考试名单不一定完全相同,有时会有个别差异。
-
如果学员的一项成绩为0,则表示该学员缺考该项考试;如果学员的三项考试均为0分,则表示其缺考三项考试;对于缺考三项考试的人,不应将其纳入参加考试人员的总人数。
要求:
-
请在给定工程中、按照给定输入、完成程序、并调用输出函数输出结果。
-
可以依据自身情况,选择VC6、VS2003、VS2005、VS2008、VS2010工程中任一种;
将文件InputA.txt、InputB.txt和InputC.txt等3个文件放在C:\目录下;
输出文件output.txt也会输出到C:\目录下。
不要更改输入输出文件的目录位置。
-
数据的输入输出。
请通过Input函数取得单项成绩如下:
int Input(char TestKind, Score_t **ppScoreList, int * pNum);
请通过Output函数输出结果。仅仅要输出获奖者,严禁输出未获奖者。
int Output(AwardStud_t *AwardList, int Num);
-
请在CodeTest.cpp文件中完成程序;
-
要求程序工整、符合程序规范。
-
完成程序功能所需要的数据结构和函数,已经分别实现在工程中。如有不足,也可以补充。
//考试类型
#define TEST_A ('A')
#define TEST_B ('B')
#define TEST_C ('C')
//优胜等级
#define GRADE_ONE ('1')//一等奖
#define GRADE_TWO ('2')//二等奖
//某个学员的某项成绩信息,用于输入
typedefstruct {
char StudNo[5]; //学员ID
int Score; //某项考试的成绩
}Score_t;
//某个优胜学员的信息,用于输出
typedefstruct {
char StudNo[5]; //学生ID
int TotalScore; //三项考试的总成绩
char Grade; //优胜等级.GRADE_ONE:一等奖;GRADE_TWO:二等奖
}AwardStud_t;
//函数名称:Input
//参数说明
// char TestKind,:输入参数,表示考试的类型,如TEST_A、TEST_B、TEST_C
// ScoreList_t **ScoreList:输出参数,记录某项考试的成绩单(数组)的首地址
// int * Num:输出参数,记录成绩单上学员的个数
//功能说明
//根据考试类型取得考试的成绩单(数组)的首地址和人数
int Input(char TestKind, Score_t **ppScoreList, int * pNum);
//函数名称:Output
//参数说明
// AwardStud_t *AwardList:输出参数,记录着优胜者数组的首地址
// int Num:输出参数,记录着优胜者的数量
int Output(AwardStud_t *AwardList, int Num);
//函数名称:Sort
//参数说明
// AwardStud_t *pTotalScoreList:输入/输出参数排序数组的首地址
// int TotalNum:输入参数排序数组元素的个数
//功能说明
//排序一个数组:按照数组元素的域值----总成绩(TotalScore)由高到低排列
int Sort(AwardStud_t *pTotalScoreList, int TotalNum);
//File: io.h
#ifndef __INPUT__
#define __INPUT__
//考试类型
#define TEST_A ('A')
#define TEST_B ('B')
#define TEST_C ('C')
//优胜等级
#define GRADE_ONE ('1') //一等奖
#define GRADE_TWO ('2') //二等奖
//某个学员的某项成绩信息,用于输入
typedef struct {
char StudNo[5]; // 学员ID
int Score; // 某项考试的成绩
}Score_t;
//某个优胜学员的信息,用于输出
typedef struct {
char StudNo[5]; // 学生ID
int TotalScore; // 三项考试的总成绩
char Grade; // 优胜等级.GRADE_ONE:一等奖;GRADE_TWO:二等奖
}AwardStud_t;
//函数名称: Input
//参数说明
// char TestKind, :输入参数,表示考试的类型,如TEST_A、TEST_B、TEST_C
// ScoreList_t **ScoreList:输出参数,记录某项考试的成绩单(数组)的首地址
// int * Num:输出参数,记录成绩单上学员的个数
//功能说明
// 根据考试类型取得考试的成绩单(数组)的首地址和人数
int Input(char TestKind, Score_t **ppScoreList, int * pNum);
//函数名称:Output
//参数说明
// AwardStud_t *AwardList: 输出参数,记录着优胜者数组的首地址
// int Num: 输出参数,记录着优胜者的数量
int Output(AwardStud_t *AwardList, int Num);
#endif
//File: lib.h
#ifndef __LIB_H__
#define __LIB_H__
//函数名称: Sort
//参数说明
// AwardStud_t *pTotalScoreList :输入/输出参数 排序数组的首地址
// int TotalNum :输入参数 排序数组元素的个数
//功能说明
// 排序一个数组:按照数组元素的域值----总成绩(TotalScore)由高到低排列
int Sort(AwardStud_t *pTotalScoreList, int TotalNum);
#endif
//File: stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
//File: targetver.h
#pragma once
// The following macros define the minimum required platform. The minimum required platform
// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run
// your application. The macros work by enabling all features available on platform versions up to and
// including the version specified.
// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.
#endif
//File: CodeTest.cpp
// CodeTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string.h"
#include "io.h"
#include "lib.h"
#define MAXNUM_OF_STUD (100)
//符合一等奖
bool isFixedOne(Score_t* pSstuA, Score_t* pSstuB, Score_t* pSstuC, int numSstu, char * pNumStu)
{
for(int i=0; i<numSstu; i++)
{
if(0 == strcmp(pSstuA[i].StudNo, pNumStu) && pSstuA[i].Score < 75)
{
return false;
}
if(0 == strcmp(pSstuB[i].StudNo, pNumStu) && pSstuB[i].Score < 75)
{
return false;
}
if(0 == strcmp(pSstuC[i].StudNo, pNumStu) && pSstuC[i].Score < 75)
{
return false;
}
}
return true;
}
//符合二等奖
bool isFixedTwo(Score_t* pSstuA, Score_t* pSstuB, Score_t* pSstuC, int numSstu, char * pNumStu)
{
for(int i=0; i<numSstu; i++)
{
if(0 == strcmp(pSstuA[i].StudNo, pNumStu) && pSstuA[i].Score < 60)
{
return false;
}
if(0 == strcmp(pSstuB[i].StudNo, pNumStu) && pSstuB[i].Score < 60)
{
return false;
}
if(0 == strcmp(pSstuC[i].StudNo, pNumStu) && pSstuC[i].Score < 60)
{
return false;
}
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
//从此处开始答题
Score_t *pScoreListA;
int numA;
Input(TEST_A, &pScoreListA, &numA);
Score_t *pScoreListB;
int numB;
Input(TEST_B, &pScoreListB, &numB);
Score_t *pScoreListC;
int numC;
Input(TEST_C, &pScoreListC, &numC);
//计算总成绩
int numMax=numB;
if(numA > numB)
{
numMax = numA;
}
else if(numC > numB)
{
numMax = numC;
}
//按三项总成绩高低分数排列
AwardStud_t totalScoreList[MAXNUM_OF_STUD]={0};
for(int i=0; i<numMax; i++)
{
char ch[5] = {0};
if(strcmp(pScoreListA[i].StudNo,ch) != 0)
{
strcpy(totalScoreList[i].StudNo, pScoreListA[i].StudNo);
}
if(strcmp(pScoreListB[i].StudNo,ch) != 0)
{
strcpy(totalScoreList[i].StudNo, pScoreListB[i].StudNo);
}
if(strcmp(pScoreListC[i].StudNo,ch) != 0)
{
strcpy(totalScoreList[i].StudNo, pScoreListC[i].StudNo);
}
totalScoreList[i].TotalScore = pScoreListA[i].Score + pScoreListB[i].Score + pScoreListC[i].Score;
}
Sort(totalScoreList,numMax);
//Output( totalScoreList, numMax);
//AwardStud_t pTotalScoreList[100];
int j=0;
//寻找一等奖
int numOne = numMax * 0.1;
int countOne = 0;
for(int i=0; i<numOne; i++)
{
if( pScoreListA[i].Score >= 75 &&
pScoreListB[i].Score >= 75 &&
pScoreListC[i].Score >= 75 )
{
totalScoreList[i].Grade = '1';
countOne++;
for(int k=0; k<numMax; k++)
{
if( totalScoreList[i].TotalScore == totalScoreList[k].TotalScore &&
totalScoreList[k].Grade != '1' &&
isFixedOne(pScoreListA,pScoreListB,pScoreListC,numMax,totalScoreList[k].StudNo))
{
totalScoreList[k].Grade = '1';
countOne++;
}
}
}
}
//寻找二等奖
int numTwo = numMax * 0.3;
for(int i=0; i<numTwo; i++)
{
if( pScoreListA[i].Score >= 60 &&
pScoreListB[i].Score >= 60 &&
pScoreListC[i].Score >= 60 &&
totalScoreList[i].Grade != '1')
{
totalScoreList[i].Grade = '2';
countOne++;
for(int k=0; k<numMax; k++)
{
if( totalScoreList[i].TotalScore == totalScoreList[k].TotalScore &&
totalScoreList[k].Grade != '1' &&
totalScoreList[k].Grade != '2' &&
isFixedOne(pScoreListA,pScoreListB,pScoreListC,numMax,totalScoreList[k].StudNo))
{
totalScoreList[k].Grade = '2';
countOne++;
}
}
}
}
Output( totalScoreList, countOne);
return 0;
}
//File: io.cpp
#include "stdafx.h"
#include "io.h"
#include "lib.h"
int ReadFromFile(char *FileName, Score_t *pList,int *pNum);
int Input(char TestKind, Score_t **ppScoreList, int * pNum)
{
#define MAXNUM_OF_STUD (100)
static Score_t ScoreListA[MAXNUM_OF_STUD] = {0};
static Score_t ScoreListB[MAXNUM_OF_STUD] = {0};
static Score_t ScoreListC[MAXNUM_OF_STUD] = {0};
static int NumA = 0;
static int NumB = 0;
static int NumC = 0;
static bool flag = false;
if ( flag == false )
{
ReadFromFile("C:\\inputA.txt",ScoreListA, &NumA);
ReadFromFile("C:\\inputB.txt",ScoreListB, &NumB);
ReadFromFile("C:\\inputC.txt",ScoreListC, &NumC);
flag = true;
}
if ( TestKind == TEST_A )
{
*ppScoreList = ScoreListA;
*pNum = NumA;
}
else if ( TestKind == TEST_B )
{
*ppScoreList = ScoreListB;
*pNum = NumB;
}
else if ( TestKind == TEST_C )
{
*ppScoreList = ScoreListC;
*pNum = NumC;
}
else
{
printf("\n Input Error");
}
return 0;
}
int ReadFromFile(char *FileName, Score_t *pList,int *pNum)
{
int i;
int StudNum = 0;
FILE* fp;
fp = fopen(FileName,"r");
if (fp == NULL)
{
printf(FileName);
printf("doesn't exist\n");
fclose(fp);
return -1;
}
for (i = 0; i < MAXNUM_OF_STUD; i++ )
{
if (fscanf(fp,"%s", pList[i].StudNo) == EOF)
{
break;
}
if (fscanf(fp,"%d", &pList[i].Score) == EOF)
{
break;
}
StudNum++;
}
fclose(fp);
*pNum = StudNum;
return 0;
}
int Output(AwardStud_t *AwardList, int Num)
{
FILE* fp;
int i;
fp = fopen("C:\\Result.txt","w");
if (fp == NULL)
{
printf("File can't be created\n");
fclose(fp);
return -1;
}
for ( i = 0; i < Num; i++ )
{
fprintf(fp,"%s\t", AwardList[i].StudNo);
fprintf(fp,"%d\t", AwardList[i].TotalScore);
fprintf(fp,"%c\t", AwardList[i].Grade);
fprintf(fp,"\n");
}
fclose(fp);
return 0;
}
//File: lib.cpp
#include "stdafx.h"
#include "io.h"
#include "lib.h"
//函数名称: Sort
//参数说明
// AwardStud_t *pTotalScoreList :输入/输出参数 排序数组的首地址
// int TotalNum :输入参数 排序数组元素的个数
//功能说明
// 排序一个数组:按照数组元素的域值----总成绩(TotalScore)由高到低排列
int Sort(AwardStud_t *pTotalScoreList,int TotalNum)
{
int i, j;
AwardStud_t Temp;
for (i = 0; i < TotalNum-1; i++)
{
for (j = 0; j < TotalNum-1-i; j++)
{
if ( pTotalScoreList[j].TotalScore< pTotalScoreList[j+1].TotalScore)
{
memcpy(&Temp, &pTotalScoreList[j],sizeof(AwardStud_t));
memcpy(&pTotalScoreList[j], &pTotalScoreList[j+1],sizeof(AwardStud_t));
memcpy(&pTotalScoreList[j+1],&Temp,sizeof(AwardStud_t));
}
}
}
return 0;
}
// stdafx.cpp : source file that includes just the standard includes
// CodeTest.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
//File: stdafx.cpp
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file