例2.1
/*******布尔类型使用举例*******/
#include "stdafx.h"
#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;
}
例2.2
/*******功能:赋值表达式语句的使用*******/
#include "stdafx.h"
#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 "stdafx.h"
#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 "stdafx.h"
#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 "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;
cout<<"The result is: "<<ch<<endl;
return 0;
}
例2.6
/*******自动类型转换*******/
#include "stdafx.h"
#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;
}
例题2.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
<<"ab= "<<ab<<endl
<<"c= "<<c<<endl
<<"ac= "<<ac<<endl;
return 0;
}
作业二
#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float x,y,z,H,S,C;
cout<<"请输入△的三个边长度"<<endl;
cin>>x;
cin>>y;
cin>>z;
if(x+y>z&&x+z>y&&y+z>x)
{
H=(x+y+z)/2;
S=sqrt(H*(H-x)*(H-y)*(H-z));
C=x+y+z;
cout<<"△面积S="<<S<<endl;
cout<<"△周长C="<<C<<endl;
}
else
{
cout<<"您输入的数据不能组成相应的△"<<endl;
}
return 0;
}
书本练习习题3(1)
#include "stdafx.h"
#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 "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
float x=2.5,y=4.7,m;
int a=7;
m=x+a%3*(int(x+y)%2)/4;
cout<<"m="<<m<<endl;
return 0;
}
#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a,b,c;
double x1,x2,e;
cout<<"请依次输入一元二次方程组ax^2+bx+c=0中系数a,b,c的值"<<endl;
cin>>a>>b>>c; //先求出e值方便判断是否有实根
e=b*b-4*a*c;
x1=(-b+sqrt(double(e)))/(2*a);
x2=(-b-sqrt(double(e)))/(2*a);
if(e>=0)
{
if(x1=x2)
{
cout<<"此方程只有一个解,x1=x2="<<x1<<endl;
}
else
{
cout<<"此方程有两个解,分别是:"<<x1<<endl;
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
}
else
{
cout<<"该函数不存在实根"<<endl;
}
return 0;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
char a[25],b[25],c[25],e[25]; //定义四个一维字符型数量组
int k;
cout<<"欢迎进行名字加密与解密程序"<<endl; //加密解密选择
cout<<"加密请输入“1”,解密请输入任意数字"<<endl;
cin>>k;
if(k==1) //加密
{
int i;
cout<<"请输入你要加密的名字"<<endl; //输入要加密的名字
fflush(stdin); //清除输入缓存
cin>>a; //从键盘获取字符
cout<<"该名字的加密成果为:"<<endl; //加密结果输出
for(i=0;i<25;i++)
{
if(a[i]==0)
{
break;
}
b[i]=a[i];
b[i]=b[i]+10; //加密方法
c[i]=b[i];
cout<<c[i];
}
cout<<endl;
}
else //解密
{
cout<<"请输入你要解密的名字"<<endl; //输入要解密的名字
fflush(stdin); //清除输入缓存
cin>>a;
cout<<"该名字的解密成果为:"<<endl; //解密结果输出
for(int i=0;i<25;i++)
{
if(a[i]==0)
{
break;
}
b[i]=a[i];
b[i]=b[i]-10; //解密方法
e[i]=b[i];
cout<<e[i];
}
cout<<endl;
}
return 0;
}
最后表示。。各种不懂。。。大多都是参考照打。。。找不到的那些更不会。。。
发现要在vc上运行要打#include "stdafx.h" 。。但网上一般都是省略的。。
错误一般在于漏打各种标点。。。。