#include<iostream>
#include<vector>
#include<fstream>
#include<sstream>
using namespace std;
class StrHandler
{
protected:
vector<char>storage_ch;
public:
StrHandler() {}
virtual ~StrHandler() {}
virtual void function(const string& filename) {};
};
class PrintStr :
public StrHandler
{
public:
PrintStr() {}
void function(const string& filename)
{
cout << ifstream(filename).rdbuf() << endl;
}
};
class GetMostStr :
public StrHandler
{
public:
GetMostStr() {}
virtual ~GetMostStr() {}
void function(const string& filename)
{
ifstream ifs(filename);
string longest, temp;
while (ifs >> temp)
{
if (temp.length() > longest.length())
{
longest = temp;
}
}
cout << "包含字符数最多的字符串是" << longest << endl;
}
};
class Transform :
public StrHandler
{
public:
Transform() {}
virtual ~Transform() {}
void function(const string& filename)
{
ifstream ifs(filename);
while (!ifs.eof())
{
char ch = ifs.get();
if (ch >= 'A' && ch <= 'Z')
{
storage_ch.push_back(ch - ('A' - 'a'));
}
else
{
storage_ch.push_back((ch));
}
}
cout << "转换完成" << endl;
cout << "我们进行测验是否真的全部转换成了小写字符" << endl;
for (int i = 0; i < storage_ch.size(); i++)
{
cout << storage_ch[i];
}
}
};
void readStrings(const string& file, StrHandler* p)
{
p->function(file);
}
int main()
{
string file = "C:\\Users\\4399\\test2.txt";
PrintStr pstr;
cout << "接下来进行转换操作" << endl;
readStrings(file, &pstr);
GetMostStr gmstr;
readStrings(file, &gmstr);
Transform tran;
cout << "接下来进行转换大小写操作" << endl;
readStrings(file, &tran);
return 0;
}
实验E13:虚函数与运行时多态
最新推荐文章于 2021-12-05 17:53:49 发布