作出决策之bool数据类型

/*1.bool数据类型的使用。 
  2.bool数据的输入和输出。*/ 
#include<iostream> 
#include<fstream> 
#include<iomanip> 
usingnamespace std; 
int main() 
{ 
        bool salty,hard,acidic,good_taste,have_service; 
        double sodium,Ca,Mg,pH; 
 
        ifstream infile("A.txt"); 
        infile>>sodium>>Ca>>Mg>>pH; 
        salty=(sodium>4000); 
        hard=(Ca>40&&Mg>20); 
        acidic=(pH<7);//注意:逻辑运算符把真的结果化为1赋给变量,把假的结果化为0赋给变量。 
        cout<<"Water composition" 
                <<"  salty"<<salty<<"  hard"<<hard<<"  acidic"<<acidic; 
        cout<<boolalpha<<"Water composition"//注意:使用boolalpha操纵器后,bool变量的输出值为"true"或"false"。 
                <<"  salty"<<salty<<"  hard"<<hard<<"  acidic"<<acidic; 
        cout<<"You believe that this water tastes good" 
                "(Enter zero for no or nonzero for yes)" 
                <<endl; 
        cin>>good_taste; 
        cout<<"Service of this water is provided to" 
                "your home(enter true or false)" 
                <<endl; 
        cin>>boolalpha>>have_service; 
    cout<<boolalpha<<"tastes good-"<<good_taste 
            <<"have service-"<<have_service; 
        return0; 
} 
/*bool数据类型:bool声明的变量的数值很方便地被认为是"1"或"0",其中"1"相当于"真","0"相当于"假". 
 
  bool型变量的用途:当事件的状态可以被认为两种状态时,使用bool型变量比较方便。 
 
  bool型变量赋值: 
  1.在赋值语句的右端写一句逻辑表达式。eg  salty=(sodium>4000); 
  2.我们可以把整型值赋给bool型变量。当值非零时为真;当值为零时为假。 
  3.同样可以把关键字true、false直接赋给bool型变量。 
 
  bool型变量值的输出:当直接输出时,值为0或1; 
                      当使用boolalpha操纵器输出时,值变为true或false.*/
posted @ 2012-06-23 16:56  蚂蚁踩死了大象  阅读(524)  评论(0)    收藏  举报