一、编程练习
- 12_1
#include<iostream>
#include<cstring>
class Cow
{
char name[20];
char * hobby;
double weight;
public:
Cow();
Cow(const char * nm,const char * ho,double wt);
Cow(const Cow &c);
~Cow();
Cow &operator=(const Cow &c);
void showCow() const;
};
Cow::Cow()
{
name[0]='\0';
hobby=new char;
hobby[0]=0;
weight=0.0;
}
Cow::Cow(const char * nm,const char * ho,double wt)
{
strcpy(name,nm);
hobby=new char[sizeof(ho)];
strcpy(hobby,ho);
weight=wt;
}
Cow::Cow(const Cow &c)
{
strcpy(name,c.name);
hobby=new char[sizeof(c.hobby)];
strcpy(hobby,c.hobby);
weight=c.weight;
}
Cow::~Cow()
{
std::cout<<"destroy\n";
delete [] hobby;
}
Cow &Cow::operator=(const Cow &c)
{
if(this==&c)
return *this;
delete [] hobby;
hobby=new char[sizeof(c.hobby)];
strcpy(name,c.name);
strcpy(hobby,c.hobby);
weight=c.weight;
return *this;
}
void Cow::showCow() const
{
std::cout<<"Name: "<<name<<std::endl
<<"Hobby: "<<hobby<<std::endl
<<"Weight: "<<weight<<std::endl;
}
int main()
{
Cow c1;
c1.showCow();
Cow c2=Cow("steve","guitar",72.5);
c2.showCow();
}
- 12_2
#ifndef STRING1_H_INCLUDED
#define STRING1_H_INCLUDED
#include<iostream>
using std::ostream;
using std::istream;
class String
{
private:
char * str;
int len;
static int num_strings;
static const int CINLIM=80;
public:
String(const char *s);
String();
String(const String &s);
~String();
int length()const {return len;};
String &operator=(const String &s);
String &operator=(const char *s);
const char & operator[](int i) const;
char & operator[](int i);
void Stringlow();
void Stringupper();
int charcount(char c);
friend bool operator<(const String &s1,const String &s2);
friend bool operator>(const String &s1,const String &s2);
friend bool operator==(const String &s1,const String &s2);
friend String operator+(const char *s1,const String &s2);
friend ostream & operator<<(ostream &os,const String &s);
friend istream & operator>>(istream &is,String &s);
String operator+(const String &s);
String operator+(const char *s);
static int HowMany();
};
#endif
#include<iostream>
#include"String1.h"
#include<cctype>
#include<cstring>
using std::cin;
using std::cout;
int String::num_strings=0;
int String::HowMany()
{
return num_strings;
}
String::String(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
}
String::String()
{
len=4;
str=new char;
str[0]=0;
num_strings=0;
}
String::String(const String &s)
{
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
num_strings++;
}
String::~String()
{
--num_strings;
delete [] str;
}
String &String::operator=(const String &s)
{
if(this==&s)
return *this;
len=s.len;
delete [] str;
str=new char[len+1];
strcpy(str,s.str);
return *this;
}
String &String::operator=(const char *s)
{
len=strlen(s);
delete [] str;
str=new char[len+1];
strcpy(str,s);
return *this;
}
const char & String::operator[](int i) const
{
return str[i];
}
char & String::operator[](int i)
{
return str[i];
}
void String::Stringlow()
{
for(int i=0;i<len;i++)
{
str[i]=tolower(str[i]);
}
}
void String::Stringupper()
{
for(int i=0;i<len;i++)
{
str[i]=toupper(str[i]);
}
}
int String::charcount(char c)
{
int n=0;
for(int i=0;i<len;i++)
{
if(str[i]==c)
n++;
}
return n;
}
bool operator<(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)<0);
}
bool operator>(const String &s1,const String &s2)
{
return s2<s1;
}
bool operator==(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)==0);
}
String operator+(const char *s1,const String &s2)
{
int total_len=strlen(s1)+s2.len;
char *temp=new char[total_len+1];
strcpy(temp,s1);
strcpy(temp+strlen(s1),s2.str);
temp[total_len]='\0';
return String(temp);
}
String String::operator+(const String &s)
{
int total_len=len+s.len;
char *temp=new char[total_len+1];
strcpy(temp,str);
strcpy(temp+len,s.str);
temp[total_len]='\0';
return String(temp);
}
String String::operator+(const char *s)
{
int total_len=len+strlen(s);
char *temp=new char[total_len+1];
strcpy(temp,str);
strcpy(temp+len,s);
temp[total_len]='\0';
return String(temp);
}
ostream & operator<<(ostream &os,const String &s)
{
os<<s.str;
return os;
}
istream & operator>>(istream &is,String &s)
{
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
s=temp;
while(is && is.get()!='\n')
continue;
return is;
}
int main()
{
String s1(" and I am a C++ student.");
String s2="Please enter your name: ";
String s3;
cout<<s2;
cin>>s3;
s2="My name is "+s3;
cout<<s2<<".\n";
s2.Stringupper();
cout<<"The string\n"<<s2<<"\ncontains "<<s2.charcount('A')
<<" 'A' characters in it.\n";
s1="red";
String rgb[3]={String(s1),String("green"),String("blue")};
cout<<"Enter th name of a primary color for mixing light: ";
String ans;
bool success=false;
while(cin>>ans)
{
ans.Stringlow();
for(int i=0;i<3;i++)
{
if(ans==rgb[i])
{
cout<<"That's right!\n";
success=true;
break;
}
}
if(success)
break;
else
cout<<"Try again!\n";
}
cout<<"Bye\n";
return 0;
}
- 12_3
#ifndef Stock2__H_INCLUDED
#define Stock2__H_INCLUDED
#include<iostream>
using std::ostream;
class Stock
{
private:
char *company;
int shares;
double share_val;
double total_val;
void set_tot(){total_val=shares*share_val;};
public:
Stock();
Stock(const char *s,long n=0,double pr=0.0);
~Stock();
void buy(long num,double price);
void sell(long num,double price);
void update(double price);
friend ostream & operator<<(ostream &os,const Stock & s);
const Stock & topval(const Stock &s) const;
};
#endif
#include<iostream>
#include"Stock2.h"
#include<cstring>
Stock::Stock()
{
company=new char[8];
strcpy(company,"no name");
shares=0;
share_val=0.0;
total_val=0.0;
}
Stock::Stock(const char *s,long n,double pr)
{
company=new char[strlen(s)];
strcpy(company,s);
if(n<0)
{
std::cout<<"Number of shares can't be negative;"
<<company<<" shares wet to 0.\n";
shares=0;
}
else
shares=n;
share_val=pr;
set_tot();
}
Stock::~Stock()
{
delete [] company;
}
void Stock::buy(long num,double price)
{
if(num<0)
{
std::cout<<"Number of shares purchased can't be negative. "
<<"Traction is aborted.\n";
}
else
{
shares+=num;
share_val=price;
set_tot();
}
}
void Stock::sell(long num,double price)
{
if(num<0)
{
std::cout<<"Number of shares sold can't be negative. "
<<"Traction is aborted.\n";
}
else if(num>shares)
{
std::cout<<"You can't sell more than you have! "
<<"Traction is aborted.\n";
}
else
{
shares-=num;
share_val=price;
set_tot();
}
}
void Stock::update(double price)
{
share_val=price;
set_tot();
}
ostream & operator<<(ostream &os,const Stock & s)
{
using std::ios_base;
ios_base::fmtflags orig=std::cout.setf(ios_base::fixed,ios_base::floatfield);
std::streamsize prec=std::cout.precision(3);
os<<"Company:"<<s.company<<std::endl
<<"Shares:"<<s.shares<<std::endl
<<"Share_val:$ "<<s.share_val<<std::endl;
std::cout.precision(2);
os<<"Total_val:$ "<<s.total_val<<std::endl;
std::cout.setf(orig,ios_base::floatfield);
std::cout.precision(prec);
return os;
}
const Stock & Stock::topval(const Stock &s) const
{
if(s.total_val>total_val)
return s;
else
return *this;
}
const int STKS=4;
int main()
{
Stock stocks[STKS]=
{
Stock("NanoSmart",12,20.0),
Stock("Boffo Objects",200,2.0),
Stock("Monolithic Obelisks",130,3.25),
Stock("Fleep Enterprises",60,6.5),
};
std::cout<<"Stock holdings:\n";
for(int i=0;i<STKS;i++)
std::cout<<stocks[i];
const Stock *top=&stocks[0];
for(int i=1;i<STKS;i++)
top=&top->topval(stocks[i]);
std::cout<<"Most valuable holding:\n";
std::cout<<*top;
return 0;
}