0
#include<iostream>
using namespace std;
int main()
{
cout<<"number of bytes in int is: "<<sizeof(int)<<endl;
cout<<"number of bytes in int is: "<<sizeof(int)<<endl;
cout<<"number of bytes in long int is: "<<sizeof(long)<<endl;
cout<<"number of bytes in short int is: "<<sizeof(short)<<endl;
cout<<"number of bytes in unsigned int is: "<<sizeof(unsigned int)<<endl;
cout<<"number of bytes in float is: "<<sizeof(float)<<endl;
cout<<"number of bytes in double is: "<<sizeof(double)<<endl;
cout<<"number of bytes in char is: "<<sizeof(char)<<endl;
cout<<"number of bytes in bool is: "<<sizeof(bool)<<endl;
}
2-1
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
bool flag=true;
cout<<flag<<endl;
cout<<boolalpha<<flag<<endl;
cout<<flag+5.00<<endl;
flag=0;
cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl;
flag=0.0;
cout<<"执行语句flag=0.0;后flag的值为:"<<boolalpha<<flag<<endl;
flag=2;
cout<<"执行语句flag=2;后flag的值为:"<<boolalpha<<flag<<endl;
return 0;
}
2-2
#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)/3;
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;
long 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+2;
k=i+++2;
++i=1;
cout<<"i= "<<i<<endl;
cout<<"j= "<<j<<endl;
cout<<"k= "<<k<<endl;
return 0;
}
2-5
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"please input a character: "<<endl;
cin>>ch;
ch=ch<='a'&&ch>='A'?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<<"a= "<<a<<endl;
cout<<"b= "<<b<<endl;
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;
return 0;
}
2-7
#include<iostream>
using namespace std;
int main()
{
int ab,ac;
float 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<cmath>
using namespace std;
int main()
{
float a,b,c,p;
cout<<"请分别输入三角形的三条边长a,b,c:"<<endl;
cin>>a>>b>>c;
p=(a+b+c)/2;
cout<<"所输入的三角形面积为:"<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;
cout<<"所输入的三角形周长为:"<<2*p<<endl;
return 0;
}
3-1
#include<iostream>
#include<cmath>
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;//(e+f)/g=2,sqrt((double)n)*1.2/g=1.2
cout<<"k="<<(e+f)/g+sqrt((double)n)*1.2/g+m<<endl;
return 0;
}
3-2
#include<iostream>
using namespace std;
int main()
{
float k,x=2.5,y=4.7;
int a=7;
k=x+a%3*(int(x+y)%2)/4;
cout<<"k= "<<x+a%3*(int(x+y)%2)/4<<endl;//int(x+y)%2=1,a%3*1=1,1/4=0
return 0;
}
输入一个一元二次方程的三个系数,并计算其方程的解,然后输出
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float a,b,c,d,x1,x2;
cout<<"请依次输入一个一元二次方程的三个系数a,b,c: "<<endl;
cin>>a>>b>>c;
d=b*b-4*a*c;
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
cout<<"一元二次方程的第一个解x1= "<<x1<<endl;
cout<<"一元二次方程的第二x2= "<<x2<<endl;
return 0;
}
编写程序,自己确定一个加密算法,将自己的音标姓名(英文)加密,并输出加密后结果,请注释你的加密算法
#include<iostream>
using namespace std;
int main()
{
float a;
cout<<"请输入查看本人音标姓名(英文)的密码:"<<endl;
cin>>a;
if(a==123456)
cout<<"本人音标姓名(英文)为:Huang Mingwu"<<endl;
else
cout<<"您输入的密码错误,请重新输入"<<endl;
return 0;
}
在一个自动控制设备中,控制字位数16位,控制设备产生机械动作(如削,压等)的是指令字的低8位,其中保护强制停机动作的控制命令是低8位是全为0,控制报警声音是指令的高第1位,0为报警,1为不报警。请编写程序,在紧急状况启动时,向控制器输入控制指令
#include<iostream>
using namespace std;
int main()
{
long float a,b;
cout<<"请输入紧急状况启动时的输入控制指令:"<<endl;
cin>>a;
if(a==1111111000000000)
cout<<"报警!!!"<<endl;
else
cout<<"您输入的指令有错误,请重新输入"<<endl;
return 0;
}
出现错误次数较多的是:
1、没有先定义就开始使用
2、表达式写不标准。例如3乘以p,我会直接写成3p,而不是3*p
3、有时定义错变量的类型,以至于出现乱码的情况