第一题
/*第八章:编程练习 1 */
#include <iostream>
using namespace std;
void loop_print(const char* str, int n = 0);
int main()
{
loop_print("Hello World!",2);
loop_print("Hello World!",1);
loop_print("Hello World!", 5);
return 0;
}
void loop_print(const char* str, int n)
{
static int func_count = 0;
func_count++;
cout<<func_count<<endl;
if(n == 0)
{
//cout<<"Arguments = 0 ;\n";
cout<<str<<endl;
}else{
for(int i = 0;i < func_count; i++)
{
cout<<str<<endl;
}
}
}
犯错记录: 没有看清楚问题,没搞明白func_count的用处,误以为跟n是一个用法,后改正。
第二题
/*第八章:编程练习 2*/
#include <iostream>
using namespace std;
using namespace std;
struct CandyBar{
string name;
float weight;
int hot;
};
void create_candy(CandyBar& candy,
string name = "Millennium Munch",float weight = 2.85, int hot = 350);
void show_candy(const CandyBar& candy);
int main(){
CandyBar cb ;
create_candy(cb);
show_candy(cb);
create_candy(cb,"Nestle",1.2,200);
show_candy(cb);
return 0;
}
void create_candy(CandyBar& candy, string name ,float weight, int hot){
candy.name = name;
candy.weight = weight;
candy.hot = hot;
}
void show_candy(const CandyBar& candy){
cout<<"name: " << candy.name<<endl;
cout<<"weight: "<< candy.weight<<endl;
cout<<"hot: "<< candy.hot<<endl;
}
学到了 热量的英语是caloric,还有复数?-- calories
emmmmmmm
第三题
/*第八章:编程练习 3*/
#include <iostream>
using namespace std;
void change(string& s);
int main()
{
string st;
cout<<"(Enter ! to quit): ";
getline(cin, st);
while(st != "!")
{
change(st);
cout<<st<<endl;
getline(cin, st);
}
cout<<"Bye."<<endl;
return 0;
}
void change(string& s)
{
for( int i = 0; i < s.size(); i++){
s[i] = toupper(s[i]);
}
}
emmm 想了半天才发现有toupper!!!