這是我當前的程式,其中我取 N 個自然數的 3 個不同(或相同)值并計算它們的校驗和,必須使用 long 資料型別,因為我還需要能夠計算超過最大整數。我無法更改資料型別。現在我還需要捕捉用戶輸入非自然數的時間,例如 -23, 2.3 ... 我已經做了 if 陳述句來捕捉是否輸入了負數和超過 MAX 的 long 的數字,實際問題是當我輸入十進制數它跳過我的 if 條件并列印出 printf 函式但不列印任何其他函式,我嘗試使用 x % 1 !=0 捕獲十進制數這不起作用,因為 x 的實際值似乎不是存盤為小數而不是整數,我通過列印 x 的值在我的程式的各個地方確認了這一點。我剛開始學習 C 才 2 周,還沒有真正掌握所有內容,但我真的似乎無法在我的程式中找到問題。我知道我的代碼看起來像意大利面條式的代碼。
PS請原諒我糟糕的英語
#include <stdio.h>
#include <stdlib.h>
int checksum(long q){
long countingVariable = 0;
while(q > 0)
{
countingVariable = q%10; // steht für sx = s q
q/=10; // steht für q = q/10
}
return countingVariable;
}
int main(void) {
long x,y,z;
long long max_long = 2147483647;
printf("Bitte geben sie ihre erste Zahl ein:\n"); // enter first number
scanf("%ld",&x);
long sx = quersumme_1(x); // checksum of x first number
if ( x > 0 && x < max_long && x % 1 != 0){ // check if positive and natural number
printf("Bitte geben Sie ihre zweite Zahl ein:\n"); //enter second number
scanf("%ld",&y);
if (y > 0 && y < max_long){
long sy = quersumme_2(y); //checksum of y second number
printf("Bitte geben sie ihre dritte Zahl ein:\n");// enter third number
scanf("%ld",&z);
if (z > 0 && z < max_long){
long sz = quersumme_3(z); // checksum of z 3rd number
if (sx>sy && sx>sz && sy>=sz){
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
} else if (sx>sy && sx>z && sz>sy){
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
} else if (sy>sx && sy>sz && sx>=sz){
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
} else if (sy>sx && sy>sz && sz>sx){
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
} else if(sz>sx && sz>sy && sx>=sy){
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
} else if(sz>sx && sz>sy && sy>sx){
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
} else if (sx==sy && sx==sz && sy==sz){
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
}
// the big if tree is just a sorting "algorithm" it sorts the values of checksums
}if (z < 0){ //check for negative number z
printf("Falsche Eingabe: Minus Zahl"); //error
exit(0);
}if (z > max_long){ // cant exceed Max value of long
printf("Falsche Eingabe: Zahl overflowed long");// error
exit(0);
}
}if (y < 0){ //check for negative number y
printf("Falsche Eingabe: Minus Zahl"); // error
exit(0);
}if (abs(y) > max_long){ // cant ewxceed max value of long
printf("Falsche Eingabe: Zahl overflowed long"); /error
exit(0);
}
}if (x < 0){ //check for negative numbers
printf("Falsche Eingabe: Minus Zahl"); // error
printf("%ld",x);
exit(0);
}
if (x > max_long){ // cant exceed max of long
printf("Falsche Eingabe: Zahl overflowed long"); // error
exit(0);
}
return 0;
}
uj5u.com熱心網友回復:
一個long型別不能存盤十進制值,scanf將始終嘗試以匹配用戶輸入到你告訴它期望的值的型別。
在您的情況下,%ld意味著如果用戶輸入十進制數,scanf將忽略它,并且不會在y.
在 C 中有兩種方法可以解決這個問題:
從用戶那里讀取一個字串并檢查其中的字符以確保它們都是數字(或者沒有小數點或您需要檢查的任何其他內容)。
然后您可以使用atol將字串轉換為long.檢查
scanf回傳值。
scanf回傳來自用戶的專案與您的期望相匹配并存盤在您提供的變數中的數量。
在您的情況下,如果輸入了有效的整數,它應該回傳 1。
此方法不會告訴您用戶輸入的是十進制數還是單詞而不是自然數,但您會知道輸入是否有效。
uj5u.com熱心網友回復:
的最好是使用fgets()讀出的行的用戶輸入的到一個字串,然后決議該字串。
讓我們說你被困住了scanf()(太糟糕了)。
首先,檢查回傳值
// scanf("%ld",&y);
if (scanf("%ld",&y) != 1) {
printf("Non-numeric input");
exit(0);
}
現在讀取下一個字符
unsigned char ch;
if (scanf("%c",&ch) != 1) {
printf("Failed to read next character");
exit(0);
}
if (!isspace(ch)) {
printf("Unexpected character following a number %d %c\n", ch, ch);
exit(0);
}
現在檢查范圍
if (y < min_long || y > max_long) {
printf("Out of range %ld\n", y);
exit(0);
}
存在簡化。
再次更好地使用fgets().
一個健壯但未經檢查的示例:
#define LINE_SZ 100 // Max expected line size
char buf[LINE_SZ * 2]; // Let us read even up to 2x expected
if (fgets(buf, sizeof buf, stdin) == NULL) {
printf("Nothing read\n", y);
exit(EXIT_FAILURE);
}
errno = 0;
char *endptr;
long y = strtol(buf, &endptr, 0);
if (buffer == endptr) {
printf("Non-numeric input \"%s\"\n", buf);
exit(EXIT_FAILURE);
}
if (errno || y < long_min || long_max) {
printf("Input \"%s\" outside [%ld %ld] range\n", buf, long_min, long_max);
exit(EXIT_FAILURE);
}
// Skip trailing white-space
while (isspace((unsigned char)*endptr)) {
endptr ;
}
if (*endptr) {
printf("Trailing junk in \"%s\"\n", buf);
exit(EXIT_FAILURE);
}
printf("Success %ld\n", y);
如果您不能使用fgets()將一行讀入字串,請嘗試
char buf[100];
if (scanf("
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383271.html
標籤:C
下一篇:對C字串陣列使用簡單的空指標
