第五次课程作业

第五次课程作业

GIT链接

四则运算代码框架:

具体实现在上方git链接。

对栈的学习:

下面为自己写的栈的代码

#include<iostream>
using namespace std;
class Stack{
private:
	int top;
	int *a;
	int size;
public:
	void push(int);//进栈
	void pop();//出栈
	bool stackempty();//是否为空
	bool stackfull();//是否满
	Stack();
	Stack(int);//构造栈,大小自拟
};
Stack::Stack(int how_many){
	size=how_many;
	a=(int *)malloc(4*how_many);
	top=0;
}
void Stack::push(int t){
	if(!stackfull()){
		*(a+top)=t;
		top++;
	}else cout<<"Error:the stack is full"<<endl;
}
void Stack::pop(){
	if(!stackempty()){
		top--;
	}else cout<<"Error:the stack is empty"<<endl;
}
bool Stack::stackempty(){
	if(top==0)return true;
	return false;
}
bool Stack::stackfull(){
	if(top==size)return true;
	return false;
}

栈可以用数组写也可以用链表写,数组比较方便点。

感想:

栈的用途极为广泛,在源程序编译中表达式的计算、过程的嵌套调用和递归调用等都要用到栈。

原文地址:https://www.cnblogs.com/qwe1/p/6890855.html