如何计算此 C++ 代码中的正确答案
How to count the correct answers in this C++ code
我正在尝试 运行 这段代码,它将计算用户做出的正确答案的数量。
但我不知道怎么做。我想我应该使用 if
或嵌套的 if
来做到这一点。我该怎么做?
这将要求用户输入他们的正确答案 A、B、C 或 D。然后我想递增 n:
#include <stdio.h>
#include <conio.h>
#define p printf
#define s scanf
main () {
int Ttl;
char Ln, Mi, Fn,
A1, A2, A3, A4, A5, A6, A7, A8, A9, A10,
A11, A12, A13, A14, A15, A16, A17, A18, A19, A20;
clrscr();
p("\n)
p("\n\nEnter your last name: ");
s("%s", &Ln);
p("\nEnter your middle initial: ");
s("%s", &Mi);
p("\nEnter your first name: ");
s("%s", &Fn);
p("\nType the corresponding letter to each question.");
p("\n1.) Shortcut for'New Line'");
p("\n A. Ctrl + R C. Ctrl + M ");
p("\n B. Ctrl + QC D. Ctrl + KC ");
p("\nAnswer: ");
s("%s", A1);
p("\n2.) Shortcut for'Up Arrow Key'");
p("\n A. Ctrl + Z C. Ctrl + X ");
p("\n B. Ctrl + QC D. Ctrl + E ");
p("\nAnswer: ");
s("%s", A2);
p("\n3.) Shortcut for'Down Arrow Key'");
p("\n A. Ctrl + X C. Ctrl + E ");
p("\n B. Ctrl + C D. Ctrl + R ");
p("\nAnswer: ");
s("%s", A3);
p("\n4.) Shortcut for'Scroll Down'");
p("\n A. Ctrl + X C. Ctrl + R ");
p("\n B. Ctrl + E D. Ctrl + Z ");
p("\nAnswer: ");
s("%s", A4);
p("\n5.) Shortcut for'Page Up'");
p("\n A. Ctrl + K C. Ctrl + R ");
p("\n B. Ctrl + Q D. Ctrl + C ");
p("\nAnswer: ");
s("%s", A5);
p("\n6.) Shortcut for'Page Down'");
p("\n A. Ctrl + Q C. Ctrl + K ");
p("\n B. Ctrl + C D. Ctrl + Z ");
p("\nAnswer: ");
s("%s", A6);
p("\n7.) Shortcut for'Top of a File'");
p("\n A. Ctrl + KY C. Ctrl + KC ");
p("\n B. Ctrl + QC D. Ctrl + QR ");
p("\nAnswer: ");
s("%s", A7);
p("\n8.) Shortcut for'Bottom of a File'");
p("\n A. Ctrl + QC C. Ctrl + KY ");
p("\n B. Ctrl + QS D. Ctrl + KC ");
p("\nAnswer: ");
s("%s", A8);
p("\n9.) Shortcut for'Copy Block'");
p("\n A. Ctrl + QY C. Ctrl + KC ");
p("\n B. Ctrl + KY D. Ctrl + QC ");
p("\nAnswer: ");
s("%s", A9);
p("\n10.) Shortcut for'Delete Block'");
p("\n A. Ctrl + QR C. Ctrl + QC ");
p("\n B. Ctrl + KC D. Ctrl + KY ");
p("\nAnswer: ");
s("%s", A10);
p("\n11.) Hot Keys Command 'Saves the file currently being edited'");
p("\n A. F6 C. F2 ");
p("\n B. F4 D. F9 ");
p("\nAnswer: ");
s("%s", A11);
p("\n12.) Hot Keys Command'Loads a File'");
p("\n A. F7 C. F1");
p("\n B. F3 D. F8");
p("\nAnswer: ");
s("%s", A12);
p("\n13.) Hot Keys Command'Compiles and link the program'");
p("\n A. F4 C. F3 ");
p("\n B. F2 D. F9 ");
p("\nAnswer: ");
s("%s", A13);
p("\n14.) Hot Keys Command'Compiles file to .obj file'");
p("\n A. Alt + F9 C. Alt + F5 ");
p("\n B. Alt + F2 D. Alt + F7 ");
p("\nAnswer: ");
s("%s", A14);
p("\n15.) Hot Keys Command'Next error'");
p("\n A. Alt + F4 C. Alt + F9 ");
p("\n B. Alt + F7 D. Alt + F8 ");
p("\nAnswer: ");
s("%s", A15);
p("\n16.) Hot Keys Command'Previous error'");
p("\n A. Alt + F7 C. Alt + F9 ");
p("\n B. Alt + F3 D. Alt + F1 ");
p("\nAnswer: ");
s("%s", A16);
p("\n17.) Hot Keys Command'Activates the file menu'");
p("\n A. Alt + E C. Alt + C ");
p("\n B. Alt + F D. Alt + O ");
p("\nAnswer: ");
s("%s", A17);
p("\n18.) Hot Keys Command'Quit the Turbo C program'");
p("\n A. Alt + D C. Alt + P ");
p("\n B. Alt + C D. Alt + X ");
p("\nAnswer: ");
s("%s", A18);
p("\n19.) Hot Keys Command'Runs the program'");
p("\n A. Ctrl + F3 C. Ctrl + F2 ");
p("\n B. Ctrl + F5 D. Ctrl + F9 ");
p("\nAnswer: ");
s("%s", A19);
p("\n20.) Hot Keys Command'Switches between windows'");
p("\n A. F2 C. F6 ");
p("\n B. F4 D. F8 ");
p("\nAnswer: ");
s("%s", A20);
p("Thank you for answering the following question");
p("\nCalculating correct answers. Please wait...");
delay(5000);
p("\nYour total correct answer is: ");
p("\n\t\tEnd of the Program");
getche();
}
我建议使用有序映射数据结构。这由 key 和 value 对组成。如果您使用的是标准库,它有一个有序的地图数据结构,该结构与用于数据存储的底层 red-black tree 一起运行。
对于您的示例,您可以简单地使用问题编号(或任何组合)作为键,并将结果答案作为值。
因此,要检查学生是否正确回答了问题,您需要输入问题键进行查找,然后 return 输入正确的答案值。然后您可以将此值与所选答案进行比较。
Here is the documentation for the standard library ordered map data structure
地图可以很容易地预先填充您布置的数据。只需创建一个函数来解析您的文本文件并创建一组 key/value 对插入到地图中。这是程序员非常普遍的使用模式。许多软件工程公司添加了诸如 PopulateValuesFromFile(...) 之类的函数来为数据结构填充数据。它还使代码看起来更清晰,因为它没有填充大量硬编码字符串。
这是一种使用结构数组的方法。这样你就可以在同一个地方保留尽可能多的参数(与问题相关):
#include <stdio.h>
typedef struct {
const char *question;
int correct_answer;
int given_answer;
} question_t;
int main() {
question_t questions[] = {
{"how many wings does bird have?\n1) 55\t3) 3\n2) 5\t4) 2", 4},
{"how many heads does frog have?\n1) 55\t3) 3\n2) 1\t4) 2", 2},
{NULL},
};
int number_of_corrent_unswers = 0;
for(int i=0; questions[i].question != NULL; i++) {
puts(questions[i].question);
scanf("%d", &questions[i].given_answer);
if(questions[i].given_answer == questions[i].correct_answer) {
number_of_corrent_unswers++;
}
}
printf("number of correct answers: %d\n", number_of_corrent_unswers);
return 0;
}
我正在尝试 运行 这段代码,它将计算用户做出的正确答案的数量。
但我不知道怎么做。我想我应该使用 if
或嵌套的 if
来做到这一点。我该怎么做?
这将要求用户输入他们的正确答案 A、B、C 或 D。然后我想递增 n:
#include <stdio.h>
#include <conio.h>
#define p printf
#define s scanf
main () {
int Ttl;
char Ln, Mi, Fn,
A1, A2, A3, A4, A5, A6, A7, A8, A9, A10,
A11, A12, A13, A14, A15, A16, A17, A18, A19, A20;
clrscr();
p("\n)
p("\n\nEnter your last name: ");
s("%s", &Ln);
p("\nEnter your middle initial: ");
s("%s", &Mi);
p("\nEnter your first name: ");
s("%s", &Fn);
p("\nType the corresponding letter to each question.");
p("\n1.) Shortcut for'New Line'");
p("\n A. Ctrl + R C. Ctrl + M ");
p("\n B. Ctrl + QC D. Ctrl + KC ");
p("\nAnswer: ");
s("%s", A1);
p("\n2.) Shortcut for'Up Arrow Key'");
p("\n A. Ctrl + Z C. Ctrl + X ");
p("\n B. Ctrl + QC D. Ctrl + E ");
p("\nAnswer: ");
s("%s", A2);
p("\n3.) Shortcut for'Down Arrow Key'");
p("\n A. Ctrl + X C. Ctrl + E ");
p("\n B. Ctrl + C D. Ctrl + R ");
p("\nAnswer: ");
s("%s", A3);
p("\n4.) Shortcut for'Scroll Down'");
p("\n A. Ctrl + X C. Ctrl + R ");
p("\n B. Ctrl + E D. Ctrl + Z ");
p("\nAnswer: ");
s("%s", A4);
p("\n5.) Shortcut for'Page Up'");
p("\n A. Ctrl + K C. Ctrl + R ");
p("\n B. Ctrl + Q D. Ctrl + C ");
p("\nAnswer: ");
s("%s", A5);
p("\n6.) Shortcut for'Page Down'");
p("\n A. Ctrl + Q C. Ctrl + K ");
p("\n B. Ctrl + C D. Ctrl + Z ");
p("\nAnswer: ");
s("%s", A6);
p("\n7.) Shortcut for'Top of a File'");
p("\n A. Ctrl + KY C. Ctrl + KC ");
p("\n B. Ctrl + QC D. Ctrl + QR ");
p("\nAnswer: ");
s("%s", A7);
p("\n8.) Shortcut for'Bottom of a File'");
p("\n A. Ctrl + QC C. Ctrl + KY ");
p("\n B. Ctrl + QS D. Ctrl + KC ");
p("\nAnswer: ");
s("%s", A8);
p("\n9.) Shortcut for'Copy Block'");
p("\n A. Ctrl + QY C. Ctrl + KC ");
p("\n B. Ctrl + KY D. Ctrl + QC ");
p("\nAnswer: ");
s("%s", A9);
p("\n10.) Shortcut for'Delete Block'");
p("\n A. Ctrl + QR C. Ctrl + QC ");
p("\n B. Ctrl + KC D. Ctrl + KY ");
p("\nAnswer: ");
s("%s", A10);
p("\n11.) Hot Keys Command 'Saves the file currently being edited'");
p("\n A. F6 C. F2 ");
p("\n B. F4 D. F9 ");
p("\nAnswer: ");
s("%s", A11);
p("\n12.) Hot Keys Command'Loads a File'");
p("\n A. F7 C. F1");
p("\n B. F3 D. F8");
p("\nAnswer: ");
s("%s", A12);
p("\n13.) Hot Keys Command'Compiles and link the program'");
p("\n A. F4 C. F3 ");
p("\n B. F2 D. F9 ");
p("\nAnswer: ");
s("%s", A13);
p("\n14.) Hot Keys Command'Compiles file to .obj file'");
p("\n A. Alt + F9 C. Alt + F5 ");
p("\n B. Alt + F2 D. Alt + F7 ");
p("\nAnswer: ");
s("%s", A14);
p("\n15.) Hot Keys Command'Next error'");
p("\n A. Alt + F4 C. Alt + F9 ");
p("\n B. Alt + F7 D. Alt + F8 ");
p("\nAnswer: ");
s("%s", A15);
p("\n16.) Hot Keys Command'Previous error'");
p("\n A. Alt + F7 C. Alt + F9 ");
p("\n B. Alt + F3 D. Alt + F1 ");
p("\nAnswer: ");
s("%s", A16);
p("\n17.) Hot Keys Command'Activates the file menu'");
p("\n A. Alt + E C. Alt + C ");
p("\n B. Alt + F D. Alt + O ");
p("\nAnswer: ");
s("%s", A17);
p("\n18.) Hot Keys Command'Quit the Turbo C program'");
p("\n A. Alt + D C. Alt + P ");
p("\n B. Alt + C D. Alt + X ");
p("\nAnswer: ");
s("%s", A18);
p("\n19.) Hot Keys Command'Runs the program'");
p("\n A. Ctrl + F3 C. Ctrl + F2 ");
p("\n B. Ctrl + F5 D. Ctrl + F9 ");
p("\nAnswer: ");
s("%s", A19);
p("\n20.) Hot Keys Command'Switches between windows'");
p("\n A. F2 C. F6 ");
p("\n B. F4 D. F8 ");
p("\nAnswer: ");
s("%s", A20);
p("Thank you for answering the following question");
p("\nCalculating correct answers. Please wait...");
delay(5000);
p("\nYour total correct answer is: ");
p("\n\t\tEnd of the Program");
getche();
}
我建议使用有序映射数据结构。这由 key 和 value 对组成。如果您使用的是标准库,它有一个有序的地图数据结构,该结构与用于数据存储的底层 red-black tree 一起运行。
对于您的示例,您可以简单地使用问题编号(或任何组合)作为键,并将结果答案作为值。
因此,要检查学生是否正确回答了问题,您需要输入问题键进行查找,然后 return 输入正确的答案值。然后您可以将此值与所选答案进行比较。
Here is the documentation for the standard library ordered map data structure
地图可以很容易地预先填充您布置的数据。只需创建一个函数来解析您的文本文件并创建一组 key/value 对插入到地图中。这是程序员非常普遍的使用模式。许多软件工程公司添加了诸如 PopulateValuesFromFile(...) 之类的函数来为数据结构填充数据。它还使代码看起来更清晰,因为它没有填充大量硬编码字符串。
这是一种使用结构数组的方法。这样你就可以在同一个地方保留尽可能多的参数(与问题相关):
#include <stdio.h>
typedef struct {
const char *question;
int correct_answer;
int given_answer;
} question_t;
int main() {
question_t questions[] = {
{"how many wings does bird have?\n1) 55\t3) 3\n2) 5\t4) 2", 4},
{"how many heads does frog have?\n1) 55\t3) 3\n2) 1\t4) 2", 2},
{NULL},
};
int number_of_corrent_unswers = 0;
for(int i=0; questions[i].question != NULL; i++) {
puts(questions[i].question);
scanf("%d", &questions[i].given_answer);
if(questions[i].given_answer == questions[i].correct_answer) {
number_of_corrent_unswers++;
}
}
printf("number of correct answers: %d\n", number_of_corrent_unswers);
return 0;
}