#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <string>
using namespace std;
class CacheStack
{
private:
int capacity; // max length
int cur_length; // length
string file_path; //cache file
vector<string> readFromFile();
void writeToFile(vector<string>);
void add_node(string); // add node to file
void del_last_node(); // delete last node
void del_first_node(); // delete first node
string get_last_node(); // get the last node
public:
CacheStack(int, string);
bool empty(); // cache empty
int size(); // max_length
int length(); // current length
string pop(); // pop and delete last node
int push(string); // push node
};
CacheStack::CacheStack(int max_len = 10, string file_path = "D:/msd_reissue")
:capacity(max_len), file_path(file_path)
{
//init :chech file
fstream fs;
fs.open(file_path, ios::in);
if (!fs.is_open())
{
// file not exists
// create file
fs.open(file_path, ios::out);
fs << 0; // write node number to file
}
fs >> cur_length;
fs.close();
}
vector<string> CacheStack::readFromFile()
{
vector<string> vect;
string line;
fstream fs;
fs.open(file_path, ios::in);
fs >> cur_length;
// read all node
while (getline(fs, line))
{
if (line != "")
{
vect.push_back(line);
}
}
cur_length = vect.size();
fs.close();
return vect;
}
void CacheStack::writeToFile(vector<string> vect)
{
// clean file
fstream fs;
fs.open(file_path, ios::trunc);
fs.close();
// write vect to file
fs.open(file_path, ios::out);
fs << vect.size() ;
cur_length = vect.size();
for (vector<string>::iterator it = vect.begin(); vect.end() != it; it++)
{
fs << endl<< *it;
}
fs.close();
}
void CacheStack::add_node(string node)
{
vector<string> vect = readFromFile();
vect.push_back(node);
writeToFile(vect);
}
void CacheStack::del_first_node()
{
vector<string> vect = readFromFile();
vect.erase(vect.begin());
writeToFile(vect);
}
void CacheStack::del_last_node()
{
vector<string> vect = readFromFile();
if (0 != vect.size())
{
vect.erase(vect.end() - 1);
writeToFile(vect);
}
}
string CacheStack::get_last_node()
{
vector<string> vect = readFromFile();
if (0 == vect.size())
{
throw string("stack empty!get last failed!");
}
string back= vect.back();
vect.erase(vect.end() - 1);
writeToFile(vect);
return back;
}
int CacheStack::size()
{
return capacity;
}
int CacheStack::length()
{
return cur_length;
}
bool CacheStack::empty()
{
return (0 == cur_length);
}
int CacheStack::push(string node)
{
if (cur_length == capacity)
{
del_first_node();
}
add_node(node);
return cur_length;
}
string CacheStack::pop()
{
string str="";
try {
str = get_last_node(); // throw error when empty
}
catch (string str) {
cout << "pop failed:stack empty! \n";
}
return str;
}
int main()
{
string file_path = "D:/msd_reissue";
CacheStack cs(10,file_path);
int choose; // input choose
string data; // input data
bool flag = true; // while flag
while (flag)
{
cout << "1:push node\n2:pop node\n3:exit\n";
cout << "Please choose func:";
cin >> choose;
switch (choose)
{
case 1: // push
cout << "Please input node(enter 'exit' to end):\n";
while(true)
{
cin >> data;
if ("exit" == data)
{
break;
}
cs.push(data);
}
break;
case 2: // pop
data = cs.pop();
cout << "pop:" << data << endl;
break;
case 3: // exit
flag = false;
break;
}
}
return 0;
}
看不看的懂123456789
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- YoungGeeker 2022-07-22 10:23关注
所以你想问什么?秀你本身吗?还是看不懂?有注释在上面!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
- ¥20 软件测试决策法疑问求解答
- ¥15 win11 23H2删除推荐的项目,支持注册表等
- ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
- ¥15 qt6.6.3 基于百度云的语音识别 不会改
- ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
- ¥15 神经网络怎么把隐含层变量融合到损失函数中?
- ¥15 lingo18勾选global solver求解使用的算法
- ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
- ¥20 测距传感器数据手册i2c