西安交大C++的作业答案.docx

上传人:b****8 文档编号:10934301 上传时间:2023-02-23 格式:DOCX 页数:47 大小:22.23KB
下载 相关 举报
西安交大C++的作业答案.docx_第1页
第1页 / 共47页
西安交大C++的作业答案.docx_第2页
第2页 / 共47页
西安交大C++的作业答案.docx_第3页
第3页 / 共47页
西安交大C++的作业答案.docx_第4页
第4页 / 共47页
西安交大C++的作业答案.docx_第5页
第5页 / 共47页
点击查看更多>>
下载资源
资源描述

西安交大C++的作业答案.docx

《西安交大C++的作业答案.docx》由会员分享,可在线阅读,更多相关《西安交大C++的作业答案.docx(47页珍藏版)》请在冰豆网上搜索。

西安交大C++的作业答案.docx

西安交大C++的作业答案

第一章

1-2

#include

usingnamespacestd;

intmain()

{

doublea,b,c;

cout<<"请输入长:

";

cin>>a;

cout<<"请输入宽:

";

cin>>b;

cout<<"请输入高:

";

cin>>c;

doubles,v;

s=a*b*2+b*c*2+a*c*2;

v=a*b*c;

cout<<"长方体的表面积s="<

cout<<"长方体的体积v="<

return0;

}

1-7

#include

#include

usingnamespacestd;

intmain()

{

doublen,y;

cout<<"请输入n:

";

cin>>n;

y=pow((1.0+1.0/n),n);

cout<<"y="<

return0;

}

1-8

#include

#include

usingnamespacestd;

intmain()

{

doublex,y;

cout<<"请输入一个大于0的数x:

";

cin>>x;

y=sin(x)-log(x)+sqrt(x)-5;

cout<<"y="<

return0;

}

1-9

#include

#include

usingnamespacestd;

intmain()

{

doublex,y;

cout<<"请输入x:

";

cin>>x;

if((x*x-3*x+2)<=0)

return1;

else

{

y=x/(sqrt(x*x-3*x+2));

cout<<"y="<

}

return0;

}

 

第二章

2-2

#include

usingnamespacestd;

intmain()

{

doublec,f;

cout<<"请输入一个摄氏温度:

"<

cin>>c;

f=9.0*c/5.0+32;

cout<<"对应于的华氏温度为:

"<

return0;

}

2-3

#include

#include

usingnamespacestd;

intmain()

{

doublex,y;

cout<<"输入x:

";

cin>>x;

y=sin(x*x)/(1-cos(x));

cout<<"y="<

return0;

}

 

2-5

#include

#include

usingnamespacestd;

intmain()

{

intm,n,k;

doublei,j;

cout<<"请输入三个整数:

";

cin>>m>>n>>k;

i=m+n+k;

j=i/3;

k=j+0.5;

cout<<"和为:

"<

cout<<"平均值为:

"<

cout<<"平均值的四舍五入整数值:

"<

return0;

}

2-7

#include

usingnamespacestd;

intmain()

{

charch;

for(inti=0;i<3;i++)

{

cout<<"请输入一个字母:

"<

cin>>ch;

if(ch>='A'&&ch<='Z')

{

ch=ch-'A'+'a';

cout<<"大小写转换后为:

"<

}

elseif(ch>='a'&&ch<='z')

{

ch=ch-'a'+'A';

cout<<"大小写转换后为:

"<

}

else

{

ch=ch;

cout<<"大小写转换后为:

"<

}

}

return0;

}

第三章

3-4

#include

#include

usingnamespacestd;

intmain()

{

doubley,u;

inti=1,n;

cout<<"请输入n:

";

cin>>n;

if(n<0)

cout<<"输入不合法"<

elseif(n=0)

{

cout<<"y="<<1<

}

else

{

while(i<=n,i++)

u=u*i;

cout<<"y="<

}

return0;

}

3-5

#include

usingnamespacestd;

intmain()

{

intu=0;

inti=1;

intn;

intk=1;

cout<<"请输入n:

";

cin>>n;

while(k<=n)

{

u=u+i;

i=i+2;

k++;

}

cout<<"和为:

"<

return0;

}

3-10

#include

#include

usingnamespacestd;

intmain()

{

doubleu=0,k=1,i=1;

intn;

cout<<"请输入n:

";

cin>>n;

while(i<=n)

{

u=u+k;

i=i+1;

k=k*i;

}

cout<<"结果为:

"<

return0;

}

