第三周作业

一:课本例题

例2.1

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    bool flag=true;  
  7.    cout<<flag<<endl;  
  8.    cout<<boolalpha<<flag<<endl;  
  9.    cout<<flag+5<<endl;  
  10.    flag=0;  
  11.    cout<<"执行语句flag=o;后flag的值为:"<<boolalpha<<flag<<endl;  
  12.    flag=0.0;  
  13.    cout<<"执行语句flag=0.0后flag的值为:"<<boolalpha<<flag<<endl;  
  14.        return 0;  
  15.   
  16. }  


例如2.2

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    int a,b,c,d;  
  7.    a=4;  
  8.    b=a;  
  9.    a=5;  
  10.    c=d=6;  
  11.    c*=a;  
  12.    d%=a+b;  
  13.    cout<<"a="<<a<<endl  
  14.        <<"b="<<b<<endl  
  15.        <<"c="<<c<<endl  
  16.        <<"d="<<d<<endl;  
  17.        return 0;  
  18.   
  19. }  

例2.3

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    short i,j,m,n;  
  7.    i=1000;  
  8.    j=1000;  
  9.    m=i+j;  
  10.    n=i*j;  
  11.   
  12.    cout<<"m="<<m<<endl;  
  13.    cout<<"n="<<n<<endl;  
  14.        return 0;  
  15.   
  16. }  

例2.4

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    int i=6,j,k,temp;  
  7.    j=++i;  
  8.    k=i++;  
  9.    ++i=1;  
  10.    cout<<"i="<<i<<endl  
  11.        <<"j="<<j<<endl  
  12.        <<"k="<<k<<endl;  
  13.        return 0;  
  14.   
  15. }  


例2.5

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    char ch;  
  7.    cout<<"please input a character:";  
  8.    cin>>ch;  
  9.    ch=ch>='a'&&ch<='z'?ch-'a'+'A':ch;  
  10.   
  11.    cout<<"The result is:"<<ch<<endl;  
  12.        return 0;  
  13.   
  14. }  
二: .编写程序输入一个三角形的三条边,计算其面积和周长

#include <iostream.h>
#include <math.h>
int main()
{
	double a,b,c,s,zc,mj;     
	cout<<"请输入第一条边:";
	cin>>a;
    cout<<"请输入第二条边:";
    cin>>b;
    cout<<"请输入第三条边:";
	cin>>c;
	if(a+b>c && a+c>b && b+c>a)
    {
		zc=a+b+c
		s=(a+b+c)/2;
		mj=sprt(s*(s-a)*(s-b)*(s-c));
		cout<<"所输入的三条边组成三角形的周长&面积:"<<zc<<mj<<endl;
    }
	else
		cout<<所输入的三条边不能组成三角形!"<<endl;
	return 0;
}

. 编写程序计算并输出课本本章习题 3 表达式的值并分析结果 

no.1

#include<iostream>    
#include <math.h>    
using namespace std;    
    
int main()    
{    
    int e=1,f=4,g=2;    
    double m=10.5,n=4.0,k;    
    k=(e+f)/g+sqrt((double)n)*1.2/g+m;
    cout<<k<<endl;
	return 0;
}

no.2

#include<iostream>    
#include <math.h>    
using namespace std;    
    
int main()    
{    
    double x=2.5,y=4.7,z;    
    int a=7;    
    z=x+a%3*(int(x+y)%2)/4;    
    cout<<z<<endl;  
    
    return 0;   
 }  

四: 编写一个程序求一元二次方程的解

#include <iostream.h>
#include <math.h>
void main(void)
{
double a,b,c,d;
char ch('y');
do{
cout<<"请依次输入二次方程ax^2+bx+c=0的系数(a不等于0):"<<endl;
cin>>a>>b>>c;
if(-0.0001<a<0.0001)                    //浮点数不宜直接判断相等
{cout<<"不是二次方程"<<endl;continue;}
d=b*b-4*a*c;
if(d==0)cout<<"方程有两个相等实根:"<<-b/(2*a)<<endl;
else if(d<0)cout<<"方程有两个不相等复根:"<<-b/(2*a)<<"+i"<<sqrt(-d)/(2*a)<<' '<<-b/(2*a)<<"-i"<<sqrt(-d)/(2*a)<<endl;
else cout<<"方程有两个不相等实根:"<<-b/(2*a)+sqrt(d)/(2*a)<<' '<<-b/(2*a)+sqrt(d)/(2*a)<<endl;
cout<<"继续?(y/n):";cin>>ch;
}while(ch=='y'||ch=='Y');
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值