1.输入课本各个例题,调试运行程序,并分析程序,将每一个程序改写2到3个版本,自己分析程序结果,然后再调试运行,核对分析结果的对错。
2.编写程序输入一个三角形的三条边,计算其面积和周长;
3.编写程序计算并输出课本本章习题3表达式的值并分析结果。
4.编写一个程序,输入一个一元二次方程的三个系数,并计算其方程的解,然后输出。
5.编写程序,自己确定一个加密算法,将自己的音标姓名(英文)加密,并输出加密后结果,请注释你的加密算法。
6.在一个自动控制设备中,控制字位数16位,控制设备产生机械动作(如削,压等)的是指令字的低8位,其中保护强制停机动作的控制命令是低8位是全为0,控制报警声音是指令的高第1位,0为报警,1为不报警。请编写程序,在紧急状况启动时,向控制器输入控制指令。
7.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。
1.1
#include<iostream>
using namespace std;
int main()
{
cout<<"number of bytes in int is:"<<sizeof(int)<<endl;
cout<<"number of bytes in long is:"<<sizeof(long)<<endl;
cout<<"number of bytes in short is:"<<sizeof(short)<<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 long double:"<<sizeof(long double)<<endl;
cout<<"number of bytes in char is:"<<sizeof(char)<<endl;
return 0;
}
1.2
#include<iostream>
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<<flag<<endl;
flag = 0.0;
cout<<"执行语句flag=0.0;后flag的值为:"<<boolalpha<<flag<<endl;
return 0;
}
1.3
#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;
}
1.4
#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;
}
1.5
#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;
}
1.6
#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;
}
2
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a,b,c,p,s,C;
cout<<"请输入三角形的三边长度:"<<endl;
cin>>a>>b>>c;
if(a+b>c&&a+c>b&&b+c>a)
{
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
c=a+b+c;
cout<<"三角形的面积是:"<<s<<endl;
cout<<"三角形的周长是: "<<c<<endl;
}
else
{
cout<<"您输入的三边长度不能构成三角形,请再输入:"<<endl;
cin>>a>>b>>c;
}
return 0;
}
3.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;
}
k=13.7
3.2
#include<iostream>
#include <math.h>
using namespace std;
int main()
{
int a=7;
float x=2.5,y=4.7,z;
z=x+a%3*(int(x+y)%2)/4;
cout<<"z="<<z<<endl;
return 0;
}
z=2.5
4
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double a,b,c,x1,x2,t,Re,Im;
cout<<"请分别输入方程二次项、一次项、常数项系数"<<endl;
cin>>a>>b>>c;
t=b*b-4*a*c;
if(t>=0)
{
x1=(-b+sqrt(t))/(2*a);
x2=(-b-sqrt(t))/(2*a);
cout<<"x1="<<x1<<endl
<<"x2="<<x2<<endl;
}
else
{
Re=(-b)/(2*a);
Im=sqrt(-t)/(2*a);
cout<<"X1="<<Re<<"+"<<Im<<"i"<<endl;
cout<<"X2="<<Re<<"-"<<Im<<"i"<<endl;
}
return 0;
}
5
#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;
}
6
#include<iostream>
#include <bitset>
using namespace std;
int main()
{
short a,b;
cout<<"信号输入(输入一个两位数,可以是字母)"<<endl;
cin>>a;
cout<<bitset<16>(a)<<endl;
b=0xff00;
a=a&b;
cout<<"a="<<a<<endl;
cout<<bitset<16>(a)<<endl;
return 0;
}
总结错误原因
1.打代码时粗心打错打漏;
2.调用函数没打入头文件;
3.有时分析不出代码的错误之处,不知道如何改正。