练习1 #include <iostream> using namespace std; namespace { int run_time=0; } void funny(const char str[], int m=0); int main() { cout << "Call cout function 1st" << endl; funny("This is cout function"); cout << "Call cout function 2nd" << endl; funny("This is cout function"); cout << "Call cout function 3rd with second argument no equal to zero" << endl; funny("This is cout function",1); return 0; } void funny(const char str[], int m) { ++run_time; if (m>0) { for (int i=0;i<run_time;i++) cout << str << endl; } else cout << str << endl; } 练习2 #include <iostream> using namespace std; struct CandyBar { char brandname[20]; float weight; unsigned calorie; }; void setmember(CandyBar &candybar, char *name="Millennium Munch",double w=2.85,int c=350); void display(const CandyBar &candybar); int main() { CandyBar candybar; setmember(candybar); display(candybar); return 0; } void setmember(CandyBar &candybar, char *name,double w,int c) { strcpy(candybar.brandname,name); candybar.brandname[strlen(name)]='/0'; candybar.weight=w; candybar.calorie=c; } void display(const CandyBar &candybar) { cout << "BrandName: " << candybar.brandname << endl; cout << "Weight : " << candybar.weight << endl; cout << "Calorie :" << candybar.calorie << endl; } 练习3 #include <iostream> using namespace std; string &toUpp(string &str); int main() { std::string str; cout << "Enter a string (q to quit):"; getline(cin,str); while (str!="q"){ cout << toUpp(str) << endl; cout << "Next string (q to quit):"; getline(cin,str); } return 0; } string &toUpp(string &str) { for (unsigned i=0;i<str.size();i++) str[i]=toupper(str[i]); return str; } 练习4 #include <iostream> using namespace std; #include <cstring> // for strlen(), strcpy() struct stringy { char * str; // points to a string int ct; // length of string (not counting '/0') }; // prototypes for set(), show(), and show() go here void set(stringy &, const char *); void show(const char *, int =1); void show(const stringy &, int =1); int main() { stringy beany; char testing[] = "Reality isn't what it used to be. "; set(beany, testing); //first argument is a reference, // allocate space to hold copy of testing, // sets str member of beany to point to the // new block, copies testing to new block, // and sets ct member of beany show(beany); // prints member string once show(beany, 2); // prints member string twice testing[0]='D'; testing[1]='u'; show(testing); // prints testing string once show(testing,3);// prints testing string thrice show("Done! "); return 0; } void set(stringy &stry, const char *str) { stry.ct=strlen(str); char *ch=new char[stry.ct+1]; strcpy(ch,str); stry.str=ch; } void show(const char *str, int m) { for (int i=0;i<m;i++) cout << str << endl; } void show(const stringy &stry, int m) { for (int i=0;i<m;i++) { cout << stry.str << endl; cout << stry.ct << endl << endl; } } 练习5 #include <iostream> using namespace std; template <typename T> T max5(const T* __Tarray); int main() { int iarr[]={1,3,6,-4,5}; double darr[]={3.14159265,-1.236,1.414,3.2768,2.236}; int i; for (i=0;i<5;i++) cout << iarr[i] << "/t"; cout << endl << "Max : " << max5(iarr) << endl; for (i=0;i<5;i++) cout << darr[i] << "/t"; cout << endl << "Max : " << max5(darr) << endl; return 0; } template <typename T> T max5(const T* __Tarray) { T pt=__Tarray[0]; for (int i=0;i<5;i++) { if (__Tarray[i]>pt) pt=__Tarray[i]; } return pt; } 练习6 #include <iostream> using namespace std; template <typename T> T maxn(const T* __Tarray, const int __Tnum); char* maxn(char** __Tarray, const int __Tnum); int main() { int iarr[]={1,-2,4,-10,6,9}; double darr[]={1.232,-0.432,5.4321,3.0}; char *carr[20]={"Mlogy laiwgn","mingwhile maybe","cool moto","shield full","you'll meel"}; cout << "Max of iarr: " << maxn(iarr,6) << endl; cout << "Max of darr: " << maxn(darr,4) << endl; cout << "Max of carr: " << maxn(carr,5) << endl; return 0; } template <typename T> T maxn(const T* __Tarray, const int __Tnum) { T pt=__Tarray[0]; for (int i=0;i<__Tnum;i++) { if (__Tarray[i]>pt) pt=__Tarray[i]; } return pt; } char* maxn(char** __Tarray, const int __Tnum) { char* __M_Tchar=__Tarray[0]; unsigned int __M_Tnum=strlen(__M_Tchar); for (int i=0;i<__Tnum;i++) { if (strlen(__Tarray[i])>__M_Tnum) { __M_Tchar=__Tarray[i]; __M_Tnum=strlen(__M_Tchar); } } return __M_Tchar; } 练习7 #include <iostream> template <typename T> //template A void ShowArray(T arr[], int n); template <typename T> //template B void ShowArray(T * arr[], int n); struct debts { char name[50]; double amount; }; int main(void) { 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 the arr mr_E for (int i=0;i<3;i++) pd[i]=&mr_E[i].amount; cout << "Listing Mr.E's total counts of things: /n"; // things is an array of int ShowArray(things,6); //uses template A cout << "Listing Mr.E's total debts: /n"; //pd is an array of pointers to double ShowArray(pd,3); //uses template B (more specialized) return 0; } template <typename T> void ShowArray(T arr[], int n) { using namespace std; cout << "template A/n"; T ar; for(int i=0;i<n;i++) ar+=arr[i]; cout << ar << endl; } template <typename T> void ShowArray(T * arr[], int n) { using namespace std; cout << "template B/n"; T ar; for(int i=0;i<n;i++) ar+=*arr[i]; cout << ar << endl; }