2012-10-15 52 views
0

我使它顯示了股息,以便人們可以破解代碼沒有太多麻煩。它曾經工作過,但之後我不知道我做了什麼來使它停止工作。它適用於正整數。它應該顯示與用戶輸入相關的數字。部門停止工作負數

#include <iostream>          // Necessary header 
using namespace std; 

int main() 
{ 
    signed int Input, Divisor, Dividend, MSD; 

    cout << "Input:"; 
    cin >> Input; 

    Divisor = 1; 
    Dividend = Input; 
    if (Input < 0) 
    { 
     Dividend *= -1; 
     cout << "minus "; 
    } 

    cout << Dividend; 
    while (Dividend > 9) 
    { 
     Divisor = Divisor * 10; 
     Dividend = Dividend/10; 
    } 

    while (Divisor != 0) 
    { 
     MSD = Input/Divisor; 
     switch (MSD) 
     { 
      case 0: 
      cout << "zero "; 
      break; 
      case 1: 
      cout << "one "; 
      break; 
      case 2: 
      cout << "two "; 
      break; 
      case 3: 
      cout << "three "; 
      break; 
      case 4: 
      cout << "four "; 
      break; 
      case 5: 
      cout << "five "; 
      break; 
      case 6: 
      cout << "six "; 
      break; 
      case 7: 
      cout << "seven "; 
      break; 
      case 8: 
      cout << "eight "; 
      break; 
      case 9: 
      cout << "nine "; 
      break; 
     } 
    Input = Input - (MSD * Divisor); 
    Divisor /= 10; 
    } 
    return 0; 
} 
+5

你有沒有調試?????? –

回答

5

變化

if (Input < 0) 
{ 
    Dividend *= -1; 
    cout << "minus "; 
} 

if (Input < 0) 
{ 
    Input *= -1; 
    cout << "minus "; 
} 

不過說真的,你調試?:)

+0

完美。感謝你,我讓它工作。 – user1728737

+1

@ user1728737很好,但下次嘗試調試!順便說一句,你不會將問題標記爲已接受(其他兩個問題在你的檔案中有正確的答案),這是一個非常糟糕的習慣。不用謝。 :) – dreamzor

+0

不,我沒有調試,但我從現在開始。堅持我試圖標記爲已回答。 – user1728737

2

我覺得你的代碼不爲負數工作。看這句話:

while (Divisor != 0) { 
    MSD = Input/Divisor; 
    // switch statement 
} 

如果用戶輸入的Input爲負值,任何情況下,將在您的switch語句被擊中。我相信你想確保你使用Input的絕對值,而不是用戶輸入的值。

+0

*我認爲你的代碼不適用於負數。*正確。這不是分工,不能正常工作。這是代碼無法正常工作。 –

+0

一旦我在if語句中修復了Input = -Input,它就可以工作。我不需要改變這一點。 – user1728737

0

作品!謝謝。

#include <iostream>          // Necessary header 
using namespace std; 
const int DIV = 10;          // Avoid Magic Numbers 
const int REM = 9; 

int main() 
{ 
    signed int Input, Divisor, Dividend, MSD;    // Signed because negative values need to be read 

    cout << "Input:"; 
    cin >> Input; 
    cout << "Output: "; 

    if (Input < 0)           // If the input is negative, make it positive 
    { 
     Input = -Input; 
     cout << "minus "; 
    } 

    Divisor = 1;           // Initialize Divisor to 1 
    Dividend = Input;          // Dividend is equal to input number 
    while (Dividend > REM) 
    { 
     Divisor *= DIV; 
     Dividend /= DIV; 
    } 

    while (Divisor != 0)         // While the Divisor is not 0 
    { 
     MSD = Input/Divisor;        // The most significant digit is obtained 
     switch (MSD) 
     { 
     case 0:           // Multiple cases to print all digits 0 to 9 
      cout << "zero "; 
      break; 
     case 1: 
      cout << "one "; 
      break; 
     case 2: 
      cout << "two "; 
      break; 
     case 3: 
      cout << "three "; 
      break; 
     case 4: 
      cout << "four "; 
      break; 
     case 5: 
      cout << "five "; 
      break; 
     case 6: 
      cout << "six "; 
      break; 
     case 7: 
      cout << "seven "; 
      break; 
     case 8: 
      cout << "eight "; 
      break; 
     case 9: 
      cout << "nine "; 
      break; 
     } 
     Input -= MSD * Divisor;        // Change input so next digit can be obtained 
     Divisor /= DIV; 
    } 
    return 0; 
}               // End main method