C ++分数计算器

时间:2015-03-04 19:51:51

标签: c++

这是一个用于加,减,乘或除分数的C ++程序。我的程序没有编译器错误,但是当我运行它时,我从菜单中输入一个选项并立即得到输出“答案是: - 2/1409189243”。谁能告诉我为什么会这样?任何帮助表示赞赏。我是初学者。

#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;

int menu (int);
int addFractions(int, int, int, int, int, int);
int subtractFractions(int, int, int, int, int, int);
int multiplyFractions(int, int, int, int, int, int);
int divideFractions(int, int, int, int, int, int);

int main()
{
    int operation;
    int n1, d1, n2, d2, n3, d3;

    menu(operation);

    if (operation == 1)
        addFractions(n1, d1, n2, d2, n3, d3);
    if (operation == 2)
        subtractFractions(n1, d1, n2, d2, n3, d3);
    if (operation == 3)
        multiplyFractions(n1, d1, n2, d2, n3, d3);
    if (operation == 4)
        divideFractions(n1, d1, n2, d2, n3, d3);


    cout << "The answer is: " << n3 << "/" << d3;

    _getch();
    return 0;
}

int menu(int operation)
{
    cout << "This program allows the user to enter two fractions and perform";
    cout << " one of four \n arithmetic operations: addition, subtraction, ";
    cout << "multiplication, division. Please decide which operation you ";
    cout << "would like to perform. Enter '1' to add, '2' to \n subtract, '3' ";
    cout << "to multiply, or '4' to divide. \n";
    cin >> operation;
    cout << endl;

    return operation;
}

int addFractions(int n1, int d1, int n2, int d2, int n3, int d3)
{
    cout << "Enter 2 fractions to be divided. Enter one integer at a time, ";
    cout << "each divided by a space. First, enter numerator 1, then";
    cout << " denominator 1, followed by numerator 2, then denominator 2.";
    cin >> n1 >> d1 >> n2 >> d2;
    cout << endl;

    n3 = ((n1*d2) + (n2*d1)) / (d1* d2);
    d3 = d1 * d2;

    return n3, d3;

}

int subtractFractions(int n1, int d1, int n2, int d2, int n3, int d3)
{
    cout << "Enter 2 fractions to be divided. Enter one integer at a time, ";
    cout << "each divided by a space. First, enter numerator 1, then";
    cout << " denominator 1, followed by numerator 2, then denominator 2.";
    cin >> n1 >> d1 >> n2 >> d2;
    cout << endl;

    n3 = (n1*d2) - (n2*d1);
    d3 = d1 * d2;

    return n3, d3;
}

int multiplyFractions(int n1, int d1, int n2, int d2, int n3, int d3)
{
    cout << "Enter 2 fractions to be divided. Enter one integer at a time, ";
    cout << "each divided by a space. First, enter numerator 1, then";
    cout << " denominator 1, followed by numerator 2, then denominator 2.";
    cin >> n1 >> d1 >> n2 >> d2;
    cout << endl;

    n3 = n1 * n2;
    d3 = d1 * d2;

    return n3, d3;
}

int divideFractions(int n1, int d1, int n2, int d2, int n3, int d3)
{
    cout << "Enter 2 fractions to be divided. Enter one integer at a time, ";
    cout << "each divided by a space. First, enter numerator 1, then";
    cout << " denominator 1, followed by numerator 2, then denominator 2.";
    cin >> n1 >> d1 >> n2 >> d2;
    cout << endl;

    n3 = n1 * d2;
    d3 = n2 * d1;

    return n3, d3;
}

1 个答案:

答案 0 :(得分:2)

你不能return n3, d3;。如果您将该函数声明为int foo(...,则必须返回单个int

如果您的前4个参数是输入,最后两个是输出,请将功能更改为

int subtractFractions(int n1, int d1, int n2, int d2, int& n3, int& d3)

请注意,最后两个参数是通过引用而不是按值传递的。因此,如果在函数内修改它们,修改后的值将反映在函数外部。

此外,如果函数返回一个值,您应该使用赋值来获取该变量。

int menu()
{
    int oper;
    // get operation from user
    return oper
}

之后

int operation = menu();  // call the function and get return value