#include "stdafx.h"
#include
#include
#include
#include
#include
const int SIZE = 101;
static int flag = 0;
using namespace std;
void print(string str,int n=0);
void print(string str, int n)
{
flag++;
cout << "第" << flag << "次调用print函数:\n";
if (n!=0)
{
for (int i = 0; i < flag; i++)
{
cout <<str << endl;
}
}
else
cout << str << endl;
}
void Xiti1()
{
string str;
int n=1;
while (n!=0)
{
cout << "输入字符串:";
cin.get();
getline(cin, str);
cout << "输入一个整数:";
if (cin >> n)
{
cout << "输入了字符和数字,执行双参数\n";
print(str, n);
}
else
{
cout << "仅输入字符,执行默认值\n";
print(str);
}
}
return;
}
struct CandyBar
{
char pinpai[30];
double weight;
int reliang;
};
void setting(CandyBar &cb ,const char*pp="Millennium Munch",const double weight=2.85,const int reliang=350);
void setting(CandyBar &cb, const char*pp, const double weight,const int reliang)
{
strcpy_s(cb.pinpai, pp);
cb.reliang = reliang;
cb.weight = weight;
}
void display(const CandyBar &cb);
void display(const CandyBar &cb)
{
cout << "品牌为:" << cb.pinpai<<endl;
cout << "重量为:" << cb.weight << endl;
cout << "热量为:" << cb.reliang << endl;
}
void Xiti2()
{
CandyBar cb;
char pinpai[30];
double weight;
int reliang;
setting(cb);
cout << "默认值\n";
display(cb);
cout << "输入品牌:\n";
cin.get();
cin.getline(pinpai, 30);
cout << "输入重量:";
cin >> weight;
cout << "输入热量:\n";
cin >> reliang;
setting(cb, pinpai, weight, reliang);
display(cb);
setting(cb);
cout << "默认值\n";
display(cb);
return;
}
void toup(string & str);
void toup(string & str)
{
for (unsigned int i = 0; i < str.size(); i++)
{
str[i]=toupper(str[i]);
}
}
void Xiti3()
{
cout << "输入一个字符串:\n";
string str;
cin.get();
getline(cin, str);
while ('q'!=str[0])
{
toup(str);
cout << str<<endl;
cout << "来,下一个";
getline(cin, str);
}
cout << "bye!";
return;
}
struct stringy {
char * str;
int ct;
};
void set(stringy & be,const char * str);
void set(stringy & be, const char * str)
{
be.str = new char[strlen(str) + 1];
strcpy_s(be.str, strlen(str)+1, str);
be.ct = strlen(str) + 1;
}
void show(stringy&be, int n=1);
void show(stringy&be, int n)
{
for (int i = 0; i < n; i++)
{
cout << be.str << endl;
}
cout << endl;
}
void show(char *str, int n=1);
void show(char *str, int n)
{
for (int i = 0; i < n; i++)
{
cout << str << endl;
}
cout << endl;
}
void Xiti4()
{
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;
}
template <typename T>
T max5(T * pp);
template <typename T>
T max5(T * pp)
{
T a = pp[0];
for (int i = 1; i < 5; i++)
{
if (a < pp[i]) a = pp[i];
}
return a;
}
void Xiti5()
{
int arr[5] = { 1,2,3,4,5 };
double arr2[5] = { 1.1,2.2,3.3,4.4,5.5 };
cout << max5(arr);
cout << max5(arr2);
return;
}
template<typename T>
T maxn(T *arr, int n);
template<typename T>
T maxn(T *arr,int n)
{
T a = arr[0];
for (int i = 1; i < n; i++)
{
if (a<arr[i]) a = arr[i];
}
return a;
}
template <> char*maxn(char * arr[], int n);
template <> char*maxn(char * arr[], int n)
{
char *a = arr[0];
for (int i = 1; i < n; i++)
{
if (strlen(a)<strlen(arr[i]))
{
a = arr[i];
}
}
return a;
}
void Xiti6()
{
int arr[6] = { 1,2,3,4,5,6 };
double arr1[4] = { 1.1,2.2,3.3,4.4 };
char *arr3[5] = { "asdf","sdghgj","sdfahg","agrgfjnfgj","sdfaew" };
char *arr4[5] = { "wtesgasdg","awerawegfa","werwagads","wr3gfd","wersdg" };
cout << maxn(arr, 6) << endl;
cout << maxn(arr1, 4) << endl;
cout << maxn(arr3, 5) << endl;
cout << maxn(arr4, 5) << endl;
return;
}
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;
};
template <typename T>
void SumArray(T arr[], int n)
{
using namespace std;
cout << "template A\n";
T sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
cout << sum;
cout << endl;
}
template <typename T>
void SumArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
T sum = 0;
for (int i = 0; i < n; i++)
sum += *arr[i];
cout << sum;
cout << endl;
}
void Xiti7()
{
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);
return;
}
void Xiti8()
{
return;
}
void Xiti9()
{
return;
}
void Xiti10()
{
return;
}
void Xiti10_2()
{
return;
}
int main()
{
int XitiNo = 1;
cout << "第四章编程练习答案:\n";
cout << "请输入习题编号或按(0)退出:\n";
cin >> XitiNo;
while (XitiNo != 0)
{
if (cin)
{
if (XitiNo >= 0 && XitiNo <= 10)
{
switch (XitiNo)
{
case 0:
{
cout << "即将退出程序啦!\n";
}break;
case 1:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti1();
}break;
case 2:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti2();
}break;
case 3:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti3();
}break;
case 4:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti4();
}break;
case 5:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti5();
}break;
case 6:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti6();
}break;
case 7:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti7();
}break;
case 8:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti8();
}break;
case 9:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti9();
}break;
case 10:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti10_2();
}break;
default:
{
cout << "即将退出程序啦\n";
XitiNo = 0;
}
break;
}
cout << "继续查看请输入习题编号或按(0)退出:\n";
cin >> XitiNo;
}
else
{
cout << "没有这样的编号\n继续查看请输入习题编号或按(0)退出:\n";
cin >> XitiNo;
}
}
else
{
XitiNo = 0;
}
}
system("pause");
return 0;
}