2013-07-07 187 views

回答

1

假設輸入是一個C++字符串,下面是一個遞歸解決方案的開始。我同意約阿希姆和拉森的評論;下面的方法更多地遵循rasen建議的路線。

這裏的關鍵包括cctype。這給你一個布爾檢查是否給定的字符是一個數字。

正如所寫,代碼返回一個NULL值,如果遇到一個非數字,在返回的數字中顯示爲零;您需要根據需要修改此實現。例如,請注意「23.1」返回爲2301,其中「。」被替換爲零。

這可能不是你想要的,所以想想如何實現邏輯,可能會返回一個指定的特殊字符,當遇到非數字或類似的東西時。然後,您可以搜索此返回值以查找指定char的存在,從而提供布爾函數的基礎,該布爾函數將讓您知道給定的輸入字符串是否可以轉換爲int數據類型。

main.cpp中(如下所列)的輸出是:下面

Here is the integer: Invalid character, entry must be a number: 
2301 
Here is the integer: 22 
Here is the integer: 0 
Here is the integer: 1 
Here is the integer: 32 

代碼:

// main.cpp 
// Created by bruce3141 on 7/7/13. 

/* Numeric Conversion (string to int) 
* ---------------------- 
* Demonstrates a recursive implementation of converting a string into 
* its representation as an int. Provides feedback to the user on invalid 
* entries, using isdigit() from the <cctype> import, where a invalid 
* character (a non-digit) is encountered. 
*/ 

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


/* Function prototype */ 
int stringToInt(string str); 


// Main.cpp tests a few cases below: 
int main() { 
    int n = 5; 
    string strNumbers[5] = {"23.1", "22", "-0", "+1", "32"}; 
    for (int i = 0; i < n; i++) { 
     cout << "Here is the integer: "<< stringToInt(strNumbers[i]) <<endl; 
    } 
    return 0; 
} 



/* Convert from string -> int. The code is longer because of the possibility 
* that we might have a '-' or '+' preceding the integer input, and then of 
* course multiple digits in combination with the '-' or '+' signs: 
*/ 
int stringToInt(string str) { 

/* Get the number of characters in the string: */ 
int nS = str.length(); 

/* Base Case #1: a single positive integer as input: */ 
if (nS == 1) { 
    /* This basic version provides a liitle feedback on 
    * invalid entries, using isdigit() from the <cctype> 
    import: */ 
    if (!isdigit(str[0])) { 
     cout << "Invalid character, entry must be a number: "<<endl; 
     return NULL; 
    } else { 
     /* We have to subtract the ASCII code for the character '0' so 
     that the string displays as a number in the proper range: */ 
     return str[0]-'0'; 
    } 

    /* Base Case #2: a single negative integer as input, here 
    * we deal with the possibility that a '-' precedes a number: */ 
} else if (nS == 2 && str.substr(0,1) == "-") { 
    /* Below, subtract the ASCII code for the character '0' then 
    * multiply by (-1) since the number is negative: */ 
    return (str[1] - '0')*(-1); 

    /* Base Case #3: a single postive integer as input, as indicated 
    * by a '+' character: */ 
} else if (nS == 2 && str.substr(0,1) == "+") { 
    /* Below, subtract the ASCII code for the character '0': */ 
    return (str[1] - '0'); 

    /* Below is the recursive step for negative numbers with more 
    * than one digit: */ 
} else if (nS >= 2 && str.substr(0,1) == "-") { 
    int n1 = stringToInt(str.substr(0,nS-1))*10; 
    int n2 = stringToInt(str.substr(nS-1,nS)); 
    return n1 - n2; 

    /* Below is the recursive step for positive numbers with a 
    * preceding '+' character and with more than one digit: */ 
} else if (nS >= 2 && str.substr(0,1) == "+") { 
    int n1 = stringToInt(str.substr(0,nS-1))*10; 
    int n2 = stringToInt(str.substr(nS-1,nS)); 
    return n1 + n2; 
} 

/* Below is the recursive step for positive numbers with more 
* than one digit, but with no preceding '+' character: */ 
else { 
    int n1 = stringToInt(str.substr(0,nS-1))*10; 
    int n2 = stringToInt(str.substr(nS-1,nS)); 
    return n1 + n2; 
} 

}

2

在這種情況下,有沒有真的什麼可以做。如果你試圖將一個浮點數讀入一個整數變量,那麼這個錯誤不會給出任何錯誤,因爲這個數據流(在你的例子中是數字)將整數讀取爲1,當然這是一個整數。

可能做的是peek在下一個字符,看看它是否是你期望或不。

+0

如何在偷看後檢查下一個字符是否預期?謝謝。 – user1899020

0

您可以在字符串中輸入數據,然後分析它是否處於正確的格式。如果是這樣,然後將其轉換爲您想要的類型。