c语言课程找代写有偿

编写程序,任意输入两个站点名称,输出乘坐地铁最少需要经过的车站数量(含输入的起点和终点.换乘站点只计算一次)。输入的站点名必须与线路站点名一致,不一致时提示用户重新输入

img

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NODENUM 50
#define MAXWEIGHT 65535

typedef struct Node {
    int flag;
    int weight;
} Matrix[NODENUM][NODENUM];

typedef struct MGraph {
    int arcnum;
    int vertexnum;
    Matrix adjmatrix;
} MGraph, *pMGraph;
typedef enum {
    A1,
    A2,
    A3,
    A4,
    A5,
    A6,
    A7,
    A8,
    A9,
    T1,
    A10,
    A11,
    A12,
    A13,
    T2,
    A14,
    A15,
    A16,
    A17,
    A18,
    B1,
    B2,
    B3,
    B4,
    B5,
    B6,
    B7,
    B8,
    B9,
    B10,
    B11,
    B12,
    B13,
    B14,
    B15
} index;
char* str[] = {"A1",  "A2",  "A3",  "A4",  "A5",  "A6",  "A7",  "A8",  "A9",
               "T1",  "A10", "A11", "A12", "A13", "T2",  "A14", "A15", "A16",
               "A17", "A18", "B1",  "B2",  "B3",  "B4",  "B5",  "B6",  "B7",
               "B8",  "B9",  "B10", "B11", "B12", "B13", "B14", "B15"};

void CreateSubRailWay(pMGraph mg) {
    int n = sizeof(str) / sizeof(char*);
    mg->vertexnum = n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            mg->adjmatrix[i][j].weight = MAXWEIGHT;
        }
    }
    for (int i = A1; i <= A17; i++) {
        mg->adjmatrix[i][(i + 1)].weight = 1;
        mg->adjmatrix[i + 1][i].weight = 1;
    }
    mg->adjmatrix[A18][A1].weight = 1;
    mg->adjmatrix[A1][A18].weight = 1;

    for (int i = B1; i <= B4; i++) {
        mg->adjmatrix[i][i + 1].weight = 1;
        mg->adjmatrix[i + 1][i].weight = 1;
    }
    mg->adjmatrix[B5][T1].weight = 1;
    mg->adjmatrix[T1][B5].weight = 1;
    mg->adjmatrix[T1][B6].weight = 1;
    mg->adjmatrix[B6][T1].weight = 1;
    for (int i = B6; i <= B9; i++) {
        mg->adjmatrix[i][i + 1].weight = 1;
        mg->adjmatrix[i + 1][i].weight = 1;
    }
    mg->adjmatrix[B10][T2].weight = 1;
    mg->adjmatrix[T2][B10].weight = 1;
    mg->adjmatrix[T2][B11].weight = 1;
    mg->adjmatrix[B11][T2].weight = 1;
    for (int i = B11; i <= B14; i++) {
        mg->adjmatrix[i][i + 1].weight = 1;
        mg->adjmatrix[i + 1][i].weight = 1;
    }
}

int Path[NODENUM];
int ShortestPath[NODENUM];
int used_dij[NODENUM];

int Dijk(pMGraph mg, int startpos) {
    int n = mg->vertexnum;
    for (int i = 0; i < n; i++) {
        Path[i] = startpos;
        ShortestPath[i] = mg->adjmatrix[startpos][i].weight;
        used_dij[i] = 0;
    }
    ShortestPath[startpos] = 0;
    used_dij[startpos] = 1;
    Path[startpos] = startpos;
    for (int i = 1; i < n; i++) {
        int min = MAXWEIGHT;
        int k;
        for (int j = 0; j < n; j++) {
            if (used_dij[j] != 1 && min > ShortestPath[j]) {
                min = ShortestPath[j];
                k = j;
            }
        }
        used_dij[k] = 1;
        for (int j = 0; j < n; j++) {
            if (used_dij[j] == 0 &&
                mg->adjmatrix[k][j].weight + min < ShortestPath[j]) {
                ShortestPath[j] = min + mg->adjmatrix[k][j].weight;
                Path[j] = k;
            }
        }
    }
    return 0;
}

int main(int argc, char* argv[]) {
    pMGraph mg = (pMGraph)malloc(sizeof(MGraph));
    CreateSubRailWay(mg);
    printf("输入:\n");
    char start[10], end[10];
    scanf("%s", start);
    scanf("%s", end);
    int startpos, endpos;
    for (int i = 0; i < sizeof(str) / sizeof(char*); i++) {
        if (strcmp(start, str[i]) == 0) {
            startpos = i;
        }
        if (strcmp(end, str[i]) == 0) {
            endpos = i;
        }
    }
    Dijk(mg, startpos);
    printf("站数为%d\n", ShortestPath[endpos] + 1);
    int len = sizeof(str) / sizeof(char*);
    int sitecount = 0;
    int sites[sizeof(str) / sizeof(char*)];
    int now = endpos;
    while (now != startpos) {
        sites[sitecount++] = now;
        now = Path[now];
    }
    sites[sitecount] = now;
    printf("路线为");
    for (int i = 0; i <= sitecount; i++) {
        if (i <= sitecount - 1) {
            printf("%s->", str[sites[sitecount - i]]);
        }else{
             printf("%s\n", str[sites[sitecount - i]]);
        }
    }
    return 0;
}

