您的位置:首页 > 编程语言 > C语言/C++

【学习C++】C++ Primer Plus (第六版)第八章编程练习1-7

2016-04-22 13:37 274 查看
1.
#include <iostream>
#include <string>
using namespace std;
void priStr(char *);
void priStr(char *, int n);
static int i = 1;
int main()
{
char a[10] = "liujiayu";
priStr(a);
for (int i = 1; i < 4; i++){
priStr(a, i);
}
cin.get();
return 0;

}
void priStr(char *a){
cout << a << endl;
}
void priStr(char *a, int n){
if (n != 0){
for (int j = 0; j < i; j++)
cout << a << endl;
i++;
}
else
cout << "input error" << endl;
}

2.

#include <iostream>
#include <string>
using namespace std;
struct CandyBar{
char * brandName;
double weight;
int cal;
};
void setCandy(CandyBar &a, char *n="Millennium Munch", double w=2.85, int c=350);
void showCandy(const CandyBar &);
int main()
{
CandyBar candy;
setCandy(candy, "ljy", 3.8, 400);
showCandy(candy);
setCandy(candy);
showCandy(candy);
cin.get();
return 0;
}
void setCandy(CandyBar &a, char *n, double w, int c){
a.brandName = n;
a.weight = w;
a.cal = c;
}
void showCandy(const CandyBar &a){
cout << a.brandName << endl;
cout << a.weight << endl;
cout << a.cal << endl;
}
3.
#include <iostream>
#include <string>
using namespace std;
char * l_u(string &);
int main()
{
cout << "Enter a string(q to quit): ";
string str;
while (getline(cin, str)){
if (str != "q"){
cout << l_u(str)<<endl;
delete [] l_u(str);
cout << "Next string(q to quit):";
}
else
break;
}
cout << "Bye."<<endl;
cin.get();
return 0;
}
char * l_u(string &str){
int n = str.size();
char *s = new char[n+1];
for (int i = 0; i < n; i++){
s[i] = toupper(str[i]);
}
s
= '\0';
return s;
}
4.
#include <iostream>
#include <cstring>
using namespace std;
struct stringy{
char * str;
int ct;
};
void set(stringy &, char *);
void show(const stringy &, int n = 1);
void show(const char *, 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!");
cin.get();
return 0;
}
void set(stringy &str, char *ch){
int strl = strlen(ch);
char *s = new char [strl+1];
for (int i = 0; i < strl; i++){
s[i] = ch[i];
}
s[strl] = '\0';
str.str = s;
str.ct = 2;
}
void show(const stringy &s, int n){
for (int i = 0; i < n; i++){
cout << s.str << endl;
}
}void show(const char *c, int n){
for (int i = 0; i < n; i++){
cout << c << endl;
}
}
5.
#include <iostream>
#include <string>
using namespace std;
template<typename T>
T max5(T *a);
int main()
{
int a[5] = { 0, 1, 2, 3, 4 };
double b[5] = { 0.0, 1.0, 2.0, 3.0, 4.9 };
cout << max5(a) << endl;
cout << max5(b) << endl;
cin.get();
return 0;
}
template<typename T>
T max5(T *a){
T temp=a[0];
for (int i = 1; i < 5; i++){
if(a[i]>temp)
temp = a[i];
}
return temp;
}
6.

#include <iostream>
#include <string>
using namespace std;
template<typename T>
T maxn(T a[],int n);
template <> char* maxn<char *>(char * a[], int n);
int main()
{
int a[6] = { 0, 1, 2, 3, 4,5 };
double b[4] = { 0.0, 1.0, 2.0, 3.9 };
char* c[5] = { "liu", "jia", "yu", "good", "girl" };
cout << maxn(a,6) << endl;
cout << maxn(b,4) << endl;
cout << maxn(c, 5) << " at " << static_cast<const void *>(maxn(c, 5)) << endl;
cin.get();
return 0;
}
template<typename T>
T maxn(T *a,int n){
T temp=a[0];
for (int i = 1; i < n; i++){
if(a[i]>temp)
temp = a[i];
}
return temp;
}
template <> char* maxn<char *>(char * a[], int n){
int temp = strlen(a[0]);
int l=0;
for (int i = 1; i < n; i++){
if (strlen(a[i]) > temp){
temp = strlen(a[i]);
l = i;
}
}
return a[l];
}
7.
#include <iostream>
using namespace std;
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()
{
int things[6] = { 13, 31, 103, 301, 310, 130 };
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 << "Counting Mr.E's counts of things:\n";
SumArray(things, 6);
cout << "Counting Mr.E's debts:\n";
SumArray(pd, 3);
cin.get();
return 0;
}
template <typename T>
void SumArray(T arr [], int n){
cout << "Template A\n";
T sum=arr[0];
for (int i = 1; i < n; i++){
sum += arr[i];
}
cout << sum<<endl;
}
template <typename T>
void SumArray(T * arr [], int n){
cout << "Template B\n";
T sum = *arr[0];
for (int i = 1; i < n; i++){
sum += *arr[i];
}
cout << sum << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++