谭浩强C++第二版,第六章课后习题

  • Post author:
  • Post category:其他


#include “includes.h”

int arrays[20];

void swap(int& a, int& b)

{


int temp;

temp = a;

a = b;

b = temp;

return;

}

void  swap_str(char* &a, char* &b)

{


char *temp;

temp = a;

a = b;

b = temp;

return;

}

void Question6_1(int &a,int &b,int &c)

{


cout << “6-1” << endl;

if (a > b)

swap(a, b);

if (b > c)

swap(b, c);

if (a > c)

swap(a, c);

cout << a << b << c<<endl;

return;

}

void Question6_2(char *a, char *b, char *c)

{

if (strcmp(a, b)> 0)

swap_str(a, b);

if (strcmp(a, c) > 0)

swap_str(a, c);

if (strcmp(b, c) > 0)

swap_str(b, c);

cout << a << b << c << endl;

return;

}

void input_10_number()

{


int i = 0;

for (i = 0; i < 10; i++)

{


cin >> arrays[i];

}

}

void deal_10_word()

{


Position max_number, min_number;

max_number.value = 0;

max_number.index = 0;

min_number.value = 0;

min_number.index = 0;

int i = 0;

for (i = 0; i < 10; i++)

{


if (arrays[i] > max_number.value)

{


max_number.value = arrays[i];

max_number.index = i;

}

if (arrays[i] <= min_number.value)

{


min_number.value = arrays[i];

min_number.index = i;

}

}

swap(arrays[0], arrays[min_number.index]);

swap(arrays[9], arrays[max_number.index]);

}

void print_10_words()

{


int i;

for (i = 0; i < 10; i++)

{


cout << arrays[i] << ” “;

}

cout << endl;

return;

}

void Question6_3()

{


input_10_number();

deal_10_word();

print_10_words();

}

void move_words(int a[],int position)

{


int recv_array[10];

int temp_array[10];

int recv_position;

recv_position = position;

for (int i = 0; i < 10; i++)

{


recv_array[i] = a[i];

}

}

void Question6_4(int m)

{


int a[10] = { 0,1,2,3,4,5,6,7,8,9 };

int b[100];

int i = 0,b_index = 0;

int length = (sizeof(a) / sizeof(a[0]));

for (i = 0; i < length; i++)

{


b_index = ((i + m) % length);

b[b_index] = a[i];

}

cout << “m=” << m << endl;

for (i = 0; i < length; i++)

{


cout << b[i] << ” “;

}

cout << endl;

}

void Question6_5(int digit_number)

{


int a[10000],search_index=0;

int count=0,left_number;

left_number = digit_number;

int result = 0,j=0;

cout << “digitnumber=” << digit_number;

for (int i = 0; i < digit_number; i++)

{


a[i] = i;

}

while (left_number != 0)

{


if (a[j] != -1)

{


if (count == 2)

{


a[j] = -1;

left_number–;

count = 0;

}

else

{


count++;

result = j;

}

}

j++;

j = j % digit_number;

}

cout << “result:” << result << endl;

}


void Question6_6(char * str)

{


char* p_str;

p_str = str;

int count_str = 0;

//当p_str指向的数组元素值不为空,进行循环体,

//值为空时结束循环体。

while (*p_str)

{


count_str++;

p_str++;

}

cout << “str length:” << count_str<<endl;

}

void Question6_7_copy_str(int start_position)

{


char str[100] = “abcdefghijklmnopqrstuvwxyz”;

char copy_to_str[100];

char* p_str, * p_copy_to_str;

p_copy_to_str = copy_to_str;

p_str = str;

p_str = p_str + start_position;

while (*p_str)

{


*p_copy_to_str = *p_str;

p_copy_to_str++;

p_str++;

}

//复制后后的p_copy_to_str中,系统没有为它自动分配‘\0’,需要手动添加。

*p_copy_to_str = ‘\0’;

p_copy_to_str = copy_to_str;

p_str = str;

cout << “original:” << p_str << endl;

cout << “result:” << start_position << ” ” << p_copy_to_str <<endl;

return;

}

