money & money:perator-=(double a)
{
while(jf<a-int(a))
{
jf+=100;
yuan--;
}
jf=jf-(a-int(a));
yuan=yuan-int(a);
return * this;
}
第19章
//**********************
//*********19.1*********
//******Daiyuebing******
//**********************
# include <iostream.h>
# include <fstream.h>
void main()
{
ifstream fin("d://example.dat",ios::nocreate);
if(fin.fail())
{
cerr<<"error open files!"<<endl;
return;
}
int i=0;
char letter;
letter=fin.get();
while (!fin.eof())
{
while(letter!='/n')
{
letter=fin.get();
}
letter=fin.get();//缺少改语句进入死循环
i++;
}
cout<<"line:"<<i<<endl;
}
//**********************
//*********19.2*********
//******Daiyuebing******
//**********************
# include <iostream.h>
# include <iomanip.h>
class RMB
{
public:
RMB(double v=0.0)
{
yuan=int(v);
jf=int((v-yuan)*100);
}
void display(ostream & out);
private:
unsigned int yuan;
unsigned int jf;
};
void RMB::display(ostream & out)
{
out<<"¥"<<setw(8)<<setfill('0')<<yuan
<<"."<<setw(2)<<setfill('0')<<jf<<setfill(' ')<<endl;
}
ostream & operator<<(ostream & oo,RMB & d)
{
d.display(oo);
return oo;
}
void main()
{
RMB rmb(45678.25);
cout<<rmb;
}
//**********************
//*********19.3*********
//******Daiyuebing******
//**********************
# include <strstrea.h>
char * c="123456789/n";
void main()
{
istrstream ind(c);
char letter;
letter=ind.get();
while(letter!='/n')
{
cout<<letter<<" ";
letter=ind.get();
}
cout<<endl;
}
//**********************
//*********19.4*********
//******Daiyuebing******
//**********************
# include <iostream.h>
# include <ctype.h>//tupper(letter)
# include <iomanip.h>//iomanip.h
void main()
{
for(char letter='A';letter<='Z';letter++)
{
cout<<hex
<<setiosflags(ios::uppercase)
<<int(letter)<<" ";
}
cout<<endl;
}
//**********************
//*********19.5*********
//******Daiyuebing******
//**********************
# include <fstream.h>
# include <iostream.h>
class person
{
public:
person();
person(char * a,char * b=0,char* c=0,char* d=0,char * e=0)
{
name=a;
dep=b;
tel1=c;
street=d;
tel2=e;
}
void display(ostream & out)
{
out<<"Name:"<<name<<"Dept:"<<dep<<"tel of office:"<<tel1
<<"address:"<<street<<"tel of home:"<<tel2<<endl;
}
protected:
char * name;
char * dep;
char * tel1;
char * street;
char * tel2;
};
ostream & operator<<(ostream & m,person & n)
{
n.display(m);
return m;
}
void main()
{
fstream fdev("example.dat",ios::in|ios:ut|ios::ate,filebuf::sh_none);
char * j=0;
char * k=0;
char * x=0;
char * y=0;
char * z=0;
cin>>j>>k>>x>>y>>z;
while(j!=0)
{
fdev<<j<<" "<<k<<" "<<x<<" "<<y<<" "<<z<<"/n";
cin>>j>>k>>x>>y>>z;
}
cout<<endl;
while(!fdev.eof())
{
char * a=0;
char * b=0;
char * c=0;
char * d=0;
char * e=0;
fdev>>a>>b>>c>>d>>e;
person p=person(a,b,c,d,e);
cout<<p;
}
cout<<endl;
}
第20章
//**********************
//*********20.1*********
//******Daiyuebing******
//**********************
# include <iostream.h>
# include <string.h>
template <class Type> Type Min(Type a,Type b)//template
{
{
return a<b?a:b;
}
}
char * Min(char * a,char * b)
{
return (strcmp(a,b)?a:b);
}
void main()
{
cout<<"Min(/"Hello/",/"Gold/" is "
<<Min("Hello","Gold"<<endl<<endl;
cout<<"Min(56,72) is "
<<Min(56,72)<<endl;
}
//**********************
//*********20.2*********
//*********stack.h******
//**********************
# include <iostream.h>
const int size=100;
template <class t>class Stack
{
public:
Stack();
~Stack();
void Push(t n);
t Pop();
void display(ostream & );
private:
t * stack;
int tos;
};
template <class t>
Stack<t>::Stack()//注意此处Stack后面的<t>不能缺。
{
stack = new t[size];
tos=-1;
}
template <class t>
Stack<t>::~Stack()//注意此处Stack后面的<t>不能缺。
{
tos=-1;
delete stack;
}
template <class t>
void Stack<t>:ush(t n)
{
if(tos==size-1)
{
cout<<"Over!!";
return;
}
else
{
tos=tos+1;
stack[tos]=n;
return;
}
}
template <class t>
t Stack<t>:op()
{
t temp=stack[tos];
tos=tos-1;
return temp;
}
template <class t>
void Stack<t>::display(ostream & oo)
{
while(tos!=-1)
{
oo<<stack[tos];
tos--;
}
}
template <class t>
ostream & operator<<(ostream & a,Stack <t> b)
{
b.display(a);
return a;
}
//**********************
//*********20.2*********
//*******main.cpp*******
//**********************
# include "stack.h"
void main()
{
Stack<int> sta;
sta.Push(2);
sta.Push(25);
sta.Push(3645);
int temp=sta.Pop();
cout<<temp<<endl;
cout<<sta;
}
第21章
//**********************
//*********21.1*********
//********daiyb*********
//**********************
char & operator[](int k)
{
if(0<=k && k<len)
{
if(!strcmp(p[k],m))
{
return p[k];
}
else
{
throw 0;
}
}
throw Range(k)
}
catch(int)
{
cout<<"ERROR!"<<endl;
}
//**********************
//*********21.2*********
//********daiyb*********
//**********************
class A{
public:
class Error{};
A()
{
n = new int;
init();
}
void init()
{
//do something ...
throw Error();
}
private:
int* n;
};
void main()
{
try{
A a;
}
catch(A::Error&{
cout <<"error when initialize./n";
}
}