1、
#include <iostream>
#include <string>
using namespace std;
class stu
{
private:
string name;
int age;
int score;
public:
stu(){} //无参构造
stu(string n,int a,int s):name(n),age(a),score(s){}//有参构造
~stu(){}//析构函数
void show()
{
cout<<name<<":"<<age<<":"<<score<<endl;
}
};
class party
{
private:
string activity;
string organize;
public:
party(){}//无参构造
party(string a,string o):activity(a),organize(o){}//有参构造
~party(){}//析构函数
void show()
{
cout<<activity<<":"<<organize<<endl;
}
};
class cstu:public stu,public party
{
private:
string ocupation;
public:
cstu(){}//无参构造
cstu(string n,int a,int s,string b,string o,string m):stu(n,a,s),party(b,o),ocupation(m){}//有参构造
~cstu(){}//析构函数
void show()
{
cout<<ocupation<<endl;
}
};
int main()
{
cstu c1("zhangsan",22,99,"study","tuanyuan","student");
c1.stu::show();
c1.party::show();
c1.show();
return 0;
}
2、
#include <iostream>
#define MAX 8
typedef int datatype;
using namespace std;
class queue
{
private:
datatype data[MAX];
int front;//队头
int tail;//队尾
public:
void init()
{
front=0;
tail=0;
}
bool empty()
{
return front==tail?1:0;
}
bool full()
{
return front==(tail+1)%MAX?1:0;
}
int size()
{
cout<<((tail+MAX-front)%MAX)<<endl;
return 0;
}
void push(datatype e)
{
if(full()==1)
{
cout<<"queue full"<<endl;
return ;
}
data[tail]=e;
tail=(tail+1)%MAX;
}
void pop()
{
if(empty()==1)
{
cout<<"queue empty"<<endl;
return ;
}
front=(front+1)%MAX;
}
void show()
{
int i;
for(i=front;i!=tail;i=(i+1)%MAX)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
queue c1;
c1.init();
if(c1.empty()==1)
{
cout<<"empty"<<endl;
}
else
{
cout<<"no empty"<<endl;
}
if(c1.full()==1)
{
cout<<"full"<<endl;
}
else
{
cout<<"no full"<<endl;
}
c1.push(3);
c1.push(6);
c1.push(9);
c1.push(8);
c1.push(3);
c1.push(6);
c1.push(9);
c1.push(8);
c1.show();
c1.size();
c1.pop();
c1.show();
c1.size();
cout << "Hello World!" << endl;
return 0;
}
3、仿照上图循环顺序队列,写出栈
#include <iostream>
typedef int datatype;
#define MAX 20
using namespace std;
class stack
{
private:
datatype data[MAX];
int top;
public:
void init()
{
top=-1;
}
bool empty()
{
return (top==-1)?1:0;
}
bool full()
{
return (top==MAX-1)?1:0;
}
void size()
{
int s=0;
s=top+1;
cout<<s<<endl;
}
void push(datatype e)
{
if(full()==1)
{
cout<<"stack full"<<endl;
return ;
}
top++;
data[top]=e;
}
void pop()
{
if(empty()==1)
{
cout<<"stack empty"<<endl;
return;
}
top--;
}
void show()
{
int i;
for(i=0;i<=top;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
stack s1;
s1.init();
if(s1.empty()==1)
{
cout<<"empty"<<endl;
}
else
{
cout<<"no empty"<<endl;
}
if(s1.full()==1)
{
cout<<"full"<<endl;
}
else
{
cout<<"no full"<<endl;
}
s1.empty();
s1.full();
s1.push(6);
s1.push(9);
s1.push(5);
s1.show();
s1.size();
s1.pop();
s1.show();
s1.size();
s1.pop();
s1.pop();
s1.pop();
return 0;
}