【问题标题】:C++ program for grading multiple True or False tests用于对多个 True 或 False 测试进行评分的 C++ 程序
【发布时间】:2019-08-11 20:31:19
【问题描述】:

问题:

我的主要问题是我很难将包含数组的函数连接到主函数。

您学校的历史老师在为真/假测试评分时需要帮助。学生的 ID 和测试答案存储在一个文件中。文件中的第一个条目包含测试的答案,格式为:

TFFTFFTTTTFFTFTTFTFT

文件中的每个其他条目都是学生 ID,后跟一个空白,然后是学生的回答。例如条目:

ABC54301 TFTTFTT TFTFTFFTTFT

表示学生证是ABC54301,问题1的答案是True,问题2的答案是False > 等。该学生没有回答问题9。考试有20题,班级有150多个学生。每答对一题得两分,答错一题扣一分,不答题得零分。编写一个处理测试数据的程序。输出应该是学生的 ID,然后是答案,然后是考试成绩,然后是考试成绩。假设以下年级:

90%–100%, A; 80%–89.99%,乙; 70%–79.99%,C; 60%–69.99%,D;和 0%–59.99%,F.

代码

// Chap9BBProg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int stux;
char stuGrade;
int correctAnswers(char[], char[]);
char studentGrade(int score);
char ansKey[10];
char stuA[10];
int main()
{
    ifstream inFile;
    ofstream outFile;
    inFile.open("TFInput.txt");
    outFile.open("TFOutput.txt");
    double score;
    char grade;
    string key;
    string studentID;
    string stuAnswers;
    getline(inFile, key);
    outFile << "The correct answers are " << key << endl << endl;
    while (getline(inFile, studentID, ' '))
    {
        outFile << studentID << " ";
        getline(inFile, stuAnswers);
        stux = studentGrade(stux);
        outFile << " " << stuAnswers << endl;
    }
    return 0;
}
int correctAnswers(char answerKey[], char studentAnswers[])
{
    int i;
    int tempscore;
    for (i = 0; i < 22; i++)
    {
        if (answerKey[i] == studentAnswers[i])
        {
            tempscore += 2;
        }
        else if (studentAnswers[i] == ' ')
        {
            tempscore += 0;
        }
        else
        {
            tempscore -= 1;
        }
    }
    cout << tempscore << endl;
    return tempscore;
}
char studentGrade(int x)
{
    int i;
    double score = 0;
    char grade = ' ';
    score = x / 40.0 * 100;
    for (i = 0; i < 30; i++)
    {
        if (score >= 90)
            grade = 'A';
        else if (score < 90 && score > 79)
            grade = 'B';
        else if (score <= 79 && score > 69)
            grade = 'C';
        else if (score <= 69 && score > 60)
            grade = 'D';
        else if (score <= 59)
            grade = 'F';
    }
    return grade;
}

【问题讨论】:

  • 顺便提一下,这是输入信息:
  • TTFTFTTTFTFTFFTTFTTF ABC54102 T FTFTFTTTFTTFTTF TF DEF56278 TTFTFTTTFTFTFFTTFTTF ABC42366 TTFTFTTTFTFTFFFTF ABC42586 TTTTFTTT TFTFFFTF
  • 我的主要问题是我很难将包含数组的函数连接到主函数。 -- 这并没有关注问题所在。此外,如果您在 C++ 的某些方面遇到问题,您应该编写一个小程序,可能 5 或 6 行,展示您遇到的问题。无需发布您的整个作业。
  • 对不起,不习惯使用本网站。我很难将数据从函数 correctAnswers() 传输到主函数的末尾。我还需要打印等级值。任何帮助将不胜感激。
  • 当只有 20 个问题(不是 22 个)时,为什么要从 0 循环到 21?还有一点是你的成绩计算不正确。试试if (score &gt;= 90) ... else if (score &gt;= 80) ... else ... .. 你到底为什么要从 0 循环到 29 反复计算成绩?!

标签: c++ arrays algorithm


【解决方案1】:

注意到一些小问题,例如在函数 correctAnswers() 中,变量 tempscore 未初始化,并且注意到 char[] 和字符串之间的函数参数冲突。

int stux;
char stuGrade;
int correctAnswers(string, string);
char studentGrade(int score);
char ansKey[10];
char stuA[10];
int main()
{
    ifstream inFile;
    ofstream outFile;
    inFile.open("TFInput.txt");
    outFile.open("TFOutput.txt");
    double score;
    string key;
    string studentID;
    string stuAnswers;
    getline(inFile, key);
    outFile << "The correct answers are " << key << endl << endl;
    while (getline(inFile, studentID, ' '))
    {
        outFile << studentID << " ";
        getline(inFile, stuAnswers);
        score = correctAnswers(key, stuAnswers);  //Changed here
        stuGrade = studentGrade(score);  //Changed here
        outFile << " Score: " << score <<" Grade: " << stuGrade << endl;  //Changed here
    }
    return 0;
}

int correctAnswers(string answerKey, string studentAnswers) //Changed here Array to string
{
    int i;
    int tempscore = 0; //Changed here Initialized to 0
    for (i = 0; i < 21; i++)  //Changed 22 to 21 here
    {
        if (answerKey[i] == studentAnswers[i])
        {
            tempscore += 2;
        }
        else if (studentAnswers[i] == ' ')
        {
            tempscore += 0;
        }
        else
        {
            tempscore -= 1;
        }
    }
    cout << tempscore << endl;
    return tempscore;
}

char studentGrade(int x)
{
    int i;
    double score = 0;
    char grade = ' ';
    score = x / 40.0 * 100;

    if (score >= 90)
        grade = 'A';
    else if (score < 90 && score > 79)
        grade = 'B';
    else if (score <= 79 && score > 69)
        grade = 'C';
    else if (score <= 69 && score > 60)
        grade = 'D';
    else if (score <= 59)
        grade = 'F';

    return grade;
}

【讨论】:

  • @drescherjm:我没有碰 studentGrade。已编辑。谢谢。
猜你喜欢
  • 2018-09-05
  • 2020-10-18
  • 2019-12-04
  • 2020-06-27
  • 2010-09-11
  • 2018-01-21
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多