简单的解码程序C++

Simple decoding program C++

本文关键字:C++ 程序 解码 简单      更新时间:2023-10-16

我正在上C++课程,学年中期我们要做一个模拟cw/练习。它没有标记,只是为了练习。基本上,我们必须进行

(a) 从文件中读取文本并将其内容存储在字符A.(b)计算使用两个平行数组(B和C)的A中的每个字母:一个包含26个字母,另一个包含相应的字母发生的百分比。(c) 使用排序算法(例如,冒泡排序算法)对上述两个并行数组进行排序出现百分比的降序。(d) 应用(b)和(c)到训练和编码文本。存储两组平行数组(用于训练和编码文本)以供进一步使用。(e) 使用上面两组排序的并行数组来查找和显示训练和编码中字母的一对一映射文本。(f) 将编码消息中的字母替换为字母它们表示(g)交互地向用户询问一对字符,将它们存储在两个字符变量中(例如,X和Y),以及将数组中出现的所有字母X替换为字母Y字符。(h) 保存存储在字符数组中的解码文本到文件。(i) 能够重复(f)、(g)和(h)与用户希望。

我们必须先做一个过程代码,然后是面向对象的。

    #include <fstream> //for file I/O
#include <iostream> //for cout, endl
#include <string> //for countletters
using namespace std;
int countletters(/*in*/ int& sum) //counting the number of letters contained in the file
{
    string line;
    ifstream inData ;
    inData.open("encoded.txt");
    while(!inData.eof())
    {
        getline(inData,line);
        int numofChars= line.length();
        for (unsigned int n = 0; n<line.length();n++)
        { 
            if (line.at(n) == ' ')
            {
            numofChars--;
            }
        }
        sum=numofChars+sum;
    }
    inData.close();
    //sum is the number of letters inside the encoded.txt file
}
void fileintoarray(int& sum)
{
  int arraysize = sum;
  char myArray[arraysize];
  char current_char;
  int num_characters = 0;
  int i = 0;
  ifstream myfile ("encoded.txt");
    if (myfile.is_open())
        {
            while (!myfile.eof())
            {
                myfile >> myArray[i];
                i++;
                num_characters ++;
            }
            for (int i = 0; i <= num_characters; i++)
            {
                cout << myArray[i];
            }
            system("pause");
    }
}
int main()
{
    int sum=0;
    countletters();
    fileintoarray();
    return 0;
}

这是我到目前为止写的,第二个函数不起作用。它无法编译。

有人能帮我做这个吗?

您正在访问函数"fileintoarray"中的变量"sum",但它不在您在"countletters"中声明的范围内。

学习如何返回值以及如何将参数传递给函数-或者,如果你还没有学会,并且无论如何都应该学会-使用全局变量,因为这有点像面向对象的版本。