南大C++:《程序设计教程 用C++语言编写》答案(五)
这些是我自己写的代码,全部通过编译。
5.8.8 编写一个程序,从键盘输入一个字符串,分别统计其中的大写字母、小写字母以及数字的个数。
/*
P165 5.8.8
编写一个程序,从键盘输入一个字符串,分别统计其中的大写字母、小写字母以及数字的个数。
*/
#include <iostream>
using namespace std;
int main()
{
int u, l, n;
char s[100];
while (1)
{
u = l = n = 0;
cout << "String:";
cin >> s;
int i = 0;
while (s[i] != '\0')
{
if (s[i] >= 'A'&&s[i] <= 'Z')
u++;
else if (s[i] >= 'a'&&s[i] <= 'z')
l++;
else if (s[i] >= '0'&&s[i] <= '9')
n++;
i++;
}
cout << "字符串:" << s << "中大写字母共" << u << "个;小写字母共" << l << "个;数字共" << n << "个。" << endl;
}
}
5.8.10 实现下面的数组元素交换位置函数:该函数能够把数组a的前m个元素与后n个元素交换位置。
/*
P166 5.8.10
实现下面的数组元素交换位置函数:
该函数能够把数组a的前m个元素与后n个元素交换位置。
*/
#include <iostream>
using namespace std;
void rev(int a[], int m, int n)
{
int t;
for (int i = m; i <= (m + n) / 2; i++)
{
t = a[i];
a[i] = a[n+m - i];
a[n+m - i] = t;
}
}
void swa(int a[], int m, int n)
{
rev(a, 0, m + n - 1);
rev(a, 0, n-1);
rev(a, n, m + n - 1);
}
int main()
{
int a[100];
int i;
int m, n;
while (1)
{
i = 0;
cout << "数组:";
while (cin >> a[i])
{
if (a[i] == -1)
break;
i++;
}
i--;
cout << "m:";
cin >> m;
cout << "n:";
cin >> n;
for (int j = 0; j <= i; j++)
cout << a[j] << ' ';
cout << endl;
swa(a, m, n);
for (int j = 0; j <= i; j++)
cout << a[j] << ' ';
cout << endl;
}
}
5.8.11 编写一个程序,计算一个矩阵的鞍点。矩阵的鞍点是指矩阵中的一个位置,该位置上的元素在其所在的行上最大,列上最小。(一个矩阵也可能没有鞍点)
/*
P166 5.8.11
编写一个程序,计算一个矩阵的鞍点。矩阵的鞍点是指矩阵中的一个位置,该位置上的元素在其所在的行上最大,列上最小。(一个矩阵也可能没有鞍点)
*/
#include <iostream>
using namespace std;
int max(int a[],int l)
{
int max=0;
for (int i = 1; i < l; i++)
{
if (a[i]>a[max])
max = i;
}
return max;
}
bool isAndian(int a[][20],int m,int dm, int dn)
{
for (int i = 0; i < m; i++)
{
if (a[i][dn] < a[dm][dn])
return false;
}
return true;
}
int main()
{
int m, n;
int flag;
int a[20][20];
while (1)
{
flag = 0;
cout << "矩阵行数:";
cin >> m;
cout << "矩阵列数:";
cin >> n;
for (int i = 0; i < m; i++)
{
cout << "输入矩阵第" << i + 1 << "行:";
for (int j = 0; j < n; j++)
cin >> a[i][j];
}
for (int i = 0; i < m; i++)
{
int dn = max(a[i], n);
if (isAndian(a, m, i, dn))
{
cout << "鞍点:第" << i+1 << "行,第" << dn+1 << "列。" << endl;
flag = 1;
}
}
if (flag == 0)
cout << "不存在鞍点!" << endl;
}
}
5.8.12 编程实现在一个由NxN(N为大于1的奇数)个方格组成的方阵中,填入1、2、3、...、N^2各个数,使得每一行、每一列以及两个对角线上的数的和均相等(奇数幻方问题)。
/*
P166 5.8.12
编程实现在一个由NxN(N为大于1的奇数)个方格组成的方阵中,填入1、2、3、...、N^2各个数,使得每一行、每一列以及两个对角线上的数的
和均相等(奇数幻方问题)。
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
int a[20][20];
int tm, tn;
while (1)
{
cout << "方阵行数(奇数):";
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
a[i][j] = 0;
}
a[0][n / 2] = 1;
tm = 0;
tn = n / 2;
for (int i = 2; i <= n*n; i++)
{
if (tm == 0 && tn == n - 1)
tm = 1;
else if (tm == 0)
{
tn += 1;
tm = n - 1;
}
else if (tn == n - 1)
{
tm -= 1;
tn = 0;
}
else if (a[tm - 1][tn + 1] == 0)
{
tm -= 1;
tn += 1;
}
else
{
tm += 1;
}
a[tm][tn] = i;
}
cout << "方阵:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
cout << endl;
}
}
5.8.13 实现strlen、strcpy、strcat、strncat、strcmp以及strncmp函数。
/*
P166 5.8.13
实现strlen、strcpy、strcat、strncat、strcmp以及strncmp函数。
*/
#include <iostream>
using namespace std;
int strlen1(char s[])
{
int n = 0;
while (s[n] != '\0')
n++;
return n;
}
char *strcpy1(char dest[], char s[])
{
int i;
for (i = 0; s[i] != '\0'; i++)
dest[i] = s[i];
if (i != 0)
dest[i] = '\0';
return dest;
}
char *strncpy1(char dest[], char s[],int n)
{
int i;
for (i = 0; i < n&&s[i] != '\0'; i++)
dest[i] = s[i];
if (i != 0)
dest[i] = '\0';
return dest;
}
char *strcat1(char dest[], char s[])
{
int i=0;
int j = 0;
while (dest[i] != '\0')
i++;
while (s[j] != '\0')
dest[i++] = s[j++];
dest[i] = '\0';
return dest;
}
char *strncat1(char dest[], char s[],int n)
{
int i = 0;
int j = 0;
while (dest[i] != '\0')
i++;
while (j < n&&s[j] != '\0')
dest[i++] = s[j++];
dest[i] = '\0';
return dest;
}
bool strcmp1(char s1[], char s2[])
{
int i = 0;
while (s1[i] == s2[i] && s1[i] != '\0')
i++;
if (s1[i] == '\0'&&s2[i] == '\0')
return true;
return false;
}
bool strncmp1(char s1[], char s2[],int n)
{
int i = 0;
while (s1[i] == s2[i] && s1[i] != '\0'&&i < n)
i++;
if (i == n || (s1[i] == '\0'&&s2[i] == '\0'))
return true;
return false;
}
int main()
{
char a[100];
char b[100];
int n;
while (1)
{
cout << "string1:";
cin >> a;
cout << "string2:";
cin >> b;
}
}