在不知道将回答多少问题的情况下跟踪程序中的答案

时间:2014-01-27 06:13:03

标签: c++ visual-studio-2010 loops computer-science

我一直试图弄清楚如何显示“你正确回答了8个问题中的7个。你的得分为95.5%,等级为A.”在我的程序中。我写了大部分内容,但现在花了一些时间在互联网上寻找方法。我不熟悉数组。

我的程序是一个分数计算器,应该计算基本操作(+, - ,*,除法),并且当我输入%作为我的哨兵值时应该退出。

这是我到目前为止所写的内容。任何反馈都非常感谢。在不知道用户重复该过程(程序)的次数的情况下,如何正确回答问题的数量,我将如何跟踪某些事情?这就是我到目前为止所做的:

#include <iostream>
#include <cstdlib>

using namespace std;

enum MenuSelection {
    NONE=0,
    ADD=1,
    SUBTRACT=2,
    MULTIPLY=3,
    DIVIDE=4,
    QUIT=5,
    END=6,
};

int menu() {
    int MenuSelection=0;
    do {
        cout<<"\n1) Add"<<endl;
        cout<<"2) Subtract"<<endl;
        cout<<"3) Multiply"<<endl;
        cout<<"4) Divide"<<endl;
        cout<<"5) %"<<endl;
        cin>>MenuSelection;
        //
        if (MenuSelection<=NONE||MenuSelection>=END)
            cout<<"Please pick a number from 1-5 as shown in the menu selection."<<endl;
        if (MenuSelection==QUIT) {
            cout<<"You have chosen to exit the program"<<endl;
            cout<<"You have answered \n";
            exit(1);
        }
    }
    while (MenuSelection<=NONE||MenuSelection>=END);
    //
    return MenuSelection;
}

void addFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result) {        //
    NUM_result=(num1* den2)+(num2*den1);
    //
    DEN_result=den1*den2;
}

void subFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result) {
    NUM_result=(num1*den2)-(num2*den1);
    //
    DEN_result=den1*den2;
    //
}

void multiplyFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result) {
    NUM_result=num1*num2;
    //
    DEN_result=den1* den2;
}

void divideFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result) {            //
    NUM_result=num1*den2;
    //
    DEN_result=den1*num2;
}

void input_values_FROM_USER(int &num1, int &num2, int &den1, int &den2)
{
    cout<<endl;
    cout<<"Enter the numerator for first fraction-->";
    cin>>num1;
    cout<<"Enter denominator for first fraction--> ";
    cin>>den1;
    cout<<"Enter numerator for second fraction--> ";
    cin>>num2;
    cout<<"Enter denominator for second fraction--> ";
    cin>>den2;
    cout<<"-------------------------------------------\n\n\n";
}

void outputResults(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result, int operation) {
    if (operation==ADD) {
        cout<<"The sum of the two is--> "<<num1<<'/'<<den1<<'+'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
    }
    if (operation==SUBTRACT) {
        cout<<"The difference of the two is--> "<<num1<<'/'<<den1<<'-'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
    }
    if (operation==MULTIPLY) {
        cout<<"The product of the two is--> "<<num1<<'/'<<den1<<'*'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
    }
    if (operation==DIVIDE) {
        cout<<"The quotient of the two is--> "<<num1<<'/'<<den1<<'/'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
    }
}

void main() {
    int MenuSelection=NONE;
    int num1=0, num2=0, den1=0, den2=0;
    int NUM_result=0, DEN_result=0;
    cout<<"Author: Jose Soto."<<endl;
    cout<<"CSCI 110-- Jose Soto's Project 2 (Basic Fraction Arithmetic)\n\n"<<endl;
    do {
        MenuSelection=menu();
        input_values_FROM_USER(num1, num2, den1, den2);
        if (MenuSelection==ADD)
        {
            addFractions(num1, num2, den1, den2, NUM_result, DEN_result);
        }
        if (MenuSelection==SUBTRACT) {
            subFractions(num1, num2, den1, den2, NUM_result, DEN_result);
        }
        if (MenuSelection==MULTIPLY) {
            multiplyFractions(num1, num2, den1, den2, NUM_result, DEN_result);
        }
        if (MenuSelection==DIVIDE) {
            divideFractions(num1, num2, den1, den2, NUM_result, DEN_result);
        }
        outputResults(num1, num2, den1, den2, NUM_result, DEN_result, MenuSelection);
    }
    while (MenuSelection!=QUIT);
    cin.get();
}

0 个答案:

没有答案