Skip to content

Commit

Permalink
有bug的错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
chaibo committed Mar 21, 2020
1 parent fda5ae5 commit 6e7d48c
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 44 deletions.
63 changes: 57 additions & 6 deletions src/Personal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,86 @@
#include"geomtry_component.h"
#include"table.h"
#include <random>
#include <regex>
#include <string>
#include <sstream>
#include "error.h"

using namespace std;

int main(int argc, char** argv)
{
ifstream infile;
ofstream outfile;
string getLine;
int n;
if (!strcmp(argv[1], "-i") && !strcmp(argv[3], "-o"))
{
infile = ifstream(argv[2]);
outfile = ofstream(argv[4]);
if (!(infile.is_open()))
{
cout << "open file fail" << endl;
exit(0);
}
}
else if (!strcmp(argv[3], "-i") && !strcmp(argv[1], "-o"))
{
infile = ifstream(argv[4]);
outfile = ofstream(argv[2]);
if (!(infile.is_open()))//TODO : is_open or good
{
cout << "open file fail" << endl;
exit(0);
}
}
else
{
exit(0);
}
Table table = Table();
table.insertFromStream(infile);

getline(infile, getLine);
regex reg1("\\s*\\d+\\s*");
if (!regex_match(getLine, reg1)) {
//input n error

cout << "n is out of range" << endl;
}
stringstream stream;
stream << getLine;
stream >> n;
if (!n) {
cout << "n is 0" << endl;
}

for (int i = 0; i < n; i++)
{
try
{
if (!getline(infile, getLine))
{
cout << "n is smaller than the true line num or IO error" << endl;
break;
}

table.insertFromString(getLine);
}
catch (const std::domain_error& de)
{
cout << de.what() << " domain" << endl;
}
catch (Doublication & dl)
{
cout << dl.what() << " dl" << endl;
}
catch (pointDoublication & pdl)
{
cout << pdl.what() << " pointd" << endl;
}
}

outfile << table.getPointNum() << endl;
Circle circle = Circle(Point(0, 0), 1);
table.eraseCircle(circle);
cout << table.getPointNum() << endl;
circle = Circle(Point(2, 0), 1);
table.eraseCircle(circle);
cout << table.getPointNum() << endl;
return 0;
}
Expand Down
29 changes: 29 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef ERROR_H
#define ERROR_H
#include<exception>
#include<stdexcept>
using namespace std;
class Doublication : public exception
{
public:
Doublication() {};
const char* what()
{
return "Component Doublication";
}
private:

};

class pointDoublication : public exception
{
public:
pointDoublication() {};
const char* what()
{
return "point Doublication";
}
private:

};
#endif // !ERROR_H
106 changes: 71 additions & 35 deletions src/table.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include"table.h"

#include<regex>
#include <sstream>
#include "error.h"
Table::Table()
{
n = 0;
Expand Down Expand Up @@ -32,6 +34,11 @@ size_t Table::getPointNum()
return pointSet.size();
}

vector<exception>& Table::getExceptions()
{
return exceptVector;
}

void Table::eraseLine(Line* l)
{

Expand Down Expand Up @@ -81,6 +88,10 @@ void Table::updatePointSet()
void Table::insertLine(Line& l)
{
for (auto temp : lineSet) {
if (*temp == l)
{
throw Doublication();
}
temp->GetCrossPoint(pointSet ,&l);
}
for (Circle temp : circleSet) {
Expand All @@ -96,53 +107,78 @@ void Table::insertCircle(Circle& circle)
}
for (Circle temp : circleSet)
{
if (temp == circle)
{
throw Doublication();
}
circle.GetCrossToCircle(pointSet, temp);
}
circleSet.insert(circle);
}

void Table::insertFromStream(ifstream& infile)
void Table::insertFromString(string& inStream)
{
regex reg1("\\s+((L|R|S)\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+\\s+) | (C\\s+\\d+\\s+\\d+\\s+\\d+\\s+)");
regex reg3("\\s*((L|R|S)\\s+(0+|0*[1-9]\\d{0,4})\\s+(0+|0*[1-9]\\d{0,4})\\s+(0+|0*[1-9]\\d{0,4})\\s+(0+|0*[1-9]\\d{0,4})\\s+)|(C\\s+(0+|0*[1-9]\\d{0,4})\\s+(0+|0*[1-9]\\d{0,4})\\s+(0*[1-9]\\d{0,4}))\\s*");

if (!regex_match(inStream, reg3)) {
//throw domain_error(inStream);
}
stringstream stream;
stream << inStream;
char type;
double x0, x1, y0, y1;
infile >> n;
for (int i = 0; i < n; i++)
int x0, x1, y0, y1;
stream >> type;


switch (type)
{
infile >> type;
switch (type)
{
case 'L':
{
infile >> x0 >> y0 >> x1 >> y1;
Straight* straight = new Straight(x0, y0, x1, y1);
insertLine(*straight);
break;
}
case 'R':
{
infile >> x0 >> y0 >> x1 >> y1;
Ray* ray = new Ray(x0, y0, x1, y1);
insertLine(*ray);
break;
}
case 'S':
case 'L':
{
stream >> x0 >> y0 >> x1 >> y1;
if (Point(x0, y0) == Point(x1, y1))
{
infile >> x0 >> y0 >> x1 >> y1;
Segment* segment = new Segment(x0, y0, x1, y1);
insertLine(*segment);
break;
throw pointDoublication();
}
case 'C':
Straight* straight = new Straight(x0, y0, x1, y1);
insertLine(*straight);
break;
}
case 'R':
{
stream >> x0 >> y0 >> x1 >> y1;
if (Point(x0, y0) == Point(x1, y1))
{
infile >> x0 >> y0 >> x1;
Circle circle(Point(x0, y0), x1);
insertCircle(circle);
break;
throw pointDoublication();
}
default:
Ray* ray = new Ray(x0, y0, x1, y1);
insertLine(*ray);
break;
}
case 'S':
{
stream >> x0 >> y0 >> x1 >> y1;
if (Point(x0, y0) == Point(x1, y1))
{
// dealing the wrong!
}
throw pointDoublication();
}
Segment* segment = new Segment(x0, y0, x1, y1);
insertLine(*segment);
break;
}
case 'C':
{
stream >> x0 >> y0 >> x1;

Circle circle(Point(x0, y0), x1);
insertCircle(circle);
break;
}
default:
{
// dealing the wrong!
cout << "Here is unreachable" << endl;
}
}

}
6 changes: 3 additions & 3 deletions src/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ class Table

void insertLine(Line& l);
void insertCircle(Circle& c);
void insertFromStream(ifstream& infile);
void insertFromString(string& infile);
set<Point>& getPointSet();
set<Line*>& getLineSet();
set<Circle>& getCircleSet();
void eraseLine(Line* l);
void eraseCircle(Circle& c);
size_t getPointNum();

vector<exception>& getExceptions();
private:
int n;
set<Point> pointSet;
set<Line*> lineSet;
set<Circle> circleSet;

vector<exception> exceptVector;
void updatePointSet();
};

Expand Down

0 comments on commit 6e7d48c

Please sign in to comment.