第3题
// 1.头文件#ifndef GOLF_H_#define GOLF_H_classGolf{private:staticconstint Len =40;char fullname[Len];int handicap;public:Golf();Golf(constchar*name,int handi);intsetgolf();voidre_handicap(int hc);voidshowgolf()const;~Golf();};#endif// 2.函数定义#include"golf.h"#include<cstring>#include<iostream>
Golf::Golf(){
fullname[0]='\0';
handicap =0;}//功能:通过成员函数参数,初始化Golf类对象//参数:指向字符的指针、int//返回:无
Golf::Golf(constchar*name,int handi){
std::strcpy(fullname, name);
handicap = handi;}//功能:通过用户输入,来设置调用该函数的Golf类对象//参数: 无// 返回:成功则返回1,失败则返回0int Golf::setgolf(){using std::cin;using std::cout;char temp_n[Len];int temp_h;
cout <<"Enter the fullname\n";
cin.get(temp_n, Len);char ch = cin.get();// 如果输入字符串数目超限,那么会读取到非'\n'; 如果输入字符串数目没有超限, 那么会读取'\n'if(cin.good()&& temp_n[0]!='\0'&& ch =='\n')//表示名字读取正常,并且字符串数目未超过限制数目{
cout <<"Enter the handicap:\n";
cin >> temp_h;
cin.get();*this=Golf(temp_n, temp_h);return1;//返回1,表示成功设置一个Golf类对象}//注意:读取失败并且返回0的原因有很多,如上述if语句的测试条件if(cin.eof()){
cout <<"cin-fullname failed due to EOF.\n";}elseif(cin.fail()){
cout <<"cin-fullname failed due to TypeMismatched.\n";}elseif(fullname[0]=='\0'){
cout <<"QUIT.\n";}elseif(ch !='\n'){
cout <<"cin-fullname failed due to Input String Too Long.\n";while(cin.get()!='\n'){continue;}}return0;}// 功能:重新设置handicap的值// 参数:int// 返回:无void Golf::re_handicap(int hc){
handicap = hc;}// 功能:显示类对象Golf的成员// 参数:常量结构引用// 返回:无void Golf::showgolf()const{using std::cout;using std::endl;
cout <<"Name: "<< fullname
<<" Handicap: "<< handicap << endl;}
Golf::~Golf(){}// 3.主函数#include"golf.h"#include<iostream>int exi =1;intmain(){using std::cin;using std::cout;using std::endl;
cout <<"How many information do you want to input?\n";int many;
cin >> many;
many++;if(many >0){
Golf array[many];
cin.get();
array[0]=Golf("DongZhaoCheng",120);//调用构造函数,初始化对象数组的一个对象
cout <<"Now, the array has one member!\n";
cout << endl;while(exi < many && array[exi].setgolf())//exi = 1是具有外部链接性的全局变量{++exi;}
cout << endl;
cout <<"You can display the info by choicing #: (1 ~ "<< exi <<"):"<< endl;int j;
cin >> j;while(j <1|| j > exi){
cout <<"Sorry,check and input again:\n";
cin >> j;}
array[j -1].showgolf();
cout << endl;
cout <<"You can modify the handicap in the array of golf, "<< endl
<<"Please input the index: (1 ~ "<< exi <<"):\n";
cin >> j;while(j <1|| j > exi){
cout <<"Sorry,check and input again:\n";
cin >> j;}
cout <<"Ok!,Please enter the modified value:\n";int temp;
cin >> temp;
array[j -1].re_handicap(temp);
cout <<"Modify Success:\n";
array[j -1].showgolf();}else{
cout <<"Your enter is wrong!\n";}
cout <<"BYEBYE!!!\n";return0;}
第四题
// 1.头文件namespace SALES
{constdouble c_d4[7]={34.44,56.66,33.33,56.45,98.99,67.33,56.44};classSales{private:staticconstint QUARTERS =4;double sales[QUARTERS];double average;double max;double min;public:// 默认构造函数Sales();// 功能:使用参数列表中的参数,设创建并初始化类对象的值,找出数组中较小的4个元素赋给类数据成员sales// 然后计算出min、max、average// double数据数组、数组的项数int n// 返回:无Sales(constdouble ar[],int n);~Sales();// 功能:设置调用对象的值voidsetSales();// 功能:显示调用对象的成员voidshowSales()const;};}// namespace SALES// 2.函数定义#include"class.h"#include<cstdlib>#include<iostream>namespace SALES
{
Sales::Sales(constdouble ar[],int n){intmycomp(constvoid*p1,constvoid*p2);double copyar[n];for(int i =0; i < n; i++){
copyar[i]= ar[i];}qsort(copyar, n,sizeof(double), mycomp);for(int i =0; i <4; i++){
sales[i]= copyar[i];}
max = sales[3];
min = sales[0];double total =0;for(int i =0; i <4; i++){
total += sales[i];}
average = total /4;}// 通过与用户交互,设置调用对象的值void Sales::setSales(){intmycomp(constvoid*p1,constvoid*p2);
std::cout << std::endl;
std::cout <<"How many number will you enter?\n";int n;
std::cin >> n;double arrar_input[n];
std::cout <<"Starting enter(1th number):\n";int i =0;while(i < n && std::cin >> arrar_input[i])//这里写的有点简陋了,哎呀急着买菜做饭去呢{
i++;if(i < n)
std::cout <<"Enter next number:\n";}*this=Sales(arrar_input, n);}void Sales::showSales()const{
std::cout.precision(4);
std::cout <<"sales array:\t";for(int i =0; i <4; i++){
std::cout << sales[i]<<"\t";}
std::cout << std::endl;
std::cout <<"Average: "<< average << std::endl;
std::cout <<"Max: "<< max << std::endl;
std::cout <<"Min: "<< min << std::endl;}intmycomp(constvoid*p1,constvoid*p2){int ret =0;constdouble*a1 =(constdouble*)p1;constdouble*a2 =(constdouble*)p2;if(*a1 >*a2){
ret =1;}elseif(*a1 <*a2){
ret =-1;}else{
ret = ret;}return ret;}
Sales::Sales(){for(int i =0; i < QUARTERS; i++)
sales[i]=0;
average =0;
max =0;
min =0;}
Sales::~Sales(){}}// namespace SALES// 3.主函数#include"class.h"#include<cstdlib>#include<iostream>intmain(){usingnamespace SALES;
Sales s1;//使用默认构造函数
s1 =Sales(c_d4,7);//通过自定义的构造函数创建临时对象,并赋值给s1
Sales s2;
s1.showSales();
std::cout <<"**********************************\n";
s2.setSales();//通过和用户交互来实现设置对象
s2.showSales();}
第五题
// 1.头文件#ifndef STACK_H_#define STACK_H_struct customer
{char fullname[35];double payment;};typedef customer Item;classStack{private:enum{
MAX =10};
Item items[MAX];int top;//top的值代表栈顶的序号public:Stack();boolisempty()const;boolisfull()const;boolpush(const Item &item);boolpop(Item &item);};#endif// 2.函数定义// 类成员函数的定义#include"stack.h"// 构造函数
Stack::Stack(){
top =0;}bool Stack::isempty()const{return top ==0;}bool Stack::isfull()const{return top == MAX;//说明已经满了}bool Stack::push(const Item &item){if(top < MAX){
items[top++]= item;returntrue;}elsereturnfalse;}bool Stack::pop(Item &item)//将弹出元素给参数{if(top >0){
item = items[--top];returntrue;}elsereturnfalse;}// 3.主函数#include"stack.h"#include<cctype>#include<iostream>intmain(){usingnamespace std;
Stack st;//定义了一个类对象char ch;
customer po;double tot_payment =0;
cout <<"Please enter A to add a customer-info order,\n"<<"P to pop a customer-info,Q to quit.\n";while(cin >> ch &&toupper(ch)!='Q'){while(cin.get()!='\n')//清除每行后续不需要的字符{continue;}if(!isalpha(ch)){
cout <<"Check your input!!!\n";continue;}switch(ch){case'a':case'A':if(st.isfull()){
cout <<"Stack is full\n";}else{
cout <<"Enter a customer's name(< 35 chars):";
cin.getline(po.fullname,35);
cout <<"Enter the customer's payment:";
cin >> po.payment;
st.push(po);}break;case'p':case'P':if(st.isempty()){
cout <<"Sorry.Stack is empty\n";}else{
st.pop(po);//将栈顶元素弹出,放在变量po中
tot_payment += po.payment;
cout <<"The tot_payment is "<< tot_payment << endl;}break;}
cout <<"Please enter A to add a customer-info order,\n"<<"P to pop a customer-info,Q to quit.\n";}
cout <<"Bye\n";return0;}
第六题
// 1.头文件#ifndef MOVE_H_#define MOVE_H_classMove{private:double x;double y;public:Move(double a =0,double b =0);voidshowmove()const;// 函数返回值是Move类对象// 此函数将m的x与调用对象的x相加,以获得新的x;y同理// 创建一个新的对象,初始化为x和y的值,并返回它
Move add(const Move &m)const;voidreset(double a =0,double b =0);// 将x和y重置为a和b的值};#endif// 2.函数定义#include"move.h"#include<iostream>
Move::Move(double a,double b){
x = a;
y = b;}void Move::showmove()const{
std::cout <<"X = "<< x << std::endl
<<"Y = "<< y << std::endl;}
Move Move::add(const Move &m)const{
Move n_object;
n_object.x = m.x + x;
n_object.y = m.y + y;return n_object;}void Move::reset(double a,double b){
x = a;
y = b;}// 3.主函数#include"move.h"#include<iostream>intmain(){
Move obj;
obj.showmove();
obj =Move(4.8,9.1);
obj.showmove();
Move obj2 = obj.add(obj);
obj2.showmove();
obj.reset();
obj.showmove();
obj2.reset();
obj2.showmove();return0;}
第七题
// 1.头文件#ifndef PLORGA_H_#define PLORGA_H_classPlorg{private:enum{
SIZE =20};char name[SIZE];int CI;public:// 此举是为了消除警告:ISO C++ forbids converting a string constant to 'char*'Plorg(char*n =(char*)"Plorga",int ci =50);voidModCi(constint ci);voidShowPlorg()const;};#endif// 2.函数定义#include"plorga.h"#include<cstring>#include<iostream>
Plorg::Plorg(char*n,int ci){strcpy(name, n);
CI = ci;}void Plorg::ModCi(constint ci){
CI = ci;}void Plorg::ShowPlorg()const{
std::cout <<"Name: "<< name << std::endl;
std::cout <<"CI: "<< CI << std::endl;}// 3.主函数#include"plorga.h"#include<iostream>intmain(){
Plorg obj1;
obj1.ShowPlorg();// 此举是为了消除警告:ISO C++ forbids converting a string constant to 'char*'char*s =(char*)"Snail Dong";
obj1 =Plorg(s,10000);
obj1.ShowPlorg();
obj1.ModCi(9999);
obj1.ShowPlorg();return0;}
第八题
// 1.头文件#ifndef LIST_H_#define LIST_H_typedefdouble Item;// 暂定以double类型的数据作为ItemclassList{private:enum{
MAX =10};
Item items[MAX];int top;//用来描述列表中有几个成员public:List();voidAddItem(const Item &);boolIsEmpty()const;boolIsFull()const;voidVisit(void(*pf)(Item &));voidShowList()const;};#endif// 2.函数定义#include"listt.h"#include<iostream>
List::List(){for(int i =0; i < MAX; i++){
items[i]=0;}
top =0;}void List::AddItem(const Item &item){if(IsFull()){
std::cout <<"The list is full\n";}else{
items[top++]= item;}}bool List::IsEmpty()const{return top ==0;}bool List::IsFull()const{return top == MAX;}void List::Visit(void(*pf)(Item &item)){for(int i =0; i < top; i++){pf(items[i]);}}void List::ShowList()const{if(IsEmpty()){
std::cout <<"The list is empty!\n";}else{
std::cout <<"#:\t";for(int i =0; i < top; i++){
std::cout << i +1<<"\t";}
std::cout <<"\n";
std::cout.precision(3);
std::cout <<"Con:\t";for(int i =0; i < top; i++){
std::cout << items[i]<<"\t";}
std::cout <<"\n";}}// 3.主函数#include<iostream>#include"listt.h"#include<cmath>voidsqrt_(Item &item);voidAdd_1(Item &item);intmain(){
List obj1;// 测试函数List().
obj1.ShowList();// 测试函数Isempty().
obj1.AddItem(12.3);
obj1.ShowList();for(int i =0; i <9; i++){
obj1.AddItem(i+2.5);}
obj1.AddItem(5);//体现函数IsFull().
obj1.ShowList();
obj1.Visit(sqrt_);
std::cout<<"After Calling function of sqrt_.\n";
obj1.ShowList();
obj1.Visit(Add_1);
std::cout<<"After Calling function of Add_1.\n";
obj1.ShowList();return0;}// 求得每一项的平方根voidsqrt_(Item &item){
item =sqrt(item);}voidAdd_1(Item &item){++item;}