我是C ++初学者。我只想检查学生对一组答案键的答案。可以说,我有一个学生,被这样声明:
//Declare and array of student answers;
string student1 [] = {"A", "B", "C", "A", "B","C","A","B","A","A"};
答案键的声明如下:
//Declare an array set of answer key
string keys [] = {"A", "B", "C", "A", "B","C","A","B","A","A"};
想象答案键是从1到10的正确答案。然后我要检查学生的答案是否与声明的答案键匹配:
for(const string &key : keys){
for(const string &answer : answers){
if(key == answer){
cout << "Correct" << endl;
}else{
cout << "Wrong" << endl;
}
}
}
我的第一个问题是,它给我以下结果:
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
第二个是,我想再增加5个学生来检查他们的答案。谢谢。
答案 0 :(得分:2)
尝试使其成为一个循环:
for(int i = 0;i < 10;i++)
{
if (student1[i] == keys[i])
{
cout << "Correct" << endl;
}
else
{
cout << "Wrong" << endl;
}
}
第二个,只需要排列一组学生:
string students[5][10] = {{...},{...},{...},{...},{...}};
添加新循环:
for (int j = 0; j < 5; j++)
并更改if语句:
if (students[j][i] == keys[i])
希望您能理解
答案 1 :(得分:1)
让我先检查一下您想要的东西。您有一组学生和一个带有正确答案的测试。每个学生都有特定的答案列表。 您只需要将学生的答案与主要答案或正确答案进行比较即可。
在您的示例中,我们从student1开始。我们有十个问题,而student1的所有答案都是正确的,那么为什么您的代码显示的行数超过十行?
您只需要一个就创建了两个for循环。
您可以这样进行:
for (int i=0;i<10;i++) {
if (student1[i] == keys[i]) {
std::cout << "Correct " <<student1[i]<<" "<< keys[i]<< std::endl;
}else std::cout<< "Wrong" <<std::endl;
}
您只需要比较第一个数组(学生1)中的答案i和第二个数组中相同位置i中的关键答案(键)。除非我确实了解错误,否则不需要进行两个for循环。
对于第二个问题,这是一个模糊的问题。根据您当前在C / C ++中的级别,您将有很多。
如果您想增加更多的学生,只需声明更多 学生喜欢您对student1所做的事情:
std::string student1[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };
std::string student2[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
std::string student3[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
//...
然后您可以定义一个数组作为学生列表。
string list[3][10] = {student1,student2,student3};
以下是测试三个学生成绩的代码:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
void showResult(const string student1[],const string keys[]){
for (int i=0;i<10;i++) {
if (student1[i] == keys[i]) {
std::cout << "Correct " << std::endl;
}else std::cout<< "Wrong" <<std::endl;
}
}
int main(int argc, char** argv) {
std::string student1[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };
std::string student2[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
std::string student3[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
string list[3][10] = {student1,student2,student3};
std::string keys[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };
for(int i=0;i<3;i++){
cout<<"student "<<i+1<<endl;
for(int j=0;j<10;j++) cout<<list[i][j]<<" ";
cout<<endl;
showResult(list[i],keys);
cout<<endl;
}
return 0;
}
否则,如果您想获得更多动态效果,则应尝试使用std :: vector中的Vectors。 C ++中的向量比经典数组具有更多的用途。您可以在以下主题中阅读有关此主题的更多信息:
答案 2 :(得分:0)
您可以使用for循环来执行此操作,其他人已经展示了这一点,但是您也可以使用std :: transform和lambda来执行此操作,只是为了好玩。
#include <algorithm>
[...]
std::vector<string> correctness;
std::transform(student1,student1+10,keys,std::back_inserter(v),[](const string& a,const string& b){if (a==b) return "Correct"; else return "Wrong";});
for (const auto& ans : correctness) std::cout << ans << endl;