运行结果:

img

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7614113
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:c语言:输入两个整形数并打印,如果用户不慎输入了非法字符,那么程序提示“输入数据类型错误”。
  • 除此之外, 这篇博客: <栈和队列>题目练习《数据结构(C语言版)》中的 5.用无头单链表存储队列,其头指针指向队头结点,尾指针指向队尾结点,则在进行出队操作时( ) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • A.仅修改队头指针

    B.队头、队尾指针都要修改

    C.队头、队尾指针都可能要修改

    D.仅修改队尾指针

  • 您还可以看一下 钟洪发老师的鸿蒙设备开发之C知识点全面梳理课程中的 实现插入文件内容小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    我可以帮你完成这个任务。首先,你需要构建一个地铁网络的数据结构来表示所有的站点和线路,并且需要实现一个函数来计算最短路径。下面是一个可能的解决方案:

    1. 定义地铁网络的数据结构:
    #define MAX_STATIONS 100
    
    typedef struct {
        char name[50];  // 站点名称
    } Station;
    
    typedef struct {
        int start;  // 起始站点索引
        int end;    // 终点站点索引
        int stations[MAX_STATIONS];  // 地铁经过的站点索引
        int count;  // 经过的站点数量
    } Path;
    
    typedef struct {
        Station stations[MAX_STATIONS];  // 所有站点
        int numStations;  // 站点数量
        int adjacencyMatrix[MAX_STATIONS][MAX_STATIONS];  // 邻接矩阵表示线路
    } SubwayNetwork;
    
    1. 实现函数来初始化地铁网络:
    void initSubwayNetwork(SubwayNetwork* network) {
        // 初始化站点数量为0
        network->numStations = 0;
        // 初始化邻接矩阵
        for (int i = 0; i < MAX_STATIONS; i++) {
            for (int j = 0; j < MAX_STATIONS; j++) {
                network->adjacencyMatrix[i][j] = 0;
            }
        }
    }
    
    void addStation(SubwayNetwork* network, const char* name) {
        // 检查是否已经达到最大站点数量
        if (network->numStations >= MAX_STATIONS) {
            printf("站点数量达到最大限制\n");
            return;
        }
        // 添加新的站点
        strcpy(network->stations[network->numStations].name, name);
        network->numStations++;
    }
    
    void addConnection(SubwayNetwork* network, int station1, int station2) {
        network->adjacencyMatrix[station1][station2] = 1;
        network->adjacencyMatrix[station2][station1] = 1;
    }
    
    1. 实现函数来计算最短路径:
    void findShortestPath(SubwayNetwork* network, const char* startStation, const char* endStation, Path* shortestPath) {
        int start = -1;
        int end = -1;
    
        // 查找起点和终点的索引
        for (int i = 0; i < network->numStations; i++) {
            if (strcmp(network->stations[i].name, startStation) == 0) {
                start = i;
            }
            if (strcmp(network->stations[i].name, endStation) == 0) {
                end = i;
            }
        }
    
        // 如果没找到站点,提醒用户重新输入
        if (start == -1 || end == -1) {
            printf("输入的站点名与线路中的站点名不一致\n");
            return;
        }
    
        // 使用广度优先搜索算法找到最短路径
        int visited[MAX_STATIONS] = {0};  // 标记是否已经访问过
        int queue[MAX_STATIONS] = {0};    // 存储待访问的站点
        int prev[MAX_STATIONS] = {-1};    // 记录访问路径
        int rear = 0;                     // 队列尾
        int front = 0;                    // 队列头
        int found = 0;                    // 是否找到终点
    
        visited[start] = 1;
        queue[rear++] = start;
    
        while (front < rear) {
            int current = queue[front++];
            if (current == end) {
                found = 1;  // 找到终点
                break;
            }
            for (int i = 0; i < network->numStations; i++) {
                if (network->adjacencyMatrix[current][i] == 1 && visited[i] == 0) {
                    visited[i] = 1;
                    queue[rear++] = i;
                    prev[i] = current;
                }
            }
        }
    
        // 根据访问路径生成最短路径
        shortestPath->count = 0;
        int current = end;
        while (current != -1) {
            shortestPath->stations[shortestPath->count++] = current;  // 存储路径上的站点
            current = prev[current];
        }
        shortestPath->start = start;
        shortestPath->end = end;
    }
    
    void printPath(SubwayNetwork* network, const Path* path) {
        printf("从 %s 到 %s 的最短路径(包括换乘站点):\n", network->stations[path->start].name, network->stations[path->end].name);
        for (int i = path->count - 1; i >= 0; i--) {
            printf("%s ", network->stations[path->stations[i]].name);
        }
        printf("\n");
    }
    
    1. 使用上述代码来解决你的问题:
    int main() {
        // 初始化地铁网络
        SubwayNetwork network;
        initSubwayNetwork(&network);
    
        // 添加站点和线路
        addStation(&network, "站点1");
        addStation(&network, "站点2");
        addStation(&network, "站点3");
        addConnection(&network, 0, 1);
        addConnection(&network, 1, 2);
    
        // 输入起点和终点
        char startStation[50];
        char endStation[50];
        printf("请输入起始站点名:");
        scanf("%s", startStation);
        printf("请输入终点站点名:");
        scanf("%s", endStation);
    
        // 计算最短路径
        Path shortestPath;
        findShortestPath(&network, startStation, endStation, &shortestPath);
    
        // 打印最短路径
        printPath(&network, &shortestPath);
    
        return 0;
    }
    

    这样就完成了一个简单的地铁最短路径计算程序。请注意,上述代码只是一种解决方案,实际上还有很多其他方法来解决这个问题。如果有任何问题,请随时向我提问。

