[英]C++ Missing words in Output File
下面是我的程序,将文件输入到堆栈中,然后以相反的顺序输出到文件中,它按预期运行,但在输出时却遗漏了一些单词。
UPDATE** 它省略了开始加倍堆栈的单词,这样当堆栈的大小为 10 时,它不会在第 10 项之后插入第 11 项; 我试图通过执行“ i <= topStack ”来编辑增加 Stacksize 函数,但这会导致编译器错误。
#include <iostream>
#include <string>
#include<fstream>
//#include "ArgumentManager.h"
#include "Stack.h"
using namespace std;
string stripSpecials(string str)
{
int i=0,len=str.length();
while(i<len)
{
char c=str[i];
if(((c>='A')&&(c<='Z'))||((c>='a')&&(c<='z')) || c == '\'')
{
++i;
}
else
{
str.erase(i,1);
--len;
}
}
return str;
}
int main(int argc, char** argv) {
// ArgumentManager am(argc, argv); //Instantiating parser for command line arguments
const int STACK_SIZE=5;
int count = 0;
//create and link input...
// ifstream infile(am.get("A").c_str()); // file to read from, getting name from command line
// ofstream outfile(am.get("C").c_str()); // output file, getting name from command line
ifstream infile;
infile.open("input.txt");
//error message for file open fail
if (infile.fail())
cout << "Error opening input file.\n";
//...and output files
ofstream outfile;
outfile.open("output.txt");
if (outfile.fail())
cout << "Error opening Output file.\n";
arrayStack<string> myStack(10);
std::string word,final_word;
while (infile >> word)
{
final_word = stripSpecials(word);
myStack.push(final_word);
}
while(!myStack.stackIsEmpty())
{
word = myStack.top();
myStack.pop();
outfile << word <<" ";
}
outfile << endl;
outfile << myStack.count;
infile.close();
outfile.close();
return 0;
}
Stack.H 文件
//ARRAY BASED STACK TEMPLATE
#ifndef H_ArrayStack
#define H_ArrayStack
#include <iostream>
using namespace std;
template<class Type>
class arrayStack {
private:
int topStack; // the top of the STACK
void stackCopy(const arrayStack<Type>& newArrayStack);
Type *list; // array based needs pointer to hold the stack element
static const int growthFactor = 2;
static const int initialMaxSize = 10;
public:
int count; //Count how many times the stack doubled
int maxStackSize; // the maximum height of the STACK
const arrayStack<Type>& operator=(const arrayStack<Type>&);
void stackInitialize() {
topStack = 0;
}
; //Ensure the array stack is empty
bool stackIsEmpty() const {
return (topStack == 0);
}
; //check if stack is empty, is const so will not be messed with
bool stackIsFull() const {
return topStack == maxStackSize;
}
; // just like line 8 except check if it is full
void push(const Type& word); // add a word to the array STACK
void pop(); //remove a word from the array and increment the top
Type top() const; //returns the top of the STACK
void increaseStackSize(); // double the size of stack when stack becomes full
arrayStack(int size); //the default constructor
arrayStack(const arrayStack<Type>& newArrayStack); // the copy constructor which allows a new STACK
~arrayStack() {
delete[] list;
}
;
// it is an array so to ensure no memory leaks the stack must be deleted after use
};
template<class Type>
void arrayStack<Type>::push(const Type& word) {
if (topStack != maxStackSize){
list[topStack++] = word; // adding a new word to the STACK
}
else
increaseStackSize();
count++;
}
template<class Type>
void arrayStack<Type>::pop() {
if (!stackIsEmpty()) {
topStack--;
count--;
}
}
template<class Type>
Type arrayStack<Type>::top() const {
if (topStack == 0) {
return 0;
} else
return list[topStack - 1];
}
template<class Type>
arrayStack<Type>::arrayStack(int size) {
maxStackSize = size;
count = 0;
topStack = 0;
list = new Type[maxStackSize];
}
template<class Type>
void arrayStack<Type>::stackCopy(const arrayStack<Type>& newArrayStack) {
delete[] list;
maxStackSize = newArrayStack.maxStackSize;
topStack = newArrayStack.topStack;
list = new Type[maxStackSize];
for (int j = 0; j < topStack; j++)
list[j] = newArrayStack.list[j];
}
template<class Type>
arrayStack<Type>::arrayStack(const arrayStack<Type>& newArrayStack) {
list = NULL;
stackCopy(newArrayStack);
}
template<class Type>
const arrayStack<Type>& arrayStack<Type>::operator=(
const arrayStack<Type>& newArrayStack) {
if (this != &newArrayStack)
stackCopy(newArrayStack);
return *this;
}
template<class Type>
void arrayStack<Type>::increaseStackSize() {
maxStackSize = growthFactor * maxStackSize;
Type* temp = new Type[maxStackSize];
for (int i = 0; i < topStack; i++)
temp[i] = list[i];
delete[] list;
list = temp;
}
#endif
这是发生错误的输入和输出文件
输入文件:从前,有一个小女孩住在森林附近的一个村庄里。 每次出门,小女孩都披着一件红色的骑马披风,所以村里的人都叫她小红帽。
输出文件:胡德骑马红色小她叫村中大家这么斗篷骑马红了穿女孩一点出来去她每当林住谁女孩附近的一个村庄稍微在那里时间后一旦2
(忽略 2,它是为了知道堆栈加倍了多少次)粗体和斜体是输出文件中缺少的词。
在:
void arrayStack<Type>::push(const Type& word) {
if (topStack != maxStackSize){
list[topStack++] = word; // adding a new word to the STACK
}
else
increaseStackSize();
count++;
}
请注意,当增加堆栈大小时,您不会添加单词。 然而,这是对push
的调用。
我要补充的是,放弃的是你偶尔会失去一个词; 你在代码中偶尔会做什么?
所以:
template<class Type>
void arrayStack<Type>::push(const Type& word) {
if (topStack == maxStackSize){
increaseStackSize();
}
list[topStack++] = word; // adding a new word to the STACK
count++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.