计算机课后习题解答

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。



习题5

1. 编写函数求一个数的阶乘n!,在主函数中调用该函数,并输出结果。

#include

double fun(int k);

void main()

{

int n;

double m;

cout<<"input n:";

cin>>n;

m=fun(n);

cout<
}



double fun(int n)

{

int i;

double mul=1;

for(i=1;i<=n;i++) mul*=i;

return mul;

}

2. 编写函数将一个小写字母转换为大写字母,在主函数中调用该函数,并输出结果。

#include

char convert(char);

void main()

{

char c;

cout<<"please input c:";

cin>>c;

cout<
}



char convert(char ch)

{

char c=ch>='a'&&ch<='z'?ch-32:ch;

return c;

}



/******************

#include

#include

char convert(char);

void main()

{

char str[20],i,l;

cout<<"please input the string:";

cin.get(str,20);

cout<<"转换前:"<
l=strlen(str);

for(i=0;i
str[i]=convert(str[i]);

cout<<"转换后:"<
}



char convert(char ch)

{

char c=ch>='a'&&ch<='z'?ch-32:ch;

return c;

}

************/

3. 编写函数判断输入的年份是否为闰年,在主函数中调用该函数,并输出结果。

#include

int isleap(int);

void main()

{

int year,m;

cout<<"input year:";

cin>>year;

m=isleap(year);

if(m)

cout<
else

cout<
}



int isleap(int year)

{

if(year%4==0&&year%100!=0||year%400==0)

return 1;

return 0;

}

4. 编写函数计算x的n次幂。在主函数中输出a3+b4的值,a和b在主函数中输入。

#include

double fun(int,int);

void main()

{

int a,b;

cout<<"input a,b:";

cin>>a>>b;

double add=fun(a,3)+fun(b,4);

cout<<"add="<
}



double fun(int x,int n)

{

double mul=1;

for(int i=1;i<=n;i++)

mul*=x;

return mul;

}

5. 编写程序求方程ax2+bx+c=0的根,用三个函数分别求出当b2-4ac大于0,小于0和等于0时的根。要求从主函数输入a,b,c的值并输出结果。

(答案见实验十 2.(1))

6. 编写函数求长度为n的整型数组a中的最大值和最小值,在主函数中进行调用。

#include

int maxf(int x[],int n);

int minf(int x[],int n);

const int N=10;

void main()

{

int a[N],n;

cout<<"please input n:";

cin>>n;

cout<<"input the array:";


for(int i=0;i
cin>>a[i];

cout<<"result: max="<
}



int maxf(int x[],int n)

{

int max=x[0];

for(int i=1;i
if(max
return max;

}



int minf(int x[],int n)

{

int min=x[0];

for(int i=1;i
if(min>x[i]) min=x[i];

return min;

}

7. 编写函数对一个字符串进行降序排序,在主函数中输入该字符串,并输出结果。

#include

void sort(char[]);

const int N=20;

void main()

{

char a[N];

cout<<"input the array:";

cin>>a;

sort(a);

cout<<"after sorted,the string is: "<
}



void sort(char x[])

{

char t;

for(int i=0;x[i];i++);

int length=i;

for(i=0;i
for(int j=0;j
if(x[j]
{

t=x[j];

x[j]=x[j+1];

x[j+1]=t;

}

}

8. 编写函数实现一个字符串的逆序。在主函数中实现输入,调用函数实现字符串的逆序后,输出该字符串。

#include

#include

void reverse(char *,int);

void main()

{

char str[20];

int l;

cout<<"please input the string: ";

cin.getline(str,20);

cout<<"the original string is:"<
l=strlen(str);

reverse(str,l);

cout<<"the result string is: "<
}



void reverse(char *s,int n)

{

int i,j;

char c;

for(i=0,j=n-1;i
{

c=s[i];

s[i]=s[j];

s[j]=c;

}

}

9. 编写函数,用折半查找法,在长度为n的整型数组a中查找某个数据b。

(答案见实验十 2.(4))

10. 编写两个函数,分别求两个整数的最大公约数和最小公倍数。用主函数调用这两个函数,并输出结果,两个整数由键盘输入。

#include

void fun(int x,int y);

void main()

{

int a,b;

cout<<"Enter two integer:\n";

cin>>a>>b;

fun(a,b);

}



void fun(int x,int y)

{

int m,n,res; /*被除数, 除数, 余数*/

m=x;

n=y;

res=m%n;

while (res!=0)

{

m=n;

n=res;

res=m%n;

}

cout<<"Greatest common divisor: "<
cout<<"Lease common multiple :"<
}

11. 编写函数,功能为将字符串a中的字符t1用字符t2替换。在主函数中进行调用,然后输出结果。

(答案

见实验十 2.(5))

12. 编写4个同名函数sum,分别求计算3个int型数据、3个double型数据、2个int型数据和2个double型数据的和。

#include

int sum(int,int,int);

int sum(int,int);

double sum(double,double,double);

double sum(double,double);

void main()

{

cout<
cout<
cout<
cout<
}



int sum(int a, int b, int c)

{

return a+b+c;

}



int sum(int a, int b)

{

return a+b;

}



double sum(double a, double b, double c)

{

return a+b+c;

}



double sum(double a, double b)

{

return a+b;

}

相关文档
最新文档