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;
}



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值