3-13

#include

#include

usingnamespacestd;

intmain()

{

inta,n,k,u=0,i=1,j=1;

cout<<"请输入a,n:

";

cin>>a>>n;

k=a;

while(i<=n)

{

u=u+k;

k=k+10*j*a;

j=j*10;

i=i+1;

}

cout<<"结果为:

"<

return0;

}

 

第四章

4-1

#include

usingnamespacestd;

intmain()

{

intM[4][4]=

{

5,6,7,9,

2,8,5,4,

3,7,16,15,

1,4,8,11,

};

intMt[4][4];

inti,j;

for(i=0;i<4;i++)

for(j=0;j<4;j++)

{

Mt[i][j]=M[j][i];

}

cout<<"矩阵a的转置阵为:

"<

for(i=0;i<4;i++)

{

for(j=0;j<4;j++)

cout<

cout<

}

return0;

}

4-3

#include

#include

usingnamespacestd;

intfac(intn)

{

intresult=1;

while(n>1)

{

result*=n;

n=n-1;

}

returnresult;

}

intmain()

{

intn;

cout<<"请输入行数:

"<

cin>>n;

for(intl=0;l

{

for(intm=0;m<=l;m++)

cout<

cout<

}

return0;

}

4-7

#include

#include

usingnamespacestd;

char*my(char*string)

{

char*ptr=string;

while(*ptr!

=0)

ptr++;

do

{

ptr--;

}while(*ptr=='');

*(++ptr)=0;

returnstring;

}

intmain()

{

charstr[100];

cout<<"请输入一个末尾带空格的字符串"<

cin.get(str,100);

cout<<"截取前的原始字符串是:

"<

cout<<"截取空格后的字符串是:

"<

cout<<"["<

cout<

return0;

}

4-8

#include

#include

usingnamespacestd;

char*myltrim(char*string)

{

char*ptr=string;

while(*ptr=='')

ptr++;

strcpy(string,ptr);

returnstring;

}

intmain()

{

charstr[100];

cout<<"请输入开头带空格的字符串"<

cin.get(str,100);

cout<<"截取前的原始字符串是:

"<

cout<<"截取空格后的字符串是:

"<

cout<<"["<

cout<

return0;

}

 

第五章

5-2

#include

usingnamespacestd;

intmain()

{

charch;

for(inti=0;i<3;i++)

{

cout<<"请输入一个字母:

"<

cin>>ch;

if(ch>='A'&&ch<='Z')

{

ch=ch-'A'+'a';

cout<<"大小写转换后为:

"<

}

elseif(ch>='a'&&ch<='z')

{

ch=ch-'a'+'A';

cout<<"大小写转换后为:

"<

}

else

{

ch=ch;

cout<<"大小写转换后为:

"<

}

}

return0;

}

5-4

#include

intGCD(intx,inty);

intLCM(intx,inty,intz);

intmain(void)

{

usingnamespacestd;

cout<<"请输入两个正整数:

\n";

intx;

inty;

cout<<"第一个正整数:

";

cin>>x;

cout<<"第二个正整数:

";

cin>>y;

intG_CD=GCD(x,y);//最大公约数

intL_CM=LCM(x,y,G_CD);//最小公倍数=两数乘积除以最大公约数

cout<<"最大公约数是:

"<

cout<<"最小公倍数是:

"<

return0;

}

intGCD(intx,inty)

{

intz=x%y;

while(z!

=0)

{

x=y;

y=z;

z=x%y;

}

returny;

}

intLCM(intx,inty,intz)

{

returnx*y/z;

}

5-5

#include

#include

usingnamespacestd;

intmtk(intn)

{

intr,s,t,q;

r=n/100;

s=n%10;

t=n%100;

q=t/10;

t=r+s+q;

returnt;

}

intmain()

{

for(inti=201;i<1000;i++)

{

if(mtk(i)==5)

cout<

}

return0;

}

5-9

#include

#include

usingnamespacestd;

boolis_prime(intn)

{

if(n<2)

returnfalse;

for(inti=2;i*i<=n;i++)

{if(n%i==0)

returnfalse;

}

returntrue;

}

intmain()

{

intn;

cout<<"请输入一个数n:

";

cin>>n;

if(is_prime(n))

{

cout<<"该数是一个素数"<

}

else

{

cout<<"该数不是一个素数"<

}

return0;

}

 

第六章

6-5

#include"stdio.h"

intmain()

{

intm;

char*months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};

printf("InputMonth:

");

scanf("%d",&m);

printf("Themonthis:

%s",*(months+m-1));

}

 

6-6

#include

usingnamespacestd;

intmain()

{

chara[20];

inti=0;

cout<<"请输入一串字符:

\n";

cin>>a;

for(;a[i];i++)

{

if(a[i]>'a'&&a[i]<'z')

a[i]-=32;

elseif(a[i]>'A'&&a[i]<'Z')

a[i]+=32;

}

for(i=0;a[i];i++)

cout<

cout<

return0;

}

 

6-7

#include

usingnamespacestd;

intmain()

{

charstr[100];

char*ptr=str;

inttotal,capital,small,numeral,others;

total=capital=small=numeral=others=0;

cout<<"请输入字符串:

"<

cin.get(ptr,100);

while(*ptr!

=0)

{

total++;

if(*ptr>='A'&&*ptr<='Z')capital++;

elseif(*ptr>='a'&&*ptr<='z')small++;

elseif(*ptr>='0'&&*ptr<='9')numeral++;

elseothers++;

ptr++;

}

cout<<"该字符串含有的大写字母个数:

"<

cout<<"该字符串含有的数字字符个数:

"<

cout<<"该字符串含有的其它字符个数:

"<

cout<<"该字符串含有的其它字符个数:

"<

cout<<"该字符串总共含有的字符个数:

"<

return0;

}

6-16

#include

#include

usingnamespacestd;

doubleintegral(doublea,doubleb,double(*fun)(double),intn)

{

doubleh=(b-a)/n;

doublesum=((*fun)(a)+(*fun)(b))/2;

inti;

for(i=1;i

sum+=(*fun)(a+i*h);

sum*=h;

returnsum;

}

doubleyici(doubler)

{

doubley=r;

returny;

}

doubleerci(doubles)

{

doublet=s*s;

returnt;

}

doublesanci(doubleu)

{

doublev=u*u*u;

returnv;

}

intmain()

{

doublea=0,b=1;

cout<<"sin(x)结果是"<

cout<<"x结果是"<

cout<<"x*x结果是"<

cout<<"x*x*x结果是"<

cout<<"exp(x)结果是"<

return0;

}

 

第七章

7-1

#include

#include

usingnamespacestd;

classDog

{

charname[20];

intage;

charsex;

floatweight;

public:

voidRegister(char*Name,intAge,charSex,floatWeight);

char*GetName(){returnname;}

intGetAge(){returnage;}

charGetSex(){returnsex;}

floatGetWeight(){returnweight;}

voidSpeak(){cout<<"Arf!

Arf!

"<

};

voidDog:

:

Register(char*Name,intAge,charSex,floatWeight)

{

strcpy(name,Name);

age=Age;

sex=Sex;

weight=Weight;

}

intmain()

{

charname[20],sex;

intage;

floatweight;

Dogdog1;

cout<<"Pleaseinputname,age,sexandweight"<

cin>>name>>age>>sex>>weight;

dog1.Register(name,age,sex,weight);

cout<<"Dog’sname:

"<

cout<<"Dog’sage:

"<

cout<<"Dog’ssex:

"<

cout<<"Dog’sweight:

"<

cout<<"Dogspeak:

";

dog1.Speak();

return0;

}

7-2

#include

usingnamespacestd;

constdoublepi=3.14;

classCircle

{

public:

voidset();

voidshow();

doublecircumference();

doublearea();

private:

intx,y;

doubler;

};

voidCircle:

:

set()

{

cout<<"请输入圆心坐标和圆的半径"<

cin>>x;

cin>>y;

cin>>r;

}

voidCircle:

:

show()

{

cout<<"该圆为"<

cout<<"圆心("<

cout<<"半径"<

}

doubleCircle:

:

circumference()

{

return2*pi*r;

}

doubleCircle:

:

area()

{

returnpi*r*r;

}

intmain()

{

Circlec;

c.set();

c.show();

cout<<"圆的周长为"<

cout<<"圆的面积为"<

return0;

}

7-4

#include

usingnamespacestd;

classAddress{

charName[30];

charStreetAddress[30];

charCity[30];

charPostcode[30];

public:

Address(char*a,char*b,char*c,char*d){

strcpy(Name,a);

strcpy(StreetAddress,b);

strcpy(City,c);

strcpy

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 社交礼仪

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1