C++语言第三周作业

例题2.1
/******************************************* 
  **   功能:布尔类型使用举例                                 ** 
********************************************/  
#include<iostream>                                                  //编译预处理命令  
#include<iomanip>                                                   //使用控制符boolalpha需使用此头文件  
using namespace std;                                                //使用标准名空间std  
int main()                                                          //主函数  
{  
    bool flag=true;                                            //定义布尔型变量flag,并初始化为true  
    cout<<flag<<endl;                                          //默认情况下为非bool字母(noboolalpha),输出整型值1  
    cout<<boolalpha<<flag<<endl;                               //使用输入格式控制符,输出布尔型值  
    cout<<flag+5<<endl;  
    flag=0;                                                    //在算术运算中,把布尔型数据当作整型数据,输出6  
    cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl; //可以给bloo类型的变量赋任意类型的值  
    flag=0.0;                                                  //0.0为double类型的数值  
    cout<<"执行语句flag=0.0;后flag的值为:"<<boolalpha<<flag<<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;    
}    



例题2.3


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



例题2.4


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



例题2.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;    
    cout <<"The result is: "<<ch<<endl;    
    return 0;    
}    



 求三角形的边长和面积


#include<iostream>     
using namespace std;    
int main()    
{    
    char ch = 'c';    
    int a,b = 13;    
    float x,y;    
    x = y = 2.0;    
    a = ch + 5;    
    x = b / 2 / x;    
    y = b / y / 2;    
   cout<<"a = "<<a<<endl    
        <<"x = "<<x<<endl    
        <<"y = "<<y<<endl;    
    return 0;    
}    
#include<iostream>     
#include<math.h>     
using namespace std;    
int main()    
{    
    float a,b,c,S,C,p = (a+b+c)/2;    
    cout<<"请输入三角形三边的长度:"<<endl;    
    cin>>a>>b>>c;    
    
    if  (a+b>c&&a+c>b&&b+c>a)    
    {      
        S = sqrt(p*(p-a)*(p-b)*(p-c));    
        C = a+b+c;    
        cout<<"该三角形面积S = "<<S<<endl    
            <<"该三角形周长C = "<<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;    
}  


/*************课本第二章习题3第2题*************/  
#include<iostream>  
using namespace std;  
int main()  
{  
    float x=2.5,y=4.7;  
    int a=7;  
    x+a%3*(int(x+y)%2)/4;  
    return 0;  
}     



一元二次方程求解


#include<iostream>     
#include<cmath>     
using namespace std;    
    
int main()    
{    
    double a,b,c,disc,p,q,d,x1,x2;    
    cout<<"请输入方程三个系数a、b、c的值:";    
    cin>>a>>b>>c;    
    disc=b*b-4*a*c;    
    if(a==0&&b==0)    
    {    
        cout<<"此方程无解"<<endl;    
        return 0;    
    }    
    else if(disc>=0)    
    {     
        d=sqrt(disc);    
        p=-b/(2*a);    
        q=d/(2*a);    
        x1=p+q;    
        x2=p-q;    
    
        cout<<"方程的实根为x1:"<<x1<<endl;    
        cout<<"方程的实根为x2:"<<x2<<endl;    
    }    
        else     
        {    
            float vir=sqrt(-disc);    
            cout<<"方程的虚根为:"<<-b/(2*a)<<"+"<<vir/(2*a)<<"i"<<endl;    
            cout<<"方程的虚根为:"<<-b/(2*a)<<"-"<<vir/(2*a)<<"i"<<endl;    
    
        }           
        return 0;    
}


加密


#include<iostream>       
using namespace std;      
int main()      
{         
    int  i=1,x=1;      
    char name[20],a;      
    cout<<"请输入你的音标姓名(英文):"<<endl;      
    cin.get(name,20);  cout<<"加密结果为"<<endl;    
    for(i=strlen(name);i>=0;i--)      
    {    x=x++;     
        a=name[i]+x;    
        cout<<a;     
              
    }     
    return 0;     
} 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值