1.
#include<iostream>
using namespace std;
int n = 0;
void show(const char* a,int b=0)
{
n++;
if (b == 0)
{
cout << a<<endl;
}
else {
for (int i = 0; i < n; i++)
{
cout << a<<endl;
}
}
}
int main()
{
char a[] = "good";
show(a);
show(a, 10);
show(a, 20);
}
2.
#include<iostream>
using namespace std;
struct candybar {
const char *name;//必须加const ,原因还未知。
double heavy;
int hot;
};
void cand(candybar& a, const char* b = "Millennium Munch",const double c = 2.85,const int d = 350)
{
a.name = b;
a.heavy = c;
a.hot = d;
}
void show(candybar& a)
{
cout << "name:" << a.name<<endl;
cout << "heavy:" << a.heavy<<endl;
cout << "hot:" << a.hot << endl;
}
int main()
{
candybar a,b;
cand(a);
cand(b, "zhanglincong", 521.23,150);
show(a);
show(b);
return 0;
}
3.
#include<iostream>
#include<string>
using namespace std;
void transfer(string& a)
{
for (int i = 0; i < a.size(); i++)
{
if (a[i] == ' ')
continue;
a[i] = a[i] - 32;
}
}
int main()
{
string a;
cout << "Enter a string (q to quit):";
getline(cin,a);
while (a[0] != 'q' || a.size() > 1)
{
transfer(a);
cout << a<<endl;
cout << "Enter a string (q to quit):";
getline(cin, a);
}
cout << "bye!";
return 0;
}
4.
#include<iostream>
using namespace std;
#include<cstring>
struct stringy {
char* str;
int ct;
};
void set(stringy& a, char* ch)
{
a.str = new char[strlen(ch) + 1];
a.ct = strlen(ch);
a.str = ch;
}
void show(const stringy&a,const int b = 1)
{
for(int i=0;i<b;i++)
cout << a.str << endl;
}
void show(const char* a, int b = 1)
{
for (int i = 0; i < b; i++)
cout << a << endl;
}
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;
}
5.
#include<iostream>
using namespace std;
template <typename T>
T show(T *a)
{
T max = 0;
for (int i = 0; i < 5; i++)
{
if (i == 0)
max = a[i];
else if (a[i] > max)
max = a[i];
}
return max;
}
int main()
{
int a[5] = { 2,1,56,3565,489 };
double b[5] = { 135.23,13.12,487.66,45.12,56544.12 };
cout << show(a)<<endl;
cout << show(b) << endl;
return 0;
}
6.
#include<iostream>
using namespace std;
template <typename T>
T maxn(T A[], int len)
{
T max=A[0];
for (int i = 0; i < len; i++)
max = A[i] > max ? A[i] : max;
return max;
}
template<>char* maxn(char* a[], int len)
{
int max = -1;
char* p = NULL;
for (int i = 0; i < len; ++i)
{
if (strlen(a[i]) > max)
{
max = strlen(a[i]);
p = a[i];
}
}
return p;
}
int main()
{
int a[6] = { 1, 2, 3, 4, 5, 6 };
double b[4] = { 1.1, 2.2, 3.3, 4.4 };
const char* c[5] = { "fuck", "fuckfuck", "fuckfuckfuck", "fuckfuckfuckfuck", "fuckfuckfuckfuckfuck" };
cout << maxn(a, 6) << endl;
cout << maxn(b, 4) << endl;
cout << maxn(c, 5) << endl;
return 0;
}
7.
#include <iostream>
template <typename T> // template A
void SumArray(T arr[], int n);
template <typename T> // template B
void SumArray(T* arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
using namespace std;
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];
// set pointers to the amount members of the structures in mr_E
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Listing Mr. E's counts of things:\n";
// things is an array of int
SumArray(things, 6); // uses template A
cout << "Listing Mr. E's debts:\n";
// pd is an array of pointers to double
SumArray(pd, 3); // uses template B (more specialized)
cin.get();
return 0;
}
template <typename T>
void SumArray(T arr[], int n)
{
using namespace std;
T sum = 0;
cout << "template A\n";
for (int i = 0; i < n; i++)
sum = sum + arr[i];
cout << sum << endl;
}
template <typename T>
void SumArray(T* arr[], int n)
{
using namespace std;
T sum = 0;
cout << "template B\n";
for (int i = 0; i < n; i++)
sum = sum + *arr[i];
cout << sum << endl;
}
第七题直接抄的,感觉知识点都一样