#include<iostream>using namespace std;void printInfo(const char* info , int funtionType = 0);int main(){ char name[10] = {"Good day!"}; for(int i = 0 ; i < 3 ; ++i) printInfo(name,i); return 0;}void printInfo(const char* info , int funtionType){ if(funtionType == 0) cout<<info<<endl; else { cout<<"非默认参数"<<endl; cout<<info<<endl; }}//这题如果完全按数上的题目要求,需要用到静态变量,但这是后面第9章的内容。//所以我就大致按照题目的意思改动了下
第二题#include<iostream>#include<cstring>using namespace std;struct candyBar{ char* names; double weight; int kal;};void setInfo(candyBar& cake, const char* nm= "Millennium" , double wei = 2.85 , int k = 350);void showInfo(const candyBar& cake);int main(){ candyBar myCake; setInfo(myCake); showInfo(myCake); delete [] myCake.names; return 0;}void setInfo(candyBar& cake , const char* nm , double wei , int k){ int Len = strlen(nm); cake.names = new char[Len+1]; strcpy(cake.names,nm); cake.weight = wei; cake.kal = k;}void showInfo(const candyBar& cake){ cout<<"名称"<<cake.names<<endl; cout<<"重量"<<cake.weight<<endl; cout<<"热量"<<cake.kal<<endl;}
第三题#include<iostream>#include<string>#include<cctype>using namespace std;void turnString(string& str);int main(){ string st; string QUIT = "q"; while(1) { cout<<"请输入测试单词,输入q结束程序"; getline(cin,st); if(!cin or st == QUIT) break; else turnString(st); cout<<st<<endl; } return 0;}void turnString(string& str){ int Len = str.size(); for(int i = 0 ; i < Len ; ++i) str[i] = toupper(str[i]);}