C++ primer plus 第八章编程练习
2017-03-10 10:35
441 查看
1、
2、
3、
5、
7、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第八章编程练习 第7题
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
using namespace std;
template<typename T>
T SumArray(T arr[], int n);
template<typename T>
T SumArray(T * arr[], int n);
struct debts
{
char name[50];
double amout;
};
int main()
{
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].amout;
cout << "Sum of Mr.E's counts of things:\n" << SumArray(things, 6) << endl;
cout << "Sum of Mr.E's debts:\n" << SumArray(pd, 3) << endl;
return 0;
}
template<typename T>
T SumArray(T arr[], int n)
{
cout << "template A\n";
T sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
template<typename T>
T SumArray(T * arr[], int n)
{
T sum = 0;
cout << "template B\n";
for (int i = 0; i < n; i++)
{
sum += *arr[i];
}
return sum;
}
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/09 From:C++ primer plus 第八章编程练习 第1题 *********************************************************************************************/ #include "stdafx.h" #include<iostream> #include<string> using namespace std; void print(const char *str, int n=0); int main() { const char * str="well,well,well"; cout << "++++++++++++++++++++++++\n"; print(str); cout << "++++++++++++++++++++++++\n"; print(str, 3); return 0; } void print(const char *str, int n) { if (n <= 0) n = 1; for (int i = 0; i < n;i++) cout << str<<endl; }
2、
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/09 From:C++ primer plus 第八章编程练习 第2题 *********************************************************************************************/ #include "stdafx.h" #include<iostream> using namespace std; struct CandyBar { char Name[20]; double Weight; int Calories; }; void fill(CandyBar &a,const char * N = "Millennium", const double W = 2.85,const int C = 350); void show(const CandyBar &b); int main() { CandyBar s,z; fill(s); show(s); char name[30]; double weight; int calories; cout << "Please enter the name:"; cin.getline(name, 30); cout << "Please enter the weight:"; cin >> weight; cout << "Please enter the calories:"; cin >> calories; fill(z, name, weight, calories); show(z); return 0; } void fill(CandyBar &a, const char* N, const double W,const int C) { strcpy_s(a.Name , N); a.Weight = W; a.Calories = C; } void show(const CandyBar &b) { cout << "Name:" << b.Name << endl; cout << "Weight:" << b.Weight << endl; cout << "Calories:" << b.Calories << endl; }
3、
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/09 From:C++ primer plus 第八章编程练习 第3题 *********************************************************************************************/ #include "stdafx.h" #include<iostream> #include<string> #include<cctype> using namespace std; void transform(string &str); int main() { cout << "Enter a string (q to quit):"; string s; getline(cin, s); while (s != "q") { transform(s); cout << s << endl; cout << "Next string (q to quit):"; getline(cin, s); } cout << "Bye." << endl; return 0; } void transform(string &str) { for (int i = 0; i < str.size(); i++) str[i] = toupper(str[i]); }4、
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/09 From:C++ primer plus 第八章编程练习 第4题 *********************************************************************************************/ #include "stdafx.h" #include<iostream> #include<string> using namespace std; struct stringy { char *str; int ct; }; void set(stringy &s, const char *str); void show(const stringy&s, int n=1); void show(char *str, 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 &s, const char *str) { int len = strlen(str); s.ct = len; s.str = new char(len + 1); strcpy_s(s.str,(len+1), str); } void show(const stringy&s,int n) { for (int i = 0; i < n; i++) cout << s.str<<endl; } void show(char *str, int n) { for (int i = 0; i < n; i++) cout << str << endl; }
5、
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/09 From:C++ primer plus 第八章编程练习 第6题 *********************************************************************************************/ #include "stdafx.h" #include<iostream> //#include<string> using namespace std; template<typename T> T max5(T z[]); int main() { int a[5] = { 1, 3, 6, 2, 5 }; double b[5] = { 2.4, 5.1, 1.6, 3.3, 9.1 }; int amax; double bmax; amax = max5(a); cout << "amax=" << amax << endl; bmax = max5(b); cout << "bmax=" << bmax << endl; return 0; } template<typename T> T max5(T z[]) { T temp; temp = z[0]; for (int i = 1; i < 5; i++) { if (temp <= z[i]) temp = z[i]; } return temp; }6、
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/09 From:C++ primer plus 第八章编程练习 第6题 ***************** 4000 ****************************************************************************/ #include "stdafx.h" #include<iostream> //#include<string> using namespace std; template<typename T> T maxn(T z[],int n); template<>char* maxn(char * a[], int n); int main() { int a[6] = { 1, 3, 6, 2, 5,2 }; double b[4] = { 5.1, 1.6, 3.3, 9.1 }; char *c[5] = { "sss", "s", "sssssssss", "ss", "ssss" }; cout << "amax=" << maxn(a, 6) << endl; cout << "bmax=" << maxn(b, 4) << endl; cout << "clongest=" << maxn(c, 5) << endl; return 0; } template<typename T> T maxn(T z[],int n) { T temp; temp = z[0]; for (int i = 1; i < n; i++) { if (temp <= z[i]) temp = z[i]; } return temp; } template<>char* maxn(char * a[], int n) { char * temp = a[0]; for (int i = 0; i < n; i++) { if (strlen(temp) < strlen(a[i])) temp=a[i]; } return temp; }
7、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第八章编程练习 第7题
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
using namespace std;
template<typename T>
T SumArray(T arr[], int n);
template<typename T>
T SumArray(T * arr[], int n);
struct debts
{
char name[50];
double amout;
};
int main()
{
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].amout;
cout << "Sum of Mr.E's counts of things:\n" << SumArray(things, 6) << endl;
cout << "Sum of Mr.E's debts:\n" << SumArray(pd, 3) << endl;
return 0;
}
template<typename T>
T SumArray(T arr[], int n)
{
cout << "template A\n";
T sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
template<typename T>
T SumArray(T * arr[], int n)
{
T sum = 0;
cout << "template B\n";
for (int i = 0; i < n; i++)
{
sum += *arr[i];
}
return sum;
}
相关文章推荐
- C++ Primer Plus第五版 第八章 编程练习答案
- c++ primer plus(第6版)中文版 第八章编程练习答案
- C++ Primer Plus第六版 第八章 编程练习答案
- c++ primer plus(文章6版本)中国版 编程练习答案第八章
- 【学习C++】C++ Primer Plus (第六版)第八章编程练习1-7
- C++ Primer Plus (第6版)编程练习 代码-----第八章
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第八章(chapter 8) 1-4
- C++ Primer Plus 第五版 第六章 编程练习 7
- C++ Primer Plus(第6版) 第6章 编程练习
- C++ Primer Plus(第6版) 第7章 编程练习
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习9
- C++ Primer Plus(第6版) 第5章 编程练习
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习6
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习7
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习2
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习6
- 【C++ Primer Plus 编程练习】 3.7
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习2
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习1
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习3