C++

来源:互联网 发布:风驰网络加速器官网 编辑:程序博客网 时间:2024/05/01 07:00

//多选题

//IO.cpp

#include "assign_judge.h"

static QuestionItem_t Question[QUESTION_MAX];//题目数组
static int JudgeMethod = 0;//方案判断
static int ScoreMethod = 0;//判分方案
static int Num = 0;//题目总数

 

/*****************************************
*GetQuestionNum      获得试卷中试题总数目
******************************************/
int GetQuestionNum()
{
 FILE* fp;
 int i;
 int Score;

 //对Question进行初始化
 memset(&Question, 0, sizeof(Question));
 
 fp = fopen("D:\\question.txt","r");
 
 if (fp == NULL)
 {
  fprintf(fp,"\n File quetsion.txt doesn't exist");
  return -1;
 }

 //读 Methods
 if ((fscanf(fp,"%d,",&JudgeMethod) == EOF) || (fscanf(fp,"%d,",&ScoreMethod) == EOF))
 {
  fclose(fp);
  return -2;
 }

 //读 Questions
 for (i = 0; i < QUESTION_MAX; i++)
 {
  
  //读选择的答案
  if (fscanf(fp,"%s,",&Question[i].Choice) == EOF)
  {
   break;
  }

  //读正确答案
  if (fscanf(fp,"%s,",&Question[i].Answer) == EOF)
  {  
   break;
  }

  //读每题的分数
  if (fscanf(fp,"%d,",&Score) == EOF)
  {
   break;
  }
  Question[i].Score = (float)Score;

  Question[i].No = i + 1;//设置题号
 } 

 Num = i;
 fclose(fp);

 return Num;
}


/*****************************************
*GetPaperAndAnswer   获得试卷信息和标准答案
*QuestionItem_t *Paper  试卷信息:试题列表
******************************************/
int GetPaperAndAnswer( QuestionItem_t *Paper)
{
 if (Paper == NULL) 
 {
  return -1;
 }
 
 //获得试卷信息和标准答案
 memcpy(Paper,Question, sizeof(QuestionItem_t)*Num);
 
 return 0;
}

 

/*****************************************
*GetMethod   获得判分方案及每题分数分配方法
*int *pScoreMethod   分数分配
*int *pJudgeMethod   判分方案
******************************************/
//获得判分方案及每题分数分配方法
int GetMethod(int *pScoreMethod, int *pJudgeMethod)
{
 if ((pJudgeMethod == NULL) || (pScoreMethod == NULL))
 {
  return -1;
 }

 *pScoreMethod = ScoreMethod;
 *pJudgeMethod = JudgeMethod;

 return 0;
}


/*****************************************
*GetQuestionNum              输出试卷成绩
*float TotalScore            最后成绩
******************************************/
int OutputResult(float TotalScore) 
{

 FILE* fp = fopen("d:\\Score.txt","w");
 
 fprintf(fp,"================Score ====================\n");

 fprintf(fp,"Mark:%d \n",(int)TotalScore);

 fclose(fp);

 return 0;
}

 

//IO.h

#ifndef __IO_H__
#define __IO_H__

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#define  QUESTION_MAX  100 

using namespace std;

typedef struct {
 int  No;   //题目编号
 char Choice[10]; //答题情况
 char Answer[10]; //正确答案
 float  Score;  //题目满分
}QuestionItem_t;

//获得试卷中试题总数目
int GetQuestionNum();

//获得试卷信息和标准答案
int GetPaperAndAnswer( QuestionItem_t *Paper/*试卷信息:试题列表*/);

//获得判分方案及每题分数分配方法
int GetMethod(int  *pScoreMethod, //分数分配1:第一种/ 2:第二种/3: 第三种
   int  *pJudgeMethod  //判分方案1:第一种/ 2:第二种/3: 第三种
   );

//输出试卷成绩
int OutputResult(float TotalScore); // TotalScore为最后成绩


#endif

//assign.h

#ifndef __ASSIGN_JUDGE__
#define __ASSIGN_JUDGE__
#include "IO.h"


int assign1(QuestionItem_t *Paper, int iNum);//题目数决定
int assign2(QuestionItem_t *Paper, int iNum);//选项数与全部正确答案数比值
int assign3(QuestionItem_t *Paper, int iNum);//出题者指定

float judge1(QuestionItem_t *Paper, int iNum);//多选少选均不得分
float judge2(QuestionItem_t *Paper, int iNum);//少选可得部分分数,而多选不得分
float judge3(QuestionItem_t *Paper, int iNum);//选对一个选项得一定分值,选错一个选项扣掉同样的分值

#endif

//assign.cpp

 

#include "assign_judge.h"


/*********************************************
*assign1       题目数决定
*QuestionItem_t *Paper    题目信息结构指针
*int iNum       总题目数
***********************************************/
int assign1(QuestionItem_t *Paper, int iNum)
{

 //题目数决定
 for(int i = 0; i < iNum; i++)
 {
  (Paper + i)->Score = (100.0f)/iNum;
 }

 return 0;
}


