《C++程序与设计》 WORK SEVEN
T 1
//编写一个子函数reverse(n),对一个三位整数实现逆序输出。
//例如,在主函数中输入123,调用子函数后,在主函数中输出结果321。
#include <iostream>
using namespace std;
int main ()
{
int reverse (int n);
int n = 0;
cin >> n;
cout << reverse (n) << endl;
system ("pause");
}
int reverse (int n)
{
int a , b , c;
a = n % 10;
b = n / 10 % 10;
c = n /100;
return (a*100 + b * 10 + c);
}
T 2
//求a! + b! + c!的值。
//用一个函数fac(n)求n!。
//a, b, c的值由主函数输入,最终得到的值在主函数中输出。
#include <iostream>
using namespace std;
int main ()
{
int fac (int n);
int a , b ,c;
cin >> a >> b >> c;
cout << (fac (a) + fac (b) + fac (c)) << endl;
system ("pause");
}
int fac (int n)
{
int sum = 1;
for (int i = 1;i <= n ; i++)
{
sum = sum * i;
}
return sum;
}
T 3
//编写一个函数double fsum(int n),
//如果n是偶数,计算1 + 1 / 2 + 1 / 4 + … + 1 / n的值;
//如果n是奇数,计算1 + 1 / 3 + 1 / 5 + …1 / n的值。
//n是在主函数中从键盘输入的大于1的正整数。
#include <iostream>
using namespace std;
int main ()
{
double fsum (int n);
int n = 0;
cin >> n;
if (n > 1)
{
cout << fsum (n) << endl;
}
else
{
cout << "请输入大于 1 的正整数\n";
}
system ("pause");
}
double fsum (int n)
{
double sum = 0;
if (n % 2 == 0)
{
cout << "您输入的是偶数:" << n << endl;
for (int i = 2; i <= n; i = i + 2 )
{
sum = sum + 1.0 / i;
}
return (sum + 1);
}
else
{
cout << "您输入的是奇数:" << n << endl;
for (int i = 3; i <= n; i = i + 2 )
{
sum = sum + 1.0 / i;
}
return (sum + 1);
}
}
T 4
//写两个函数,
//分别求两个整数的最大公约数和最小公倍数
//用主函数调用两个函数,并输出结果,两个整数由键盘输入。
#include <iostream>
using namespace std;
int main ()
{
int max_yue (int a , int b);
int min_b (int a , int b);
int a = 0 , b = 0;
cin >> a >> b;
cout << max_yue (a,b) << endl;
cout << min_b (a,b) <<endl;
system ("pause");
}
int max_yue (int a , int b)
{
if (a > b)
{
if (a % b == 0)
{
return b;
}
else
{
b --;
}
}
else
{
if (b % a == 0)
{
return a;
}
else
{
a --;
}
}
}
int min_b (int a , int b)
{
int temp = a * b;
if (a > b)
{
if (a % b == 0)
{
return temp / b;
}
else
{
b --;
}
}
else
{
if (b % a == 0)
{
return temp / a;
}
else
{
a --;
}
}
}
T 5
//编写一个函数f(n),对一个整数,求出它的位数以及各位数字之和。
//在主函数中输入整数,调用f(n),在子函数中输出结果。
#include <iostream>
using namespace std;
int main ()
{
void f (int n);
cout << "Please enter a integer :\n";
int n = 0;
while (cin >> n)
{
f (n);
}
system ("pause");
return 0;
}
void f (int n)
{
int number = 0; //位数
int i = 10;
int figure = 0;
int sum = 0;
while (n != 0)
{
figure = n % i;
sum = sum + figure;
n = n / i;
number ++;
}
cout << "整数的位数为:" << number
<< "\n整数的各位数字之和为:" << sum << "\n";
}
T 6
//求方程ax2 + bx + c = 0的根,
//用3个函数分别求当b2 - 4ac大于0、等于0和小于0时的根,并输出结果。
//从主函数输入a, b, c的值。
#include <iostream>
using namespace std;
int main ()
{
int delta (int a ,int b ,int c);
void root_z (int a ,int b , int c );
void root_0 (int a , int b , int c);
int a ,b ,c;
int del = 0;
int root = 0;
cin >> a >> b >> c;
del = delta (a ,b ,c);
if (del > 0)
{
root_z (a,b,c);
}
else if (del == 0)
{
root_0 (a,b,c);
}
else if (del < 0)
{
cout << "此方程无实数根" << endl;
}
system ("pause");
}
int delta (int a ,int b ,int c)
{
int delta = 0;
delta = b*b - 4*a*c;
return delta;
}
void root_z (int a ,int b , int c )
{
int delta (int a ,int b ,int c);
int root_z = 0;
root_z = (-b + delta(a,b,c)) / (2*a);
cout << root_z << " ";
root_z = (-b - delta(a,b,c)) / (2*a);
cout << root_z << endl;
}
void root_0 (int a , int b , int c)
{
int root_0 = 0;
root_0 = (-b) / (2*a);
cout << root_0 << endl;
}