-
m_pBuffer = new char[size]; 使用new动态申请了size个char类型大小的空间,如果把char换成类名时,就是申请size个类名大小的空间。但只要是给类实例化对象申请空间,就要调用类的构造函数,而所写的构造函数如果是 Coordinate(int x , int y ); 不是默认构造函数,参数没有默认值,在实例化对象时,就必须给赋予x,y值,否则就会报错,而使用new运算符申请多个内存时,无法为每个对象都赋初值x,y。那样就会报错。因此就需要将构造函数改为有默认值的构造函数 Coordinate(int x = 0, int y = 0); 这样的话编译就可以通过了,但每个对象的x,y值都是0 .查看全部
-
栈没有数据类型上的限制,普通的数据类型可以,复杂的数据类型也可以 复杂的数据类型作为栈的元素,如果还需要用原来的方式进行数据的传入,就需要有一个默认的构造函数,即为Coordinate(int x=0 ,int y =0) char *pButter的功能是指向栈空间,指向栈空间的前提是,这个栈是什么类型的栈,就应该指向什么类型的栈空间,现在是Coordinate数据类型,所以要将char改为Coordinate的数据类型 遍历针对不同的数据成员,遍历的时候使用不同的方法,遍历比较简单的数据类型,使用简单的遍历方法就可以 不做运算符重载,原来的输出cout是无法输出的,因为输出的不是简单的数据类型了。可以使用运算符重载或者使用 void printCoordinate()函数进行输出 声明和定义不能都定义默认参数,默认参数一般都是在声明的时候定义查看全部
-
括号匹配 int main(void) { MyStack<char> *pStack=new MyStack<char> (30);//用来存放扫描字符串当中所找到的字符 MyStack<char> *pNeedStack=new MyStack<char> (30);//记录栈顶急需的字符 char str[]="[()]]"; char currentNeed=0;//表示当前需要的字符,赋初值用ARIS码 for(int i=0;i<strlen(str);i++) { if(str[i]!=currentNeed) { pStack->push(str[i]); switch(str[i]) { case'[': if(currentNeed!=0) { pNeedStack->push(currentNeed); } currentNeed=']'; break; case'(': if(currentNeed!=0) { pNeedStack->push(currentNeed); } currentNeed=')'; break; default://为了适用于后面有多出来的情况,例如:[()]] cout<<"字符串不匹配"<<endl; return 0; } } else { char elem; pStack->pop(elem); if(!pNeedStack->pop(currentNeed))//判断出栈是否正确,如果栈里没有可pop出的字符就要赋值0 { currentNeed=0; } } } if(pStack->stackEmpty()) { cout<<"字符串括号匹配"<<endl; } else { cout<<"字符串括号不匹配"<<endl; } delete pStack; pStack=NULL; delete pNeedStack; pNeedStack=NULL; return 0; }查看全部
-
坐标类: #ifndef COORDINATE_H #define COORDINATE_H class Coordinate { public: Coordinate(int x=0,int y=0);//默认构造函数,应用于栈 void printCoordinate(); //operator<<,运算符重载使遍历通过。或通过调用本身有的printCoordinate()函数 private: int m_iX; int m_iY;//数据成员复杂的时候要使用拷贝构造函数来push }; #endif 实现: #include"Coordinate.h" #include<iostream> using namespace std; Coordinate::Coordinate(int x,int y) { m_iX=x; m_iY=y; } void Coordinate::printCoordinate () { cout<<"("<<m_iX<<","<<m_iY<<")"<<endl; }查看全部
-
和c语言的 数据结构 差不多啊查看全部
-
一个栈就可以了,首先刚开始要入栈,然后接下来将栈顶元素和待匹配的下一个元素比较,如果相等则出栈,不相等则入栈,一直这样下去,直到字符串结束,最后栈中如果有剩余元素,那么就是不匹配
查看全部 -
我的栈 (续): void push(const elemtype &c) { if (isfull()) { cout << "满了"; // throw .. } node *p = new node(c); top->next = p; p->last = top; top = p; ++size; } elemtype pop() { if (isempty()) { cout << "空了"; // theow .. } elemtype x = top->data; node *p = top; top = p->last; top->next = NULL; delete p; --size; return x; } // 遍历函数,默认从栈底到栈顶 void traverse(bool isfrombottom = true) { node *p; if (isfrombottom) { p = base->next; while (p) { cout << p->data; p = p->next; } cout<<endl; } else { p = top; while (p != base) { cout << p->data; p = p->last; } cout<<endl; } } int lenth() { return size; } void clearStack() { node *p = base->next,*q; while (p) { q = p; p = p->next; delete q; } top = base; size = 0; base->next = NULL; } };查看全部
-
#include<iostream> using namespace std; #define elemtype char struct node { elemtype data; node *last; node *next; // 新定义的结点的默认构造函数 node(const elemtype data = 0,node *last = NULL,node *next = NULL) { this->data = data; this->last = last; this->next = next; } ~node() { data = 0; last = next = NULL; } }; class stack{ private: node *base; //栈底 node *top; //栈顶 int size; //栈的实际大小 public: stack() { // 申请一个哨兵结点作为栈底,哨兵的data没有意义 // 如果申请物理空间失败,抛出异常 if (!(base = new node())) { cout << "内存不足!"; // throw ... } top = base; size = 0; } ~stack() { node *p=base,*q=NULL; while(p) { q = p; p = p->next; delete q; } } bool isfull() { // 尝试申请一个node,如果可以申请说明未满 node *p=new node; if (!p) { return true; } else { delete p; return false; } } bool isempty() { return !size; // 也可以写成 return top == base; }查看全部
-
二进制转换查看全部
-
2哦哟哟查看全部
-
main.cpp #include <iostream> #include <stdlib.h> #include "MyStack.h" using namespace std; //实现栈 int main(void) { MyStack *pStack = new MyStack(5); pStack->push('h'); pStack->push('e'); pStack->push('l'); pStack->push('l'); pStack->push('o'); pStack->stackTraverse(true); pStack->clearStack(); cout<<pStack->stackLength()<<endl; if(pStack->stackEmpty()) { cout << "栈为空" << endl; } delete pStack; pStack = NULL; return 0; }
MyStack.cpp // #include "MyStack.h" #include <iostream> using namespace std; MyStack::MyStack(int size)//分配内存初始化栈空间,设定栈容量,栈顶 { m_iSize = size; m_pBuffer = new char[size]; m_iTop = 0;//表示此时的栈是个空栈 } MyStack::~MyStack() { delete []m_pBuffer; } bool MyStack::stackEmpty()//判定栈是否为空,为空返回true,非空返回false { if(0 == m_iTop) { return true; } return false; } bool MyStack::stackFull() { if(m_iTop == m_iSize) { return true; } return false; } void MyStack::clearStack()//清空栈 { m_iTop = 0; } int MyStack::stackLength()//已有元素的个数 { return m_iTop; } bool MyStack::push(char elem)//元素入栈,栈顶上升 { //先判断是否栈满 if(stackFull()) { return false; } m_pBuffer[m_iTop] = elem; m_iTop++; return true; } //法二 //char MyStack::pop() //{ // if(stackEmpty()) // { // throw 1; // } // else // { // m_iTop--; // return m_pBuffer[m_iTop]; // } //} bool MyStack::pop(char &elem)//元素出栈,栈顶下降 { //先判定是否为空 if(stackEmpty()) { return false; } m_iTop--;//入栈之后栈顶指向的是一个空位置 elem = m_pBuffer[m_iTop]; return true; } void MyStack::stackTraverse(bool isFromButoom)//遍历栈中所有元素 { if(isFromButoom) { for(int i = 0; i < m_iTop;i++)//栈的长度就是m_iTop { cout<< m_pBuffer[i] << ","; } } else { for(int i = m_iTop-1; i>=0;i--) { cout << m_pBuffer[i] << ","; } } }
MyStack.h #ifndef INC_0131_MYSTACK_H #define INC_0131_MYSTACK_H class MyStack { public: MyStack(int size);//分配内存初始化栈空间,设定栈容量,栈顶 ~MyStack();//回收栈空间内存 bool stackEmpty();//判定栈是否为空,为空返回true,非空返回false bool stackFull(); void clearStack();//清空栈 int stackLength();//已有元素的个数 bool push(char elem);//元素入栈,栈顶上升 bool pop(char &elem);//元素出栈,栈顶下降 void stackTraverse(bool isFromButoom);//遍历栈中所有元素 private: char *m_pBuffer;//栈空间指针 int m_iSize;//栈容量 int m_iTop;//栈顶,栈中元素个数栈顶为1说明有一个元素 }; #endif //INC_0131_MYSTACK_H
查看全部 -
当成员变量比较复杂时要用到拷贝构造函数
查看全部 -
1.注意 当函数形参是引用时,实参必须是变量或者变量的引用,而不能是具体的值 例如:如果要传 3,必须先int a=3,再把a传进去 2.新建文件到项目里时要勾选那两个选项
查看全部 -
出栈时,当前m_iTop指向的是一个空位置,于是要做减一操作。查看全部
-
老师的看起来好不得劲啊 #include<iostream> #include<stack> #include<cstdlib> using namespace std; int eight(int m) { int n=0; stack<int>s; do { n=m%8; m=m/8; s.push(n); }while(m!=0); while(s.size()!=0) { cout<<s.top(); s.pop(); } cout<<endl; } int sixteen(int m) { int p=0; stack<int>s; do { p=m%16; m=m/16; s.push(p); }while(m!=0); while(s.size()!=0) { cout<<s.top(); s.pop(); } cout<<endl; } int two(int m) { int i=0; stack<int>s; do { i=m%2; m=m/2; s.push(i); }while(m!=0); while(s.size()!=0) { cout<<s.top(); s.pop(); } cout<<endl; } int main() { int m; cin>>m; eight(m); sixteen(m); two(m); system("pause"); return 0; }查看全部
举报
0/150
提交
取消