C 程序,用于分级多个True或False测试
C++ program for grading multiple True or False tests
问题:
我的主要问题是我很难将包含数组的函数连接到主要功能。
您学校的历史老师需要帮助分级 troo/false 测试。学生的ID和测试答案存储在文件中。文件中的第一个条目包含表格中的测试答案:
tfftffttttttfftftftt
文件中的其他每个条目都是学生ID,然后是空白,然后是学生的回答。例如,条目:
abc54301 tftfttt tftftfffttft
表示学生ID是 ABC54301 ,并且问题的答案1
是 true ,问题2
的答案是 false ,等等,所以在。这个学生没有回答问题9
。考试有20
问题,而且课程的学生不仅仅是150
学生。每个正确的答案都被授予两分,每个错误的答案都会扣除一分,而没有答案获得零分。编写一个处理测试数据的程序。输出应为学生的ID,其次是答案,然后是测试分数,然后是测试成绩。假设以下年级规模:
90%–100%,a;80%–89.99%,b;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;
}
EG在函数recripswers()中注意到的一些小问题,可变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;
}
相关文章:
- Arduino-C++ bool 不会从 false 变为 true
- 如何使用boost定义布尔类,可能的值应该是TRUE或FALSE?
- C++如果使用 lambda 表达式的语句返回 true,但输出来自 false,为什么
- MFC 的 OnInit() 函数中的返回 true 和返回 false 有什么区别
- std::is_array 当它应该返回 false 时返回 true
- 如果BOOL仅从false到true一次,我可以避免锁定布尔
- `如果constexpr(false)`始终评估为true
- 是否应在分配时将布尔值截断为 true 或 false
- 我想返回 True/False 关于值是否在一组值中
- 尝试将字符串变量转换为布尔值会导致 "true" 和 "false" 都等于 0
- C 程序,用于分级多个True或False测试
- true/false功能如果存在总和
- 查找n连续字符的函数返回true/false
- 我正在尝试创建一个函数,如果一个元素在向量中,则返回 true/false,但我收到错误
- 将 true/false 分配给 std::string:这是怎么回事?
- 如果数据类型为整数值或双数值,则当c++返回true/false时
- 如果某个条件语句中的某些内容导致该条件语句不再为true/false,则该语句是否停止执行
- bool递归函数中的true/false优先级
- c++ bool返回0 1而不是true false
- While循环为true false,不加增量