C++ primer plus 第八章習題答案

#include
#include <stdio.h>
#include <cctype>
#include <string>
#include <cstring>
using namespace std;
#pragma warning(disable:4996)
int num_count=0;
void print_word(const char * word,const int &num=0)//8.1
{
const char * word1 = {0};
word1 = word;//練習字符串之間的賦值
if (num == 0)
{
num_count++;
cout << "The word: ";
printf("%s", word1);//字符串的C語言打印方式
cout<< " num_count: " << num_count<<endl;
}
else {
for (int i = 0; i < num_count; i++)
{
cout << word<<endl;
}
}
}
struct CandyBar //8.2
{
const char * Grand;
double candy_weight;
int candy_hot;
};
void set_CandyBar(CandyBar &candy,const char *Grand="Millennium Munch",double candy_weight=2.85,int candy_hot=350)//8.2
{
candy.candy_hot = candy_hot;
candy.candy_weight = candy_weight;
candy.Grand=Grand;
}
void show_CandyBar(const CandyBar & candy) //8.2
{
cout << "Candy Grand: "<<candy.Grand << endl;
cout << "Candy Weight: " << candy.candy_weight << endl;
cout << "Candy Hot: " << candy.candy_hot << endl;
}
void change_string(string & word)//8.3
{
int string_num = word.size();
/*char * big_word={};
char * big_word = new char[string_num];//char類型的輸出
for(int i=0;i<string_num;i++)
big_word[i]=toupper(word[i]);
for (int i = 0; i<string_num; i++)
cout << big_word[i];//直接輸出指針會發生內存泄漏,需要一個一個輸出。
delete big_word;*/
for (int i = 0; i<string_num; i++)
word[i] = toupper(word[i]);//使用這個函數時,無法把每個值賦給另外的string類,原因待議。
}
struct stringy { //8.4
char *str;
int ct;
};
void set(stringy &beany, char * testing)//8.4
{
int num;
num = strlen(testing);
    beany.str =new char[num+1];//這裏很關鍵,並沒有包括\0字符,所以需要加1
strcpy(beany.str, testing);
beany.ct = num;
}
void show(const stringy &beany, int times=1)//8.4
{
for (int i = 0; i < times; i++)
{
cout << "Beany.str: " << beany.str<<endl;
cout << "Beany.ct: " << beany.ct << endl;
}
}
void show(const char * testing, int times = 1)//8.4
{
for (int i = 0; i < times; i++)
{
cout << "testing: " << testing << endl;
}
}
template <typename T>
T max5(T array[],int num=5) //8.5
{
T max = array[0];
for (int i = 1; i < num; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
template <typename T> T maxn(T array[], int num = 5) //8.6
{
T max = array[0];
for (int i = 1; i < num; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
template <> const char* maxn<const char*>(const char * array[], int num) //8.6
{//具體化的類型是const int 故這裏需要const int;
int array_max_len= strlen(array[num-1]),long_num=0;
for(int i=num-1;i>=0;i--)

if (strlen(array[i]) >= array_max_len)
{
array_max_len = strlen(array[i]);
long_num = i;
}
}
return array[long_num];
}
template<typename T>
T SumArray(T arr[], int n) //8.7
{
int sum=0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
template<typename T>
T SumArray(T *arr[], int n) //8.7
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += *arr[i];
}
return sum;
}
void main()
{


/*for (int i = 0; i < 3; i++)
{
print_word("Hello!");
}
print_word("Hello!world", 3);*///8.1


/*CandyBar candy;
set_CandyBar(candy);
show_CandyBar(candy);*/ //8.2


/*string input_string; //8.3
cout << "Enter a string (q to quit): ";
do{ 
if (!(getline(cin,input_string))) break;
if (input_string == "q") break;
change_string(input_string);
cout << input_string << endl;
cout << "Next string(q to quit): ";
} while (input_string != "q");
cout << "Bye." << endl;*/


/*stringy beany;     //8.4
char testing[] = "Raeality isn't what it used to be.";
set(beany, testing);
show(beany);
cout << endl;
show(beany, 2);
cout << endl;
delete beany.str;
testing[0] = 'D';
testing[1] = 'u';
show(testing);
cout << endl;
show(testing, 3);
cout << endl;
show("Done!");*/


/*int array1[5] = { 12,15,78,20,35 };  //8.5
int array1_max = 0;
double array2_max = 0;
double array2[5] = { 22.2,52.3,52.4,89.5,79.6 };
array1_max=max5(array1);
array2_max=max5(array2);
cout << "Array1_max: " << array1_max << endl;
cout << "Array2_max: " << array2_max << endl;*/ 


/*int array1[6] = { 12,15,78,20,35,50 }; //8.6
int array1_max = 0;
double array2_max = 0;
const char * array3_long = {};
double array2[4] = { 22.2,52.3,52.4,89.5 };
const char * array3[5] = { "Hello!","OK!","HEllo!","Good","Bad" };
array1_max=maxn(array1);
array2_max=maxn(array2);
array3_long=maxn(array3,5);
cout << "Array1_max: " << array1_max << endl;
cout << "Array2_max: " << array2_max << endl; 
cout << "Array3_long: " << array3_long << endl;*/


struct debts { //8.7
char name[50];
double amount;
};
int things[6] = { 13,31,103,301,310,130 };
struct debts mr_E[3] = {
{"Ima Wolfe",2400.0},
{"Ura Foxe", 1300.0},
{"Iby Stout",1800.0}
};
double *pd[3];
for (int i = 0; i < 3; i++)
{
pd[i] = &mr_E[i].amount;
}
cout << "Suming Mr.E's counts of things:\n";
cout<<SumArray(things, 6)<<endl;
cout << "Suming Mr.E's debts:\n";
cout << SumArray(pd, 3) << endl;
system("pause");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章