1.输入课本各个例题,调试运行程序,并分析程序,将每一个程序改写2到3个版本,自己分析程序结果,然后再调试运行,核对分析结果的对错。
2.编写程序输入一个三角形的三条边,计算其面积和周长;
3.编写程序计算并输出课本本章习题3表达式的值并分析结果。
4.编写一个程序,输入一个一元二次方程的三个系数,并计算其方程的解,然后输出。
5.编写程序,自己确定一个加密算法,将自己的音标姓名(英文)加密,并输出加密后结果,请注释你的加密算法。
6.在一个自动控制设备中,控制字位数16位,控制设备产生机械动作(如削,压等)的是指令字的低8位,其中保护强制停机动作的控制命令是低8位是全为0,控制报警声音是指令的高第1位,0为报警,1为不报警。请编写程序,在紧急状况启动时,向控制器输入控制指令。
7.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。
例题1
// 43444.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#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
#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;
}
例题3
#include <iostream>
using namespace std;
int main()
{ int a,b,c,d;
a=4;
b=a;
a=5;
c=d=6;
d%=a+b;
cout<<"a="<<a<<endl
<<"b="<<<b<endl
<<"c="<<c<<endl
<<"d="<<d<<endl;
return 0;
}
例题4
#include <iostream>
usong namspace 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;
}
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"please input a charcter:";
cin>>ch:
ch=ch>='a'&&ch<='z'?ch-'a'+'A':ch;
cout<<"The result is:"<<ch<<endl;
rreturn0;
}
例题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;
}
输入一个三角形的三条边,计算其面积和周长
// TZK.cpp : Defines the entry point for the console application.
//
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,m,s;
cout<<"请分别输入三角形三边:"<<endl;
cin>>a>>b>>c;
m=(a+b+c)/2;
if((a+b>c)||(a+c>b)||(b+c>a))
{
s=sqrt(m*(m-a)*(m-b)*(m-c));
c=a=b=c
cout<<"该三角形面积为"<<s<<endl;
cout<<"该三角形周长为"<<c<<endl;
}
else
cout<<"无法组成三角形";
return 0;
}
习题3
(1)(2)在同一个程序里
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int e=1,f=4,g=2,a=7;
float m=10.5,n=4.0,k;
float x=2.5,y=4.7,z;
k=(e+f)/g+sqrt((double)n)*1.2/g+m;
cout<<"k="<< k<<endl
<<"z="<<z<<endl;
return 0;
}
编写一个程序,输入一个一元二次方程的三个系数,并计算其方程的解,然后输出
#include<iostream.h>
#include<math.h>
void main()
{
double a,b,c,d,e,x1,x2;
cout<<"请输入要求解的一元二次方程:"<<endl;
cout<<"a: ";
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"c: ";
cin>>c;
cout<<a<<"*x*x"<<"+"<<b<<"*x"<<"+"<<c<<"=0"<<endl;
d=b*b-4*a*c;
if(d<0)
{
cout<<"There is no x."<<endl;
}
if(d==0)
{
cout<<"无解."<<endl;
x1=x2=(-b)/(2*a);
cout<<"x1=x2="<<x1<<endl;
}
if(d>0)
{
e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
return 0;
}
}
编写程序,自己确定一个加密算法,将自己的音标姓名(英文)加密(字母减去1)
#include <iostream>
using namespace std;
int main()
{
int n;
char a[]={"tzk"};
n=sizeof(a)-1;
for (int i=0;i<n;i++)
{
a[i]=a[i]-1;
cout<<a[i];
}
.在一个自动控制设备中,控制字位数16位,控制设备产生机械动作(如削,压等)的是指令字的低8位,其中保护强制停机动作的控制命令是低8位是全为0,控制报警声音是指令的高第1位,0为报警,1为不报警。请编写程序,在紧急状况启动时,向控制器输入控制指令。
这题参考了同学的也看不懂,希望能在课堂上讲一讲
收集错误信息原因
1.TZK.cpp
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(9) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(15) : error C2143: syntax error : missing ';' before '}'
执行 cl.exe 时出错.(忘记分号了)
2.TZK.cpp
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(8) : error C2065: 'fliat' : undeclared identifier
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(8) : error C2146: syntax error : missing ';' before identifier 'm'
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(8) : error C2065: 'm' : undeclared identifier
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(8) : error C2065: 'n' : undeclared identifier
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(8) : error C2065: 'k' : undeclared identifier
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(9) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(14) : error C2018: unknown character '0xa3'
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(14) : error C2018: unknown character '0xbb'
F:\Program Files\Microsoft Visual Studio\MyProjects\TZK\TZK.cpp(15) : error C2143: syntax error : missing ';' before '}'
执行 cl.exe 时出错.(函数输入错误)