C++用链表构建一个停车场计费程序

最近学了一手数据结构,开始试着构建链表的应用。

参考资料:(1)单链表实现的学生管理系统https://blog.csdn.net/y____xiang/article/details/80045000

                  (2)C++如何获取系统时间https://blog.csdn.net/chen134225/article/details/79396149

#include
#include
#include
using namespace std;
#pragma warning(disable:4996)
struct Carinfo {//车辆信息存储结构
    int len = 0;
    char carname[20];
    double dur = 0.0;
    double start = 0.0;
    double end = 0.0;
    int inyear; int inmonth; int inday;
    int inhour; int inmin; int insec;
    Carinfo *next;//下一辆车
};
Carinfo* createcarinfo()//构建链表
{
    Carinfo *carinfo = new Carinfo;
    carinfo->next = NULL;
    return carinfo;
}
void gettime(Carinfo*n)//better获取当前时间
{
    struct tm*local;
    time_t t;
    t = time(NULL);
    local = localtime(&t);
    n->inyear = local->tm_year + 1900;
    n->inday = local->tm_mday;
    n->inmonth = local->tm_mon + 1;
    n->inhour = local->tm_hour;
    n->inmin = local->tm_min;
    n->insec = local->tm_sec;
    clock_t start;//计时函数
    start = clock();
    n->start = start;
}
Carinfo *createnextcar(char *newcarname)//构建链表
{
    Carinfo *nextcar = new Carinfo;
    strcpy(nextcar->carname, newcarname);
    gettime(nextcar);
    nextcar->next = NULL;
    return nextcar;
}
void insertcar(Carinfo*nextcar, char*name)//插入车辆
{    
    if (nextcar->len != 5)//停车场容量
    {
        nextcar->len++;
        Carinfo *newnextcar = createnextcar(name);
        Carinfo*temp = nextcar;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        newnextcar->next = NULL;
        temp->next = newnextcar;
    }
    else {
        cout << "停车场已满!" << endl;
    }
    
}
void pay(Carinfo*m)//计费 1秒1块钱
{
    struct tm*local;
    time_t t;
    t = time(NULL);
    local = localtime(&t);
    int outyear = local->tm_year + 1900;
    int outday = local->tm_mday;
    int outmonth = local->tm_mon + 1;
    int outhour = local->tm_hour;
    int outmin = local->tm_min;
    int outsec = local->tm_sec;
    cout << " | 出库时间:" << outyear << "/" << outmonth << "/" << outday << "/" << outhour << "/" << outmin << "/" << outsec << endl;
    cout << "收取费用:" << m->dur * 1 << endl;
}
void deletecar(Carinfo *decar, char*name)//退车
{
    if (decar->len != 0)
    {
        decar->len--;
        Carinfo*temp = decar;
        Carinfo*p = decar->next;
        if (p==NULL)//如果链表为空
        {
            cout << "wrong无车辆" << endl;
        }
        else{
            while (p->carname!=NULL&&strcmp(p->carname, name) != 0)//比较字符串
            {
                if (p->next == NULL)
                {
                    cout << "没有此车辆的信息" << endl;
                    return ;
                }
                else
                {
                    temp = temp->next;
                    p = p->next;
                }        
            }
            clock_t end;
            end = clock();
            p->end = end;
            p->dur = (p->end - p->start)/1000;
            pay(p);
            temp->next = p->next;
            delete p;
        }
    }
    else
    {
        cout << "停车场已空!" << endl;
    }
        
}
void searchcar(Carinfo*sercar, char*name)//查车 没用到
{
    Carinfo*temp = sercar->next;
    while (strcmp(temp->carname, name) != 0)//比较字符串
    {
        if (temp->next == NULL)
        {
            cout << "查询失败,无此车辆信息" << endl; return;
        }
        temp = temp->next;
    }
    cout << "search succ " << endl;
}
void infodisplay(Carinfo *list)//打印车信息
{
    Carinfo*temp = list->next;
    if (temp == NULL)
    {
        cout << "无打印车辆信息" << endl;
    }
    while (temp)
    {
        cout << "车牌号:" << temp->carname << " | 入库时间:" << temp->inyear << "/" << temp->inmonth << "/" << temp->inday << "/" << temp->inhour << "/" << temp->inmin << "/" << temp->insec << endl;
        temp = temp->next;
    }
}
void clear()//清屏
{
    system("cls");
    cout << "============" << endl;
    cout << "*1.车辆入库*" << endl;
    cout << "*2.车辆出库*" << endl;
    cout << "*3.查询现停*" << endl;
    cout << "============" << endl;
}
void mainwork()
{
    Carinfo *car = createcarinfo();
    int i;
    cin >> i;
    while (cin) {
        switch (i)
        {
        case 1: {
            char n[20];
            cout << "1.车辆入库,";
            cout << "请输入入库车辆信息:";
            cin >> n;
            insertcar(car, n);
        }break;
        case 2: {
            char n[20];
            cout << "2.车辆出库,";
            cout << "请输入出库车辆信息:";
            cin >> n;
            deletecar(car, n);
        }break;
        case 3: {
            cout << "打印现停车辆信息" << endl;
            infodisplay(car);
        }break;
        default:cout << "命令错误,重新输入" << endl; break;
        }
        cout << "下一个命令:" << endl;
        cin >> i;
        clear();
    }
}
int main()
{    
    clear();
    mainwork();
    system("pause");
    return 0;
}

好好学习,天天向上!

你可能感兴趣的:(C++用链表构建一个停车场计费程序)