H_小白不白 2023-08-13 17:56 采纳率: 0%
浏览 19

c语言数据结构航班信息的录入

//main.c
#include "fligh.h"

int main()
{
    while(1)
    {
        Welcome();
        char ch = getch();// 读取字符

        switch(ch)
        {
            case '1':// 录入
                Import_fligh();
                break;
            case '2':// 显示
                break;
            case '3':// 按要求查找
                break;
            case '4':// 信息排序
                break;
            case '0':// 退出
                break;
        }
    }

    return 0;
}




```c

```c
//fligh.c
#include "fligh.h"

// 欢迎
void Welcome()
{
    printf("\tWelcome to Flight System\t\n");
    printf("==========================================\n");
    printf("[1]Import [2]Print [3]Find [4]Sort [0]Quit\n");
    printf("==========================================\n");
}

// 录入航班信息
void Import_fligh()
{
    // 创建一个新的节点
    fligh_node *head = malloc(sizeof(fligh_node));
    fligh *pnew = malloc(sizeof(fligh));
    head->next = NULL;

    // 头插法
    if(head == NULL)
    {
        head = pnew;
    }
    else
    {
        head = head->next;
    }
    
    printf("输入航班号:\n");
    scanf("%s",head->data.number);
    printf("输入起点站:\n");
    scanf("%s",head->data.staddress);
    printf("输入终点站:\n");
    scanf("%s",head->data.arraddress);
    printf("输入班期:\n");
    scanf("%s",head->data.date);
    printf("输入机型:\n");
    scanf("%s",head->data.type);
    printf("输入起飞时间:\n");
    scanf("%s",head->data.stime);
    printf("输入到达时间:\n");
    scanf("%s",head->data.atime);
    printf("输入航票价:\n");
    scanf("%s",head->data.price);

    printf("信息录入成功\n");
    system("pause");// 暂停
    system("cls");// 清屏
}

```c
fligh.h
#ifndef __FLIGHT_H
#define __FLIGHT_H
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef struct fligh
{
    char number[10];// 航班号
    char staddress[10];// 起点站
    char arraddress[10];// 终点站
    char date[10];// 班期
    char type;// 机型
    int stime;// 起飞时间
    int atime;// 到达时间
    float price;// 票价
}fligh;

typedef struct fligh_node
{
    fligh data;
    struct fligh_node *next;
}fligh_node;

//欢迎
void Welcome();
// 录入航班信息
void Import_fligh();

#endif

求大佬解答,应该是创建节点有问题,但是本人找不出来


```

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2023-08-14 09:43
    关注

    整体修改完善如下,改动处见注释,供参考:

    //fligh.h
    #ifndef __FLIGHT_H
    #define __FLIGHT_H
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    typedef struct fligh
    {
        char number[10];// 航班号
        char staddress[10];// 起点站
        char arraddress[10];// 终点站
        char date[10];// 班期
        char type[16];// 机型         char type; 修改
        char stime[10];// 起飞时间    int  stime;修改
        char atime[10];// 到达时间    int  atime;修改
        float price;// 票价
    }fligh;
    
    typedef struct fligh_node
    {
        fligh  *data;           // 修改     
        struct fligh_node* next;
    }fligh_node;
    
    fligh_node* Head = NULL;    // 修改
    
    //欢迎
    void Welcome();
    // 录入航班信息
    void Import_fligh();
    //显示航班信息
    void print();              // 修改
    
    #endif
    
    //---------------------------------------------
    
    
    //main.c
    #include "fligh.h"
    
    int main()
    {
        while (1)
        {
            Welcome();
            char ch = getch();// 读取字符
    
            switch (ch)
            {
            case '1':// 录入
                Import_fligh();
                break;
            case '2':// 显示
                print();
                break;
            case '3':// 按要求查找
                break;
            case '4':// 信息排序
                break;
            case '0':// 退出
                break;
            }
        }
    
        return 0;
    }
    
    
    //----------------------------------------------
    
    
    //fligh.c
    #include "fligh.h"
    
    // 欢迎
    void Welcome()
    {
        printf("\tWelcome to Flight System\t\n");
        printf("==========================================\n");
        printf("[1]Import [2]Print [3]Find [4]Sort [0]Quit\n");
        printf("==========================================\n");
    }
    
    // 录入航班信息
    void Import_fligh()
    {
        // 创建一个新的节点
        if (!Head) {                                       // 修改
            Head = (fligh_node*)malloc(sizeof(fligh_node));// 修改
            Head->data = NULL;                             // 修改
            Head->next = NULL;
        }
        
        fligh_node* pnew = (fligh_node*)malloc(sizeof(fligh_node));// 修改
        pnew->data = (fligh*)malloc(sizeof(fligh));                // 修改   
        pnew->next = NULL;
    
        // 头插法
        pnew->next = Head->next;   // 修改
        Head->next = pnew;         // 修改
    
        printf("输入航班号:\n");
        scanf("%s", pnew->data->number);
        printf("输入起点站:\n");
        scanf("%s", pnew->data->staddress);
        printf("输入终点站:\n");
        scanf("%s", pnew->data->arraddress);
        printf("输入班期:\n");
        scanf("%s", pnew->data->date);
        printf("输入机型:\n");
        scanf("%s", pnew->data->type);
        printf("输入起飞时间:\n");
        scanf("%s", pnew->data->stime);
        printf("输入到达时间:\n");
        scanf("%s", pnew->data->atime);
        printf("输入航票价:\n");
        scanf("%f", &pnew->data->price);  //scanf("%s", pnew->data->price); 修改
    
        printf("信息录入成功\n");
        system("pause");// 暂停
        system("cls");// 清屏
    }
    
    void print()                          // 修改
    {
        fligh_node* pt = NULL;
        if (!Head || !Head->next)
            printf("NULL\n");
        else {
            pt = Head->next;
            printf("%-10s %-10s %-10s %-10s %-16s %-10s %-10s %-10s\n",
                "航班号", "起点站", "终点站", "班期", "机型", "起飞时间", "到达时间", "票价");
            while (pt) {
                printf("%-10s %-10s %-10s %-10s %-16s %-10s %-10s %-10.2f\n", 
                    pt->data->number,pt->data->staddress, pt->data->arraddress, pt->data->date, 
                    pt->data->type,  pt->data->stime, pt->data->atime, pt->data->price);
                pt = pt->next;
            }
        }
    }
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 8月13日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价