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

时间:2017-10-31 00:48:41

标签: c++

下面是我的程序,它将一个文件输入一个堆栈,然后以相反的顺序输出到一个文件中,它运行,因为它应该在输出时遗漏一些单词。

更新**它省去了堆栈加倍的单词,这样当堆栈的大小为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

以下是发生错误的输入和输出文件

输入文件:很久以前,有一个小女孩住在森林附近的一个村庄里。每当她外出时,小女孩都穿着红色斗篷,所以村里的每个人都叫她小红帽。

输出文件:Hood Riding Red Little她称之为村里所有人所以斗篷骑着红色穿着女孩小 出去了她每当森林附近的村庄一个<强烈的> 生活 那个女孩很少有人在那里度过一次 2

(IGNORE THE 2,它知道多少时间堆叠了) 粗体和斜体是输出文件中缺少的单词。

1 个答案:

答案 0 :(得分:1)

在:

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++; } 的调用。

我想补充一点,赠品就是你偶尔会失去一个词;你的代码中偶尔会做些什么?

所以:

/* updates/addition */

* {
  box-sizing: border-box;
}

.searchArea {
  display: table;
  margin: auto;
}

.formWrapper,
.formButtons {
  display: table-cell;
  vertical-align: middle;
}

.formButtons {
  white-space: nowrap;
  font-size: 0;
}

.formButtons button {
  font-size: 1rem;
  /* to hide swallow white-space and instead float:right initially used*/
}


/* end addition */


/*search box*/

.playerInput {
  width: 450px;
  min-width: 300px;
  font-size: 1.3em;
  padding-left: 20px;
  height: 55px;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  background-color: rgba(34, 49, 63, 0.8);
  color: #fff;
  transition: 0.2s ease-out;
  vertical-align: top;
}

.playerInput:focus {
  background-color: rgba(255, 255, 255, 0.60);
  color: #111;
  transition: 0.2s ease-in;
}

.upvoteButton {
  border: none;
  height: 55px;
  padding: 8px 24px;
  font-size: 0.8em;
  font-family: "Droid Sans", sans-serif;
  background-color: #00ab9e;
  color: #fff;
  transition: 0.2s ease-out;
}

.downvoteButton {
  border: none;
  height: 55px;
  padding: 8px 24px;
  font-size: 0.8em;
  font-family: "Droid Sans", sans-serif;
  background-color: #fcce66;
  color: #fff;
  transition: 0.2s ease-out;
}

.injuryButton {
  border: none;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  height: 55px;
  padding: 8px 24px;
  font-size: 0.8em;
  font-family: "Droid Sans", sans-serif;
  background-color: #fb655a;
  color: #fff;
  transition: 0.2s ease-out;
}

.upvoteButton:hover {
  background-color: #02ffb6;
  color: #fff;
  transition: 0.2s ease-out;
}

.downvoteButton:hover {
  background-color: #fbf03e;
  color: #fff;
  transition: 0.2s ease-out;
}

.injuryButton:hover {
  background-color: #fd483a;
  color: #fff;
  transition: 0.2s ease-out;
}