|
|
发表于 2022-6-7 23:51:06
|
显示全部楼层
本楼为最佳答案
 确实奇怪,为什么会没有人帮你呢?
我帮你看了看,你的思路是对的
但是这么写不是很好,我的调试器检查出了这个溢出
还是强制转换一下unsigned类型吧
- $ cat main.c
- #include <stdio.h>
- int main() {
- int ch, num = 0, num0; // ch为输入的字符,num存储结果用的整形数值
- printf("请输入待转换的字符串:");
- do {
- ch = getchar();
- if(ch >= '0' && ch <= '9') {
- num0 = num; //临时存储,用于比较是否溢出
- #if 1
- num = 10 * num + (ch - '0'); //按照10进制记录已读入的值
- #else
- num = (unsigned)10 * num + (ch - '0'); //按照10进制记录已读入的值
- #endif
- if(num < num0)
- break; //溢出了,跳出循环,不再读取字符。
- } else if(num) break; //不是数字,则跳出循环。
- } while((ch != '\n'));
- if(num < num0) //溢出时打印结果
- printf("数值超出范围,结果未定义!\n");
- else if(num != 0) //正常时打印结果
- printf("结果是:%d\n", num);
- else
- printf("并未找到任何数值!\n"); //没有数字时打印结果
- return 0;
- }
- $ gcc-debug -o main main.c
- $ ./main
- 请输入待转换的字符串:2147483647
- 结果是:2147483647
- $ ./main
- 请输入待转换的字符串:2147483648
- main.c:11:17: runtime error: signed integer overflow: 2147483640 + 8 cannot be represented in type 'int'
- 数值超出范围,结果未定义!
- $
- $
- $
- $ vim main.c
- $ cat main.c
- #include <stdio.h>
- int main() {
- int ch, num = 0, num0; // ch为输入的字符,num存储结果用的整形数值
- printf("请输入待转换的字符串:");
- do {
- ch = getchar();
- if(ch >= '0' && ch <= '9') {
- num0 = num; //临时存储,用于比较是否溢出
- #if 0
- num = 10 * num + (ch - '0'); //按照10进制记录已读入的值
- #else
- num = (unsigned)10 * num + (ch - '0'); //按照10进制记录已读入的值
- #endif
- if(num < num0)
- break; //溢出了,跳出循环,不再读取字符。
- } else if(num) break; //不是数字,则跳出循环。
- } while((ch != '\n'));
- if(num < num0) //溢出时打印结果
- printf("数值超出范围,结果未定义!\n");
- else if(num != 0) //正常时打印结果
- printf("结果是:%d\n", num);
- else
- printf("并未找到任何数值!\n"); //没有数字时打印结果
- return 0;
- }
- $ gcc-debug -o main main.c
- $ ./main
- 请输入待转换的字符串:2147483648
- 数值超出范围,结果未定义!
- $
复制代码
如果不考虑负数的话,可以直接作为无符数来计算
超过了0x80000000就是溢出(包括这个数)
- $ cat main.c
- #include <stdio.h>
- int main() {
- //int ch, num = 0, num0; // ch为输入的字符,num存储结果用的整形数值
- int ch;
- unsigned int num = 0;
- printf("请输入待转换的字符串:");
- do {
- ch = getchar();
- if(ch >= '0' && ch <= '9') {
- num = num * 10 + (ch - '0');
- if(num >= 0x80000000) break; //溢出了,跳出循环,不再读取字符。
- } else if(num) break; //不是数字,则跳出循环。
- } while((ch != '\n'));
- if(num >= 0x80000000) //溢出时打印结果
- printf("数值超出范围,结果未定义!\n");
- else if(num != 0) //正常时打印结果
- printf("结果是:%d\n", num);
- else
- printf("并未找到任何数值!\n"); //没有数字时打印结果
- return 0;
- }
- $ gcc-debug -o main main.c
- $ ./main
- 请输入待转换的字符串:2147483648
- 数值超出范围,结果未定义!
- $ ./main
- 请输入待转换的字符串:2147483647
- 结果是:2147483647
- $
复制代码
你的这个程序还有一个bug
0是不是数字,0是数字
但是
- $ cat main.c
- #include <stdio.h>
- int main() {
- int ch, num = 0, num0; // ch为输入的字符,num存储结果用的整形数值
- printf("请输入待转换的字符串:");
- do {
- ch = getchar();
- if(ch >= '0' && ch <= '9') {
- num0 = num; //临时存储,用于比较是否溢出
- #if 1
- num = 10 * num + (ch - '0'); //按照10进制记录已读入的值
- #else
- num = (unsigned)10 * num + (ch - '0'); //按照10进制记录已读入的值
- #endif
- if(num < num0)
- break; //溢出了,跳出循环,不再读取字符。
- } else if(num) break; //不是数字,则跳出循环。
- } while((ch != '\n'));
- if(num < num0) //溢出时打印结果
- printf("数值超出范围,结果未定义!\n");
- else if(num != 0) //正常时打印结果
- printf("结果是:%d\n", num);
- else
- printf("并未找到任何数值!\n"); //没有数字时打印结果
- return 0;
- }
- $ gcc-debug -o main main.c
- $ ./main
- 请输入待转换的字符串:0
- 并未找到任何数值!
- $ ./main
- 请输入待转换的字符串:abcd1234
- 结果是:1234
- $ ./main
- 请输入待转换的字符串:abcd0
- 并未找到任何数值!
- $ ./main
- 请输入待转换的字符串:abcd0000
- 并未找到任何数值!
- $
复制代码
这个bug自己改吧
|
|