C++输出文件中缺少单词

C++ Missing words in Output File

本文关键字:单词 输出 文件 C++      更新时间:2023-10-16

下面是我的程序,它将文件输入到堆栈中,然后以相反的顺序输出到文件中。它按预期运行,但在输出时会漏掉一些单词。

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,这是为了知道堆栈翻了多少倍)粗体和斜体是输出文件中缺少的单词。

In:

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++;
}