1. 程式人生 > >C ++ Primer Plus 第六版 第八章程式設計練習答案

C ++ Primer Plus 第六版 第八章程式設計練習答案

1.編寫通常接受一個引數(字串的地址),並列印該字串的函式。不過,如果提供了第二個引數(int型別),且該引數不為0,則該函式列印字串的次數將為該函式被呼叫的次數(注意,字串的列印次數不等於第二個引數的值,而等於函式被呼叫的次數)。是的,這是一個非常可笑的函式,但它讓讀者能夠使用本章介紹的一些技術。在一個簡單的程式中使用該函式,以演示該函式是如何工作的。

#include <iostream>
#include <string>
using namespace std;
void scanf ( string str )

{
	cout << str << endl;

}

void scanf ( string str, int n )

{
	if ( n != 0 )
	{
		cout << str << endl;

		scanf ( str, --n );
	}




}
int main()
{

	int n;
	string str;
	cout << "Please enter a string and number" << endl;
	while ( cin >> str >> n )
	{
		if ( n == 0 )
			scanf ( str );
		else scanf ( str, n );
		cout << "Please enter a string and number" << endl;
	}
	return 0;
}


2、CandyBar結構飽含3個成員。第一個成員儲存candy bar的品牌名稱;第二個成員儲存candy bar的重量(可能有小數);第三個成員儲存candy bar的熱量(整數)。請編寫一個程式,它使用一個這樣的函式,即將CandyBar的引用、char指標、double和int作為引數,並用最後3個值設定相應的結構成員。最後3個引數的預設值分別為"Millennium Munch"、2.85和350。另外,該程式還包含一個以CandyBar的引用為引數,並顯示結構內容的函式。請儘可能使用const.

note:如果預設引數char b[]不加const可以執行但會提示

warning: ISO C++ forbids converting a string constant to 'char*' 

因為

 const char * 和 char * 不是一回事
因為字串常量所在的地址
內容是不可修改的
所以用 const char *,可以在編譯期間就禁止修改行為 
#include <iostream>
#include <cstring>
const int ArSize = 100;
using namespace std;
struct candybar
{
	char brand[ArSize];
	double weight;
	int energy;
};
void cset ( candybar &c, const char b[] = "Millennium Munch", double w = 2.85, int e = 350 )
{
	strcpy ( c.brand, b );
	c.weight = w;
	c.energy = e;
}
void display ( const candybar &c )
{
	cout << c.brand << endl << c.weight << endl << c.energy << endl;

}
int main()
{
	candybar c;
	char b[ArSize];
	double w;
	int e;
	cset ( c );
	display ( c );
	cout <<endl<< "Please set struct c new volume" << endl;
	cout << "c.brand = ";
	cin.getline ( b, ArSize );
	cin.clear();
	cin.sync();
	cout << endl;
	cout << "c.weight = ";
	cin >> w;
	cout << endl;
	cout << "c.energy = ";
	cin >> e;
	cout << endl;
	cset ( c, b, w, e );
	display ( c );
	return 0;
}

3、編寫一個函式,它接受一個指向string物件的引用作為引數,並將該string物件的內容轉換為大寫,為此可使用函式toupper()。.然後編寫一個程式,它通過使用一個迴圈讓你能夠用不同的輸入來測試這個函式,該程式執行情況如下:
enter a string (q to quit) :go away
GO AWAY
next string (q to quit) : good grief !
GOOD GRIEF!
next string (q to quit) : q
bye.
這題我沒包含cctype,因為我的編譯器已自動加進去了,你們用的天一起可能沒有,所以要自己加
#include <iostream>
#include <string>

using namespace std;

void change ( string &s )
{
	int i = 0;
	while ( s[i] != '\0' )
	{
		if ( islower ( s[i] ) )
			s[i]=toupper ( s[i] );
		cout << s[i++];
	}
	cout << endl;
}
int main()
{
	string str;
	cout << "Enter a string (q to quit): ";
	while ( getline ( cin, str ) && str != "q" )
	{
		change ( str );
		cout << "next string (q to quit): ";
	}
	cout << "Bye.";

	return 0;
}


4.下面是一個程式框架
#include<iostream>
using namespace std;
#include<cstring> 

//for strlen(),strcpy() 

//指向字串

struct stringy {
char * str; //points to a string
int ct; //length of string (not couting '\0')
};
 
// prototypes for set(), show(), and show() go here

 // set、show、show 三個函式的原型
