如何将一个分数分成两个整数c ++

时间:2016-10-12 22:45:46

标签: c++

我正在完成作业。我需要让用户输入#/#格式的分数。如何将顶部和底部设置为两个单独的变量?

这是我尝试过的一大块代码,但我对第二个变量一无所知:

#include <iostream>
#include <conio.h>
#include <cstdio>
#include <regex>

using namespace std;

int main() {
    string firstFraction;

    cout << "Enter your first real Fraction: " << endl;
    firstFraction = cin.get();

    string delimiter = "/";

    string numerator = firstFraction.substr(0,firstFraction.find(delimiter));

    size_t pos = firstFraction.find("/");
    string denominator = firstFraction.substr(pos); 

    cout << numerator << " / " << denominator << endl;
    _getch();
    return 0;
}

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    string fraction;

    cout << "Enter your first real Fraction: " << endl;
    getline(cin, fraction);

    istringstream iss(fraction);

    string numerator, denominator;
    getline(iss, numerator, '/');
    getline(iss, denominator);

    cout << numerator << " / " << denominator << endl;

    cin.get();
    return 0;
}

答案 1 :(得分:0)

firstFraction = cin.get();

在此行后打印firstFraction,看看它是否包含您认为包含的内容。看看the reference(你应该做的事情!)......

  

[..]读取一个字符并返回(如果可用)。 [..]

...我们了解到您只是阅读单个字符(未格式化的输入)。你打算如何拆分结果(单个字符)字符串?

有多种方法可以正确地进行,可供选择的方法很大程度上取决于您的需求。一个简短的,不完整的清单:

  • std::getline整行,然后在其中搜索/
  • std::getline直到下一个/std::getline直到行尾。 (我不是真的推荐这个)
  • 格式化输入,for example

    #include <iostream>
    using namespace std;
    
    int main() {
        unsigned int nominator, denominator;
        char sep;
        cin >> nominator >> sep >> denominator;
    
        if (!cin || sep != '/') {
            cerr << "Well... you know, that failed somehow." << endl;
            return 1;
        }
        cout << "Fraction: " << nominator << "/" << denominator << endl;
        return 0;
    }
    

    虽然这也允许输入

    3   / 4
    

    3
    /
    4
    

当然,你应该抽象一下,例如创建一个fraction类,并编写一个(成员)函数read_fraction(如果需要,还可以提供合适的operator>>

答案 2 :(得分:0)

这可以像

一样简单
#include <iostream>
#include <sstream>
#include <string>
//using namespace std; dangerous! Use with caution

int main()
{
    int num; // want a number as numerator
    int denom; // and a number as denomenator
    char divsign; // and a character to hold the / 

    std::cout << "Enter your first real Fraction: " << std::endl;
    // user input contains at least a numerator a / and a denominator
    // anything less fails. anything more will slip through. If this is a
    // problem, add another >> to see if there is more in the stream
    if (std::cin >> num >> divsign >> denom && // all input read successfully
        divsign == '/') // and the division operator was present
    { // got what we need. Print it.
        std::cout << num << " / " << denom << std::endl;
    }
    else
    { // bad input. insult user.
        std::cout << "Bogus user input. No fraction for you" << std::endl;
    }
    return 0;
}

它有许多潜在的失败案例,例如:

999999999999 / 2
Bogus user input. No fraction for you

整数溢出。输入太大了。和

1/1dfjklghaljkgadlfhjgklahd
1 / 1

最后一个角色后的废话