题一
#include <iostream>
using namespace std;void print(char *str, int n = 0);int main()
{char str[20] = "leonardo liu";print(str);print(str, 5);print(str, 16);return 0;
}void print(char *str, int n)
{static int flag = 0; // 唯一初始化flag++;if (n == 0)cout << str << endl;else{for (int i = 0; i < flag; i++)cout << str << endl;}cout << endl;return;
}
题二
#include <iostream>
using namespace std;
struct CandyBar
{char *name;double weight;int calorie;
};
void Show(CandyBar &candy, char *p = "Millennium, Munch", double a = 2.85, int b = 350);
int main()
{CandyBar can1;Show(can1);cout << can1.calorie << endl;return 0;
}
void Show(CandyBar &candy, char *p, double a, int b)
{candy.name = p;candy.weight = a;candy.calorie = b;cout << candy.name << endl;cout << candy.weight << endl;cout << candy.calorie << endl;
}
题三
#include <iostream>
#include <cctype>
using namespace std;
void upper(string &str);
int main()
{string str1;cout << "Enter a string (q to quit):";while (str1 != "q") // string 类型可以直接比较{upper(str1);cout << "Next string (q to quit): ";getline(cin, str1);}cout << "Bye.";return 0;
}
void upper(string &str)
{for (int i = 0; str[i]; i++)str[i] = toupper(str[i]);cout << str << endl;
}
题四
#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{char *str; // string pointerint ct; // length of string (not counting '\0')
};
void set(stringy &rs, char a[]);
void show(stringy s, int a = 1);
void show(string str, int a = 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! ");return 0;
}
void set(stringy &rs, char a[])
{strcpy(rs.str, a);rs.ct = strlen(a);
}
void show(stringy s, int a)
{for (int i = 0; i < a; i++){cout << "Str: " << s.str << endl;cout << "length: " << s.ct << endl;}
}
void show(string str, int a)
{for (int i = 0; i < a; i++){cout << "Str: " << str << endl;}
}
题五
#include <iostream>
using namespace std;
const int SIZE = 5;
template <class Any>
Any max(Any a[]);
int main()
{int int_arr[SIZE];double double_arr[SIZE];cout << "Enter five int numbers: ";for (int i = 0; i < SIZE; i++){cin >> int_arr[i];}cout << "Enter five double numbers: ";for (int i = 0; i < SIZE; i++){cin >> double_arr[i];}cout << "The maximum of int array is " << max(int_arr) << endl;cout << "The maximum of int array is " << max(double_arr) << endl;cout << "Done!";return 0;
}
template <class Any>
Any max(Any a[])
{Any temp = a[0];for (int i = 1; i < SIZE; i++){a[i] > temp ? temp = a[i] : temp = temp;}return temp;
}
题六
#include <iostream>
#include <cstring>
using namespace std;
template <class Any>
Any maxn(Any a[], int n);
template <>
const char *maxn<const char *>(const char *arr[], int n);
int main()
{int int_arr[6] = {1, 2, 3, 4, 5, 6};double double_arr[4] = {1.1, 2.2, 3.3, 1.5};const char *char_arr[5] = {"one", "two", "three", "four", "five"};cout << "The maximum of int array is " << maxn(int_arr, 6) << endl;cout << "The maximum of int array is " << maxn(double_arr, 4) << endl;cout << "The maximum of char array is " << maxn(char_arr, 5) << endl;cout << "The address of the longest string is: " << (void *)maxn(char_arr, 5) << endl; //return 0;
}
template <class Any>
Any maxn(Any a[], int n)
{Any temp = a[0];for (int i = 1; i < n; i++){a[i] > temp ? temp = a[i] : temp = temp;}return temp;
}
template <>
const char *maxn(const char *arr[], int n)
{const char *temp = arr[0];for (int i = 1; i < n; i++){strlen(arr[i]) > strlen(temp) ? temp = arr[i] : temp = temp;}return temp;
}
题七
#include <iostream>
template <typename T>
T SumArray(T arr[], int n);template <typename T>
T SumArray(T *arr[], int n);
using namespace std;
struct debts
{char name[50];double amount;
};
int main(void)
{int things[6] = {13, 31, 103, 301, 310, 130};struct debts mr_R[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_R[i].amount;cout << "Listing Mr. R's counts of things: \n"<< SumArray(things, 6) << endl;cout << "Listing Mr R's debts: \n"<< SumArray(pd, 3) << endl;return 0;
}
template <typename T>
T SumArray(T arr[], int n)
{using namespace std;T temp;// cout << "template A\n";for (int i = 0; i < n; i++)temp += arr[i];return temp;
}template <typename T>
T SumArray(T *arr[], int n)
{// cout << "template B\n";T temp;for (int i = 0; i < n; i++)temp += *arr[i];return temp;
}