1.输入课本各个例题,调试运行程序,并分析程序,将每一个程序改写2到3个版本,自己分析程序结果,然后再调试运行,核对分析结果的对错。
2.编写程序输入一个三角形的三条边,计算其面积和周长;
3.编写程序计算并输出课本本章习题3表达式的值并分析结果。
4.编写一个程序,输入一个一元二次方程的三个系数,并计算其方程的解,然后输出。
5.编写程序,自己确定一个加密算法,将自己的音标姓名(英文)加密,并输出加密后结果,请注释你的加密算法。
6.在一个自动控制设备中,控制字位数16位,控制设备产生机械动作(如削,压等)的是指令字的低8位,其中保护强制停机动作的控制命令是低8位是全为0,控制报警声音是指令的高第1位,0为报警,1为不报警。请编写程序,在紧急状况启动时,向控制器输入控制指令。
7.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。
例一
// 例2.1.cpp : Defines the entry point for the console application.
//
#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;
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;
}
例三
// 例三.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
int mian()
{
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;
}
例四
// 例四.cpp : Defines the entry point for the console application.
//
#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;
}
例五
// 例五.cpp : Defines the entry point for the console application.
//
#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;
}
例七
// 例七.cpp : Defines the entry point for the console application.
//
#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;
}
试验结果
请输入三边的长度:
8
9
10
该三角形的周长为:27
该三角形的面积为:34.197
Press any key to continue
习题(1)
// 习题三(1).cpp : Defines the entry point for the console application.
//
#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;
}
结果
运算结果k为:13.7
Press any key to continue
习题(2)
// 习题三(2).cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float x=2.5,y=4.7,z;
int a=7;
z=x+a%3*(int(x+y)%2)/4;
cout<<"运算结果z为:"<<z<<endl;
return 0;
}
结果
运算结果z为:2.5
Press any key to continue
一元二次方程求实数解简单程序
// *******************************
***一元二次方程求实数解简单程序***
**********************************//
#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double a,b,c,k1,k2,z;
cout<<"请输入ax2+bx+c=0中的a:";
cin>>a;
cout<<"请输入ax2+bx+c=0中的b:";
cin>>b;
cout<<"请输入ax2+bx+c=0中的c:";
cin>>c;
z=sqrt(b*b-4*a*c);
if(z>=0)
{
k1=(-b+z)/(2*a);
k2=(-b+z)/(2*a);
cout<<"该方程的两个根k1,k2分别是:"<<k1<<"和"<<k2<<endl;
}
else
cout<<"该方程没实数解"<<endl;
return 0;
}