//1 ************************************
using namespace std;
void disp(const char *s,int n = 1 );
static int l = 0;
int main(){
cout << "please input:\n";
char s[20];
cin.get(s, 20);
disp(s);
cout << "the another version which uses two parameters: \n";
disp(s, 5);
system("pause");
return 0;
}
void disp(const char *s, int n ) {
l++;
cout << " enter is : "<<endl;
if (n) {
for (int i = 0; i < l; ++i) {
cout << s << endl;
}
}
cout << "\n";
}
//2 ************************************
const int max = 20;
using namespace std;
struct candybar {
char broad[max];
double weight;
int caro;
};
void set(candybar &, char * b = "Millennium Munch", double w = 2.85, int c = 350);
void show(const candybar &);
int main(){
cout << "please input the info: \n " << endl;
candybar p;
char bro[max];
cout << "enter the broad name:\n";
cin.get(bro, max);
cout << "enter the weight \n";
double we;
cin >> we;
cin.get();
cin.get();
int ca;
cout << "enter the caro: \n";
cin >> ca;
set(p,bro,we,ca);
show(p);
system("pause");
return 0;
}
void show(const candybar & ca) {
cout << "******the info****** \n";
cout << "the broad name: "<<ca.broad<<endl;
cout << "the weight: " << ca.weight << endl;
cout << "the caro: " << ca.caro << endl;
}
void set(candybar &ca, char * b , double w, int c )
{
int i = 0;
for (i = 0; b[i] != '\n'&&i < strlen(b); ++i)
ca.broad[i] = b[i];
ca.broad[i] = '\0';
ca.weight = w;
ca.caro = c;
}
//3************************************
using namespace std;
void upper(string &s);
int main()
{
string s;
cout << "Enter a string (q tp quit): ";
while (cin >> s&&s != "q")
{
upper(s);
cout << "Next string(q to quit): ";
}
system("pause");
return 0;
}
void upper(string &s)
{
for (int i = 0; i < s.size(); ++i) {
s[i] = toupper(s[i]);
cout << s[i] ;
}
cout << endl;
}
//4************************************
#include<string>
#include<cstring>
using namespace std;
struct stringy {
char *str;
int ct;
};
void set(stringy &s,char *);
void show(const stringy &s,int n=1);
void show(const char *s,int n=1);
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
show(beany);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing, 3);
show("done!");
system("pause");
return 0;
}
void set(stringy &s,char *ch)
{
s.ct = strlen(ch);
s.str = new char[s.ct+1];
strcpy_s(s.str,strlen(ch)+1 ,ch);
}
void show(const stringy &s,int n)
{
while (--n > 0)
cout << s.str<<endl;
}
void show(const char *s,int n)
{
while (--n > 0)
cout << s << endl;
}
//5************************************
template<typename T>
T max(T a[5]);
int main()
{
using namespace std;
int a[5] = { 0,1,2,3,4 };
double b[5] = { 0.0,1.1,2.2,3.3,4.4 };
cout<< max(a) <<endl;
cout<< max(b) <<endl;
system("pause");
return 0;
}
template<typename T>
T max(T a[5])
{
T temp = 0;
for (int i = 0; i < 5; ++i)
{
temp = a[i] > a[i + 1] ? a[i] : a[i+1];
}
return temp;
}
//6************************************
template<typename T>
T maxn(T a[],int n);
template <>char *maxn(char **s, int n);
int main()
{
using namespace std;
int a[6] = { 1,2,3,4,5,6 };
double b[4] = { 1,2,3,4 };
cout << maxn(a, 3) << endl;
cout << maxn(b, 3) << endl;
char *str[3] = { "abc","abcd","abcde" };
cout << maxn(str, 3) << endl;
system("pause");
return 0;
}
template<typename T>
T maxn(T a[],int n)
{
T temp = 0;
for (int i = 0; i < n; ++i)
{
temp = a[i] > a[i + 1] ? a[i] : a[i + 1];
}
return temp;
}
template <>char *maxn(char **s, int n)
{
for (int i = 1; i < n; i++)
s[0] = strlen(s[i]) > strlen(s[0]) ? s[i] : s[0];
return s[0];
}
//7************************************
template <typename T>
void SumArray(T arr[], int n);
template <typename T>
void SumArray(T * arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
using namespace std;
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 << "Listing Mr. E's counts of things:\n";
SumArray(things, 6);
cout << "Listing Mr. E's debts:\n";
SumArray(pd, 3);
system("pause");
return 0;
}
template <typename T>
void SumArray(T arr[], int n)
{
using namespace std;
T temp = 0;
cout << "The sum of template A\n";
for (int i = 0; i < n; i++)
temp += arr[i];
cout << temp << endl;
}
template <typename T>
void SumArray(T * arr[], int n)
{
using namespace std;
T temp = 0;
cout << "The sum of template B\n";
for (int i = 0; i < n; i++)
temp += *arr[i];
cout << temp << endl;
}
C++ Primer Plus 第六版 第八章 练习题参考答案
?著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事?!?“怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...