第三周作业

来源:互联网 发布:mac出现五国怎么解决 编辑:程序博客网 时间:2024/06/05 18:48
例题1  /***************************** ***功能:布尔类型使用举例   ** ******************************/  #include<iostream>    //编译预处理命令  #include<iomanip>   //使用控制符boolalpha需使用此头文件  using namespace std;  //使用标准名字空间 std    int main()        //主函数  {      bool flag=true; //定义布尔型变量flag,并初始化为ture      cout<<flag<<endl;  //默认情况下为bool字母(noboolalpha),输出布尔型值 1      cout<<boolalpha<<flag<<endl;  //使用输出格式控制符boolalpha,输出布尔值      cout<<flag+5<<endl;  //在算术运算中,把布尔数据当作整型数据,输出6      flag=0;  //可以给bool类型的变量赋任意类型的值      cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl;      flag=0.0;  //0.0为double类型的数值      cout<<"执行语句flag=0.0;后flag的值为:"<<boolalpha<<flag<<endl;        return 0;  }     例题2  /***************************** ***功能:赋值表达式语句的使用   ** ******************************/  #include<iostream>    //编译预处理命令  #include<iomanip>   //使用控制符boolalpha需使用此头文件  using namespace std;  //使用标准名字空间 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;      例题3  #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;   }        例题4  #include<iostream>  using namespace std;    int main()  //主函数  {     int i=6,j,k,temp;       j=++i;  //先对变量i自增,i的值变为7,之后把id值7赋给变量j     k=j++;  //先把变量i的值7赋给变量k,然后i的值自增,i的值自增,i的值变为8     ++i=1;  //++i可以作为左值,执行完该语句后变量i的值为i     cout<<"i="<<i<<endl         <<"j="<<j<<endl         <<"k="<<k<<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;  }    例题7  #include<iostream>  using namespace std;  int main()  {    int ab,ac;    double b=3.14;    char c='A';    ab=int(b);    ac=int(c);    cout<<"a="<<b<<endl;    cout<<"ab="<<ab<<endl;    cout<<"c="<<c<<endl;    cout<<"ac="<<ac<<endl;    return 0;  }  


主观题

作业2

/*****计算三角形的周长与面积******/   #include <iostream>  #include <cmath>  using namespace std;  int main()  {float a,b,c,d,e;  do  {  cout<<"分别输入三条边的长度"<<endl;  cin>>a>>b>>c;  d=(a+b+c)/2;  e=sqrt(d*(d-a)*(d-b)*(d-c));      cout<<"数据有误"<<endl;  }  while(a+b<c||a+c<b||b+c<a);  {cout<<"三角形周长="<<2*d<<endl;  cout<<"三角形面积="<<e<<endl;}  return 0;  }    调用sqrt函数时需使用#include <cmath>  输入数据不满足构成三角形时报错,并使用do-while语句提示重新输入  


作业3
#include<iostream>    #include<math.h>    using namespace std;        int main()    {        int a=7,e=1,f=4,g=2;        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;                      z=x+a%3*(int(x+y)%2)/4;                cout<<"k="<< k<<endl            <<"z="<<z<<endl;        return 0;    }    

作业4

#include<iostream>    #include<math.h>      using namespace std;      int main()      {          double x1,x2,a,b,c,d;          cout<<"输入方程x^2+bx+c=0中的a:";          cin>>a;          cout<<"输入方程x^2+bx+c=0中的b:";          cin>>b;          cout<<"输入方程x^2+bx+c=0中的c:";          cin>>c;          d=sqrt(b^2-4*a*c);          if(d>0)          {              x1=(-b+d)/(2*a);              x2=(-b-d)/(2*a);              cout<<"方程有两个根,且为:x1="<<x1<<",x2="<<x2<<endl;          }          if(d=0)          {              x1=x2=(-b)/(2*a);              cout<<"方程有两个根,且为: x1="<<x1<<",x2="<<x2<<endl;          }          if(d<0)          {              cout<<"该方程无解!"<<endl;          }          return 0;    }    

作业5

include<iostream>    #using namespace std;        int main()          {              char a[50],b[50];                          int i;             cout<<"输入姓名拼音"<<endl;               cin.get(a,50);                     cout<<"加密后"<<endl;            for(i=0;i<50;i++)            {                  if(a[i]==0)                  {                      break;                  }                  b[i]=a[i];                 b[i]=b[i]+5;                 cout<<b[i];             }              cout<<endl;                                  return 0;       }    


0 0
原创粉丝点击