例题2.5原题
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"please input a character:";
cin>>ch;
ch=ch>='a'&&ch<='z'?ch-'a'+'A':ch;
//上述语句等价于ch=ch>='a'&&ch<='z'?ch-32:ch;
cout<<"The result is:"<<ch<<endl;
return 0;
}
例题2.5修改版
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"please input a character:";
cin>>ch;
ch=ch>='A'&&ch<='Z'?ch-'A'+'a':ch;
cout<<"The result is:"<<ch<<endl;
return 0;
}
例题2.6原题
#include<iostream>
using namespace std;
int main()
{
char ch='c';
int a,b=13;
float x,y;
x=y=2.0;
a=ch+5;
x=b/2/x;
y=b/y/2;
cout<<"a="<<a<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
return 0;
}
例题2.6修改版
#include<iostream>
using namespace std;
int main()
{
char ch='A';
int a,b=13;
float x,y;
x=y=2.01;
a=ch;//a赋值为65,ch先转化为int型(即字符A的ASCII值),再运算
x=b/2*x;//x赋值为12.06,先做整除运算,再转换成double与x运算
y=ch/2.0;//y赋值为32.5,ch先转化为int型,再转化为double型,在与2.0运算
cout<<"a="<<a<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
return 0;
}
例题2.7原题
#include<iostream>
using namespace std;
int main()
{
int ab,ac;
double b=3.14;
char c='A';
ab=int(b);
ac=int(c);
cout<<"b="<<b<<endl;
cout<<"ab="<<ab<<endl;
cout<<"c="<<c<<endl;
cout<<"ac="<<ac<<endl;
return 0;
}