C-C++习题一期末考试例题(附答案)(6)

江畔旧时月 分享 2021-06-02 下载文档

四、程序设计题

1. 编写子函数double fact(int n)计算n!,要求主函数键盘输入整数n,调用子函数计算n! ,并显示结果。

#include<iostream.h>

double fact(int n);

int main( )

{

int a;

cin>>a;

cout<<fact(a)<<endl;

}

double fact(int n){

if(n==1) return 1;

return n*fact(n-1);

}

2. 用公式求 的近似值,直到某一项的绝对值小于0.0001为止。

#include <iostream>

using namespace std;

#include<math.h>

int main()

{

double a, b = 3, c, d = 0;

for (a = 1; 1 / b > 0.0001; a =a + 4,b=b+4)

{

c = 1 / a - 1 / b;

d = c + d;

}

cout<<4*d<<endl;

return 0;

}

3.编写程序比较两个字符串,若相等,返回0;前字符串大,返回1;否则,返回-1;请完成子函数。#include<iostream.h>

int stringCompare(char s1[10],char s2[10]);

void main( )

{

char str1[10],str2[10]; int flag;

cout<<"Please input 2 strings:\n";

cin>>str1>>str2;

5


C-C++习题一期末考试例题(附答案)(6).doc 将本文的Word文档下载到电脑

下一篇:牡丹花茶的研制与开发

相关推荐
相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7 元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xxxxxx QQ:xxxxxx