//统计不用类型字符个数

void Question6_8()

{


char str[100];

int lower=0, upper=0, digit=0, others=0,space = 0,i=0;

//错误输入带有空格的字符串方式:cin >> str;

//应该用getchar(),连续接受字符串。

while ((str[i] = getchar())!=’\n’)

i++;

//请注意字符串拼接,在最后添加’\0’结束符。

str[i] = ‘\0’;

cout << str;

char* p_str;

p_str = str;

//以’\0’为结束符。

while (*p_str!=’\0′)

{


if (*p_str >= ‘0’ && *p_str <= ‘9’)

digit++;

if (*p_str >= ‘a’ && *p_str <= ‘z’)

lower++;

else if (*p_str >= ‘A’ && *p_str <= ‘Z’)

upper++;

else if (*p_str == ‘ ‘)

space++;

else

others++;

p_str++;

}

cout << “大写字母:” << upper << endl;

cout << “小写字母:” << lower << endl;

cout << “空格:” << space << endl;

cout << “其他字符:” << others << endl;

}

void Question6_9(int n)

{


int arrays[6][6] =

{


{1,2,3,4,5},

{6,7,8,9,10},

{11,12,13,14,15},

{16,17,18,19,20},

{21,22,23,24,25}

};

int row = 0, col = 0,step;

for (int i = 0; i < n; i++)

{


for (int col = 0; col < i; col++)

{


swap(arrays[i][col], arrays[col][i]);

}

}

for (int i = 0; i < 5; i++)

{


for (int j = 0; j < 5; j++)

{


cout << arrays[i][j] << ” “;

}

cout << endl;

}

}

void Question6_10()

{


int arrays[6][6] =

{


{1,2,3,4,5},

{6,7,8,9,10},

{11,12,13,14,15},

{16,17,18,19,20},

{21,22,23,24,25}

};

int data[100];

int data_index=0;

int  min=0,pos_x,pos_y;

for (int i = 0; i < 5; i++)

{


for (int j = 0; j < 5; j++)

{


if (arrays[2][2] < arrays[i][j])

{


swap(arrays[i][j], arrays[2][2]);

}

if(arrays[0][0] > arrays[i][j])

{


swap(arrays[0][0], arrays[i][j]);

}

}

}

for (int i = 0; i < 5; i++)

{


for (int j = 0; j < 5; j++)

{


if (i == 0 && j == 0)

continue;

if (arrays[0][4] > arrays[i][j])

{


swap(arrays[0][4], arrays[i][j]);

}

}

}

for (int i = 0; i < 5; i++)

{


for (int j = 0; j < 5; j++)

{


if ((i == 0 && j == 0)|| (i == 0 && j == 4))

continue;

if (arrays[4][0] > arrays[i][j])

{


swap(arrays[4][0], arrays[i][j]);

}

}

}

for (int i = 0; i < 5; i++)

{


for (int j = 0; j < 5; j++)

{


if ((i == 0 && j == 0) || (i == 0 && j == 4)||(i==4&&j==0))

continue;

if (arrays[4][4] > arrays[i][j])

{


swap(arrays[4][4], arrays[i][j]);

}

}

}

for (int i = 0; i < 5; i++)

{


for (int j = 0; j < 5; j++)

{


cout << arrays[i][j] << ” “;

}

cout << endl;

}

}

void Sort_String(char** str ,int Ncase)

{


for (int i = 0; i < Ncase; i++)

{


for (int j = i; j < Ncase; j++)

{


if (strcmp(str[i], str[j]) > 0)

{


swap_str(str[i], str[j]);

}

}

}

for (int i = 0; i < Ncase; i++)

cout << str[i] << endl;

}

void Question6_11_12()

