#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stack>
#define maxsize 100
#define infinity 1024
using namespace std;
class Network {
private:
char** name;
int** map;
int** path;
int n=0;
public:
int getN() {
return this->n;
}
Network() {
//memset(name, 0, maxsize);
this->n = 0;
this->map = NULL;
this->path = NULL;
}
Network(int n) {
if(this->n==0)this->n = n;
this->map = new int* [this->n];
this->path = new int* [this->n];
for (int i = 0; i < this->n; i++) {
this->map[i] = new int[this->n];
this->path[i] = new int[this->n];
memset(this->map[i], infinity, this->n);
memset(this->path[i], -1, this->n);
}
}
void show() {
for (int i = 0; i < this->n; i++) {
printf("%d:%s ",i, this->name[i]);
}
printf("\n");
}
void filereader(const char* name) {
FILE* file = fopen(name, "r");
if (!file) {
printf("文件不存在..");
exit(-1);
}
fscanf(file, "%d", &this->n);
this->name = new char* [this->n];
this->map = new int* [this->n];
this->path = new int* [this->n];
for (int i = 0; i < this->n; i++) {
this->name[i] = new char[maxsize];
this->map[i] = new int[this->n];
this->path[i] = new int[this->n];
for (int j = 0; j < this->n; j++) {
this->map[i][j] = infinity;
this->path[i][j] = 0;
}
this->map[i][i] = 0;
}
for (int i = 0; i < this->n; i++)
fscanf(file, "%s", this->name[i]);
char src[maxsize]={0};
char dst[maxsize]={0};
int affair=0;
while ((fscanf(file, "%s%s%d", src, dst, &affair)) != EOF) {
int src_i = 0;
int dst_i = 0;
for (int i = 0; i < this->n; i++) {
if (!strcmp(src, this->name[i]))
src_i = i;
if (!strcmp(dst, this->name[i]))
dst_i = i;
}
map[src_i][dst_i] = map[dst_i][src_i] = affair;
}
for (int i = 0; i < this->n; i++) {
for (int j = 0; j < this->n; j++) {
if (this->map[i][j] < infinity) {
this->path[i][j] = i;
}
}
}
this->floyd();
}
void floyd() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (map[j][k] > map[j][i] + map[i][k]) {
map[j][k] = map[j][i] + map[i][k];
path[j][k] = i;
}
}
}
}
//this->printpath(1, 1);
}
void printpath(int src, int dst) {
/*for (int i = 0; i < this->n; i++) {
for (int j = 0; j < this->n; j++) {
printf("%d ", this->map[i][j]);
}
printf("\n");
}
printf("\n\n");
for (int i = 0; i < this->n; i++) {
for (int j = 0; j < this->n; j++) {
printf("%d ", this->path[i][j]);
}
printf("\n");
}*/
stack<int>* myStack = new stack<int>;
do {
myStack->push(dst);
dst = this->path[src][dst];
} while (src != dst);
myStack->push(dst);
int sum = 0;
while (!myStack->empty()) {
if (myStack->size() > 1) {
int temp = myStack->top();
printf("%10s --", this->name[temp]);
myStack->pop();
sum += this->map[myStack->top()][temp];
printf("%d-->", this->map[myStack->top()][temp]);
}
else {
printf("%10s\n", this->name[myStack->top()]);
myStack->pop();
}
}
printf("\nsum payment is %d.", sum);
getchar();
getchar();
}
};
class System {
private:
Network* traffic;
int choice;// 1 最小中转 2 最小花费 3 最低路程
int Transportation;// 1 火车 2 高铁 3 飞机
int destination, source;
bool back = false;
public:
System() {
this->choice = -1;
this->Transportation = 0;
this->destination = this->source = -1;
traffic = new Network[9];
for (int i = 0; i < 9; i++) {
char first = '1' + i / 3;
char second = '1' + i % 3;
char filepath[8] = { 0 };
filepath[0] = first;
filepath[1] = '/';
filepath[2] = second;
filepath[3] = '.';
filepath[4] = 't';
filepath[5] = 'x';
filepath[6] = 't';
filepath[7] = 0;
traffic[i].filereader(filepath);
/*traffic[i].filereader("1/1.txt");*/
}
}
void getChoice() {
system("cls");
while (this->choice < 1 || this->choice>3) {
cout << "*****************************" << endl;
cout << "* 1.最小中转 *" << endl;
cout << "* 2.最小花费 *" << endl;
cout << "* 3.最小路程 *" << endl;
cout << "* 0.退出系统 *" << endl;
cout << "*****************************" << endl;
cout << "你的选择>";
cin >> this->choice;
system("cls");
if (this->choice == 0) {
cout << "欢迎下次使用~" << endl;
exit(0);
}
}
}
void getTranspartation() {
system("cls");
while (this->Transportation < 1 || this->Transportation>3) {
cout << "**************************" << endl;
cout << "* 1.火车 *" << endl;
cout << "* 2.高铁 *" << endl;
cout << "* 3.飞机 *" << endl;
cout << "* 0.返回 *" << endl;
cout << "**************************" << endl;
cout << "你的选择>";
cin >> this->Transportation;
system("cls");
if (this->Transportation == 0) {
this->back = true;
return;
}
}
}
void srcanddst(int n) {
while (true) {
cout << "输入起始地点>";
cin >> this->source;
cout << "输入目的地点>";
cin >> this->destination;
if (this->source >= 0 && this->destination >= 0 && this->source < n && this->destination < n) {
break;
}
else if (this->source == -1||this->destination==-1) {
break;
}
else {
system("cls");
printf("错误输入,请重新输入..");
}
}
}
void running() {
while (true) {
this->getChoice();
this->getTranspartation();
if (this->back) {
this->back = false;
this->choice = 0;
continue;
}
this->traffic[this->Transportation + 3 * (this->choice - 1) - 1].show();
this->srcanddst(this->traffic[this->Transportation + 3 * (this->choice - 1) - 1].getN());
if (this->source == -1||this->destination==-1) {
this->choice = -1;
this->Transportation = -1;
continue;
}
this->traffic[this->Transportation+3*(this->choice-1)-1].printpath(this->source, this->destination);
}
}
};
int main(void) {
System* sys = new System();
sys->running();
return 0;
}
基于Floyd的C++路径选择系统
最新推荐文章于 2024-05-20 22:11:41 发布