大家好! 我正在尝试做这样的任务: 通过编写几个构造函数(包括复制构造函数)来创建堆栈od字符串。所有元素都以数组形式提供。如果数组具有固定大小,则在超载的情况下增加它。创建pop(),push()函数。创建一个函数,删除每个偶数元素。
在确定堆栈大小后出现问题
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cstddef>
using namespace std;
class Stack
{
string *stck; //stack pointer
int sz; //stack size
int ts; //top of the stack
public:
//constructor
Stack(int size)
{
size=sz;
stck = new string[sz];
ts = -1;
};
//destructor
~Stack() {
delete []stck;
};
//copy constructor
Stack (const Stack &ob);
void Push(string itm);
string Pop();
void Show(int sz);
private:
void Resize(int sz);
};
void Stack::Push(string itm)
{
if (ts == sz)
{
cout<<"Stack is overflow"<<endl;
Resize(ts);
}
else stck[ts++] = itm;
}
string Stack::Pop()
{
if(ts<0)
{
cout<<"Stack is free"<<endl;
return 0;
}
else return stck[--ts];
}
void Stack::Show(int sz)
{
for (int i = 0; i < sz; i++)
{
std::cout << stck[i] << std::endl;
}
}
Stack::Stack (const Stack &ob)
{
cout<<"Copy constructor allocating stackPtr)"<<endl;
stck = new string;
*stck = *ob.stck;
}
//функция выделения дополнительной памяти
void Stack::Resize(int idx)
{
int new_size = sz;
while (new_size < idx)
new_size *=2;
string *new_arr = new string[new_size];
for(int i = 0; i < sz; i++)
new_arr[i] = stck[i];
delete[] stck;
stck = new_arr;
sz = new_size;
}
int main()
{
cout<<"Enter the size of the stack"<<endl;
int n;
cin>>n;
Stack mystck(n);
string str;
for(int i=0; i<n; i++)
{
cout<<"Enter an element"<<endl;
cin>>str;
mystck.Push(str);
}
for(int j = 0; j<n; j+2)
{
mystck.Pop();
mystck.Show(n);
}
return 0;
}
有人可以帮我解决这个问题吗?