编写一个程序,提示用户输入两个long
值。实现一个名为negative_count()
的函数,该函数接受数据类型为long
的两个参数,并返回一个整数,该整数是负数的参数个数。然后函数main()
显示结果
例如:
输入数据类型的两个整数" long":-1264364007 -2012334695
输入的负数为2
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
long num1,num2;
void Negative_Count(long int,long int);
int main(void) {
printf("Enter two number: ");
scanf("%ld%ld",&num1,num2);
("you entered %ld%ld",&num1,&num2);
Negative_Count(num1,num2);
return (0);
}
void Negative_Count(long int num1,long int num2)
{
if (num1,num2<0)
printf("%ld%ld is negative.",&num1,&num2);
else if (num1,num2>0)
printf("%ld%ld is positive.",&num1,&num2);
else if (num1>0, num2<0)
printf("%ld is negative",&num2);
else if (num1<0, num2>0)
printf("%ld is negative",&num1);
}
我可以使用带有两个变量的循环吗?
答案 0 :(得分:2)
scanf("%ld%ld",&num1,num2);
应该是
scanf("%ld%ld", &num1, &num2);
("you entered %ld%ld",&num1,&num2);
应该是
printf("you entered %ld %ld", num1, num2);
if (num1,num2<0)
printf("%ld%ld is negative.",&num1,&num2);
else if (num1,num2>0)
printf("%ld%ld is positive.",&num1,&num2);
else if (num1>0, num2<0)
printf("%ld is negative",&num2);
else if (num1<0, num2>0)
printf("%ld is negative",&num1);
应该是
if (num1 < 0 && num2 < 0)
printf("%ld and %ld are negative.", num1, num2);
else if (num1 > 0 && num2 > 0)
printf("%ld and %ld are positive.", num1, num2);
else if (num1 > 0 && num2 < 0)
printf("%ld is negative", num2);
else if (num1 < 0 && num2 > 0)
printf("%ld is negative", num1);
使用 和 运算符(&&
):如果两个操作数均为非零,则条件为真。
请注意printf
(与scanf
相对)并不需要变量的地址,只需传递值。
答案 1 :(得分:0)
您的 scanf 和 printf 格式存在问题。 if语句中的条件检查也是错误的。
您可以修改以下代码,以便正常工作......
#include <stdio.h>
#include <stdlib.h>
void Negative_Count(long int,long int);
int main(void) {
long num1,num2;
printf("Enter two number: ");
scanf("%ld%ld",&num1,&num2);
printf("you entered %ld%ld \n",num1,num2);
Negative_Count(num1,num2);
return 0;
}
void Negative_Count(long int num1,long int num2)
{
if (num1<0 && num2<0) {
printf("%ld , %ld are negative.",num1,num2);
}
else if (num1>0 && num2>0) {
printf("%ld , %ld are positive.",num1,num2);
}
else if (num1>0 && num2<0){
printf("%ld is negative",num2);
}
else if (num1<0 && num2>0){
printf("%ld is negative",num1);
}
}