如何在此C ++代码中计算正确答案

我正在尝试运行此代码,它将在其中计算用户做出的正确答案。但是我不知道如何,我想应该使用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("Thankyou for answering the following question");
p("\nCalculating correct answers. Please wait .......");
delay(5000);
p("\nYout total correct answer is : ");
p("\n\t\tEnd of the Program");

getche();
}
tiancai61 回答:如何在此C ++代码中计算正确答案

这里是一种使用结构数组的方法。 这样,您可以在同一位置保留尽可能多的参数(与问题相关):

#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;
}
,

我建议使用有序地图数据结构。这由键和值对组成。如果您使用的是标准库,则它具有一个有序地图数据结构,该结构与基础红黑树一起运行以进行数据存储。

以您的示例为例,您可以简单地使用问题编号(或任何组合)作为关键字,并将得到的答案用作值。

因此,要检查学生是否正确回答了问题,您需要输入问题关键字进行查找,然后返回正确的答案值。然后,您可以将该值与所选答案进行比较。

Here is the documentation for the standard library ordered map data structure

可以轻松地将您所布置的数据预先填充到地图中。只需创建一个函数即可解析您的文本文件,并创建一组插入到Map中的键/值对。对于程序员来说,这是一种非常常见的用法模式。许多软件工程公司添加了PopulateValuesFromFile(...)之类的功能来填充数据结构的数据。这也使代码看起来更加简洁,因为其中没有很多硬编码的字符串。

本文链接:https://www.f2er.com/2954982.html

大家都在问