2.1
/**********************************
** 功能:布尔类型使用举例 **
**********************************/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
bool flag=true;
cout<<flag<<endl;
cout<<boolalpha<<flag<<endl;
cout<<flag+5<<endl;
flag=0;
cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl;
flag=0.0;
cout<<"执行语句flag=0.0;后flag的值为:"<<boolalpha<<flag<<endl;
return 0;
}
由于浮点数在计算机内存中存贮的是近似值,所以语句flag=0.0执行之后,flag的值为true
2.2
/****************************
** 赋值表达式的语句使用 **
****************************/
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
a=1;
b=a;
a=2;
c=d=3;
c *=a;
d %=a + b;
cout<<"a = "<<a<<endl
<<"b = "<<b<<endl
<<"c = "<<c<<endl
<<"d = "<<d<<endl;
}
c*=a 即为c=c*a d%=a+b即为d=d%(a+b
2.3
/**************************************
**功能: 数据溢出举例 **
**************************************/
#include <iostream>
using namespace std;
int main()
{
short i,j,m,n;
i=20;
j=20;
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 = "<<j<<endl
<<"k = "<<k<<endl;
return 0;
}
赋值号“=”的左操作数必须为左值而i++为右值不可再“=”左边
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"请输入一个字母:";
cin>>ch;
ch = ch>='a' && ch<='z'? ch - 'a' + 'A' : ch;
cout<<"The result is: "<<ch<<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
<<"ab = "<<ab<<endl
<<"c = "<<c<<endl
<<"ac = "<<ac<<endl;
return 0;
}
编写程序输入一个三角形的三条边,计算其面积和周长
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a,b,c,d,C,S;
cout<<"请输入三角形的各边a,b,c的长度"<<endl;
cin>>a>>b>>c;
if(a+b>c&&a+c>b&&b+c>a)
{
d=(a+b+c)/2;
C=a+b+c;
S=sqrt(d*(d-a)*(d-b)*(d-c));
cout<<"三角形的周长为:"<<C<<endl;
cout<<"三角形的面积为:"<<S<<endl;
}
else
{
cout<<"数据错误,无法组成三角形,请重新输入"<<endl;
}
return 0;
}
第二章习题3
#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;
}
#include <iostream>
using namespace std;
int main()
{
float x = 2.5, y = 4.7;
int a = 7;
cout<<x+a%3*(int(x+y)%2)/4<<endl;
return 0;
}
输入一个一元二次方程的三个系数,并计算其方程的解,然后输出(不是很会)
/*************************************
** 求一元二次方程的根 **
*************************************/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double a,b,c,e,x1,x2,Re,Im;
cout<<"请输入方程各系数"<<endl
<<"请输入一次项系数a"<<endl;
cin>>a;
cout<<"请输入二次项系数b"<<endl;
cin>>b;
cout<<"请输入常数项系数c"<<endl;
cin>>c;
e=b*b-4*a*c;
if(e<0)
{
cout<<"该一元二次方程没有实根"<<endl;
Re=(-b)/(2*a);
Im=sqrt(-e)/(2*a);
cout<<"该一元二次方程的虚数根为:"<<endl;
cout<<"X1="<<Re<<"+"<<Im<<"i"<<endl;
cout<<"X2="<<Re<<"-"<<Im<<"i"<<endl;
}
else
{
x1=((-b)+sqrt(double(e)))/(2*a);
x2=((-b)-sqrt(double(e)))/(2*a);
if(x1=x2)
cout<<"该一元二次方程只有一个解,既 x="<<x1<<endl;
else
cout<<"该一元二次方程有两个解,既 x1="<<x1
<<" x2="<<x2<<endl;
}
return 0;
}
分析错误原因:对c++不够熟练,输入不够快,又频频发生错误,需多加练习,需要熟记个数据类型,运算符与表达式。