C++除法。看似简单的事情让我抓狂了,求建议。

4

好的,我已经学习编程一个星期了,开始使用c++。我正在编写一个算术训练程序,您可以输入要求解的方程数量,输入随机数生成器的限制,指定您想要的方程类型(/*-+),然后程序使用for循环遍历并生成方程和它们的答案存储在一个变量中,然后检查用户输入是否与此变量匹配,如果匹配则增加另一个变量以计算正确答案的数量。最后一个方程结束后,程序告诉用户他们在多少个方程中回答正确,并通过将正确答案的数量除以问题的数量然后将该值乘以100来获得此用户算术会话的准确性百分比。问题是c ++一直返回给我0值,我无法理解为什么c ++会这样。

整个程序:

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
void menu(void);
class session{
public:
session(){
            create_session();
        }
        void create_session(void){
        amount = 0;
        range_limit = 0;
        rights = 0;
        answer = 0;
        input = 0;
        type = "";
        while(amount == 0){
            cout << "\nHow many equations do you want?: "; cin >> amount;
            if(amount < 1){
                cout << "\nAmount is too low!";
                amount = 0;
            }
        }
        while(range_limit == 0){
            cout << "Enter the number range limit: "; cin >> range_limit;
            if(range_limit < 1){
                cout << "\nRange limit too low!";
                range_limit = 0;
            }
        }
        while(type == ""){
            cout << "What equation type do you want?: "; cin >> type;
            int strlen = type.size();
            if(strlen < 1){
                cout << "Invalid type input!";
                type = "";
            }
        }
        if(type == "+"){
            for(int i=0;i<amount;i++){
                int a = random();
                int b = random();
                answer = a + b;
                cout << "\n" << a << " + " << b << " = "; cin >> input;
                if(answer == input){
                    rights++;
                }
            }
        }
cout << "\nYou got " << rights << " answers right out of " << amount <<         " equations." << endl;
        cout << "Accuracy percentage: " << getAccuracy() << "%" << endl;
        int post_menu=0;
        while(post_menu == 0){
            cout << "Enter 1 to create another session or 2 to return to the menu: ";
            cin >> post_menu;
            if(post_menu == 1){
                create_session();
            }else if(post_menu == 2){
                menu();
            }else{
                cout << "Invalid input: ";
                post_menu = 0;
            }
        }
    }
    float getAccuracy(){
           float x = (rights/amount)*100;
           return x;
    }
    int random(){
        int x = 1+(rand()%range_limit);
        return x;
    }
    void set_amount(int a){
        amount = a;
    }
    void set_range_limit(int r){
        range_limit = r;
    }
    void set_rights(int R){
        rights = R;
    }
    void set_answer(int a){
        answer = a;
    }
    void set_input(int i){
        input = i;
    }
    void set_type(string t){
        type = t;
    }
private:
    int amount;
    int accuracy;
    int range_limit;
    int rights;
    int answer;
    int input;
    string type;

};
int main(){
cout << "=== WELCOME TO ARITH! === \n=========================\n";
menu();
return 0;
}

void menu(void){
//Set the seed for random number gen.
srand(time(0));

 //Set var for getting menu input, then get the menu input..
 int menu_input;
 cout << "\n[1]Create a Session. [2]Exit Arith. \nWhat would you like to do?: ";
 cin >> menu_input;

 //Now we check what the user wants and act accordingly..
 if(menu_input > 2){
    cout << "error";
    menu_input=0;
 }else if(menu_input == 1){
     session start;
 }else if(menu_input == 2){
    cout << "\nExiting Arith!";
 }else{
    cout << "error";
    menu_input=0;
 }
}

令人头疼的部分:

    float getAccuracy(){
           float x = (rights/amount)*100;
           return x;

一些程序返回0%。 有人知道为什么,以及如何获得我想要的结果。

8
整数除法是指将一个整数被另一个整数除的运算。如果两个整数恰好能整除,则结果为整数,否则结果为带余数的整数或者浮点数。在大多数编程语言中,使用符号“/”表示普通除法,而使用符号“//”表示整数除法。 - Mysticial
1
良好的问题提问方式加1分。 - jcoder
2个回答

4

rightsamount都是int类型,所以当你做除法运算时,结果会向下取整。例如,如果你计算5/2,答案将是2而不是2.5。为了解决这个问题,你需要将其中一个变量转换成float类型,像这样:(float(rights)/amount) * 100


3
整数除法不会给出结果的向下取整,而是向零舍入。例如,(-1)/2 == 0,但是 floor((-1.0)/2.0) == -1.0 - Dietrich Epp
负数的除法结果取决于编译器。标准要求(C++ 2003年第5.6.4节)不超过(a/b)*b + a%b == a,余数可以是正数或负数,因此除法的结果可能向零舍入或向下取整。 - n0rd

1

当两个整数进行除法运算时,即使使用临时变量,结果也将为整数。因此,您可以将任何一个变量转换为浮点型或双精度型或强制转换。

您只需要转换其中一种数据类型,因为另一种会隐式地提升类型。

float x = ((double)rights/amount)*100;

或者您可以默认使您的金额变量浮动,如果它不影响代码的其他部分。

此外,您可以选择进行静态转换:

float x = (static_cast<double>(rights)/amount)*100;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接