第8周项目3 多分段函数求值

本文通过一个具体的C++程序示例介绍了如何使用if-else和switch-case语句来实现不同的条件分支逻辑。程序根据用户输入的数值计算并输出相应的结果。
  1. /* 
  2. * Copyright (c) 2014,烟台大学计算机学院 
  3. * All right reserved. 
  4. *文件名:eight week2.app
  5. * 作者:柴银平
  6. * 完成时间:2014年10月12日 
  7. * 版本号:v1.0 
  8. * 
  9. * 问题描述:从键盘输入x的值,计算出y的值。 
  10. */  
  11. #include <iostream>
  12. #include <cmath>
  13. using namespace std;
  14. int main()
  15. {
  16.     double x,y;
  17.  cout<<"请输入x"<<endl;
  18.  cin>>x;
  19.  if (x<2)
  20.         y=x;
  21.     else
  22.      {
  23.   if (x<6&&x>=2)
  24.             y=x*x+1;
  25.         else
  26.         {
  27.             if (x<10&&x>=10)
  28.                 y=sqrt(x=1);
  29.             else
  30.                 y=1/(x+1);
  31.         }
  32.     }
  33.     cout<<"y"<<"="<<y<<endl;
  34.     return 0;
  35. }
  36. #include <iostream>
  37. #include <cmath>
  38. using namespace std;
  39. int main()
  40. {
  41.     double x,y;
  42.     char t;
  43.     cout<<"请输入x"<<endl;
  44.     cin>>x;
  45.     t=(x<10)+(x<6)+(x<2);
  46.     switch (t)
  47.     { case 3:y=x;break;
  48.      case 2: y=x*x+1; break;
  49.      case 1: sqrt(x+1);break;
  50.      case 0: y1/(x+1);break;
  51.       }
  52.     cout<<"Y"<<"="<<y<<endl;
  53.     return 0;
  54. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值