8.8编程练习
1.
void printInfo(char* str, int check = 0)
{
static int count = 0;
++count;
if (check == 0)
{
cout << str << endl;
}
else
{
for (int i = 0; i < count; ++i)
{
cout << str << endl;
}
}
}
int main()
{
printInfo("fsdf");
printInfo("FableGame");
printInfo("FableGame2",32);
printInfo("Hello Fable");
printInfo("Hello Fable333333333333", 5);
cin.get();
cin.get();
return 0;
}
2.
struct CandyBar
{
string name;
double weight;
int energy;
};
void fillCandyBar(CandyBar& cb, const char* name = "Millennium Munch", double weight = 2.85, int energy = 350);
void printInfo(const CandyBar& cb);
int main()
{
CandyBar cb;
fillCandyBar(cb);
printInfo(cb);
cin.get();
cin.get();
return 0;
}
void fillCandyBar(
CandyBar& cb,
const char* name /*= "Millennium Munch"*/,
double weight /*= 2.85*/,
int energy /*= 350*/)
{
cb.name = name;
cb.weight = weight;
cb.energy = energy;
}
void printInfo(const CandyBar& cb)
{
cout << "name: " << cb.name << " weight:" << cb.weight << " energy:" << cb.energy;
}
3.
#include
#include
#include
#include
using namespace std;
void change(string& str)
{
for (size_t i = 0; i < str.length(); ++i)
{
str[i] = toupper(str[i]);
}
}
int main()
{
cout << "Enter a string (q to quit): ";
string str;
getline(cin, str);
while (cin && str != "q")
{
change(str);
cout << str << endl;
cout << "Next string (q to quit): ";
getline(cin, str);
}
cout << "Bye." << endl;
cin.get();
cin.get();
return 0;
}
4.
//
// main.cpp
// HelloWorld
// 关注微信公众号:传说之路,大家共同学习
// Created by feiyin001 on 16/4/3.
// Copyright (c) 2016年 FableGame. All rights reserved.
//
#include
#include
using namespace std;
//结构
struct stringy
{
char * str;
int ct;
};
//函数声明
void set(stringy& stry, const char* ch);
void show(const stringy& str, int times = 1);
void show(const char* ch, int times = 1);
//main函数
int main(int argc, const char * argv[]) {
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& stry, const char* ch)
{
stry.ct = (int)strlen(ch);
stry.str = new char[stry.ct];
strcpy(stry.str, ch);
}
//显示内容
void show(const stringy& str, int times)
{
for (int i = 0; i < times; ++i) {
cout << str.str << endl;
}
}
//显示字符串
void show(const char* ch, int times)
{
for (int i = 0; i < times; ++i) {
cout << ch << endl;
}
}
5.
template
T max5(T* a);
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
double b[] = { 23.32, 323, 22, 353.3, 323.3, 99999 };
cout << max5(a) << endl;
cout << max5(b) << endl;
std::cin.get();
std::cin.get();
return 0;
}
template
T max5(T* a)
{
T b = a[0];
for (int i = 1; i < 5; ++i)
{
if ( b < a[i])
{
b = a[i];
}
}
return b;
}
6.
template
T maxn(T a[], int num);
template<> char* maxn(char* a[], int num);
int main()
{
int a[6] = { 1, 2, 3, 4, 5, 6 };
double b[4] = { 23.32, 323, 22, 353.3 };
cout << maxn(a, 6) << endl;
cout << maxn(b, 4) << endl;
char* c[5] = { "abc", "fable game", "FableGame", "C++", "凡人修真3D" };
cout << maxn(c, 5) << endl;
std::cin.get();
std::cin.get();
return 0;
}
template
T maxn(T a[], int num)
{
T b = a[0];
for (int i = 1; i < num; ++i)
{
if ( b < a[i])
{
b = a[i];
}
}
return b;
}
template<>
char* maxn(char* a[], int num)
{
size_t len = strlen(a[0]);
char* ch = a[0];
for (int i = 1; i < num; ++i)
{
if (len < strlen(a[i]))
{
ch = a[i];
}
}
return ch;
}
7.
修改前:
#include
#include
#include
#include
using namespace std;
template
void ShowArray(T arr[], int n);
template
void ShowArray(T* arr[], int n);
struct debts
{
char name[50];
double amount;
};
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].amount;
}
cout << "Listing Mr. E's counts of things:\n";
ShowArray(things, 6);
cout << "Listing Mr. E's debts:\n";
ShowArray(pd, 3);
std::cin.get();
std::cin.get();
return 0;
}
template
void ShowArray(T arr[], int n)
{
cout << "template A\n";
for (int i = 0; i < n; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
}
template
void ShowArray(T* arr[], int n)
{
cout << "template B\n";
for (int i = 0; i < n; i++)
{
cout << *arr[i] << ' ';
}
cout << endl;
}
修改后:
#include
#include
#include
#include
using namespace std;
template
void ShowArray(T arr[], int n);
template
void ShowArray(T* arr[], int n);
template
T SumArray(T arr[], int n);
template
T SumArray(T* arr[], int n);
struct debts
{
char name[50];
double amount;
};
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].amount;
}
cout << "Listing Mr. E's counts of things:\n";
ShowArray(things, 6);
cout << "sum: " << SumArray(things, 6) << endl;
cout << "Listing Mr. E's debts:\n";
ShowArray(pd, 3);
cout << "sum: " << SumArray(pd, 3) << endl;
std::cin.get();
std::cin.get();
return 0;
}
template
void ShowArray(T arr[], int n)
{
cout << "template A\n";
for (int i = 0; i < n; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
}
template
void ShowArray(T* arr[], int n)
{
cout << "template B\n";
for (int i = 0; i < n; i++)
{
cout << *arr[i] << ' ';
}
cout << endl;
}
template
T SumArray(T arr[], int n)
{
T sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
template
T SumArray(T* arr[], int n)
{
T sum = 0;
for (int i = 0; i < n; i++)
{
sum += *arr[i];
}
return sum;
}