简单的解码程序C++
Simple decoding program C++
我正在上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"中声明的范围内。
学习如何返回值以及如何将参数传递给函数-或者,如果你还没有学会,并且无论如何都应该学会-使用全局变量,因为这有点像面向对象的版本。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址