⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpquiz2.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <iostream>
#include <iomanip>      // for setw
#include <string>
using namespace std;
#include "randgen.h"    // for RandInt
#include "prompt.h"

// simple quiz program

int MakeQuestion()
// postcondition: creates a random question, returns the answer
{
    const int WIDTH = 7;
    RandGen gen;
    int num1 = gen.RandInt(10,20);
    int num2 = gen.RandInt(10,20);
    
    cout << setw(WIDTH) << num1 << endl;
    cout << "+" << setw(WIDTH-1) << num2 << endl;
    cout << "-------" << endl;
    
    return num1 + num2;
}

void GiveQuiz(string name, int & correct, int & total)
// precondition:  name = person taking the quiz
// postcondition: correct = # correct answers, total = # questions
{
    correct = 0;
    total = PromptRange(name + ", how many questions, ",1,10);
    int  answer,response, k;
    
    for(k=0; k < total; k++)
    {   answer = MakeQuestion();
        cout << "answer here: ";
        cin >> response;
        if (response == answer)
        {   cout << "correct! " << endl;
            correct++;
        }
        else
        {   cout << "incorrect, answer = " << answer << endl;
        }
    }
}

int main()
{
    int correctCount, total;
    string student = PromptString("what is your name? ");
    GiveQuiz(student, correctCount, total);
    int percent = double(correctCount)/total * 100;  
    cout << student << ", your score is " << percent << "%" << endl;
    
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -