【发布时间】:2021-12-18 02:30:18
【问题描述】:
这是我当前的程序,其中我取 N 个自然数的 3 个不同(或相同)值并计算它们的校验和,长数据类型的使用是必须的,因为我还需要能够计算值的校验和超过 int 的 MAX。我无法更改数据类型。现在我还需要捕捉当用户输入一个非自然数时,例如 -23、2.3 ......我已经做了 if 语句,如果输入一个负数和一个超过 long 的 MAX 的数字,实际的问题是当我输入十进制数,它跳过我的 if 条件并打印出 printf 函数,但没有打印出任何其他函数,我尝试用 x % 1 !=0 捕获十进制数,这不起作用,因为 x 的实际值似乎不是存储为小数而不是整数,我通过打印 x 的值在我的程序中的各个位置确认了这一点。 我对 C 语言真的很陌生,刚开始学习 2 周,还没有真正掌握一切,但我真的似乎无法在我的程序中找到问题。 我知道我的代码看起来像意大利面条代码。
P.S 请原谅我糟糕的英语
#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%10
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;
}
【问题讨论】:
-
您所有的
quersumme_X函数都是相同的。为什么你需要 3 个函数来做同样的事情? -
我不知道如何给一个递归 3 个值以及如何让它返回 3 个值,这就是我选择进行单独递归的原因,因为这对我来说更容易
-
没有递归。您可以只编写一个函数,并在每个需要校验和的地方调用它。
long sx = quersumme(x); ... long sy = quersumme(y); -
这是函数的主要特点之一,同一个函数可以调用任意多次。
-
如果要检测负数,只要勾选
if (x < 0),报错即可。
标签: c