例题2.1
/*******************************************
** 功能:布尔类型使用举例 **
********************************************/
#include<iostream> //编译预处理命令
#include<iomanip> //使用控制符boolalpha需使用此头文件
using namespace std; //使用标准名空间std
int main() //主函数
{
bool flag=true; //定义布尔型变量flag,并初始化为true
cout<<flag<<endl; //默认情况下为非bool字母(noboolalpha),输出整型值1
cout<<boolalpha<<flag<<endl; //使用输入格式控制符,输出布尔型值
cout<<flag+5<<endl;
flag=0; //在算术运算中,把布尔型数据当作整型数据,输出6
cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl; //可以给bloo类型的变量赋任意类型的值
flag=0.0; //0.0为double类型的数值
cout<<"执行语句flag=0.0;后flag的值为:"<<boolalpha<<flag<<endl;
return 0;
}
例题2.2
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
a = 4;
b = a;
a = 5;
c = d = 6;
c *= a;
d %= a + b;
cout<<"a = "<<a<<endl
<<"b = "<<b<<endl
<<"c = "<<c<<endl
<<"d = "<<d<<endl;
return 0;
}
例题2.3
#include<iostream>
using namespace std;
int main()
{
int i,j,m,n;
i = 1000;
j = 1000;
m = i + j;
n = i * j;
cout<<"m = "<<m<<endl;
cout<<"n = "<<n<<endl;
return 0;
例题2.4
#include<iostream>
using namespace std;
int main()
{
int i = 6,j,k,temp;
j = ++i;
k = i++;
++i = 1;
cout<<"i = "<<i<<endl
<<"j = "<<i<<endl
<<"k = "<<k<<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;
}
第二题 求三角形的边长和面积
#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
<<"x = "<<x<<endl
<<"y = "<<y<<endl;
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a,b,c,S,C,p = (a+b+c)/2;
cout<<"请输入三角形三边的长度:"<<endl;
cin>>a>>b>>c;
if (a+b>c&&a+c>b&&b+c>a)
{
S = sqrt(p*(p-a)*(p-b)*(p-c));
C = a+b+c;
cout<<"该三角形面积S = "<<S<<endl
<<"该三角形周长C = "<<C<<endl;
}
else
{
cout<<"您所输入边长无法构成三角形,请重新输入:"<<endl;
cin>>a>>b>>c;
}
return 0;
}
第三题 课本例题
/*************课本第二章习题3第1题*************/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int e=1,f=4,g=2;
float m=10.5,n=4.0,k;
k=(e+f)/g+sqrt((double)n)*1.2/g+m;
cout<<"k = "<<k<<endl;
return 0;
}
/*************课本第二章习题3第2题*************/
#include<iostream>
using namespace std;
int main()
{
float x=2.5,y=4.7;
int a=7;
x+a%3*(int(x+y)%2)/4;
return 0;
}
第四题 一元二次方程求解
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,disc,p,q,d,x1,x2;
cout<<"请输入方程三个系数a、b、c的值:";
cin>>a>>b>>c;
disc=b*b-4*a*c;
if(a==0&&b==0)
{
cout<<"此方程无解"<<endl;
return 0;
}
else if(disc>=0)
{
d=sqrt(disc);
p=-b/(2*a);
q=d/(2*a);
x1=p+q;
x2=p-q;
cout<<"方程的实根为x1:"<<x1<<endl;
cout<<"方程的实根为x2:"<<x2<<endl;
}
else
{
float vir=sqrt(-disc);
cout<<"方程的虚根为:"<<-b/(2*a)<<"+"<<vir/(2*a)<<"i"<<endl;
cout<<"方程的虚根为:"<<-b/(2*a)<<"-"<<vir/(2*a)<<"i"<<endl;
}
return 0;
}
第五题 加密运算
#include<iostream>
using namespace std;
int main()
{
int i=1,x=1;
char name[20],a;
cout<<"请输入你的音标姓名(英文):"<<endl;
cin.get(name,20); cout<<"加密结果为"<<endl;
for(i=strlen(name);i>=0;i--)
{ x=x++;
a=name[i]+x;
cout<<a;
}
return 0;
}
加密算法为,对输入字符取反,并依次加1,加2.。。。加n(n的值为输入字符串字节长度加1)
第六题 没看懂是什么意思
第七题 (1)有时候符号经常打错,间距也容易搞错。(2)对c++的运算还不是很熟悉,有些算法搞不清楚,多看点书。(3)有些不懂的还是去百度才知道,以后尽量自己解决