【发布时间】:2017-12-07 13:25:23
【问题描述】:
我的困惑是,如何在程序中通过编写显示的那段代码来计算正数和负数的数量。 请解释一下这些代码!
int num[5], i, neg = 0, pos = 0;
printf("enter any 5 numbers");
for (i = 0; i <= 4; i++)
scanf("%d", &num[i]);
for (i = 0; i <= 4; i++) {
num[i] < 0 ? neg++ : (pos++);
}
printf("\nnegative elements=%d", neg);
printf("\npositive elements=%d", pos);
getch();
【问题讨论】:
-
至于你的问题,你确实知道ternary (conditional) expression是如何工作的吗?
-
编译器不计算任何东西,仅供参考。
-
也许我不太清楚!
-
三元运算符的工作原理如下:
condition ? execute if true : execute if false. -
num[i]<0?neg++:(pos++);--->if (num[i]<0) neg++; else pos++;
标签: c