字符串遇到‘#’就删去前一个,print函数加上后就崩溃,什么原因啊

C++语言 码拜 7年前 (2017-04-17) 793次浏览
#include<iostream>
#define n 100
using namespace std;
struct Stack{
    char* base;
    char* top;
    char a[n];
    int size;
};
void init(Stack &s){
    s.base=s.a;
    s.top=s.base;
}
void push(Stack &s, char e){
    *s.top=e;
    s.top++;
    s.size++;
}
void pop(Stack &s){
    s.top--;
    s.size--;
}
void print(Stack &s){   //这里可能有问题,但找不出来
    char* p=s.base;
    while(p!=s.top)
        cout<<*p;
}
int main(){
    Stack s;
    char c;
    while(cin>>c){
        push(s,c);
        if(c=="#"){
            pop(s);
            pop(s);
        }
        if(c=="*")
            break;
    }
    print(s);
}
解决方案

20

while(p!=s.top)
        cout<<*p;

指针不移动吗


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明字符串遇到‘#’就删去前一个,print函数加上后就崩溃,什么原因啊
喜欢 (0)
[1034331897@qq.com]
分享 (0)