作出决策之if控制结构(2)

/*1.简单的if-else控制结构
2.条件运算符"?:"*/

#include<cmath>
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
       
double revenue=0,expenses=0,profit=0,loss=0,interest=0;
        cout
<<setprecision(2)<<setiosflags(ios::showpoint);
        cout
<<"Enter the company's revenue and expenses:"<<endl;
        cin
>>revenue>>expenses;
        cout
<<endl<<endl;

       
if(revenue>expenses)
       
{
                profit
=revenue-expenses;
                cout
<<"The company is profitable.\n"
                       
<<"The company'profit for this month"
                       
"is:$"<<profit<<endl;
       
}
       
else
       
{
                loss
=expenses-revenue;
                cout
<<"The company is running a loss.\n"
                       
<<"The company's loss for this month"
                       
"is:$"<<loss<<endl;
       
}//if-else控制结构。
        interest
=(loss>0.0)?(0.05*loss):(0.0);//?:的关系表达式。
        cout
<<"The interest the company owes on its debt is $"
               
<<interest<<endl;
}
/*简单的if-else控制结构:
  if(expression)
  {
  executable statement 1a;
  executable statement 1b;
  ......
  }
  else
  {
  executable statement 2a;
  executable statement 2b;
  ......
  }
  注意:如果if语句后面有不止一条语句,就得用花括号,具体以分号作为基准。*/
posted @ 2012-06-22 20:26  蚂蚁踩死了大象  阅读(133)  评论(0)    收藏  举报