2017-04-12 10 views
1

以下if語句只有在我的digitToFind變量爲5時才能正常工作,否則會被忽略。if語句只識別某些整數:MATLAB

if(digitToFind == R) 
    digitToFindFreq = digitToFindFreq + 1; 
end 

該程序是爲了計數在一個給定的整數的位數,並發現由用戶選擇一個特定數量的頻率。

例如:123445;位數是6,頻率4是2

digitToFindFreq = 0; 
    numOfDigits = 0; 

integerInput = input('Enter an integer: '); 
while(integerInput ~= round(integerInput)) 
    fprintf('Invalid input. Try again!\n'); 
    integerInput = input('Enter an integer: '); 
end 
digitToFind = input('Enter a digit number to find (0 to 9): '); 
while(digitToFind < 0 || digitToFind > 9 || digitToFind ~= round(digitToFind)) 
    fprintf('Invalid input. Try again!\n'); 
    digitToFind = input('Enter a digit number to find (0 to 9): '); 
end 


if(integerInput == 0 && digitToFind ~= 0) 
    numOfDigits = 1; 
    digitToFindFreq = 0; 
elseif(integerInput == 0 && digitToFind == 0) 
    numOfDigits = 1; 
    digitToFindFreq = 1; 
end 


while(integerInput >= 1) 
    integerInput = integerInput/10; 

    X = integerInput - fix(integerInput); 
    R = 10*X; 

    if(digitToFind == R) 
     digitToFindFreq = digitToFindFreq + 1; 
    end 
integerInput = integerInput - X; 
numOfDigits = numOfDigits + 1; 
end 


fprintf('\nNumber of digits: %d, Digit to find frequency: %d\n',numOfDigits,digitToFindFreq); 

我從來沒有像這樣的問題。它一定是我錯過的一些小東西,否則程序會正常工作。

回答

0

這可能是一個浮點數的問題,當你除以10乘以10,並減去餘數時,你的值不是一個整數。因此,直接==測試可能會失敗。

1.0000000000000001 == 1 % False 

你應該用這個替換您的測試:

if(abs(digitToFind - R) < 0.1) % the digit must be close to digitToFind 
    digitToFindFreq = digitToFindFreq + 1; 
end 

或者使用字符串:

integerInput = num2str(integerInput); 
digitToFind = num2str(digitToFind); 
while length(integerInput) > 0 
    R = integerInput(end); 
    integerInput = integerInput(1:end-1); 

    if strcmp(digitToFind, R) 
     digitToFindFreq = digitToFindFreq + 1; 
    end 
    integerInput = integerInput - X; 
    numOfDigits = numOfDigits + 1; 
end 

真的不過,既不使用這些方法。你並沒有利用內置索引中的Matlab來爲你做所有的辛苦工作。我重寫了你的整個代碼,所以你也可以看到如何用字符串而不是整數來進行驗證檢查。注意下面的方法,我已經改變了輸入類型,所以不需要num2str,你的整數可以大得多。

% include your error checking here, but based on strings 
integerInput = '.'; 
digitToFind = '.'; 
% Check all characters in integerInput are digits 0-9, so integer 
while ~(all(integerInput >= '0') && all(integerInput <= '9')) 
    % Include the 's' qualifier to convert input directly to string, allows 
    % for much longer integers than using num2str() later on 
    integerInput = input('Enter an integer: ', 's'); 
end 
% Check digitToFind is 0-9, and only 1 character 
while ~(all(digitToFind >= '0') && all(digitToFind <= '9')) || length(digitToFind) ~= 1 
    digitToFind = input('Enter a digit number to find (0 to 9): ', 's'); 
end 
% use logical indexing to find vector of 1s and 0s in the positions where 
% integerInput(i) = digitToFind. Then count the number of non-zero elements 
% using nnz (in built function). No need for your edge-case 0 checks either. 
numOfDigits = length(integerInput); 
digitToFindFreq = nnz(integerInput == digitToFind); 

¹上最大輸入的註記尺寸

intmax('uint64') = 18446744073709551615是最大的(無符號)整數Matlab的可以處理。請注意,realmax = 1.7977e+308顯着大!對於您的應用程序,我會假設允許輸入大於20位數字,因此將它們表示爲整數會有問題。 這可能是您的測試存在問題的另一個原因,因爲超過intmax限制的任何內容都必須作爲浮點數(實數)存儲,而不是整數!

通過在input命令中使用's'標誌,輸入數字不會被評估爲整數,而是直接轉換爲字符串。字符串變量的最大長度是only dependent on your computer's memory,因此可能會很大!例如,我可以在內存不足之前創建一個900,000,000元的字符串(1.8千兆字節)。


編輯:

在上面,檢查數字的範圍0-9是,我使用的是事實,他們的ASCII值是連續的,因爲那正是被比較。您也可以使用內置的isstrprop

while ~(all(isstrprop(integerInput, 'digit'))) 
    ...