第10章
//*****************
//* 10.1 *
//*****************
# include <iostream.h>
struct student{
char name[8];
float midd;
float over;
};//注意此处必须输入分号
student& input(student& a);
float aver(student& b);
void main()
{
student s;
s=input(s);
float m=aver(s);
cout<<s.name<<" : "<<m<<endl;
}
student& input(student& a)
{
cout<<"please input name:";
cin>>a.name;
cout<<"please input midd:";
cin>>a.midd;
cout<<"please input over:";
cin>>a.over;
return a;
}
float aver(student& b)
{
return (b.midd+b.over)/2;
}
//*****************
//* 10.2 *
//*****************
void insert(unit& head,char * b)
{
unit * i=head;
while((i->name !="jone"&&(i->next != NULL))
{
i=i->next;
}
if(i->name =="jone"
{
unit * p=new unit;
p->name="marit";
p->next=i->next;
i=p;
}
if(i->next == NULL)
{
unit * p=new unit;
p->name="marit";
p->next=NULL;
i->next=p;
}
}
//*****************
//* 10.3 *
//*****************
struct unit
{
long data;
unit * next;
};
unit * change(unit * head)
{
unit* i=head;
unit* j=NUL;
while(i != NULL)
{
unit * p=new unit;
p->data=i->data;
p->next=j;
j=p;
unit * q=i;
i=i->next;
delete q;
}
return j;
}
//*****************
//* 10.4 *
//*****************
//(1):
void ShowList(Lnode * List)
{
while(List != NULL)
{
cout<<List->data;
List=List->next;
}
}
void AddToEnd(Lnode* new1,Lnode * head)
{
while (head != NULL)
{
head=head->next;
}
head=new1;
new1->next=NULL;
}
//(2):
3
5
7
6
4
8
//(3):
void Delete(Lnode * head)
{
while(head != NULL)
{
Lnode * p;
p=head;
head=head->next;
delete p;
}
}
//*****************
//* 10.5 *
//*****************
unit * hbing(unit * la,unit * lb)
{
unit * p=la;
if((la!=NULL)&&(lb!=NULL))
{
if(la->data<=lb->data)
{
p=la;
la=la->next;
}
else
{
p->next=lb;
p=lb;
lb=lb->next;
}
}
if(la!=NULL)
{
p->next=la;
}
if(lb!=NULL)
{
p->next=lb;
}
return la;
}
//*****************
//* 10.6 *
//*****************
struct unit
{
long data;
char name[];
char sex;
int ages;
};
unit * index(unit * la)
{
unit * p=la->next;
unit * m=la;//作为新排好序的首指针
while(p!=NULL)
{
unit * q=la;//新表中第一个大于或等于p的元素位置
unit * k=q;//新表中最后一个小于p的元素位置
while(q!=NULL)&&(q->data<p->data)//查找插入位置,位于K和Q之间
{
k=q;q=q->next;
}
if(q==NULL)//到新链表尾
{
k->next=p;p=p->next;k->next->next=NULL;//新元素是表尾
}
else//在新表中间插入
{
k->next=p;p=p->next;k->next->next=q;//p插入k和q之间
}
}
return m;
}
第11章
//*****************
//* 11.1 *
//*****************
p.x+=5;
p.y+=6;
违反了类的保护机制,在类作用域外,无法访问类的被保护成员
//****************
//*11.2-Cat.h *
//****************
class Cat
{
public:
int GetAge();
void SetAge(int age);
void Meow();
protected:
int itsAge;
};
//****************
//*11.2-Cat.cpp *
//****************
# include <iostream.h>
# include "Cat.h"
int Cat::GetAge()
{
return itsAge;
}
void Cat::SetAge(int age)
{
itsAge=age;
}
void Cat::Meow()
{
cout<<"Meow.\n";
}
//****************
//* 11.2-main.cpp*
//****************
# include <iostream.h>
# include "Cat.h"
void main()
{
Cat frisky;
frisky.SetAge(5);
frisky.Meow();
cout<<"frisky is a cat who is"
<<frisky.GetAge()
<<"years old.\n";
frisky.Meow();
}
//******
//*11.3*
//******
# include <iostream.h>
class Date
{
public:
void list();
void set(int day,int month,int year);
void adda();
private:
int d;
int m;
int y;
};//注意此处的分号
void Date::list()
{
cout<<d<<"/"<<m<<"/"<<y;
}
void Date::set(int day,int month,int year)
{
d=day;
m=month;
y=year;
}
void Date::adda()
{
d=d+1;//继续修改,进行日期、月份的判断
}
void main()
{
Date d;
d.set(23,11,2004);
d.list();
cout<<endl;
d.adda();
d.list();
cout<<endl;
}
//******
//*11.4*
//******
# include <iostream.h>
class Time
{
public:
void list();
void set(int hour,int minith,int second);
private:
int h;
int m;
int s;
};//注意此处的分号
void Time::list()
{
cout<<h<<":"<<m<<":"<<s;
}
void Time::set(int hour,int minith,int second)
{
h=hour;
m=minith;
s=second;
}
void main()
{
Time t;
t.set(9,11,56);
t.list();
cout<<endl;
}
//*************
//*11.4-Time.h*
//*************
class Time
{
public:
void list();
void set(int hour,int minith,int second);
private:
int h;
int m;
int s;
};//注意此处的分号
//***************
//*11.4-Time.cpp*
//***************
# include <iostream.h>
# include "Time.h"
void Time::list()
{
cout<<h<<":"<<m<<":"<<s;
}
void Time::set(int hour,int minith,int second)
{
h=hour;
m=minith;
s=second;
}
//***************
//*11.4-main.cpp*
//***************
# include <iostream.h>
# include "Time.h"
void main()
{
Time t;
t.set(9,11,5);
t.list();
cout<<endl;
}
//******
//*11.5*
//******
# include <iostream.h>
struct Node
{
int a;
Node * next;
};
class stack
{
public:
void put(int item);
int get();
private:
Node * head;
};
void stack::put(int item)
{
Node * p=new Node;
p->a=item;
p->next=head;
head=p;
}
int stack::get()
{
Node * q=head;
head=head->next;
return q->a;
}
void main()
{
stack st;
st.put(10);
st.put(12);
st.put(14);
cout<<st.get()<<endl;
cout<<st.get()<<endl;
}