# C++程序设计第三版（谭浩强）第四章课后习题答案

4.2

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include<cmath>
using namespace std;
void delta1(float, float);
void delta2(float, float);
void delta3(float, float);
float delta;
int main()
{
float a, b, c;
cout << "Please input three numbers:" << endl;
scanf_s("%f %f %f", &a, &b, &c);
delta = b * b - 4 * a*c;
if (delta > 0)
delta1(a, b);
else if (delta == 0)
delta2(a, b);
else if (delta < 0)
delta3(a, b);
return 0;
}
void delta1(float a, float b)
{
float x1, x2;
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
cout << "x1=" << x1 << " x2=" << x2;
}
void delta2(float a, float b)
{
float x;
x = (-b) / (2 * a);
cout << "x1=x2=" << x;
}
void delta3(float a, float b)
{
float xb, sb;
sb = (-b) / (2 * a);
xb = (sqrt(-delta)) / (2 * a);
cout << "x1=" << sb << "+" << xb << "i" << endl;
cout << "x2=" << sb << "-" << xb << "i" << endl;
}

4.3

#include "stdafx.h"
#include <iostream>
#include<cmath>
using namespace std;

int main()
{
void prime(int);
int n;
cout << "Please input a number:" << endl;
cin >> n;
prime(n);
return 0;
}
void prime(int n)
{
for (int i = 2; i <= sqrt(n); i++)
if (n%i == 0)
{
cout << "This number isn't a prime number.";
break;
}
cout << "This number is a prime number.";
}

4.4

#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include<cmath>
using namespace std;

int main()
{
int fac(int);
int a, b, c;
cout << "Please input three numbers:" << endl;
scanf_s("%d %d %d", &a, &b, &c);
int x;
x = fac(a) + fac(b) + fac(c);
cout << "a!+b!+c!=" << x << endl;
return 0;
}
int fac(int n)
{
int x;
if (n == 0 || n == 1)
x = 1;
else x = fac(n - 1)*n;
return x;
}

4.5

#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include<cmath>
using namespace std;

int main()
{
float ex(int);
float sinh(int);
int n;
cout << "Please input x:" << endl;
cin >> n;
float x = sinh(n);
cout << x;
return 0;
}
float ex(int n)
{
return (exp(n));
}
float sinh(int n)
{
float sinh;
sinh = (ex(n) - ex(-n)) / 2;
return sinh;
}


4.7

#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include<cmath>
using namespace std;

int main()
{
void gotbaha(int);
cout << "Please input a number:" << endl;
int n;
cin >> n;
gotbaha(n);
return 0;
}
void gotbaha(int n)
{
bool prime(int);
if (n % 2 != 0)
cout << n << " is not a even number.";
else
{
int a = 3, b = n - 3;
for(int i=2;i<n/2;i+2)
{
if (prime(a) && prime(b))
{
cout << n << "=" << a << "+" << b;
break;
}
else
{
a = a + i;
b = b - i;
}
}
}
}
bool prime(int n)
{
for (int i = 2; i <= sqrt(n); i++)
if (n%i == 0)
return false;
return true;
}


4.8

#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include<cmath>
using namespace std;

int main()
{
int lrd(int, int);
int n, x;
cout << "Please input the n and x:" << endl;
scanf_s("%d %d", &n, &x);
int y=lrd(n, x);
cout << "The value is" << y;
return 0;
}
int lrd(int n,int x)
{
int f;
if (n == 0)
f = 1;
else if (n == 1)
f = x;
else
f = (2 * n - 1)*x - lrd(n - 1, x) - (n - 1)*lrd(n - 2, x) / n;
return f;
}

4.9

#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include<cmath>
using namespace std;

int main()
{
void hanoi(int, char, char, char);
char A, B, C;
cout << "Please input the number" << endl;
int n;
scanf_s("%d %c %c %c", &n, &A, 1, &B, 1, &C, 1);
hanoi(n, A, B, C);
return 0;
}
void hanoi(int n, char A, char B, char C)
{
void move(char, char);
if (n == 1)
move(A, C);
else
{
hanoi(n - 1, A, C, B);
move(A, C);
hanoi(n - 1, B, A, C);
}
}
void move(char x, char y)
{
cout << x << " to " << y<<endl;
}

4.10

#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include<cmath>
using namespace std;

int main()
{
void exchange(int);
int n;
cout << "Please input a number:";
cin >> n;
exchange(n);
return 0;
}
void exchange(int n)
{
if (n / 10 != 0)
exchange((int)(n/10));
cout << (char)(n % 10 + '0');
}

4.11

#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include<cmath>
using namespace std;

int main()
{
int sum(int);
int n;
cout << "Please input a number:";
cin >> n;
int y = sum(n);
cout << "The value is " << y;

}
int sum(int n)
{
int f;
if (n == 1)
f = 1;
else
f = sum(n - 1) + n * n;
return f;
}

4.12

#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include<cmath>
#define s(a,b,c) 0.5*((a)+(b)+(c))
#define area(s,a,b,c) sqrt(s*(s-(a))*(s-(b))*(s-(c)))
using namespace std;

int main()
{
int a, b, c;
cout << "Please input the length of the three sides of a triangle:";
scanf_s("%d %d %d", &a, &b, &c);
cout << "The area of this triangle is " << area(s(a,b,c),a,b,c);
return 0;
}


11-12

10-21
02-13
12-03 2316
01-03 3780
02-21 5211
12-19 3081
12-12
06-06 1424
11-16 2049