繁体   English   中英

C++ 无法从 Char 数组中打印某些单词

[英]C++ Can't print certain words from Char array

不知道如何表达这个问题,但我正在为一个作业制作一个程序,除了输入/输出之外,我们不允许使用预先存在的库。 我们也只能使用原始数据类型。 我必须读取带有单词的文本文件,从单词中删除所有标点符号,然后将这些单词存储在 2D 字符数组中。

这个问题似乎是当一个单词以非字母字符开头时,整个单词在使用cout << stack[top]时不是 output 但是当我 output 每个单独的字符使用cout << stack[top][i] ,它会产生预期的 output。

'stack' 是一个二维数组,其中包含组成单词的字符。

'top' 是表示堆栈长度的变量

代码:

#include <iostream>
#include <fstream>

using namespace std;

// Function Prototypes
void push(char word[]);
char formatCharacter(char letter);
bool isAlphabet(char letter);
char toLowercase(char letter);

// Global Variables
const int STACK_SIZE = 50000;
const int WORD_SIZE = 30;
char stack[STACK_SIZE][WORD_SIZE];
int top = 0;
int words = 0;
int wordCount[STACK_SIZE];    

int main(){
    // Local Variables
    char filename[20];
    ifstream fin;
    char word[WORD_SIZE];

    // Get file input
    cerr << "Please enter the name of the input file: ";
    cin >> filename;

    // Open file
    fin.open(filename);
    // Print error if file doesn't open, then quit the program.
    if (!fin) {
        cerr << "Error opening file " << filename << ". Program will exit." << endl;
        return 0; 
    }

    // Read the file into the stack
    while (fin >> word) {
        push(word);
    }

    // Close file
    fin.close();
}

void push(char word[]){
    if (top == STACK_SIZE) return;

    int i = 0;
    int j = 0;

    do {
        if (isAlphabet(word[i])){
            word[i] = formatCharacter(word[i]);
            stack[top][i] = word[i];
            cout << stack[top][i]; // Output fine
            j++;
        }
        i++;
    } while (word[i]);

    wordCount[words] = j;
    //cout << stack[top] << ": " << wordCount[words] << endl; // Output incorrect
    cout << endl;
    top++;
    words++;
    return;
}

bool isAlphabet(char letter){
    if ((letter < 'A' || letter > 'Z') && (letter < 'a' || letter > 'z')){
        return false;
    }
    else{
        return true;
    }
}

char formatCharacter(char letter){
    if ((letter < 'A' || letter > 'Z') && (letter < 'a' || letter > 'z')){
        letter = '\0';
    }
    else{
        if (letter >= 'A' && letter <= 'Z'){
            letter = toLowercase(letter);
        }
    }
    return letter;
}

char toLowercase(char letter){
    letter = letter + 32;
    return letter;
}

isAlphabet() 只是检查它是否是字母字符

formatCharacter() 通过将字符替换为 '\0' 来删除任何标点符号,并将大写变为小写。

输入:

Jabberwocky

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.

Output 使用cout << stack[top][i]时:

jabberwocky
twas
brillig
and
the
slithy
toves
did
gyre
and
gimble
in
the
wabe
all
mimsy
were
the
borogoves
and
the
mome
raths
outgrabe

Output 使用cout << stack[top]时:

jabberwocky: 11
: 4
brillig: 7
and: 3
the: 3
slithy: 6
toves: 5
did: 3
gyre: 4
and: 3
gimble: 6
in: 2
the: 3
wabe: 4
all: 3
mimsy: 5
were: 4
the: 3
borogoves: 9
and: 3
the: 3
mome: 4
raths: 5
outgrabe: 8

请注意缺少“twas”这个词。 我宁愿不遍历每个单词的每个字符来获得我需要的 output。 我会很感激任何建议,谢谢!

最简单的解决方法是更改:

stack[top][i] = word[i];

至:

stack[top][j] = word[i];
           ^ j not i here

这将确保'Twas最终成为twas而不是\0twas

此外, formatCharacter()应该调用isAlphabet()而不是重复条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM