【发布时间】:2023-03-21 05:03:01
【问题描述】:
我不能制作从文件中读取具有相同第一个字母和最后一个字母的单词(单词或长度没有限制)的程序。我使用类和对象,首先我无法阅读它们
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class Texts{
public:
void Realding(string &All);
void Searching(string &text, char *Word);
};
int main()
{
Texts A;
string Text; char word[40];
A.Reading(Text);
A.Searching(Text, word);
system("PAUSE");
}
void Texts::Reading(string &All)
{
string temp;
ifstream read("Text.txt");
while (getline(read, temp)) { All += temp; All += "\n"; }
cout << All;
}
void Texts::Searching(string &text, char *Word)
{
int i = 0;
int j = 0;
int letters = 0;
int zodz = 0;
int index = 0;
while (1)
{
if (text[i] == ' ' || text[i] == '\0')
{
zodz++;
if (text[0] == text[i - 1])
{
letters = j;
for (int l = 0; l < letters; l++)
{
//cout << text[l];
}
j = 0;
}
if (text[i + 1 - j] == text[i - 1])
{
letters = j;
for (int l = i - j; l < letters; l++)
{
// cout << text[l];
}
j = 0;
}
}
if (text[i] == '\0') break;
else
i++;
j++;
}
}
我无法从文件中正确读取它... Text.txt 看起来像
asdfa su egze hah ktis faf
以及如何稍后将具有第一个和最后一个相同字母的选定单词分配给数组,然后按字母顺序对它们进行排序。如果有人帮助我,谢谢。
【问题讨论】:
-
是否需要使用类?我建议您在
main函数或其他独立函数中执行此操作。它可以帮助你。 -
这段代码无法编译;我怀疑这是你真正的代码。如果您不向我们展示您的真实代码,我们很难为您提供帮助。
-
你应该一次处理一个单词;不要将它们全部读入一个字符串。
-
有要求。哦,对不起,是的,有“阅读”和“搜索”功能。这是真实的代码。我会改变功能