C++ 输入30某物和-Ex42,输出30和Ex42

C++ 输入30某物和-Ex42,输出30和Ex42,c++,C++,我不明白我会怎么做 输入将是: 3 13894 30-something -Ex42 输出需要是: 13894 30 Ex42 主要任务是生成一个函数,将双十进制数转换为十进制格式。我已经找到了那个部分,不需要帮助。我基本上删掉了所有关于双模式转换的代码,只包含了一些我想不出来的东西 #include <iostream> #include <string> #include <iomanip> using namespace std;

我不明白我会怎么做

输入将是:

3
13894
       30-something
-Ex42
输出需要是:

13894
30
Ex42
主要任务是生成一个函数,将双十进制数转换为十进制格式。我已经找到了那个部分,不需要帮助。我基本上删掉了所有关于双模式转换的代码,只包含了一些我想不出来的东西

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int to_decimal(const string& str);

int main () {
    string str; // Initializes string str for input

    cin.ignore (256, '\n'); //ignores the 3 from the input

    while (getline(cin, str)) {
        //runs str through to_decimal and outputs
        cout << to_decimal(str) << endl;
    }
}

int to_decimal(const string& str) {
    int f = 0;
    string localString; // Initialize local string variable

    //sets local string to the same as inputted string
    localString = str; //used for local string erasing

    //This is the idea I have been working on and I cant figure it out
    for (unsigned x = 0; x < localString.length(); x++) {
        f = localString.at(x);
        if (isdigit(f)) {

        } else if (f == 'E'){
                
        } else if (f == 'e') {

        } else if (f == 'X') {

        } else if (f == 'x') {

        } else if (f == '-') {

        } else if (f == ' ') {

        } else {
            f = localString.length() - x;
            localString.erase(x, f);
            break;
        }
    }
}
#包括
#包括
#包括
使用名称空间std;
int到_十进制(常量字符串和str);
int main(){
string str;//初始化输入的字符串str
cin.ignore(256,'\n');//忽略输入中的3
while(getline(cin,str)){
//运行str到_十进制并输出

cout我有点困惑。您说您需要将双十进制数转换为十进制数,但是在您的示例输出中,只有具有
Ex
的行被转换,而
30某物
保持为30,就好像它没有被转换一样-并且双十进制数中的
30
36
。数字
13894
也是如此

假设您确实希望将所有行从duodecimal转换为decimal,那么您的解决方案可以基于标准库函数
std::stoi()
它可以将字符串从大多数数字基转换为36。它要求大于
9
的数字按字母顺序进行编码-
a
Z
。因此,您只需将所有
x
转换为
a
,将所有
e
转换为
b
。例如:

int to_decimal(const string& str) {
    bool foundDigit = false;
    std::string transformedString;
    for (auto c : str) {
        if (std::isdigit(c) || c == 'E' || c =='e' || c == 'X' || c == 'x') {
            foundDigit = true;

            // If needed, convert the character.
            if (c == 'E' || c == 'e') {
                c = 'b';
            } else if (c == 'X' || c == 'x') {
                c = 'a';
            }

            transformedString += c;
        } else if (foundDigit) {
            // Skip everything to the end of the line, if we've already found some digits
            break;
        }
    }
    
    return std::stoi(transformedString, 0, 12);
}

如果您只想提取字符,然后自己进行转换,则可以执行以下操作:

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

bool isNumber(const char c)
{
    switch (c) {
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '0':
    case 'e':
    case 'E':
    case 'x':
    case 'X':
    case '-':
        return true;
    default:
        return false;
    }
}

std::string getNumber(std::istream& in)
{
    std::stringstream s;
    for (char c;in.get(c);)
    {
        if (isNumber(c)) {
            s << c;
            break;
        }
    }
    for (char c;in.get(c);)
    {
        if (!isNumber(c))
            break;
        s << c;
    }

    return s.str();
}

int main()
{
    std::string bla = "3\n13894\n       30-something\n-Ex42\n";
    std::stringstream klaf{ bla };

    for (std::string s;(s = getNumber(klaf)) != "";)        //<- use a local stringstream as input to test
    //for (std::string s;(s = getNumber(std::cin)) != "";)  //<- use std::cin for input
    {
        std::cout << s << '\n';
    }
}

因此,这并不完全是您所追求的,但它至少应该为您提供一个改进的起点。例如,您可能希望从
isNumber
中删除
-
,然后在
getNumber
中更改为逻辑,只接受它作为新数字中的第一个字符。

?“Ex42”这不是一个number@JHBonarius这是一个双数。通常是BA42,但我的老师想让我用E表示11,x表示10。我只需要弄清楚如何保持这个数字,同时去掉30岁左右的数字。需要更多的细节或清楚你试图解决/做什么。这些细节非常重要。这些不是sta标准。无论如何,这是一个奇怪的任务。因为这是一个任务,他可能不允许使用
stoi
…但他说他已经有了转换的代码,所以没关系。
3
13894
30-
e
-Ex42