例2.1
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
bool f=true;
cout<<f<<endl;
cout<<boolalpha<<f<<endl;
cout<<f+5<<endl;
f=0;
cout<<"执行语句f=0;后f的值为:"<<boolalpha<<f<<endl;
f=0.0;
cout<<"执行语句f=0.0;后f的值为:"<<boolalpha<<f<<endl;
return 0;
}
例2.2
# include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
a=4;
b=a; //将b的值赋给a
a=5;
c=d=6; //先对d赋值,再对c赋值
c*=a; //c=c*a
d%=a+b; //d=d%(a+b)
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
cout<<"d="<<d<<endl;
return 0;
}
例2.3
#include <iostream>
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>
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>
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,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<<"运算结果如下:"<<endl
<<"a="<<a<<endl
<<"b="<<b<<endl
<<"x="<<x<<endl
<<"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;
}
三角形周长和面积的计算
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a,b,c,d,C,S;
cout<<"请输入三角形的三边长度"<<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;
}
习题三,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;
}
习题三,2
#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;
}
作业6不懂怎样做,等以后学多点知识了再补上 。