- #include <stdio.h>
- #include <stdlib.h>
- int atoi(const char *nptr);
- int main()
- {
- char c[1024];
- printf("请输入一个整数:\n");
- scanf("%s", c);
- int i = 0;
- while (c[i] != '\0') {
- if (c[i] == '-') {
- printf("不能输入负数,请重新输入...\n");
- scanf("%s", c);
- i = 0;
- continue;
- }
- else if (c[i] == '.') {
- printf("不能输入小数,请重新输入..\n");
- scanf("%s", c);
- i = 0;
- continue;
- }
- else if ((c[i] < '0') || (c[i] > '9')) {
- printf("不是数字,请重新输入...\n");
- scanf("%s", c);
- i = 0;
- continue;
- }
- else if ((atoi(c) < 0) || (atoi(c) > 100)) {
- printf("不在0~100范围内,请重新输入...\n");
- scanf("%s", c);
- i = 0;
- continue;
- }
- i++;
- }
-
- int result = atoi(c);
- printf("%d\n", result);
-
- return 0;
- }
复制代码 |