【11.6】c++ primer plus 课后编程答案

来源:互联网 发布:手机网络劫持 编辑:程序博客网 时间:2024/06/06 21:42

C++ PRIMER PLUS 课后答案 
使用IDE为window7系统下的VS2010

/*user.h*/#ifndef USERSH_H_#define USERSH_H_#include <string>#include <cmath>using std::ostream;using std::istream;class Ston{private:         static const int lbs_per_sto=14;         int stone;         double pounds;         double pds_left;public:         Ston();         Ston(doublelbs);         Ston(intstn,double lbs);         ~Ston(){};          Ston& operator = (const Ston & p);          friend ostream & operator << (ostream & os, const Ston & p);         friend istream & operator >> (istream & is, Ston & p);          friend bool operator < (const Ston & p1,const Ston & p2);         friend bool operator <= (const Ston & p1,const Ston & p2);         friend bool operator >= (const Ston & p1,const Ston & p2);         friend bool operator > (const Ston & p1,const Ston & p2);         friend bool operator == (const Ston & p1,const Ston & p2);         friend bool operator != (const Ston & p1,const Ston & p2);}; #endif


 

/*userfucntion.cpp*/#include "usersh.h"#include <iostream>using std::cout;using std::cin;using std::ostream;using std::istream; Ston::Ston(int stn,double lbs){         stone=stn;         pds_left=lbs;         pounds=stn*lbs_per_sto+lbs;} Ston::Ston(){         stone=pounds=pds_left=0.0;} Ston::Ston(double lbs){         stone=int(lbs)/lbs_per_sto;         pds_left=int(lbs)%lbs_per_sto+lbs-int(lbs);         pounds=lbs;}   bool operator < (const Ston & p1,const Ston & p2) {          if (p1.pounds<p2.pounds)                  return true;          else                  return false; }  bool operator <= (const Ston & p1,const Ston & p2) {          if(p1.pounds <= p2.pounds)                   return true;          else                   return false; }   bool operator >= (const Ston & p1,const Ston & p2) {          if(p1.pounds >= p2.pounds)                   return true;          else                   return false; }   bool operator > (const Ston & p1,const Ston & p2) {          return (p2<p1); }  bool operator == (const Ston & p1,const Ston & p2) {          if(p1.pounds==p2.pounds)                   return true;          else                   return false; } bool operator != (const Ston & p1,const Ston & p2) {          if(p1.pounds!=p2.pounds)                   return true;          else                   return false; }  Ston & Ston::operator = (const Ston & p)             ////这里会出问题! {         pounds=p.pounds;         pds_left=p.pds_left;         stone=p.stone;         return *this; }   ostream & operator << (ostream &os,const Ston & p) {          os <<"pounds:"<<p.pounds<<'\n';          return os; } istream & operator >> (istream &is, Ston & p) {          cout<<"please input pounds:";          is >> p.pounds;          return is; }


 

/*main*/#include <iostream>#include <Windows.h>#include "usersh.h"#include <string>using namespace std; int main(){           const int max=6;         int more_11_num=0;          Ston Ponds_11(11.0);         Ston Ponds[max]={Ston(5.1),Ston(6.1),Ston(12.3)};          Ston Max_pound =Ponds[0];         Ston Min_pound =Ponds[0];          for(int i=3;i<max;i++)                  cin>>Ponds[i];          for(int i=0;i<max;i++)         {                  if(Max_pound < Ponds[i])                          Max_pound= Ponds[i];                   if(Min_pound > Ponds[i])                          Min_pound= Ponds[i];                   if(Ponds[i] >= Ponds_11)                          more_11_num++;           }         cout<<"thisis pounds list:"<<endl;         for(int i=0;i<max;i++)         {                  cout<<"NO."<<i+1<<':'<<Ponds[i]<<endl;         }           cout<<"the min pounds :"<<Min_pound<<endl;         cout<<"the max pounds :"<<Max_pound<<endl;         cout<<"the bigger than 11.0 pounds num is :"<<more_11_num<<endl;          system("pause");         return 0;} 


原创粉丝点击