4.6
#include
#include
using namespace std;
struct CandyBar
{
string name;
double weight;
int kaluli;
};
void show(CandyBar &c);//按值传递和引用传递均可
void main46()
{
for(int i=0;i<3;i++)
show(cb[i]);
}
void show(CandyBar &c)
{
cout<<”name=”<<c.kaluli<<” ,weight=”<<c.weight<<” ,kaluli=”<<c.kaluli<<endl;
}
4.7
#include
#include
using namespace std;
struct Pizza
{
string name;
double d;
double w;
};
Pizza get(Pizza &p);
void show(const Pizza &p);
void main47()
{
Pizza p;
for(int i=0;i<3;i++)
{
get(p);
show(p);
}
}
Pizza get(Pizza &p)//若不使用引用变量,返回时返回的是一个未知的变量
{
cout<<”Enter the Pizza name:”;
string name;
getline(cin,name);
p.name=name;
cout<<”\n Enter the d:”;
double d;
cin>>d;
p.d=d;
cout<<endl<<”Enter the weight:”;
double w;
cin>>w;
p.w=w;
return p;
}
void show(const Pizza &p)
{
cout<<”name=”<<p.name<<” ,d=”<<p.d<<” ,weight=”<<p.w<<endl
;
cin.get();
}
4.8
#include
#include
using namespace std;
struct Pizza
{
string name;
double d;
double w;
};
Pizza *get(Pizza *p);
void show(const Pizza *p);
void main48()
{
Pizza *p;
p=new Pizza();
for(int i=0;i<3;i++)
{
get(p);
show(p);
//delete p;
}
delete p;
//delete p;
}
Pizza *get(Pizza *p)//若不使用引用变量,返回时返回的是一个未知的变量
{
//p=new Pizza();
cout<<”Enter the Pizza name:”;
string name;
getline(cin,name);
p->name=name;
cout<<”\n Enter the d:”;
double d;
cin>>d;
p->d=d;
cout<<endl<<”Enter the weight:”;
double w;
cin>>w;
p->w=w;
return p;
}
void show(const Pizza *p)
{
cout<<”name=”<
;
cin.get();
}
4.9
#include
#include
using namespace std;
struct CandyBar
{
string name;
double weight;
int kaluli;
};
void show(const CandyBar *c);
void main49()
{
CandyBar *c=new CandyBar[3];//结构体数组定义方法
for(int i=0;i<3;i++,c++)
{
c->name=”String”+i;
c->weight=i;
c->kaluli=i*10;
show(c);
}
delete []c;
}
void show(const CandyBar *c)
{
cout<<”name=”<
}
4.10
#include
#include
using namespace std;
void main410()
{
array <int,3> arr;
int sum=0;
for(int i=0;i<3;i++)
{
cout<<”Enter the “<<i+1<<” time :”;
cin>>arr[i];
sum+=arr[i];
}
double pinjun;
cout<<”平均成绩为:”<<sum*1.0/3<<endl;
cin.get();
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/cprimerplus第六版课后编程答案46410/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!