一:课本例题
例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=o;后flag的值为:"<<boolalpha<<flag<<endl;
- flag=0.0;
- cout<<"执行语句flag=0.0后flag的值为:"<<boolalpha<<flag<<endl;
- return 0;
- }
例如2.2
- #include<iostream>
- #include<iomanip>
- 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>
- #include<iomanip>
- using namespace std;
- int main()
- {
- short 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>
- #include<iomanip>
- 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;
- }
例2.5
- #include<iostream>
- #include<iomanip>
- 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.h>
#include <math.h>
int main()
{
double a,b,c,s,zc,mj;
cout<<"请输入第一条边:";
cin>>a;
cout<<"请输入第二条边:";
cin>>b;
cout<<"请输入第三条边:";
cin>>c;
if(a+b>c && a+c>b && b+c>a)
{
zc=a+b+c
s=(a+b+c)/2;
mj=sprt(s*(s-a)*(s-b)*(s-c));
cout<<"所输入的三条边组成三角形的周长&面积:"<<zc<<mj<<endl;
}
else
cout<<所输入的三条边不能组成三角形!"<<endl;
return 0;
}
三 . 编写程序计算并输出课本本章习题 3 表达式的值并分析结果
no.1
#include<iostream>
#include <math.h>
using namespace std;
int main()
{
int e=1,f=4,g=2;
double m=10.5,n=4.0,k;
k=(e+f)/g+sqrt((double)n)*1.2/g+m;
cout<<k<<endl;
return 0;
}
no.2
#include<iostream>
#include <math.h>
using namespace std;
int main()
{
double x=2.5,y=4.7,z;
int a=7;
z=x+a%3*(int(x+y)%2)/4;
cout<<z<<endl;
return 0;
}
四: 编写一个程序求一元二次方程的解
#include <iostream.h>
#include <math.h>
void main(void)
{
double a,b,c,d;
char ch('y');
do{
cout<<"请依次输入二次方程ax^2+bx+c=0的系数(a不等于0):"<<endl;
cin>>a>>b>>c;
if(-0.0001<a<0.0001) //浮点数不宜直接判断相等
{cout<<"不是二次方程"<<endl;continue;}
d=b*b-4*a*c;
if(d==0)cout<<"方程有两个相等实根:"<<-b/(2*a)<<endl;
else if(d<0)cout<<"方程有两个不相等复根:"<<-b/(2*a)<<"+i"<<sqrt(-d)/(2*a)<<' '<<-b/(2*a)<<"-i"<<sqrt(-d)/(2*a)<<endl;
else cout<<"方程有两个不相等实根:"<<-b/(2*a)+sqrt(d)/(2*a)<<' '<<-b/(2*a)+sqrt(d)/(2*a)<<endl;
cout<<"继续?(y/n):";cin>>ch;
}while(ch=='y'||ch=='Y');
}