练习代码为本人所写,欢迎大家学习交流
若有疑问请留言,我将尽快回复!!!!
题8.1:
#include <iostream>
#include <ctime>
using namespace std;
void ShowStr(const char * str, int print_time = 1);
int main()
{
char str[100];
int time;
cout << "Enter the string: ";
cin.getline(str, 100);
cout << "Enter the time of show: ";
cin >> time;
ShowStr(str, time);
cout << endl;
ShowStr(str);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
void ShowStr(const char * str, int print_time) //记住定义函数的时候不写默认参数,而是在原型上默认
{
if(print_time > 0) //输入0次的时候则不显示
{
cout << str << " / ";
print_time--;
ShowStr(str, print_time);
}
}
题8.2:
#include <iostream>
#include <ctime>
using namespace std;
struct CandyBar
{
char name[128];
double weight;
int calorie;
};
void fill(CandyBar & candy_n, char * name_n, double weight_n, int calorie_n);
void show(const CandyBar & candy_n);
int main()
{
CandyBar candy1;
fill(candy1, "Millennium Munch", 2.85, 350);
show(candy1);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
void fill(CandyBar & candy_n, char * name_n, double weight_n, int calorie_n)
{
strcpy(candy_n.name, name_n); //实验证明C++使用strcpy函数是不用包含头文件<cstring>的
candy_n.weight = weight_n;
candy_n.calorie = calorie_n;
}
void show(const CandyBar & candy_n)
{
cout << "Name: " << candy_n.name << endl;
cout << "Weight: " << candy_n.weight << endl;
cout << "Calorie: " << candy_n.calorie << endl;
}
题8.3:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
void to_upper(string & srt);
int main()
{
string your_string;
cout << "Enter a string (q to quit): ";
getline(cin, your_string); //记住getline会读掉回车,但是在读数字之后用一定要先把回车读掉
while (your_string != "q")
{
to_upper(your_string);
cout << your_string << endl;
cout << "Next string (q to quit): ";
getline(cin, your_string);
}
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
void to_upper(string & srt)
{
for (int i = 0; i < srt.length(); i++)
{
srt[i] = toupper(srt[i]);
}
}
题8.4:(这里要注意)
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
struct stringy
{
char * str;
int ct;
};
void set(stringy & your_stringy, char * your_string);
void show(const stringy & your_stringy, int time = 1);
void show(const char * ar, int time = 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, 2);
show("Done!");
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
void set(stringy & your_stringy, char * your_string)
{
int string_length = strlen(your_string);
your_stringy.str = new char[string_length + 1]; //可以理解为给your_stringy.str这个指针定义是什么样的指针
//一定要用[]而不是()不然会有问题,我看别人写的这里就有bug
//这里我认为是函数局部作用new,所以不用delete
strcpy(your_stringy.str, your_string);
your_stringy.ct = string_length;
}
void show(const stringy & your_stringy, int time)
{
for (int i = 0; i < time; i++)
cout << your_stringy.str << endl;
}
void show(const char * ar, int time)
{
for (int i = 0; i < time; i++)
cout << ar << endl;
}
题8.5:(这道题我因为一个中文下输入的括号导致bug,我找了半天才找到啊,所以提醒在座各位一定要注意哈哈哈!!!)
#include <iostream>
#include <ctime>
using namespace std;
template <typename AnyType>
AnyType maxn(AnyType *ar);
int main()
{
int int_array[5] = {1, 2, 5, 4, 3};
double double_array[5] = {2.4, 4.1, 3.1, 5.1, 5.4};
cout << maxn(int_array) << endl;
cout << maxn(double_array) << endl;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
template <typename AnyType>
AnyType maxn(AnyType *ar)
{
AnyType max = ar[0];
for (int i = 1; i < 5; i++)
{
if (ar[i] > max)
max = ar[i];
}
return max;
}
题8.6:
#include <iostream>
#include <ctime>
using namespace std;
template <typename AnyType>
AnyType maxn(AnyType *ar, int size);
template <> const char * maxn(const char *str[], int n);
int main()
{
int int_array[6] = { 43, 235, 54, 232, 123, 65 };
double double_array[4] = { 32.1, 453.2, 53.3, 67.4 };
const char * str_array[5] = { "A", "AB", "ABC", "ABCDE", "ABCD" }; //str_array[5]是五个字符串的指针数组
//可以看做str_array[0]="A"
//可以看做是二维数组
int int_max = maxn(int_array, 6);
double double_max = maxn(double_array, 4);
const char * length_max_str = maxn(str_array, 5);
cout << "max of int array: " << int_max << endl;
cout << "max of double array: " << double_max << endl;
cout << "max length string of string array: " << length_max_str << endl;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
template <typename AnyType>
AnyType maxn(AnyType *ar, int size)
{
AnyType max = ar[0];
for (int i = 0; i < size; i++)
{
if (max < ar[i])
{
max = ar[i];
}
}
return max;
}
template <> const char * maxn(const char *str[], int n)
{
const char * ps = str[0];
for (int i = 0; i < n; i++)
{
if (strlen(ps) < strlen(str[i])) //注意这里比较的不是地址而是对应的字符串!!!!
{
ps = str[i];
}
}
return ps;
}
题8.7:
#include <iostream>
#include <ctime>
using namespace std;
struct debts
{
char name[50];
double amount;
};
template <typename AnyType>
AnyType SumArray(AnyType ar[], int n);
template <typename AnyType>
AnyType SumArray(AnyType * ar[], int n);
int main()
{
int thing[6] = {13, 31, 103, 301, 310, 130};
debts mr_E[3] =
{
{"A", 2400.0},
{"B", 1300.0},
{"C", 1800.0}
};
double * pd[3];
for (int i = 0; i <3; i++)
pd[i] = &mr_E[i].amount;
cout << "Sum of thing: " << SumArray(thing, 6) << endl;
cout << "Sum of debts: " << SumArray(pd, 3) << endl;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
template <typename AnyType>
AnyType SumArray(AnyType ar[], int n)
{
AnyType sum =0;
for (int i = 0; i < n; i++)
sum += ar[i];
return sum;
}
template <typename AnyType>
AnyType SumArray(AnyType * ar[], int n)
{
AnyType sum =0;
for (int i = 0; i < n; i++)
sum += *ar[i];
return sum;
}