四、程序设计题
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