{


char * str[100];

for (int i = 0; i < 10; i++)

{


str[i] = new char[100];

}

for (int i = 0; i < 10; i++)

{


cin >> str[i];

}

Sort_String(str,10);

}

//6-13

float integral(float (*p)(float), float a, float b, int n)

//用矩形法求定积分的通用函数

{


int i;

float x, h, s;

h = (b – a) / n;

x = a;

s = 0;

for (i = 1; i <= n; i++)

{


x = x + h;

s = s + (*p)(x) * h;

}

return(s);

}

float fsin(float x) // 计算 sin(x) 的函数

{


return sin(x);

}

float fcos(float x) // 计算 cos(x) 的函数

{


return cos(x);

}

float fexp(float x) // 计算 exp(x)的函数

{


return exp(x);

}


void Question6_12()

{


int Ncase;

int data[1000];

cout << “请输入数字的个数:” << endl;

cin >> Ncase;

cout << “请输入数字:” << endl;

for (int i = 0; i < Ncase; i++)

{


cin >> data[i];

}

for (int i = Ncase – 1; i >= 0; i–)

{


cout << data[i]<<” “;

}

cout << endl;

}

void Question6_14()

{


char str[] = “abcd123 123?123tab222”;

int result[1000] = {0,0,0,0,0,0,0,0,0,0};

int temp[1000];

int num_count = 0, result_cnt=0;

cout << str << endl;

char* p_str;

p_str = str;

while (*p_str != ‘\0’)

{


if (*p_str > ‘0’ && *p_str < ‘9’)

{


temp[num_count] = (*p_str) – ‘0’;

num_count++;

}

else

{


if (num_count != 0)

{


for (int i = 0; i < num_count; i++)

{


result[result_cnt] = result[result_cnt] * 10 + temp[i];

}

result_cnt++;

num_count = 0;

for (int i = num_count – 1; i >= 0; i–)

{


temp[i] = 0;

}

}

}

p_str++;

}

if (num_count != 0)

{


for (int i = 0; i < num_count; i++)

{


result[result_cnt] = result[result_cnt] * 10 + temp[i];

}

result_cnt++;

num_count = 0;

for (int i = num_count – 1; i >= 0; i–)

{


temp[i] = 0;

}

}

cout << “共有” << result_cnt << “个整数” << “分别如下:” << endl;

for (int i = 0; i < result_cnt; i++)

cout << result[i] << ” “;

cout << endl;

}


int Question6_17_strcmp(const char* str1,const char* str2)

{


const char * p_str1, * p_str2;

p_str1 = str1;

p_str2 = str2;

int result = 0;

while (true)

{


if (*p_str1 == ‘\0’ && *p_str2 == ‘\0’)

break;

if (*p_str1 == ‘\0’ && *p_str2 != ‘\0’)

return -1;

if (*p_str1 != ‘\0’ && *p_str2 == ‘\0’)

return 1;

if (*p_str1 > *p_str2)

{


result = *p_str1 – *p_str2;

return result;

}else if (*p_str1 < * p_str2)

{


result = *p_str1 – *p_str2;

return result;

}

if(*p_str1!=’\0′)

p_str1++;

if(*p_str2!=’\0′)

p_str2++;

}

return 0;

}


void Question6_18(int month)

{


char* Month[20];

for (int i = 0; i < 20; i++)

{


Month[i] = new char[20];

}

strcpy(Month[1],”January”);

strcpy(Month[2] , “February”);

strcpy(Month[3] , “March”);

strcpy(Month[4], “April”);

strcpy(Month[5], “May”);

strcpy(Month[6], “June”);

strcpy(Month[7], “July”);

strcpy(Month[8], “Auguest”);

strcpy(Month[9], “September”);

strcpy(Month[10], “October”);

strcpy(Month[11], “November”);

strcpy(Month[12], “December”);

cout << Month[month] << endl;

}



版权声明:本文为come_across原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。