int main()
{
string beany;
char testing[]="Reality isn't what it used to be.";
 
set(beany,testing); //first argument is a reference,
//allocates space to hold copy of testing
//sets str member of beany to point to the
//new block, copies testing to new block,
//and sets ct member of beany

 // 分配空間給 testing 這個變數的副本
// 設定 beany 的成員 str 指向新分配的記憶體
// 把 testing 複製到新記憶體去
//然後設定 beany 的成員 ct  //第一個引數是一個結構別名,用於儲存第二個引數傳遞的值(第二個因為是字串,所以是指標),把第二個設定為第一個結構的str成員
//大概意思就是beany.str  new一個新的字串,然後將testing賦值到這裡,然後根據字串長度要計數

show(beany); //prints member string once 輸出字串成員一次
show(beany, 2); //prints member string twice 輸出字串成員兩次
testing[0]= 'D';
testing[1] = 'u';
show(testing); //prints testing string once 輸出 testing 一次
show(testing, 3); //prints testing string thrice 輸出 testing 一次
show("Done!");
return 0;
}

請提供其中描述的函式和原型,從而完成該程式。注意,應有兩個show()函式,每個都使用預設引數。請儘可能的使用const引數。set() 使用new分配足夠的空間來儲存制定的字串。這裡使用的技術與設計和實現類使用的相似。(可能害必須修改標頭檔案的名稱,刪除using編譯指令,這取決於所用的編譯器。)

#include<iostream>
using namespace std;
#include<cstring>

struct stringy
{
	char * str; 
	int ct; 
};

void set ( stringy& a, const char *t )
{
	a.ct = strlen ( t ) + 1;
	a.str = new char[a.ct];
	strcpy ( a.str, t );
}

void show ( const stringy& a, int n = 1 )
{

	for ( int i = 0; i < n; i++ )
		cout << a.str << endl;
}

void show ( const char* t, int n = 1 )
{

	for ( int i = 0; i < n; i++ )
	{
		if ( strcmp ( t, "Done!" ) == 0 )
		{
			cout << "Done!" << endl;
			break;
		}
		cout << t << endl;
	}
}
int main()
{
	stringy beany;
	char testing[] = "Reality isn't what it used to be.";
	set ( beany, testing );
	show ( beany ); 
	show ( beany, 2 ); 
	testing[0] = 'D';
	testing[1] = 'u';
	show ( testing ); 
	show ( testing, 3 ); 
	show ( "Done!" );
	return 0;
}

5、編寫模板函式max5(),它將一個包含5個T型別元素的陣列作為引數,並返回陣列中最大的元素(由於長度固定,因此可以在迴圈中使用硬編碼,而不必通過引數來傳遞)。在一個程式中使用該函式,將T替換為一個包含5個int值的陣列和一個包含5個dowble值的陣列,以測試該函式。
#include <iostream>
using namespace std;
template<typename T>
T max(T *a)
{

	for(int i=0;i<5;i++)
		if(a[i]>a[0]) a[0]=a[i];
	return a[0];
}

int main()
{
    int a[5]={15,21,3,4,55};
    double b[5]={3.5,89.5,45.5,99.5,1.5};
    cout<<max(a)<<endl<<max(b)<<endl;
    return 0;
}

6、編寫模板函式maxn(),它將由一個T型別元素組成的陣列和一個表示陣列元素數目的整數作為引數,並返回陣列中最大的元素。在程式對它進行測試,該程式使用一個包含6個int元素的陣列和一個包含4個double元素的數組來呼叫該函式。程式還包含一個具體化,它將char指標陣列和陣列中的指標數量作為引數,並返回最長的字串的地址。如果有多個這樣的字元串,則返回其中第一個字串的地址。使用由5個字串指標組成的陣列來測試該具體化。

/*我把template<> const char* maxn( const char **a, const int n ) const去掉

template<> char* maxn( char **a, const int n ) 輸出是Fuck me,這樣的話maxn ( c, 5 )呼叫的就是第一個模板,比較地址,所以是fuckme

#include <iostream>
#include<cstring>
using namespace std;
template<typename T>
T maxn ( T *a, const int n )
{
	for ( int i = 0; i < n; i++ )
		if ( a[i] > a[0] ) a[0] = a[i];
	return a[0];
}
template<> const char* maxn( const char **a, const int n )
{
	int i,j=0;
	for(i=0;i<n;i++)
	if(strlen(a[i])>strlen(a[j])) j=i;
	return a[j];

}

int main()
{
	int a[6] = {5, 7, 3, 6, 8, 0};
	double b[4] = {1.1, 2.2, 0.1, 1.01};
	const char *c[5] = {"Fuck", "Fuck you", "Fuck you now", "Fuck you and mother", "Fuck me"};
	cout << maxn ( a, 6 ) << endl;
	cout << maxn ( b, 4 ) << endl;
	cout << maxn ( c, 5 ) << endl;

	return 0;
}