您的位置:首页 > 其它

第三周作业

2014-03-23 11:21 281 查看
(1)首先的是书上的例题作业

首先的是例题2.1 因为这周的作业有点多,而且现在还不太熟练,所以我的例题都没有改动的地方,打算有空的时候再去尝试改一下,还有一个疑问。。。程序运行的那个黑框是怎么弄到这博客上面的???
#include <iostream>

#include <iomanip>
using namespace std;

int main()
{
bool flag=true;
cout<<flag<<endl;
cout<<boolalpha<<flag<<endl;
cout<<flag+5<<endl;
flag=0;
cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl;
flag=0.0;
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= "<<endl
<<"b= "<<endl
<<"c= "<<endl
<<"d= "<<endl;
return 0;
}


现在的是例题2.3 运行结果有错误,因为n的值超过的short的取值范围,所以有错误。我按照书本上的提示,把变量类型分别设为long,int两种其他类型。

#include<iostream>
using namespace std;

int main()
{
long i,j,m,n;
i=1000;
j=1000;
m=i+j;
n=i*j;
cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;

return 0;
}


#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;
j=++i;
k=i++;
++i=1;
cout<<"i="<<I<<endl
<<"J="<<J<<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;
}


现在是例题2.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<<"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()
{
float a,b,c;
cout<<"输入三角形边长a:";
cout<<"输入三角形边长b:";
cout<<"输入三角形边长c:";
cin>>a>>b>>c;
while(a+b<=c&&a+c<=b&&b+c<=a)
{
cout<<"输入错误,请重新输入三角形边长a,b,c:";
cin>>a>>b>>c;
}
float x,y,z;
x=a+b+c;
y=x/2;
z=sqrt(y*(y-a)*(y-b)*(y-c));
cout<<"三角形的周长:"<<x<<endl;
cout<<"三角形的面积:"<<z<<endl;

return 0;
}


(3)书上的题我看有部分人是将两题合起来编写的,我也试了一下,发现也可以,所以就直接把他们合在一起了。
第一题的答案为13.7,第二题答案为2.5
#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<<endl;

float x=2.5,y=4.7,z;
int a=7;
z=x+a%3*(int(x+y)%2)/4;
cout<<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;
}


第五题不知道该从何下手,而且实在是有难度,所以我借鉴了一下同学的。
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: