オリジナルのC ++ Primer Plus第8章授業後のプログラミング演習1-7

// 1题
#include
int number = 1;
void show(char *、int n = number);
名前空間stdを使用します。
int main()
{
char a [] =“ wo jiao wxp!”;
show(a);
show(a);
show(a);

return 0;

}

void show(char * a、int n)
{
for(int i = 0; i <n; i ++)
cout <<“第” << number << "调用関数:" << a << endl;
数値++;
}

// 2题
#include
using namespace std;
struct CandBar {
char cb_name [40];
float cb_weight;
int cb_heat;
};
void storage(CandBar&、char *、double、int);
void show(const CandBar&);

int main()
{
char name [] =“ Millennium Munch”;
CandBarミル;
ストレージ(ミル、名前、2.85、350);
show(mill);

return 0;

}

void storage(CandBar&a、char * p、double b、int n)
{
int i = 0;
while(* p)
{
a.cb_name [i] = * p;
i ++;
p ++;
}
a.cb_weight = b;
a.cb_heat = n;
}

void show(const CandBar&a)
{
cout << a.cb_name << endl;
cout << a.cb_weight << endl;
cout << a.cb_heat << endl;
}
// 3题
の#include
の#include
の#include
名前空間stdを使用。
void storage(string&);
int main()
{
文字列文;
cout << "Enter a string(q to quit):";
while(getline(cin、文)&&文!=“ q”)
{
storage(sentence);
cout <<センテンス<< endl;
cout << "次の文字列(終了するq):";
}
0を返します。
}
void storage(string&a)
{
int i = 0;


a [i] = toupper(a [i]);
i ++;
}
}

4 //タイトル
の#include
の#include
使用して名前空間STD;
構造体糸{
チャー* STR、
INT CT;
};
ボイドSET(糸と、のconst char型A []);
ボイド表示(A糸、N- = INT 1); / /関数テンプレートを使用

void show(char a []、int n = 1); //関数テンプレートオーバーロードを使用する

int main()
{
stringy beany;
char Testing [] =“現実は以前とは違う。”;
セット(ビーニー、テスト);
show(beany);
show(beany、2);
Testing [0] = 'D';
Testing [1] = 'u';
表示(テスト);
show(testing、3);
show( "Done!");
//主手順がありません、自己追加されました。delete
[] beany.str;

return 0;

}
void set(stringy&b、const char add [])
{

b.str = new char[strlen(add)+1];
strcpy(b.str, add);
b.ct = strlen(add)+1;

}

void show(stringy a、int n)
{
for(int i = 0; i <n; i ++)
std :: cout << a.str << '' << a.ct << std :: endl;
}

void show(char a []、int n)
{
for(int i = 0; i <n; i ++)
std :: cout << a << std :: endl;
}
// 5题
#include
テンプレート
T max5(T a [5]);
int main()
{
int num [5] = {1,2,3,5,4};
double d [5] = {1.1、2.1、5.1、3.1、4.1};
int n;
ダブルb;
n = max5(num);
b = max5(d);
std :: cout << "num max =" << n << std :: endl;
std :: cout << "d max =" << b << std :: endl;

return 0;

}
テンプレート
T max5(T a [5])
{
T max = 0;
for(int i = 0; i <5; i ++)
(max <a [i])?max = a [i]:max = max;
最大を返す;
}
// 6题
#include
#include
テンプレート
T maxn(T a []、int n);
template <> char * maxn <char *>(char * a []、int n);
int main()
{
名前空間stdを使用;
int number [6] = {11,22,3,5,67,2};
double b [4] = {1.1、4.4,3.2,2.2};
CHAR * P [5] = {
「AAAAAAA」、
「bbbbbbbbb」、
「cccccccccccc」、
「dddddddddddd」、
「eeeeee」
}。

int maxni;
maxni = maxn(number, 6);
double maxnd;
maxnd = maxn(b, 4);
char *maxnp;
maxnp = maxn<>(p,5);
cout <<"maxni = " << maxni << endl;
cout <<"maxnd = " << maxnd << endl;
cout <<"maxnp = " << maxnp << endl;

return 0;

}
テンプレート
T maxn(T a []、int n)
{
T max = 0;
for(int i = 0; i <n; i ++)
(max> a [i])?max = max:max = a [i];

return max;

}
template <> char * maxn(char * a []、int n)
{
char * p;
p = a [0];
for(int i = 1; i <n; i ++)
{
strlen§<strlen(a [i])?p = a [i]:p = p;
}
pを返します。
}
// 7题
#include
テンプレート
T ShowArray(T arr []、int n);

テンプレート
T ShowArray(T * arr []、int n);
struct負債{
文字名[50];
倍額;
};

int main()
{
名前空間stdを使用;
int things [6] = {13、31、103、301、310、130};
intのもの;
struct負債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;

thing = ShowArray(things, 6);
debt = ShowArray(pd, 3);
cout <<"The sum of things is " << thing <<'.' << endl;
cout <<"The sum of the debt is " << debt <<'.' << endl;

return 0;

}
テンプレート
T ShowArray(T arr []、int n)
{
名前空間stdを使用;
T合計= 0;
for(int i = 0; i <n; i ++)
合計+ = arr [i];
合計を返す;
}

テンプレート
T ShowArray(T * arr []、int n)
{
名前空間stdを使用;
T合計= 0;
for(int i = 0; i <n; i ++)
合計+ = * arr [i];
合計を返す;
}

オリジナルの記事を85件公開 Like1 Visits1889

おすすめ

転載: blog.csdn.net/Tekkenwxp/article/details/104033319