【问题标题】:C++ automated scoring systemC++自动评分系统
【发布时间】:2015-05-24 02:10:59
【问题描述】:

我无法按照规范完成作业。以下是作业场景:

一所大学迫切需要一个自动化的考试评分系统。使用 C++,为大学编写一个评分系统,并为至少五名学生的考试评分。

要创建评分系统,请按照以下步骤操作:

先询问考试题数

然后询问每个问题的正确答案。请注意,多项选择测试和问题的答案从 A 到 D。

询问学生人数并通过询问他们的姓名来处理每个学生,然后遍历要求学生回答的问题。

为每个问题评分。

在最后一道题后计算学生分数并显示“学生‘插入学生姓名’得分为 10 分,满分 20 分或 50%。”

重复直到所有学生都被评分。

在所有学生都得分后,以与以前相同的方式插入打印所有学生成绩的班级列表。

这是我目前所拥有的:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    //declare variables
    char choice;
    string studentName;
    vector<char> answers;
    vector<string> names;
    int getStudents();
    int getQuestions();
    
    //calls function to get number of questions
    float questions = getQuestions();


    //Get answers
    for (int i = 0; i < questions; ++i) {
        cout << "What is the answer for question " << i + 1 << endl;
        cin >> choice;
        answers.push_back(choice);
    }

    //Get number of students
    int students = getStudents();


    //Get student names
    for (int i = 0; i < students; i++) {
        cout << "Student " << i + 1 << ", what is your name?" << endl;
        cin >> studentName;
        names.push_back(studentName);
    }

    float score = 0;
    char studentAnswer;
    vector<char> userAnswer;
    vector<float> finalScore;
        
    //gets student answers
    for (int i = 0; i < students; i++) {
        for (int j = 0; j < questions; j++) {
            cout << names[i] << ", what is your answer for question " << j + 1 << "?" << endl;
            cin >> studentAnswer;
            userAnswer.push_back(studentAnswer);
        }
    }

    

    //calculates student scores
    for (int i = 0; i < students; i++) {
        for (int j = 0; j < questions; j++) {
            if (userAnswer[j] == answers[j])
                score = score + 1;
                }       
        finalScore.push_back(score);
    }

    //outputs scores
    for (int i = 0; i < students; i++) {
        cout << names[i] << " scored " << finalScore[i] << " out of " << questions <<
            " or " << (finalScore[i] / questions) * 100 << "%" << endl;
    }

    

    system("pause");
    return 0;
}
//function to get number of questions
int getQuestions()
{
    int questions;
    cout << "How many questions are there?" << endl;
    cin >> questions;
    return questions;
}
//function to get number of students
int getStudents()
{
    int students;
    cout << "How many students are there?" << endl;
    cin >> students;
    return students;
}

最终分数返回的值不准确,我找不到错误发生的位置。

对于最后一步的排序,我被要求按照降序排列的分数和升序或字母顺序排列的名称进行排序。我能够彼此独立地对它们进行排序,但不知道如何组合它们并以这种方式排序。

【问题讨论】:

  • 关于排序,无论如何你都应该展示你到目前为止所得到的,但最好单独提出一个问题。

标签: c++ sorting


【解决方案1】:
//calculates student scores
for (int i = 0; i < students; i++) {
    score = 0; //Fabio was right you need to reset score to 0 for each student
    for (int j = 0; j < questions; j++) {
        if (userAnswer[j] == answers[j])
            score = score + 1;
            }       
    finalScore.push_back(score);
}

当您检查 userAnswer 是否等于 answers 时,您总是从 0 开始。所以您每次都在检查学生 1 的答案。你可以试试:

if (userAnswer[i*questions+j] == answers[j])

【讨论】:

  • 很好,我错过了。
  • 谢谢kyho,这解决了我的计算问题,但我不完全理解更改if语句的逻辑。你能帮我澄清一下吗?我的阅读方式是,每个新学生的外部循环都会递增,而内部循环应该从第一个问题开始检查答案。
  • userAnswer.push_back(studentAnswer) 将所有学生的答案推送到同一个向量上。因此,如果您有 3 个学生并且有 3 个问题,则向量可能类似于:{A,B,C,C,B,B,A,B,A}。在您的 if 语句中,您每次都在查看前 3 个答案,userAnswer[0]、userAnswer[1]、userAnswer[2]。在这种情况下,将 j 与 i*question 相乘得到第一个学生的 0,1,2,第二个学生的 3,4,5,第三个学生的 6,7,8。这样,您就可以在 userAnswer 向量中为每个学生获取正确的值。
【解决方案2】:

您将 score 初始化为 0,但您不会为每个学生重置它。你应该这样做:

    //calculates student scores
    for (int i = 0; i < students; i++) {
        score = 0; // here, so that it is reset for each student

顺便说一句,你的questions 变量是float,我想你想要一个int

【讨论】:

  • 我将问题变量设为浮点数,以便在最后计算百分比。不需要吗?
  • @RyanA 不,您可以将其保留为int(这是它的真实类型),如果您愿意,可以在评估公式时使用static_cast&lt;float&gt;(score)。但实际上在这种情况下你不需要它,因为finalscore[i] 已经是float,所以questions 会自动转换为float,结果也是float
猜你喜欢
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多