首页 > 解决方案 > 使用指针在 C++ 中解读单词

问题描述

对于我的 csc 102 课程,我必须创建一个程序,将文本文件中的某些字母替换为其他字母。该文件包含五个单词,我必须使用类系统。这对我来说听起来很简单,但我在类中的变量必须是指针变量。由于我现在所有的课程都在线,所以我只需要尝试使用我的教授在画布上的手写笔记来解释他希望如何完成项目。我有点理解指针是什么。指针保存一个值的地址。您必须使用 * 取消引用它才能获取存储在那里的值。但是,我只是不明白如何在这个问题中使用指针。这是四个提示中的第一个提示,我只是不明白我应该用指针做什么。我将在下面附上我的代码以及提示要求我做什么。我很感激任何帮助。

提示:问题 1:计算机键盘在将“p”/“P”读为“f”/“F”或将“n”/“N”读为“l”时存在缺陷(如人类的语音缺陷) /'L' 反之亦然

创建一个带有指针变量的类,该变量将纠正 input.txt 中每个单词的拼写,并将它们打印到 output.txt 中。

类头文件:

#pragma once
#include<iostream>
#include<fstream>
#include<string>

using namespace std;
class keyfix
{
    char* input;
    char* output;
    string* word;
    char* wordarray[10];

public:

    keyfix();
    ~keyfix();
    void readfile(ifstream&);
    void correctspelling(ofstream&);
    void outputfile();
    void print()const;


};

类实现文件:

#include "keyfix.h"

keyfix::keyfix() {
    input = new char;
    output = new char;
    *input = 'o';
    *output = 'o';
    word = new string;
    *word = "empty";
    *wordarray = new char;
}

keyfix::~keyfix() {
    delete input;
    delete output;
    delete word;

    input = output = NULL;
    word = NULL;

}

void keyfix::readfile(ifstream& a) {
    for (int i = 0; i < 10; i++) {
        a >> wordarray[i];
    }


}

我在 main 中有一个 ifstream 对象和一个 ofstream 对象。

在我看来,我计划使用字符数组而不是字符串,这样我就可以更改每个不正确的字母,而无需使用我们尚未涵盖的函数,就像<algorithms>. 我在这里真的需要一些方向。我相信你们中的一些人可以查看我的代码并告诉我,但是每当我尝试测试读取文件功能时,我都会收到一个 nullptr 错误。

编辑:我现在用 wordarray 作为纯字符数组编写它。这种方式可能会被接受,也可能不会。此外,大小为 10,因为提供了文件,并且最大的单词只有 10 个字母。

标签: c++arrays

解决方案


按照评论中的建议,我编写了没有指针的程序,然后在我的类中将变量更改为指针类型。这是任何碰巧遇到这个确切问题的未来学生的最终项目。(我在 chegg 上看到了这个问题,所以我知道他经常使用它)

#pragma once
#include<iostream>
#include<fstream>
#include<string>

using namespace std;
class keyfix
{
    char* input;
    char wordarray[10];

public:

    keyfix();
    ~keyfix();
    void readfile(ifstream&);
    void correctspelling(ofstream&);
    void print()const;


};

#include "keyfix.h"

keyfix::keyfix() {
    input = new char;
    *input = 'o';
    wordarray[10];
}

keyfix::~keyfix() {
    delete input;


    input = NULL;

}

void keyfix::readfile(ifstream& a) {
    for (int i = 0; i < 10; i++) {
        wordarray[i] = NULL;
    }
    a >> wordarray;


}

void keyfix::correctspelling(ofstream& b) {
    for (int index = 0; index < 10; index++) {
        *input = wordarray[index];
        if (*input == 'f') {
            wordarray[index] = 'p';
        }
        else if (*input == 'F') {
            wordarray[index] = 'P';
        }
        else if (*input == 'p') {
            wordarray[index] = 'f';
        }
        else if (*input == 'P') {
            wordarray[index] = 'F';
        }
        else if (*input == 'n') {
            wordarray[index] = 'l';
        }
        else if (*input == 'N') {
            wordarray[index] = 'L';
        }
        else if (*input == 'l') {
            wordarray[index] = 'n';
        }
        else if (*input == 'L') {
            wordarray[index] = 'N';
        }
        b << wordarray[index];
    }
    b << endl;


}

void keyfix::print()const {
    for (int i = 0; i < 10; i++) {
        cout << wordarray[i];
    }
    cout << endl;

}
#include"keyfix.h"

//if input is f or F, output p or P
//if input is n or N, output l or L
//if input is p or P, output f or F
//if input is l or L, output n or N


int main() {

    ifstream infile;
    infile.open("input.txt");
    ofstream outfile;
    outfile.open("output.txt");

    keyfix object;
    for (int i = 0; i < 5; i++) {
        object.readfile(infile);
        object.correctspelling(outfile);
        object.print();
    }


}

推荐阅读