第8章函数探幽
编程题
| #include <iostream> |
| using namespace std; |
| |
| |
| void loop_print(const char *str , int n = 0); |
| |
| int main(int argc, char const *argv[]) |
| { |
| |
| loop_print("Hello World!"); |
| loop_print("Hello World!"); |
| loop_print("Hello World!",1); |
| return 0; |
| } |
| |
| void loop_print(const char * str, int n ){ |
| static int count = 0; |
| count ++ ; |
| if(!n){ |
| cout << str << endl; |
| } |
| |
| else{ |
| for(int i = 0;i < count; i++){ |
| cout << "Print No." << i+1 << "." << endl; |
| cout << str << endl; |
| } |
| } |
| } |
| #include <iostream> |
| using namespace std; |
| #include <string> |
| |
| struct CandyBar{ |
| string brand; |
| double weight; |
| int colories; |
| }; |
| |
| void createCandy(CandyBar &, string, double , int); |
| void show(const CandyBar &); |
| |
| int main(int argc, char const *argv[]) |
| { |
| CandyBar cb; |
| createCandy(cb, "Millennium Munch",2.85,3); |
| show(cb); |
| return 0; |
| } |
| |
| void createCandy(CandyBar & candy, string brand, double weight, int colories){ |
| candy.brand = brand; |
| candy.weight = weight; |
| candy.colories = colories; |
| } |
| |
| void show(const CandyBar & cb){ |
| |
| cout << "Candy info:" << endl; |
| cout << "Candy Brand:" << cb.brand << endl; |
| cout << "Candy Weight: " << cb.weight << endl; |
| cout << "Candy Colories: " << cb.colories << endl; |
| } |
| #include <iostream> |
| using namespace std; |
| #include <string> |
| #include <cctype> |
| |
| void stringToUpper(string &); |
| |
| int main(int argc, char const *argv[]) |
| { |
| |
| |
| |
| |
| |
| string st; |
| cout << "Enter a string (q to quit): " ; |
| getline(cin , st); |
| while(st != "q"){ |
| stringToUpper(st); |
| cout << st << endl; |
| cout << "Next string (q to quit) :" ; |
| getline(cin, st); |
| } |
| cout << "Bye." << endl; |
| return 0; |
| } |
| |
| |
| void stringToUpper(string & str){ |
| int size = str.size(); |
| |
| |
| |
| |
| for(int i = 0; i < size; i++){ |
| if(islower(str[i])){ |
| str[i] = toupper(str[i]); |
| } |
| } |
| } |
| #include <iostream> |
| using namespace std; |
| #include <cstring> |
| |
| struct stringy |
| { |
| char *str; |
| int ct; |
| }; |
| |
| |
| void show(const string & , int n =0); |
| void show(const stringy &, int n = 0); |
| void set(stringy &, char *); |
| int main(int argc, char const *argv[]) |
| { |
| |
| stringy beany; |
| char testing[] = "Reality isn't what it used to be." ; |
| set(beany , testing); |
| |
| show(beany); |
| show(beany,2); |
| testing[0] = 'P'; |
| testing[1] = 'A'; |
| |
| show(testing); |
| show(testing,3); |
| show("Done!"); |
| |
| |
| delete beany.str; |
| |
| |
| |
| return 0; |
| } |
| |
| |
| |
| void show(const string & st , int n ){ |
| if(n == 0 ) n++; |
| for(int i = 0; i < n ;i++){ |
| cout << st << endl; |
| } |
| } |
| void show(const stringy & sty, int n ){ |
| |
| if (n == 0){ |
| n ++ ; |
| } |
| |
| for(int i = 0; i < n;i++){ |
| cout << sty.str << endl; |
| } |
| } |
| |
| void set(stringy & sty, char *st){ |
| |
| sty.ct = strlen(st); |
| sty.str = new char[sty.ct]; |
| |
| strcpy(sty.str,st); |
| } |
| #include <iostream> |
| using namespace std; |
| |
| template <class T> |
| T max5(T [] , int n=5); |
| |
| int main(int argc, char const *argv[]) |
| { |
| int x[] = {1,2,3,4,5}; |
| int res = max5(x); |
| cout << "max value of x array : " << res << endl; |
| |
| double y[] = {4.8,3.4,9.8,10,1.2}; |
| cout << "max value of y array:" << max5(y) << endl; |
| return 0; |
| } |
| |
| template <class T> |
| T max5(T x[] , int n ){ |
| int max = x[0]; |
| |
| for(int i = 0; i < n ; i++){ |
| max = max < x[i] ? x[i]: max; |
| } |
| |
| return max; |
| } |
| #include <iostream> |
| using namespace std; |
| #include <cstring> |
| |
| template <typename T> |
| T maxn(T[],int n); |
| |
| template <> char* maxn(char * str[], int n ); |
| int main(int argc, char const *argv[]) |
| { |
| |
| int arr[5] = {1,2,3,4,5}; |
| char *str [5] = {"wqewfw","dfg","fweshif","revdser","Hellofwe"}; |
| |
| cout << "Max value int arr " << maxn(arr, 5) << endl; |
| cout << "Max length string int str " << maxn(str, 5) << endl; |
| |
| return 0; |
| } |
| |
| template <typename T> |
| T maxn(T a[],int n){ |
| T max = a[0]; |
| for(int i = 0; i< n; i++){ |
| max = max > a[i] ? max : a[i]; |
| } |
| return max; |
| } |
| |
| template <> char* maxn<char*>(char * str[], int n ){ |
| cout << "Hello " << endl; |
| int pos = 0; |
| for(int i =0 ;i < n; i++){ |
| if(strlen(str[pos]) < strlen(str[i])){ |
| pos = i; |
| } |
| } |
| return str[pos]; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?