C++PRIMER PLUS第六版课后编程答案 4.6-4.10
2014-03-27 16:02
447 查看
4.6
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string name;
double weight;
int kaluli;
};
void show(CandyBar &c);//按值传递和引用传递均可
void main46()
{
CandyBar cb[3]={{"one",1.8,10},{"two",2.9,30},{"three",5.0,60}};
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 <iostream>
#include <string>
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 <iostream>
#include <string>
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="<<p->name<<" ,d="<<p->d<<" ,weight="<<p->w<<endl
;
cin.get();
}
4.9
#include <iostream>
#include <string>
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="<<c->kaluli<<" ,weight="<<c->weight<<" ,kaluli="<<c->kaluli<<endl;
}
4.10
#include <iostream>
#include <array>
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();
}
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string name;
double weight;
int kaluli;
};
void show(CandyBar &c);//按值传递和引用传递均可
void main46()
{
CandyBar cb[3]={{"one",1.8,10},{"two",2.9,30},{"three",5.0,60}};
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 <iostream>
#include <string>
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 <iostream>
#include <string>
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="<<p->name<<" ,d="<<p->d<<" ,weight="<<p->w<<endl
;
cin.get();
}
4.9
#include <iostream>
#include <string>
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="<<c->kaluli<<" ,weight="<<c->weight<<" ,kaluli="<<c->kaluli<<endl;
}
4.10
#include <iostream>
#include <array>
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();
}
相关文章推荐
- C++PRIMER PLUS第六版课后编程答案 4.6-4.10
- C++primer plus第六版课后编程练习答案4.10
- C++primer plus第六版课后编程练习答案4.6
- C++primer plus第六版课后编程练习答案3.3
- C++primer plus第六版课后编程练习答案10.1
- C++primer plus第六版课后编程练习答案12.3
- C++primer plus第六版课后编程练习答案5.9
- C++primer plus第六版课后编程练习答案6.9
- C++primer plus第六版课后编程练习答案7.9
- C++primer plus第六版课后编程练习答案10.2
- C++primer plus第六版课后编程练习答案12.4
- C++primer plus第六版课后编程练习答案7.10
- C++primer plus第六版课后编程练习答案10.3
- C++ Primer Plus 第六版 所有章节课后编程练习答案
- C++PRIMER PLUS第六版课后编程答案 4.1-4.5
- C++primer plus第六版课后编程练习答案4.2
- C++primer plus第六版课后编程练习答案6.1
- C++primer plus第六版课后编程练习答案8.4
- C++primer plus第六版课后编程练习答案10.4
- C++primer plus第六版课后编程练习答案11.4