std::cout << "输入起点城市:" << endl; cin >> start; std::cout << "输入终点城市:" << endl; cin >> end; for (int i = 0; i < graph.vexnum; i++) { if (cities[i].city == start) s = i; if (cities[i].city == end) e = i; } //最短路径 system("cls"); std::cout << "读取完成" << endl; std::cout << "请选择最短路径要求" << endl; std::cout << "时间:1 " << endl; std::cout << "价格:2 " << endl; cin >> p; system("cls"); switch (p) { case 1: std::cout << "以时间为基准:" << endl; TimeShortPath(graph, s, way, D); for (int i = 0; i < 199; ++i) { if (way[e][i] == n) { c[n - 1] = i; std::cout << cities[i].city << ' '; n++; i = -1; } } std::cout << std::endl; std::cout << "时间:" << D[e] << " h "; TxtWriting(graph, c, n); break; case 2: std::cout << "以价格为基准:" << endl; CostShortPath(graph, s, way, D); for (int i = 0; i < 199; ++i) { if (way[e][i] == n) { c[n - 1] = i; std::cout << cities[i].city << ' '; n++; i = -1; } } std::cout << std::endl; std::cout << "价格:$" << D[e]; TxtWriting(graph, c, n); break; } return 0; } //构造控制台
时间: 2024-04-27 16:22:49 浏览: 15
这段代码是控制台程序的核心部分,主要包括以下内容:
1. 通过 std::cout 输出提示信息,让用户输入起点城市和终点城市。
2. 通过循环遍历 cities 数组,找到起点城市和终点城市在数组中的下标。
3. 通过 switch 语句让用户选择最短路径的要求,即以时间为基准还是以价格为基准。
4. 根据用户的选择调用 TimeShortPath 函数或 CostShortPath 函数计算最短路径,并将结果存储在 way 和 D 变量中。
5. 通过循环遍历 way 数组,找到从起点城市到终点城市的最短路径,并将路径上经过的城市存储在 c 数组中。
6. 输出最短路径,包括经过的城市和路径总时间或总价格。
7. 调用 TxtWriting 函数将结果写入文件。
8. 返回 0 表示程序正常结束。
相关问题
#include <iostream> #include <vector> using namespace std; // 定义边结构体 struct Edge { int dest; // 目标顶点 int weight; // 边的权值 Edge(int d, int w): dest(d), weight(w) {} }; // 定义邻接表节点结构体 struct Node { int vertex; // 顶点编号 vector<Edge> edges; // 与该顶点相连的边 Node(int v): vertex(v) {} }; // 邻接表表示法构造无向网G vector<Node> buildGraph() { vector<Node> graph; int numNodes, numEdges; cout << "请输入顶点数和边数:"; cin >> numNodes >> numEdges; graph.resize(numNodes); cout << "请输入每条边的起点、终点和权值:" << endl; for (int i = 0; i < numEdges; ++i) { int start, dest, weight; cin >> start >> dest >> weight; graph[start].edges.emplace_back(dest, weight); graph[dest].edges.emplace_back(start, weight); } return graph; } // 递归地深度优先遍历该图G void dfs(vector<Node>& graph, vector<bool>& visited, int current) { visited[current] = true; cout << current << " "; for (Edge& e : graph[current].edges) { int next = e.dest; if (!visited[next]) { dfs(graph, visited, next); } } } int main() { vector<Node> graph = buildGraph(); vector<bool> visited(graph.size(), false); int start; cout << "请输入起始顶点编号:"; cin >> start; dfs(graph, visited, start); cout << endl; return 0; }运行一下这段代码
当你运行这段代码时,程序会提示你输入顶点数和边数,你需要输入两个整数。然后程序会提示你输入每条边的起点、终点和权值,你需要按照提示输入每条边的起点、终点和权值,其中起点和终点都是整数,权值可以是整数或浮点数。输入完所有边后,程序会要求你输入起始顶点编号,你需要输入一个整数。程序会从该顶点开始递归地深度优先遍历整个图,并输出遍历结果,每个遍历到的顶点编号后面会跟一个空格。最后程序会输出一个换行符,表示遍历结束。
编写一个求出租车收费的程序,输入起始站、终止站和路程。 计费标准为:3 公里以内10元,3 公里以后每1公里加 2 元,超过 15 公里,每公里加 3元。 要求:设计一个站类Station(用于设置起始站、终止站)和路程类Mile(用于设置路程),由这两个类派生出收费类Price(用于计费)。 已知Station类和Mile类以及测试main函数如下,请编写Price类。 (可以使用string类型代替给出代码中的字符数组。) #define Max 20 class Station {public: Station() { strcpy(from," "); strcpy(to," "); } Station(char f[],char t[]) { strcpy(from,f); strcpy(to,t); } void getdata() { cout<<"输入起始站终止站:"; cin>>from>>to; } void disp() { cout<<"从"<<from<<"站到"<<to<<"站"; } protected: char from[Max]; char to[Max]; }; class Mile { public: Mile(){mile=0;} Mile(double m){mile=m;} void getdata() { cout<<"输入里程:"; cin>>mile; } void disp() { cout<<"是"<<mile<<"公里"; } protected: double mile; }; int main() { Price A; //乘车, A.getdata(); //输入起始站和终点站 Price B("理工大学","中山公园",20); //乘车,有起始站和终点站 //A和B是Price的两种初始化方式。20是路程数。 cout<<"输出结果:"<<endl; A.disp(); //输出此次乘车价格 B.disp(); //输出此次乘车价格 return 0; }
Price类的代码如下:
#include <iostream>
#include <string>
using namespace std;
class Station {
protected:
string start; // 起始站
string end; // 终止站
public:
Station(string s, string e) {
start = s;
end = e;
}
};
class Mile {
protected:
double distance; // 路程
public:
Mile(double d) {
distance = d;
}
};
class Price : public Station, public Mile {
public:
Price(string s, string e, double d) : Station(s, e), Mile(d) {}
double getCharge() {
double charge;
if (distance <= 3) {
charge = 10;
} else if (distance <= 15) {
charge = 10 + (distance - 3) * 2;
} else {
charge = 10 + 12 * 2 + (distance - 15) * 3;
}
return charge;
}
};
int main() {
string start, end;
double distance;
cout << "请输入起始站:";
cin >> start;
cout << "请输入终止站:";
cin >> end;
cout << "请输入路程:";
cin >> distance;
Price p(start, end, distance);
cout << "收费:" << p.getCharge() << "元" << endl;
return 0;
}