例题1:
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
bool f=true;
cout<<f<<endl;
cout<<boolalpha<<f<<endl;
cout<<f+6<<endl;
f=0;
cout<<"执行语句f=0;后f的值为:"<<boolalpha<<f<<endl;
f=0.0;
cout<<"执行语句f=0.0;后f的值为:"<<boolalpha<<f<<endl;
return 0;
}
例题2:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
a=4;
b=a;
a=4;
c=d=9;
c*=a;
d%=a+b;
cout<<"a="<<a<<endl
<<"b="<<b<<endl
<<"c="<<c<<endl
<<"d="<<d<<endl;
return 0;
}
例题3:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
short i,j,m,n;
i=100;
j=100;
m=i+j;
n=i*j;
cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;
return 0;
}
例题4:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int i=6,j,k;
j=++i;
k=i++;
++i=1;
cout<<"i="<<i<<endl
<<"j="<<j<<endl
<<"k="<<k<<endl;
return 0;
}
例题5:
#include "stdafx.h"
#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;
return 0;
}
例题7: #include "stdafx.h"
#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 "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double a,b,c,t,
V,S;//a,b,c,为三边长,V是边长,S为面积。
cout<<"请输入三边的长度:"<<endl;
cin>>a>>b>>c;
if((a+b<c)||(a+c<b)||(b+c<a))//
cout<<"该三边不能构成三角形"<<endl;
else
{
V=a+b+c;
t=V/2;
S=sqrt(t*(t-a)*(t-b)*(t-c));//
cout<<"该三角形的周长为:"<<V<<endl;
cout<<"该三角形的面积为:"<<S<<endl;
}
return 0;
}
习题1:
#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int e=2,f=3,g=4;
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 "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float x=3.5,y=6.5,z;
int a=7;
z=x+a%3*(int(x+y)%2)/4;
cout<<"运算结果z为:"<<z<<endl;
return 0;
}