/***********************************************
*assign2    选项数与全部正确答案数比值
*QuestionItem_t *Paper 题目信息结构指针
*int iNum    总题目数
************************************************/
int assign2(QuestionItem_t *Paper, int iNum)
{
 int iCount = 0;//总选项答案

 for(int i = 0; i < iNum; i++)
 {
  iCount +=strlen((Paper + i)->Answer);
 }

 for(int i = 0; i < iNum; i++)
 {
  (Paper+i)->Score = ((float)strlen((Paper+i)->Answer)/iCount)*100;
 }

 return 0;
}

/*****************************************
*assign3    出题者指定
*QuestionItem_t *Paper 题目信息结构指针
*int iNum    总题目数
******************************************/
int assign3(QuestionItem_t *Paper, int iNum)
{
 //出题者指定
 for(int i = 0; i < iNum; i++)
 {
  cout << "请输入第" << i+1 <<"题分数:";
  cin >> (Paper+i)->Score;
 }
 
 return 0;
}

/*****************************************
*judge1    多选少选均不得分
*QuestionItem_t *Paper 题目信息结构指针
*int iNum    总题目数
******************************************/
float judge1(QuestionItem_t *Paper, int iNum)
{
 float iScore = 0;//总分
 int flag = 0;//查找标志

 for(int i = 0; i < iNum; i++)//遍历整个题目数组
 {

  //选项与答案长度不一致
  if(strlen((Paper + i)->Choice) != strlen((Paper + i)->Answer))
  {
   continue;
  }

  //按字符串挨个查找
  for(int j = 0; j < strlen((Paper + i)->Choice); j++)
  {
    for(int k = 0; k<strlen((Paper + i)->Answer); k++)
    {
     flag = 0;

     if(((Paper + i)->Choice)[j] == ((Paper + i)->Answer)[k])
     {
      flag = 1;
      break;  
     }
    }

    //选项不存在
    if(0 == flag)
    {
     break;
    }
   
  }
  if(1 == flag)
  {
   iScore += (Paper+i)->Score ;
  }
  
 }

 return iScore;//返回总分
}

/****************************************************
*judge2    少选可得部分分数,而多选不得分
*QuestionItem_t *Paper 题目信息结构指针
*int iNum    总题目数
*****************************************************/
float judge2(QuestionItem_t *Paper, int iNum)
{
 float iScore = 0;//总分
 int count = 0;//选对选项个数
 int flag = 0;//选项标志

 for(int i = 0; i < iNum; i++)
 {
  count = 0;

  //按字符串挨个查找
  for(int j = 0; j < strlen((Paper + i)->Choice); j++)
  {
    for(int k = 0; k<strlen((Paper + i)->Answer); k++)
    {
     flag = 0;

     if(((Paper + i)->Choice)[j] == ((Paper + i)->Answer)[k])
     {
      count ++;
      flag = 1;
      break;
     }
    }

    //选项不存在
    if(0 == flag)
    {
     count = 0;
     break;
    }
   
  }
  
   iScore += ((float)count/strlen((Paper + i)->Answer)) * (Paper+i)->Score ;
 }
 return iScore;
}

/*************************************************************************
*judge3    选对一个选项得一定分值,选错一个选项扣掉同样的分值
*QuestionItem_t *Paper 题目信息结构指针
*int iNum    总题目数
*************************************************************************/
float judge3(QuestionItem_t *Paper, int iNum)
{
 float iScore = 0;//总分
 int flag = 0;//查找标志
 int count = 0;//计分选项个数

 for(int i = 0; i < iNum; i++)
 {
  count = 0;

  //按选项挨个查找
  for(int j = 0; j < strlen((Paper + i)->Choice); j++)
  {
   
    for( int k = 0; k<strlen((Paper + i)->Answer); k++)
    {
     flag = 0;

     if(((Paper + i)->Choice)[j] == ((Paper + i)->Answer)[k])
     {
      count ++;
      flag = 1;
      break;
     }
    }

    //选项不存在
    if(0 == flag)
    {
     count--;
    } 
  }
  
  if(count > 0)
  {
   iScore += ((float)count/strlen((Paper + i)->Answer)) * (Paper+i)->Score ;
  }
  else
  {
   count = 0;
  }
 }
 return iScore;
}

//函数入口

#include "assign_judge.h"

int main()
{
 int iCount;//总题目数
 int JudgeMethod;//方案判断
 int ScoreMethod;//判分方案
 float  fScore = 0;  //题目满分
 QuestionItem_t *pQuestion;

 iCount = GetQuestionNum();
 //cout << iCount;
 pQuestion = new QuestionItem_t[iCount];
 GetPaperAndAnswer(pQuestion);

 GetMethod(&ScoreMethod, &JudgeMethod);

 switch(JudgeMethod){
  case 1: assign1(pQuestion, iCount); break;
  case 2: assign2(pQuestion, iCount); break;
  case 3: assign3(pQuestion, iCount); break;
  default: break;
 }

 switch(ScoreMethod){
  case 1:  fScore = judge1(pQuestion, iCount); break;
  case 2:  fScore = judge2(pQuestion, iCount); break;
  case 3:  fScore = judge3(pQuestion, iCount); break;
  default: break;
 }

 cout << setprecision(4) << fScore  <<endl;
 OutputResult(fScore);
 return 0;

}

 

原创粉丝点击