C语言程序设计(郑莉)课后习题答案 下载本文

public:

SimpleCircle(); SimpleCircle(int);

SimpleCircle(const SimpleCircle &); ~SimpleCircle() {}

void SetRadius(int); int GetRadius()const;

private: int *itsRadius; };

SimpleCircle::SimpleCircle() {

itsRadius = new int(5); }

SimpleCircle::SimpleCircle(int radius) {

itsRadius = new int(radius); }

SimpleCircle::SimpleCircle(const SimpleCircle & rhs) {

int val = rhs.GetRadius(); itsRadius = new int(val); }

int SimpleCircle::GetRadius() const {

return *itsRadius; }

int main() {

SimpleCircle CircleOne, CircleTwo(9);

cout << \cout << \return 0; }程序运行输出: CircleOne: 5 CircleTwo: 9

6-21 编写一个函数,统计一个英文句子中字母的个数,在主程序中实现输入、输出。

解: 源程序:

#include #include

int count(char *str) {

int i,num=0;

for (i=0; str[i]; i++) {

if ( (str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z') ) num++; }

return num; }

void main() {

char text[100];

cout << \输入一个英语句子:\gets(text);

cout << \这个句子里有\个字母。\endl; }

程序运行输出: 输入一个英语句子: It is very interesting! 这个句子里有19个字母。

6-22 编写函数int index(char *s, char *t),返回字符串t 在字符串s中出现的最左边的位置,如果在s中没有与t匹配的子串,就返回-1。 解: 源程序:

#include

int index( char *s, char *t) {

int i,j,k;

for(i = 0; s[i] != '\\0'; i++) {

for(j = i, k = 0; t[k] != '\\0' && s[j] == t[k]; j++, k++) ;

26

if (t[k] =='\\0') return i; }

return -1; }

void main() { int n;

char str1[20],str2[20]; cout << \输入一个英语单词:\cin >> str1;

cout << \输入另一个英语单词:\cin >> str2;

n = index(str1,str2); if (n > 0)

cout << str2 << \在\中左起第\<< \个位置。\else

cout << str2 << \不在\中。\}

程序运行输出:

输入一个英语单词:abcdefgh 输入另一个英语单词:de

de在abcdefghijk中左起第4个位置。

6-23 编写函数reverse(char *s)的倒序递归程序,使字符串s倒序。 解: 源程序:

#include #include

void reverse(char *s, char *t) { char c; if (s < t) { c = *s; *s = *t; *t = c;

reverse(++s, --t); } }

void reverse( char *s)

{

reverse(s, s + strlen(s) - 1); }

void main() {

char str1[20];

cout << \输入一个字符串:\cin >> str1;

cout << \原字符串为:\reverse(str1);

cout << \倒序反转后为:\}

程序运行输出:

输入一个字符串:abcdefghijk 原字符串为:abcdefghijk 倒序反转后为:kjihgfedcba

6-24 设学生人数N=8,提示用户输入N个人的考试成绩,然后计算出平均成绩,显示出来。 解: 源程序:

#include #include

#define N 8

float grades[N]; //存放成绩的数组

void main() { int i;

float total,average;

//提示输入成绩

for(i = 0; i < N; i++ ) {

cout << \cin >> grades[i]; }

total = 0;

for (i = 0; i < N; i++) total += grades[i]; average = total / N;

27

cout << \}

程序运行输出: Enter grade #1: 86 Enter grade #2: 98 Enter grade #3: 67 Enter grade #4: 80 Enter grade #5: 78 Enter grade #6: 95 Enter grade #7: 78 Enter grade #8: 56

Average grade: 79.75

6-25 设计一个字符串类MyString,具有构造函数、析构函数、拷贝构造函数,重载运算符+、=、+=、[],尽可能地完善它,使之能满足各种需要。(运算符重载功能为选做,参见第8章) 解:

#include #include

class MyString { public: MyString();

MyString(const char *const); MyString(const MyString &); ~MyString();

char & operator[](unsigned short offset); char operator[](unsigned short offset) const; MyString operator+(const MyString&); void operator+=(const MyString&); MyString & operator= (const MyString &);

unsigned short GetLen()const { return itsLen; } const char * GetMyString() const { return itsMyString; }

private:

MyString (unsigned short); // private constructor char * itsMyString; unsigned short itsLen; };

MyString::MyString()

{

itsMyString = new char[1]; itsMyString[0] = '\\0'; itsLen=0; }

MyString::MyString(unsigned short len) {

itsMyString = new char[len+1];

for (unsigned short i = 0; i<=len; i++) itsMyString[i] = '\\0'; itsLen=len; }

MyString::MyString(const char * const cMyString) {

itsLen = strlen(cMyString); itsMyString = new char[itsLen+1];

for (unsigned short i = 0; i

MyString::MyString (const MyString & rhs) {

itsLen=rhs.GetLen();

itsMyString = new char[itsLen+1]; for (unsigned short i = 0; i

MyString::~MyString () {

delete [] itsMyString; itsLen = 0; }

MyString& MyString::operator=(const MyString & rhs) {

if (this == &rhs) return *this;

delete [] itsMyString; itsLen=rhs.GetLen();

itsMyString = new char[itsLen+1]; for (unsigned short i = 0; i

28

return *this; }

char & MyString::operator[](unsigned short offset) {

if (offset > itsLen)

return itsMyString[itsLen-1]; else

return itsMyString[offset]; }

char MyString::operator[](unsigned short offset) const {

if (offset > itsLen)

return itsMyString[itsLen-1]; else

return itsMyString[offset]; }

MyString MyString::operator+(const MyString& rhs) {

unsigned short totalLen = itsLen + rhs.GetLen(); MyString temp(totalLen);

for (unsigned short i = 0; i

for (unsigned short j = 0; j

void MyString::operator+=(const MyString& rhs) {

unsigned short rhsLen = rhs.GetLen(); unsigned short totalLen = itsLen + rhsLen; MyString temp(totalLen);

for (unsigned short i = 0; i

for (unsigned short j = 0; j

int main() {

MyString s1(\

cout << \

char * temp = \s1 = temp;

cout << \

char tempTwo[20];

strcpy(tempTwo,\s1 += tempTwo;

cout << \cout << \

cout << \s1[4]='x';

cout << \

cout << \

MyString s2(\MyString s3; s3 = s1+s2;

cout << \

MyString s4;

s4 = \

cout << \return 0; }

程序运行输出: S1: initial test S1: Hello World

tempTwo: ; nice to be here! S1: Hello World; nice to be here! S1[4]: o

S1: Hellx World; nice to be here! S1[999]: !

S3: Hellx World; nice to be here! Another myString S4: Why does this work?

6-26 编写一个3×3矩阵转置的函数,在main()函数中输入数据。 解:

#include

29

void move (int matrix[3][3]) {

int i, j, k; for(i=0; i<3; i++) for (j=0; j

k = matrix[i][j];

matrix[i][j] = matrix[j][i]; matrix[j][i] = k; } }

void main() {

int i, j; int data[3][3];

cout << \输入矩阵的元素\for(i=0; i<3; i++) for (j=0; j<3; j++) {

cout << \第\行第\<<\个元素为:\cin >> data[i][j]; }

cout << \输入的矩阵的为:\for(i=0; i<3; i++) {

for (j=0; j<3; j++) cout << data[i][j] << \cout << endl; }

move(data);

cout << \转置后的矩阵的为:\for(i=0; i<3; i++) {

for (j=0; j<3; j++) cout << data[i][j] << \cout << endl; } }

程序运行输出: 输入矩阵的元素 第1行第1个元素为:1 第1行第2个元素为:2 第1行第3个元素为:3 第2行第1个元素为:4 第2行第2个元素为:5

第2行第3个元素为:6 第3行第1个元素为:7 第3行第2个元素为:8 第3行第3个元素为:9 输入的矩阵的为: 1 2 3 4 5 6 7 8 9

转置后的矩阵的为: 1 4 7 2 5 8 3 6 9

6-27 编写一个矩阵转置的函数,矩阵的维数在程序中由用户输入。 解:

#include

void move (int *matrix ,int n) {

int i, j, k; for(i=0; i

k = *(matrix + i*n + j);

*(matrix + i*n + j) = *(matrix + j*n + i); *(matrix + j*n + i) = k; } }

void main() {

int n, i, j; int *p;

cout << \请输入矩阵的维数:\cin >> n;

p = new int[n*n];

cout << \输入矩阵的元素\for(i=0; i

cout << \第\行第\<<\个元素为:\cin >> p[i*n + j]; }

30