c++怎么判断20道选择题的对错?

 庚的右翼cs神 发布于 2022-11-01 04:59

write a program to grade a multiple-choice exam. The exam has 20 questions, each answered with a letter in the range of ‘a’ through ‘f’.
The program should read the key from input, then read each answer and output the ID number and score. Erroneous input should result in a error message.
Input
The first line of input is the key, consisting of a string of 20 characters.
The remaining lines are exam answers, each of which consists of a student ID number, a space, and a string of characters.
The input ends with 0.
Output
Output the score for each ID number in a sigle line, consisting the ID number, a space, and the score or error message.
Sample Input
Copy sample input to clipboard
abcdefabcdefabcdefab
1234567 abcdefabcdefabcdefab
9876543 abddefbbbdefcbcdefac
5554446 abcdefabcdefabcdef
4445556 abcdefabcdefabcdefabcd
3332221 abcdefghijklmnopqrst
0
Sample Output
1234567 20
9876543 15
5554446 Too few answers
4445556 Too many answers
3332221 Invalid answers

1 个回答
  • #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    bool isAnswerValid(string ans)
    {
        if(ans.length()<20) {
            cout << " Too few answers" << endl;
            return false;
        }
        else if(ans.length() > 20) {
            cout << " Too many answers" << endl;
            return false;
        }
    
        for(auto i=ans.begin(); i!=ans.end(); i++)
            if( (char)*i<'a' || (char)*i>'f') {
                cout << " Invalid answers" << endl;
                return false;
            }
    
        return true;
    }
    
    int howManyRightAnswer(string ans, string stu)
    {
        int val = 0;
    
        if(isAnswerValid(stu)==true) {
            auto i = ans.begin();
            auto j = stu.begin();
    
            while(i!=ans.end()) {
                if( *i == *j)
                    val++;
                i++;
                j++;
            }
            return val;
        }
        else
            return  -1;
        
    }
    
    int main(void) 
    {
        string ans, stu;
        int sid, rignum;
        cin >> ans;
        if (isAnswerValid(ans)==true) {
            scanf("%d", &sid);
            while(sid!=0) {
                cin >> stu;
                cout << sid;
                rignum = howManyRightAnswer(ans,stu);
                if(rignum!=-1)
                    printf(" %d\n", rignum);
                scanf("%d", &sid);
            }
        }
    
    }
    

    VC++2010 测试没问题。

    2022-11-01 06:37 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有