int main()
{
int num[20];
printf("Enter 4 integers: ");
int n = scanf("%d %d %d %d",&num[0],&num[1],&num[2],&num[3]);
if(n != 4)
{
printf("\n");
printf("Input must consist of 4 integers\n");
exit(0);
} else if(n == 4)
{
if(sol_for_24(num))
{
printf("\n");
printf("Yes! 24 is reachable from { %d, %d, %d, %d }\n", num[0], num[1], num[2], num[3]);
} else {
printf("\n");
printf(sol_for_24(num) ? "\n" : "Noooo :( 24 is unreachable from { %d, %d, %d, %d }\n",num[0],num[1],num[2],num[3]);
}
}
return 0;
}
对于我到目前为止所写的上述主要功能代码,我想要进一步实现的是,如果我输入的数字超过或少于4倍,它应该打印出来并退出程序。但是,即使输入4位以上的数字(但只执行前4位前面的数字),它也会运行程序。我可以获得任何帮助或建议我应该如何解决这个问题吗?我真的在努力解决这个问题..谢谢。
答案 0 :(得分:0)
您可以使用fgets
和strtok
代替scanf
来实现您想要的内容。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int num[20];
printf("Enter 4 integers: ");
char read[100];
fgets(read, 100, stdin);
int count = 0;
char* p = strtok(read, " ");
while (p != NULL) {
if (count == 4) {
printf("Your input is more than 4 integers.\n");
exit(EXIT_FAILURE);
}
num[count++] = atoi(p);
p = strtok(NULL, " ");
}
if (count < 4) {
printf("Your input is less than 4 integers.\n");
exit(EXIT_FAILURE);
}
// Do other stuffs....
return 0;
}
<强>更新强>
请注意,上面的代码假定它只需要一行用户输入,它将计算该一行输入中的数字,并且用户小心不要输入除数字之外的任何其他内容。
虽然我认为这个假设对于一些简单的家庭作业来说是可行的,但它并不像其他人在评论中所说的那样强健。以下代码是strtol
更强大的版本。请注意,它也没有使用strtok
。 (感谢David C. Rankin的评论!)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main()
{
int num[20];
printf("Enter 4 integers: ");
char read[100];
fgets(read, 100, stdin);
int count = 0;
char* endptr = NULL;
char* nextptr = read;
do {
num[count] = strtol(nextptr, &endptr, 10);
if (errno == ERANGE) {
printf("Number out of range.\n");
exit(EXIT_FAILURE);
}
} while (nextptr != endptr // strtol found an integer
&& (++count < 20) // so we've read (++count) number of integers so far, also check for array out-of-bounds error
&& (nextptr = endptr) // we'll continue to read from the place where the last strtol stopped
);
// while loop condition can be false on several cases
// check if it were the case where there were non-number input
char tmp[2];
if (sscanf(nextptr, "%1s", tmp) != EOF) { // %s ignores whitespaces(i.e. '\n', ' ', '\t')
printf("Nothing other than numbers such as '%s' should be in the input\n", tmp);
exit(EXIT_FAILURE);
}
// now we know that user input contained only numbers
// check how many numbers were given
if (count < 4) {
printf("You've entered less than 4 numbers.\n");
exit(EXIT_FAILURE);
} else if (count > 4) {
printf("You've entered more than 4 numbers.\n");
exit(EXIT_FAILURE);
}
// We now have definitely 4 numbers! Do other stuffs with it!
return 0;
}
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int num[20];
int i = 0;
if(argc != 5)
{
printf("\n");
printf("Input must consist of 4 integers\n");
exit(0);
}
while(argc != 1)
{
num[i] = atoi(argv[i+1]);
argc--;
i++;
}
if(sol_for_24(num))
{
printf("\n");
printf("Yes! 24 is reachable from { %d, %d, %d, %d }\n", num[0], num[1], num[2], num[3]);
}
else
{
printf("\n");
printf(sol_for_24(num) ? "\n" : "Noooo :( 24 is unreachable from { %d, %d, %d, %d }\n",num[0],num[1],num[2],num[3]);
}
return 0;
}
上面解决了你的问题。 Seb和John Forkosh在评论中指出了正确的方向。
答案 2 :(得分:0)
如果您不能直接从主列表中使用argv []列表。我建议使用vscanf。
成功时,该函数返回成功填充的参数列表的项目数。
所以你可以用这样的东西替换你的scanf:
int my_scanf( const char * format, ... )
{
int nb_filled = 0;
va_list args;
va_start (args, format);
nb_filled = vscanf (format, args);
va_end (args);
return nb_filled;
}
并使用返回值来检查填充的args数。
答案 3 :(得分:0)
如果用户输入的数量超过某些数字,则将计数循环增加一个并返回状态以添加约束使用if else,以获得简单的计数循环。
答案 4 :(得分:0)
尝试阅读5个数字......
int tmp;
int n = scanf("%d%d%d%d%d", num, num+1, num+2, num+3, &tmp);
if (n != 4) {
fprintf(stderr, "error\n");
exit(EXIT_FAILURE);
}
/* ... rest of code ... */