我必须编写一个程序,其中main调用其他函数来测试一系列数字,判断是否有数字小于某个数,如果所有系列的数字都在两个限制之间,并且是否有负数。
我的代码返回值为1表示真和0表示假,但是任务要求将它们打印为'真'或'假'。我不确定如何从printf中将布尔答案打印为字符串。我在我的at_least.c文件和main.c文件中使用了if (atl == false) printf("false");,但它只返回一长串的true或false(例如:truetruetrue....)。
我不确定这是否是正确的编码方式,我是否把它放错位置,或者是否需要使用其他代码。
我的代码返回值为1表示真和0表示假,但是任务要求将它们打印为'真'或'假'。我不确定如何从printf中将布尔答案打印为字符串。我在我的at_least.c文件和main.c文件中使用了if (atl == false) printf("false");,但它只返回一长串的true或false(例如:truetruetrue....)。
我不确定这是否是正确的编码方式,我是否把它放错位置,或者是否需要使用其他代码。
#include "my.h"
int main (void)
{
int x;
int count = 0;
int sum = 0;
double average = 0.0;
int largest = INT_MIN;
int smallest = INT_MAX;
bool atlst = false;
bool bet = true;
bool neg = false;
int end;
while ((end = scanf("%d",&x)) != EOF)
{
sumall(x, &sum); // Calling function sumall
count++;
larger_smaller(x, &largest, &smallest); // Calling function larger_smaller
if (atlst == false)
at_least(x, &atlst); // Calling function at_least if x < 50
if (bet == true)
between(x, &bet); // Calling function between if x is between 30 and 40 (inclusive)
if (neg == false)
negative(x, &neg); // Calling function negative if x < 0
}
average = (double) sum / count;
print(count, sum, average, largest, smallest, atlst, bet, neg);
return;
}
一组数字的结果:
The number of integers is: 15
The sum is : 3844
The average is : 256.27
The largest is : 987
The smallest is : -28
At least one is < 50 : 1 // This needs to be true
All between 30 and 40 : 0 // This needs to be false
At least one is negative : 1 // This needs to be true
这是关于C语言的,我似乎找不到太多相关的信息。
附录:
以下内容是从下面一个答案中复制过来的。
这对于at_least和negative函数是有效的,但对于between函数则无效。我已经尝试过
void between(int x, bool* bet)
{
if (x >= LOWER && x <= UPPER)
*bet = false;
return;
}
我的代码。哪里出错了?
x == true
是多余的;你可以直接写x
。同样地,x == false
可以简写为!x
。 - Jon Purdy