我在写正则表达式的时候,写道验证的程序test的时候在没有报错的情况下没办法继续运行了,能帮忙看看吗?
#include <iostream>
#include <vector>
#include <stack>
#include <variant>
#include <regex>
#include <string>
using namespace std;
struct Net
{
string name = "";
int element = 0;
};
struct Position
{
int x = 0;
int y = 0;
};
class Bus
{
public:
/*
void displaystart()
{
cout << "x:" << start.x << " " << "y:" << start.y << endl;
}
void displayend()
{
cout << "x:" << end.x << " " << "y:" << end.y << endl;
}
static void Printdata(? , int indentLevel)
{
}
*/
Bus(string name, Position start, Position end, const vector<Net>& nets, int width);
//这里只需要一个bus的数据,剩下重复内容用向量
//有个问题,应该把向量nets作为类的输入
~Bus();
//封装后的访问函数:
//不封装了
string current_name;
Position current_start;
Position current_end;
vector<Net> current_nets;
int current_width;
private:
};
//构造函数
Bus::Bus(string name, Position start, Position end, const vector<Net>& nets, int width) : current_name(name),current_start(start),current_end(end),current_nets(nets),current_width(width) {}
//析构函数
Bus::~Bus()
{
}
void Search_catch_Bus(string str,vector<Bus>& Bus_List_)
{
smatch result;
regex reg("[\\(\\)]");
sregex_token_iterator pos1(str.begin(), str.end(), reg, -1);
sregex_token_iterator end1;
regex _Bus("Bus[0-9]+");
regex _start("start");
regex _end("end");
regex _x("x");
regex _y("y");
regex _width("width");
regex _nets("nets");
regex _nets1("nets[0-9]+");
regex _num("[0-9]+");
string Bus_Name = "";
bool Position1 = 0;
bool Position2 = 0;
while (pos1 != end1)
{
Position start_;
Position end_;
string Bus_Name_="";
int width_ = 0;
vector<Net> nets_;
//构造函数没有默认的形式
smatch result;
if (Bus_Name != "Bus0" && regex_match(pos1->str(), _Bus))
{
Bus Bus_(Bus_Name, start_, end_, nets_, width_);
Bus_List_.push_back(Bus_);
}
if (regex_match(pos1->str(), _Bus))
{
Bus_Name = pos1->str();
}
if (regex_match(pos1->str(), _start))
{
Position1 = 1;
Position2 = 0;
}
if (regex_match(pos1->str(), _end))
{
Position1 = 0;
Position2 = 1;
}
//以上写入了x和y
if (regex_match(pos1->str(), _x))
{
while (!regex_match(pos1->str(), _num))
{
pos1++;
}
if (Position1)
{
start_.x = stoi(pos1->str());
}
else
{
end_.x = stoi(pos1->str());
}
}
if (regex_match(pos1->str(), _y))
{
while (!regex_match(pos1->str(), _num))
{
pos1++;
}
if (Position1)
{
start_.y = stoi(pos1->str());
}
else
{
end_.y = stoi(pos1->str());
}
}
if (regex_match(pos1->str(), _width))
{
while (!regex_match(pos1->str(), _num))
{
pos1++;
}
width_ = stoi(pos1->str());
}
//vector有element和name
Net current_net;
if (regex_match(pos1->str(), _nets))
{
nets_.clear();
}
if (regex_match(pos1->str(), _nets1))
{
current_net.name = pos1->str();
while (!regex_match(pos1->str(), _num))
{
pos1++;
}
current_net.element = stoi(pos1->str());
nets_.push_back(current_net);
}
//现在已经载入所有数据
//把他们一起放到一个容器中
//完成!
pos1++;
}
}
class Test
{
public:
static void Get_Bus(const vector<Bus>& vec)
{
string choice1;
cout << "请输入想要查找总线(Bus?):" << endl;
cin >> choice1;
regex reg("\\d+");
smatch result;
if (regex_search(choice1, result, reg))
{//stoi(result.str())=数字
string choice2;
int answer = stoi(result.str());
cout << "start:" << "(" << vec[answer].current_start.x << "," << vec[answer].current_start.y << ")" << endl;
cout << "end:" << "(" << vec[answer].current_end.x << "," << vec[answer].current_end.y << ")" << endl;
cout << "width:" << vec[answer].current_width << endl;
cout << "请输入想要查找的线(net?):" << endl;
cin >> choice2;
smatch result2;
if (regex_search(choice2, result2, reg))
{
int answer2 = stoi(result2.str());
cout << "数值为:" << vec[answer].current_nets[answer2].element << endl;
}
}
}
};
int main()
{
/*
string expression = "(Bus (Bus0 (start (x 15) (y 13)) (end (x 63) (y 24)) (width 134) (nets (net0 12) (net1 46))) (Bus1 (start (x 19) (y 13)) (end (x 63) (y 24)) (width 134) (nets (net0 35))))";
//如果每个需要的元素能做出一个结构体呢?
//之后实现在bus0的容器下的nets!!
vector<Net> My_Nets;
My_Nets.push_back({ "net0",16 });
My_Nets.push_back({ "net1",16 });
My_Nets.push_back({ "net2",13 });
//注意:读取统一格式为:const auto&
cout << My_Nets[0].name << "," << My_Nets[0].element << endl;
//数据读取方面已经可以用了
// 1.接下来学习类中的构造,析构等函数的规范
vector<Bus> Bus_List;
Position My_Start;
string My_Name = "Bus0";
My_Start.x = 15;
My_Start.y = 16;
Position My_End;
My_End.x = 18;
My_End.y = 29;
int My_Width = 16;
Bus My_Bus( My_Name,My_Start, My_End, My_Nets, My_Width);
//接下来是验证:
//还是选用封装好的函数吧
Bus_List.push_back(My_Bus);
Position start = Bus_List[0].getStart();
cout << start.x << endl;
vector<Net> check_nets = Bus_List[0].getNets();
cout << check_nets[0].name << "," << check_nets[0].element << endl;
//解决!!
//现在来让我们看看怎么读取字符串?
*/
string get = "(Bus( Bus0(start(x 15)(y 13))(end(x 63)(y 24))(width 134)(nets (net0 12)(net1 46) ) ) ( Bus1(start(x 19)(y 13))(end(x 63)(y 24))(width 134)(nets (net0 35)) ) )";
vector <Bus> Bus_List;
Search_catch_Bus(get,Bus_List);
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/49252937b549487ba542a12b6bd9a52d.png "#left")
//这一步先省略了4个不同字符串分开的操作
//接下来看怎么填入数据
Test::Get_Bus(Bus_List);
cout << "\n" << endl;
system("pause");
return 0;
}
错误是这样的: