C++第二次作业

来源:互联网 发布:bearychat mac 编辑:程序博客网 时间:2024/04/28 13:03

作业要求:

       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(){ int a, b, c, d; a = 5; b = a; a = 6; c = d = 7; c *= a; d %= a + b + c; cout<<"a = "<<a<<endl  <<"b = "<<b<<endl  <<"c = "<<c<<endl  <<"d = "<<d<<endl; return 0;}

错误分析:“ * ”、“ % ”与“ = ”间不能有空格。

 

(2)

# include <iostream>using namespace std;int main(){ int i, j, m, n; i = 100; j = 100; m = i + j; n = i * j; cout<<"m = "<<m<<endl; cout<<"n = "<<n<<endl; return 0;}

错误分析:short类型取值范围较小,上述程序将i,j值改为100。(或者将short类型改成int或long类型。)


(3)

#include<iostream>  #include<iomanip>using namespace std;  int main()  {      bool a = true;    cout<<a<<endl;     cout<<boolalpha<<a<<endl;    cout<<a + 5<<endl;     a=0;      cout<<"执行语句a=0;后a的值为:"<<boolalpha<<a<<endl;      a=0.0;     cout<<"执行语句a=0.0;后a的值为:"<<boolalpha<<a<<endl;        return 0;  }  

(4)

#include<iostream>  using namespace std;    int main()  {      int i = 10, j, k, l, n;      j = --i;    k = i++;l = i--;n = ++i;    ++i = 1;    cout<<"i = "<<i<<endl          <<"j = "<<j<<endl          <<"k = "<<k<<endl<<"l = "<<l<<endl<<"n = "<<n<<endl;        return 0;  }  


(5)

#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;      //上述语句等价于ch = ch>= 'a'&&ch<= 'z'?ch - 32:ch;     cout<<"The result is: "<<ch<<endl;        return 0;  } 


(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(){double x, y, z, k, C, S;        cout<<"请输入三边的长度:"<<endl;          cin>>x;          cin>>y;          cin>>z;     if(x+y>z&&x+z>y&&y+z>x){C = x + y + z;                k = (x + y + z)/2;S = sqrt(k*(k-x)*(k-y)*(k-z));                cout<<"三角形的周长是: "<<C<<endl;                  cout<<"三角形的面积是: "<<S<<endl;}else{cout<<"输入有误"<<endl;}        return 0;}

错误分析:1、要使得“sqart()”在程序中有意义,必须用到数学调用,头文件形式为“<math.h>”;

                   2、“if”、“else”后面不能带分号。



3.

#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;  }  


4.

#include <iostream>  #include <math.h>using namespace std;    int main()  {      double a, b, c, k, x1, x2;        cout<<"请输入二次项系数a:";      cin>>a;      cout<<"请输入一次项系数b:";      cin>>b;      cout<<"请输入常数项c:";      cin>>c;    k = b*b-4*a*c;      if(k>=0)      {          x1=(-b+sqrt(k))/(2*a);          x2=(-b+sqrt(k))/(2*a);          cout<<"该方程的两个根为:"<<endl;cout<<"x1="<<x1<<endl;    cout<<"x2="<<x2<<endl;    }      else{        cout<<"该方程无实数解"<<endl;}    return 0;    }  


 


 

0 0
原创粉丝点击