简单写了下,你参考参考
上图

img

代码

#include <iostream>
#include <string>
#include <vector>
using namespace std;

// 定义地铁线路 
vector<string> line1 = {
    "临川路",  
    "北商报站", 
    "书院门站",
    "观音阁站",
    "东大街站",  
    "钟鼓楼站", 
    "崇安门站",
    "东市口站",
    "大观园站",
    "江宁路站", 
    "常熟路站",
    "五塘站",  
};
vector<string> line2 = {
    "金陵大厦",
    "人民广场",
    "建邺学院站",
    "大曹口站",
    "邗江站",    
    "鼓楼站", 
    "丰泽站",    
    "振华站", 
    "钟南山站",
    "珠江路站",
    "华夏高新园区",
    "甪直站"        
};

int main() {
    string start, end;  
    cout << "请输入出发站点:";
    cin >> start;    
    cout << "请输入目的地站点:";    
    cin >> end;
    
    // 检查站点名称是否正确   
    bool valid_start = false, valid_end = false;  
    for (string name : line1) {
        if (name == start) valid_start = true;
        if (name == end) valid_end = true;      
    }      
    for (string name : line2) {
        if (name == start) valid_start = true;
        if (name == end) valid_end = true;        
    } 
    
    // 错误则重新输入      
    while(!valid_start || !valid_end) {
     if(!valid_start) {  
         cout << "出发站点输入错误,请重新输入" << endl;  
         cin >> start;
         valid_start = false;   
     }      
     if (!valid_end){
         cout << "目的地站点输入错误,请重新输入:" << endl;
         cin >> end;
         valid_end = false;      
     }
     // 重新检查站点名称是否存在      
     for (string name : line1) {
          if (name == start) valid_start = true;          
          if (name == end) valid_end = true;                
     }    
     for (string name : line2) {
         if (name == start) valid_start = true;          
         if (name == end) valid_end = true;            
     }
}
    
    // 查找最少站数 
    int minStations = INT_MAX;
    
    if (line1.size() > 0 && line2.size() > 0) {
    int startIndex = -1;
    for (int i = 0; i < line1.size(); i++) {
        if (line1[i] == start) {
            startIndex = i;
            break; 
        }   
    }
   
    int endIndex = -1;    
    for (int i = 0; i < line1.size(); i++) {
        if (line1[i] == end) {
             endIndex = i;
             break;
        }
    }
    
    if (startIndex != -1 && endIndex != -1) {
        int stations = abs(startIndex - endIndex);
        minStations = min(stations, minStations);
    }   
}

if (line2.size() > 0) {
    int startIndex = -1;
    for (int i = 0; i < line2.size(); i++) {
        if (line2[i] == start) {
            startIndex = i;
            break;   
        }     
    }
    
    int endIndex = -1;    
    for (int i = 0; i < line2.size(); i++)  {
        if (line2[i] == end) {
             endIndex = i; 
             break;   
        }    
    }
    
    if (startIndex != -1 && endIndex != -1) {
       int stations = abs(startIndex - endIndex);  
       minStations = min(stations, minStations); 
    }  
}
    
    cout << "最少需要" << minStations << "站" << endl;      
}

这是华为机试题(地铁换乘,图的应用)
一模一样的

这就是一个最短路径的问题,结合具体的例子使用程序实现而已。在具体的实现思路方面,根据题目中你的图,使用图的最短路径广度优先算法即可实现。
具体的实现代码可以参考:
地铁最短线路(代码实现):https://www.cnblogs.com/DarkWalkerLynn/p/13930526.html
https://github.com/wengzheng/subway

可以参考我的博客例子