鱼C论坛

 找回密码
 立即注册
查看: 3092|回复: 2

[已解决]c条件运算符

[复制链接]
发表于 2023-1-28 20:24:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求奇偶数个数及各自平均数的例子,注释部分改成条件运算符怎么不行
int main(){
        int j,o,x,no,nj;
        j=o=no=nj=0;
        printf("enter some number:(0 to end)\n");
        scanf("%d",&x);
        while(x!=0){
        /*        if(x%2==0)
                {
                        o++;
                        no+=x;
                }
                else if(x%2!=0)
                {
                        j++;
                        nj+=x;
                }*/
                (x%2==0)?o++,no+=x:j++,nj+=x;
                scanf("%d",&x);
        }
        printf("o have %d and its average:%f\nj have %d and its aversge:%f",o,(float)no/o,j,(float)nj/j);
        return 0;
}
最佳答案
2023-1-28 21:26:40
运算符优先级未考虑
20行,逗号运算符的优先级低于条件运算符,应加括号
改为:
  1. (x%2==0)?(o++,no+=x):(j++,nj+=x);
复制代码


修改后完整代码:
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int j,o,x,no,nj;
  5.         j=o=no=nj=0;
  6.         printf("enter some number:(0 to end)\n");
  7.         scanf("%d",&x);
  8.         while(x!=0){
  9.         /*        if(x%2==0)
  10.                 {
  11.                         o++;
  12.                         no+=x;
  13.                 }
  14.                 else if(x%2!=0)
  15.                 {
  16.                         j++;
  17.                         nj+=x;
  18.                 }*/
  19.                 (x%2==0)?(o++,no+=x):(j++,nj+=x);
  20.                 scanf("%d",&x);
  21.         }
  22.         printf("o have %d and its average:%f\nj have %d and its aversge:%f",o,(float)no/o,j,(float)nj/j);
  23.         return 0;
  24. }
复制代码


运行结果

  1. enter some number:(0 to end)
  2. 1
  3. 5
  4. 3
  5. 8
  6. 3
  7. 0
  8. o have 1 and its average:8.000000
  9. j have 4 and its aversge:3.000000
  10. --------------------------------
  11. Process exited after 9.498 seconds with return value 0
  12. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-1-28 21:18:49 | 显示全部楼层
我来看看,先占个楼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-28 21:26:40 | 显示全部楼层    本楼为最佳答案   
运算符优先级未考虑
20行,逗号运算符的优先级低于条件运算符,应加括号
改为:
  1. (x%2==0)?(o++,no+=x):(j++,nj+=x);
复制代码


修改后完整代码:
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int j,o,x,no,nj;
  5.         j=o=no=nj=0;
  6.         printf("enter some number:(0 to end)\n");
  7.         scanf("%d",&x);
  8.         while(x!=0){
  9.         /*        if(x%2==0)
  10.                 {
  11.                         o++;
  12.                         no+=x;
  13.                 }
  14.                 else if(x%2!=0)
  15.                 {
  16.                         j++;
  17.                         nj+=x;
  18.                 }*/
  19.                 (x%2==0)?(o++,no+=x):(j++,nj+=x);
  20.                 scanf("%d",&x);
  21.         }
  22.         printf("o have %d and its average:%f\nj have %d and its aversge:%f",o,(float)no/o,j,(float)nj/j);
  23.         return 0;
  24. }
复制代码


运行结果

  1. enter some number:(0 to end)
  2. 1
  3. 5
  4. 3
  5. 8
  6. 3
  7. 0
  8. o have 1 and its average:8.000000
  9. j have 4 and its aversge:3.000000
  10. --------------------------------
  11. Process exited after 9.498 seconds with return value 0
  12. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 19:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表