我是靠谱客的博主 傻傻发夹,最近开发中收集的这篇文章主要介绍第八章程序练习,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

第八章编程练习

1. 编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个函数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。

#include<iostream>
#include<cstring>
using namespace std;
void Test(string x,int n=1);
int main(){
string a="ABC";
Test(a);
cout<<endl;
Test(a,10);
cout<<endl;
Test(a,100);
}
void Test(string x,int n){
static int Time=0;
//静态变量
Time++;
if(n==1) cout<<x;
else
for(int i=0;i<Time;i++){
cout<<x;
}
}

输出:

ABC
ABCABC
ABCABCABC

2. CandyBar结构包含3个成员。第一个成员存储candy bar的品牌名称;第二个成员存储candy bar的重量(可能有小数);第三个成员存储candy bar的热量(整数)。请编写一个程序,它使用一个这样的函数,即将CandyBar的引用、char指针、double和int作为参数并用最后3个值设置相应的结构成员。最后3个参数的默认值分别为“Millennium Munch”、2.85和350。另外,该程序还包含一个以CandyBar的引用为参数,并显示结构内容的函数,请尽可能的使用const。

#include<iostream>
using namespace std;
struct CandyBar{
string Name;
double Height;
int Hot;
};
void Test(CandyBar &x,const char* y="Millenium Munch",const double i=2.85,const int j=350);
int main(){
struct CandyBar Box_1;
struct CandyBar Box_2;
Test(Box_1);
cout<<endl;
Test(Box_2,"HUANG",170,250);
}
void Test(CandyBar &x,const char* y,const double i,const int j){
x.Name=y;
x.Hot=j;
x.Height=i;
cout<<x.Name<<endl;
cout<<x.Height<<endl;
cout<<x.Hot<<endl;
}

输出

`Millenium Munch
2.85
350
HUANG
170
250:

3.编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,为此可使用表6.4描述的函数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.

#include<iostream>
using namespace std;
void Test(string &x);
int main(){
string Str;
cout<<"Enter a string (q to quit):";
while(getline(cin,Str)&&Str!="q"){
//getline(cin,string)是针对string的,可读入空格 ;
cin 不能读入空格
Test(Str);
cout<<Str<<endl;
cout<<"Next string (q to quit):";
}
cout<<"Bye.";
}
void Test(string &x){
for(int i=0;i<x.size();i++){
//if(x[i]>=97) x[i]=(x[i]-97)+65;
//ASCALL 'a'=97
'A'=65
if(x[i]>=97) x[i]=toupper(x[i]);
//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.

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 '')
};
// prototypes for set(), show(), and show() go here
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
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
show(testing, 3); //prints testing string thrice
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 &x, char* y);
void show(const stringy &x,const int n=1);
void show(const char* x,const int n=1);
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;
}
void set(stringy &x, char* y){
x.ct = strlen(y) + 1;
x.str=new char(x.ct+1);
strcpy(x.str,y);
//使用strcpy系列的函数就要为 x.str申请空间 ,若直接给 x.str赋值则不用
}
void show(const stringy &x,const int n){
for(int i=0;i<n;i++){
cout<<x.str<<endl;
}
}
void show(const char* x,const int n){
for(int i=0;i<n;i++){
cout<<x<<endl;
}
}

5.编写模板函数max5(),它将一个包含5个T类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。

#include<iostream>
#include<cstring>
using namespace std;
template <typename T>
T max5(T x[5]);
int main(){
double a[5]={1,2,3,4,5};
int b[5]={6,7,8,20,30};
cout<<max5(a)<<endl;
cout<<max5(b)<<endl;
}
template <typename T>
T max5(T x[5]){
T max=0;
for(int i=0;i<5;i++){
if(x[i]>max) max=x[i];
}
return max;
}

输出

5
30

6.编写模板函数maxn(),它将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。在程序对它进行测试,该程序使用一个包含6个int元素的数组和一个包含4个double元素的数组来调用该函数。程序还包含一个具体化,它将char指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来测试该具体化。

#include<iostream>
#include<cstring>
using namespace std;
template <typename T>
T max5(T x[5],int n);
template <> char* max5(char* x[],int n);
int main(){
double a[4]={1,2,3,4};
int b[6]={6,7,8,20,30,75};
cout<<max5(a,4)<<endl;
cout<<max5(b,6)<<endl;
char* p[5];
char*q;
p[0]="ABCD";
p[1]="ABC";
p[2]="AB";
p[3]="ABCDEF";
p[4]="ABCDEFFF";
q=max5(p,5);
cout<<&q;
}
template <typename T>
T max5(T x[5],int n){
T max=0;
for(int i=0;i<n;i++){
if(x[i]>max) max=x[i];
}
return max;
}
template <>char* max5(char* x[],int n){
int count[5];
int max=0;
int record;
for(int i=0;i<n;i++){
count[i]=strlen(x[i]);
if(count[i]>max)
{
max=count[i];
record=i;
}
}
return x[record];
}

输出

4
75
0x6ffe28

7.修改程序清单8.14,使其使用两个名为SumArray()的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示thing的总和以及所有debt的总和。

#include<iostream>
#include<cstring>
using namespace std;
template <typename T>
void ShowArray(T arr[],int n);
template <typename T>
void ShowArray(T* arr[],int n);
struct debts{
char name[50];
double amount;
} mr_E[3]{
{"ABC",2400.0},
{"ABCD",1300.0},
{"ABCDE",1800.0}
};
int main(){
int thing[7]={13,31,103,301,310,130};
double *pd[3];
for(int i=0;i<3;i++){
pd[i]=&mr_E[i].amount;
//注: 当结构体为数组时,结构名+“. ”不显示提示
; pd是指向mr_E[]的指针
}
ShowArray(thing,6);
ShowArray(pd,3);
}
template <typename T>
void ShowArray(T arr[],int n){
int count=0;
int i=0;
while(arr[i]){
count=count+*arr;
arr++;
}
cout<<count<<endl;
}
template <typename T>
void ShowArray(T* arr[],int n){
int count=0;
for(int i=0;i<n;i++){
count=count+*arr[i];
}
cout<<count;
}

输出

888
5500

最后

以上就是傻傻发夹为你收集整理的第八章程序练习的全部内容,希望文章能够帮你解决第八章程序练习所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部