问题描述
我是C和编程世界的新手。我被要求以20为底数得到一个倒数,并打印出他转换成十进制的形式。 我知道这不是目前最有效的代码,但这是我根据目前的知识所能做到的最好的代码。 当我运行该程序并输入数字时,总是会遇到默认情况... 我想念什么? 谢谢:)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
char x;
int exponent= 1;
int sum = 0;
int flag = 0;
printf("Enter a reversed number is base 20\n");
scanf(" %c",&x);
while (x != "\n") {
switch (x) {
case '0':sum += 0;
break;
case '1':sum += 1 * exponent;
break;
case '2': sum += 2 * exponent;
break;
case '3':sum += 3 * exponent;
break;
case '4':sum += 4 * exponent;
break;
case '5':sum += 5 * exponent;
break;
case '6':sum += 6 * exponent;
break;
case '7':sum += 7 * exponent;
break;
case '8':sum += 8 * exponent;
break;
case '9':sum += 9 * exponent;
break;
case 'A':
case 'a': sum += 10 * exponent;
break;
case 'B':
case 'b':sum += 11 * exponent;
break;
case 'C':
case 'c' : sum += 12 * exponent;
break;
case 'D':
case 'd': sum += 13 * exponent;
break;
case 'E':
case 'e': sum += 14 * exponent;
break;
case 'F':
case 'f': sum += 15 * exponent;
break;
case 'G':
case 'g': sum += 16 * exponent;
break;
case 'H':
case 'h': sum += 17 * exponent;
break;
case 'I':
case 'i': sum += 18 * exponent;
break;
case 'J':
case 'j': sum += 19 * exponent;
break;
default:flag++;
break;
}
if (flag == 1) {
printf("Error! %c is not a valid digit in base 20",x);
break;
}
else {
exponent *= 20;;
scanf("%c",&x);
}
}
if (flag == 1)
return 0;
else
printf(sum);
return 0;
}
解决方法
您应该真正阅读compiler's warnings ...:
<source>: In function 'main':
<source>:10:14: warning: comparison between pointer and integer
10 | while (x != "\n") {
| ^~
<source>:10:14: warning: comparison with string literal results in unspecified behavior [-Waddress]
<source>:77:16: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [-Wint-conversion]
77 | printf(sum);
| ^~~
| |
| int
In file included from <source>:2:
/usr/include/stdio.h:332:43: note: expected 'const char * restrict' but argument is of type 'int'
332 | extern int printf (const char *__restrict __format,...);
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
您正在将x
与一个地址进行比较-字符串常量“ \ n”的地址(位于程序存储空间的某个部分)。您可能打算将x
与'\ n'进行比较。
此外,printf()
函数在参数前使用格式字符串,因此它应该为printf("%d\n",sum);
(以打印换行符)。
PS 1:始终在打开更多警告的情况下编译代码,例如gcc -W -Wall
(也许还有其他警告标记)可以更好地抓住这类错别字和小错误。
PS 2:如@Jabberwocky所述,您的程序可以做得更短(不损失可读性);另一方面,您应始终检查scanf()
的返回值以及可能由于用户输入而失败的类似功能:
#include <stdio.h>
#include <stdlib.h>
int main() {
char x;
int exponent = 1;
int sum = 0;
int num_scanned;
printf("Enter a reversed number is base 20\n");
num_scanned = scanf(" %c",&x);
if (num_scanned != 1) {
fprintf(stderr,"Invalid input\n");
exit(EXIT_FAILURE);
}
while (x != '\n') {
int digit;
if (x >= '0' && x <= '9') { digit = x-'0'; }
else if (x >= 'a' && x <= 'j') { digit = x-'a'; }
else if (x >= 'A' && x <= 'J') { digit = x-'A'; }
else {
fprintf(stderr,"%c is not a valid digit in base 20.",x);
exit(EXIT_FAILURE);
}
sum += digit * exponent;
exponent *= 20;
num_scanned = scanf("%c",&x);
if (num_scanned != 1) {
fprintf(stderr,"Invalid input\n");
exit(EXIT_FAILURE);
}
}
printf("%d\n",sum);
return EXIT_SUCCESS;
}