c++课后习题解答6-10

cout<f<

void main() {

F::G g; F f(5,10);

f.myg.Print(&f); g=f.myg;

cout<

7.5 编程题

1.按下列要求实现一个栈类的操作。 该类名为Stack,包括如下操作: (1)压栈操作:Push(); (2)弹栈操作:Pop();

(3)获取栈顶元素:Peer(); (4)判栈空操作:IsEmpty(); (5)判栈满操作:IsPull()。 设栈最多可存放50个整数。 栈中成员用数组表示。

编写一个程序,定义一个栈类的对象数组来验证该类操作。 答:栈的正常操作程序如下: #include #include class Stack {

public:

Stack(int i); ~Stack() { delete [] sta; } void Push(int i); int Pop(); void IsUpll() { if(tos==length) { cout<<\ return; } } int IsEmpty() { if(tos==0) { cout<<\ return 0; } } private: int *sta;;

16

int tos,length; }; Stack::Stack(int i) { sta=new int[i]; if(!sta) { cout<<\ abort(); } tos=0; length=i; } void Stack::Push(int i) { sta[tos]=i; tos++; } int Stack::Pop() { tos--; return sta[tos]; }

void main() { Stack a(50); a.Push(1); a.Push(2); a.Push(3); a.Push(4); a.Push(5); a.Push(6); a.Push(7); a.Push(8); a.Push(9); a.Push(10); cout<

判断栈的空、满操作,请读者修改上述程序。

2.按下列要求实现一个有关学生成绩的操作。 该类名为Student。

(1)每个学生的信息包含有姓名(字符数组)和成绩(int型)。 (2)共有5个学生,用对象数组表示。

17

(3)计算出5个学生中的最高分,并输出姓名及分数。 答:编程如下:

#include #include class Student {

>>閻忕偞娲栫槐鎴﹀礂閵婏附鐎�<<
12@gma联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4