C语言之火车管理系统

本文详细介绍了使用C++编写的火车售票系统,包括添加、显示、插入、查找、删除火车信息、统计、排序和文件操作等核心功能的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>//标准输入输出
#include <stdlib.h>//内存动态分配,要用
#include<stdbool.h>//true,false
#include <string.h>//字符串数组

// 定义正确的账号和密码
const char* correct_username = "1";
const char* correct_password = "1";

// 检查用户名和密码是否正确
int check_credentials(const char* username, const char* password)
{
    return strcmp(username, correct_username) == 0 && strcmp(password, correct_password) == 0;
}

typedef struct Train
{
    int id;
    char name[20];
    int count; // 添加计数器
    struct Train* next;
} Train;

Train* head = NULL;

//数据录入
void add_train(int id, const char* name)
{
    Train* new_train = (Train*)malloc(sizeof(Train));
    new_train->id = id;
    strcpy(new_train->name, name);
    new_train->count = 1; // 初始化计数器为1
    new_train->next = head;
    head = new_train;
}

//数据输出(显示)
void print_trains()
{

    Train* current = head;
    printf(" ID          Name\n");
    while (current != NULL)
    {

        printf(" %-10d  %-10s\n", current->id, current->name);
        current = current->next;
    }
}

//用于插入火车信息
void insert_train(int position, int id, const char* name)
{
    Train* new_train = (Train*)malloc(sizeof(Train));
    new_train->id = id;
    strcpy(new_train->name, name);

    if (position == 1)
    {
        new_train->next = head;
        head = new_train;
    }
    else
    {
        Train* current = head;
        for (int i = 1; i < position - 1 && current != NULL; i++)
        {
            current = current->next;
        }
        if (current != NULL)
        {
            new_train->next = current->next;<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值