0

I'm writing a rational class, where the following must be acceptable forms of input:

3/4, 1.4545, (7/8, (7/8), 7/8), or even ((((((5/4))), where my code should rewrite it as 5/4 for the constructor to accept.

At this point I am passing a string parameter into a constructor which calls on formatStringNumerator() and formatStringDenominator() respectively;

I haven't gotten to the decimals yet, but so far, inputs with a parenthesis to the left set the numerator value to 0 somehow. I believe the fault to be the "(" before the numbers. I've commented out my attempt at removing the parentheses to the left, but they just won't go away. I've attempted to use substr() as an alternative, but just got errors when I did. Also, when I print out the find() value I get some large number, [18446744073709551615] when the parameters cannot be found? I was under the impression it should return -1(false?) if not found.

At the first comment, the erase() ought to be deleting the first char in the string, being the parenthesis, but it just does not delete itself, and the conversion from string to int gets messy, defaulting it to 0 like I mentioned before?

int formatStringNumerator(string &rational){
    string tmp;
    int numerator;
//  if(rational.find("(") != false){
//      rational.erase(0, 1); 
//  }          //This should have deleted an open parenthesis
    if(rational.find("/") != false){
        tmp = rational.substr(0, rational.find("/"));
        cout << "\n" << tmp << endl; //cout to test if ran
    }        //Should assign numbers leading to the symbol("/") to tmp
    istringstream is;
    is.str(tmp);    //Numerator should have int value with parenthesis removed
    is >> numerator;
    return numerator;
}
int formatStringDenominator(string &rational){
    string tmp;
    int denominator;
    if(tmp.find("/") != false){
        tmp = rational.substr((rational.find("/")+1));
    }  //assign value after symbol to tmp
//  if(tmp.find(")") != false){
//      tmp.erase(tmp.find(")"));    //Successfully removes parenthesis
//      cout << "\n" << tmp << endl; //Bad condition in if statement?
//  }                                    //cout to test if ran
    istringstream is;
    is.str(tmp);
    is >> denominator;
    return denominator;
}
4

1 回答 1

0

您显示的功能是错误的。find类的成员函数std::string不返回布尔值。它返回一个类型的对象std::string::size_type例如,第一个函数可能如下所示

int formatStringNumerator( const string &rational )
{
    string tmp( rational, 0, rational.find("/") );

    tmp.erase( 0, tmp.find_first_not_of( "(" ) );

    return ( tmp.empty() ? 0 : std::stoi( tmp ) );
}

下面是一个使用函数的例子

#include <iostream>
#include <string>
using namespace std;

int formatStringNumerator( const string &rational )
{
    string tmp( rational, 0, rational.find("/") );

    tmp.erase( 0, tmp.find_first_not_of( "(" ) );

    return ( tmp.empty() ? 0 : std::stoi( tmp ) );
}

int main() 
{
    std::string s( "((((12/15" );

    std::cout << formatStringNumerator( s ) << std::endl;

    std::string t( "((((12" );

    std::cout << formatStringNumerator( t ) << std::endl;

    std::string u( "((((" );

    std::cout << formatStringNumerator( u ) << std::endl;
    return 0;
}

输出是

12
12
0
于 2014-04-18T23:38:14.360 回答