头文件

#pragma once
#ifndef ARRAYLIST_H
#define ARRAYLIST_H

#include<iostream>
#include<algorithm>
#include<fstream>
#include<string.h>

#define defaultSize 100

using namespace std;

typedef struct
{
    char brand[20];
    char type[20];
    char color[20];
    char car_number[20];
    int year;
    int month;
    int day;
}Car;

class Alist
{
private:
    int maxSize;         //Maximum size of list
    int listSize;        //Number of list items now
    int curr;            //Position of current element
    Car* listArray;        //Array holding list elements

public:
    //Car* listArray;        //Array holding list elements
    Alist(int size = defaultSize) {//Constructor
        maxSize = size;
        listSize = curr = 0;
        listArray = new Car[maxSize];
    }

~Alist() { delete[] listArray; }//Destructor

void clear() {                   //Reinitaialize the list
        delete[] listArray;          //Remove the array
        listSize = curr = 0;         //Reset the size
        listArray = new Car[maxSize];  //Recreate the array
    }

bool sortfun(Car &A,Car &B)           //the principle to sort
    {
        if (A.year < B.year)
            return true;
        else if (A.year > B.year)
            return false;
        else
        {
            if (A.month < B.month)
                return true;
            else if (A.month > B.month)
                return false;
            else
            {
                if (A.day < B.day)
                    return true;
                else
                    return false;
            }
        }
    }

void insert(Car &it) {             //Insert "it" at current position and sort
        if (listSize >= maxSize)
            cout << "                  List capacity exceeded" << endl;    
        listArray[listSize++] = it;
        Car temp;
        for (int i = 0; i < listSize; i++)
        {
            for (int j = i; j < listSize; j++)
            {
                if (sortfun(listArray[j], listArray[i])) {    //Exchange the two element to sort
                    temp = listArray[i];
                    listArray[i] = listArray[j];
                    listArray[j] = temp;
                }
            }
        }
    }

void inputNew()             //Add a new car
    {
        Car c;
        cout << endl << endl << endl << "                  Please input the information for the new car." << endl;
        cout << endl << "                  Brand: ";
        cin >> c.brand;
        cout << "                  Type: ";
        cin >> c.type;
        cout << "                  Color: ";
        cin >> c.color;
        cout << "                  Car number: ";
        cin >> c.car_number;
        cout << "                  Date(y-m-d): ";
        cin >> c.year >> c.month >> c.day;
        insert(c);
        cout << endl << "                  Add succeeded. Press any button to continue..." << endl;
    }

void modify(int pos)             //Modify the information of the car
    {
        cout << "                  Are you sure that you want to modify the car?" << endl;
        cout << "                  1.Yes  2.No                  " << endl;
        int n;
        cin >> n;
        if (n == 1)         //Sure to modify
        {
            int modi;
            cout << "                  1.Brand  2.Type  3.Color  4.Car number  5.Manufacture date" << endl;
            cout << "                  Please choose what do you want to modify:" << endl;
            cin >> modi;

Car c;
            switch (modi)
            {
            case 1:
                cin >> c.brand;
                memcpy(listArray[pos].brand, c.brand, 20);  //Modify the brand of the car
                break;
            case 2:
                cin >> c.type;
                memcpy(listArray[pos].type, c.type, 20);    //Modify the type of the car
                break;
            case 3:
                cin >> c.color;
                memcpy(listArray[pos].color, c.color, 20);   //Modify the color of the car
                break;
            case 4:
                cin >> c.car_number;
                memcpy(listArray[pos].car_number, c.car_number, 20);   //Modify the car number of the car
                break;
            case 5:
                cin >> c.year >> c.month >> c.day;
                if (c.year > 2018 || c.month <= 0 || c.month > 12 || c.day <= 0 || c.day > 31)    //Make sure the information is valid
                    cout << "                  Wrong date!!!" << endl;
                //Modify the manufacture date of the car
                listArray[pos].year = c.year;
                listArray[pos].month = c.month;
                listArray[pos].day = c.day;
                break;
            }
            cout << "                  Have Done!" << endl;
            getCar(pos);
        }
    }

void remove(int pos) {              //Remove and return the current element
        cout << "                  Are you sure that you want to delete the car?" << endl;
        cout << "                  1.Yes  2.No                  " << endl;
        int n;
        cin >> n;
        if (n == 1)                 //Sure to remove
        {
            if ((pos < 0) || (pos >= listSize))
                cout << "                  No element" << endl;
            for (int i = pos; i < listSize - 1; i++) {   //Shift the elements down
                listArray[i] = listArray[i + 1];
            }
            listSize--;
            cout << "                  The car hae been deleted!" << endl;
        }
    }

void getCar(int pos) {            //Get the car at the special position
        if (pos < 0 || pos >= listSize)
            cout << "                  Pos out of range" << endl;
        else
        {    //Print the information of the car
            cout << "                  Num: " << pos + 1 << endl;
            cout << "                  Brand: " << listArray[pos].brand << endl;
            cout << "                  Type: " << listArray[pos].type << endl;
            cout << "                  Color: " << listArray[pos].color << endl;
            cout << "                  Car number: " << listArray[pos].car_number << endl;
            cout << "                  Manufacture date: " << listArray[pos].year << "-" <<
                listArray[pos].month << "-" << listArray[pos].day << endl;
        }
    }

void display() const {        //Print all the car
        for (int i = 0; i <= listSize - 1; i++) {
            cout << "                  *************************************" << endl;
            cout << "                  Num: "<< i + 1 << endl;
            cout << "                  Brand: " << listArray[i].brand << endl;
            cout << "                  Type: " << listArray[i].type << endl;
            cout << "                  Color: " << listArray[i].color << endl;
            cout << "                  Car number: " << listArray[i].car_number << endl;
            cout << "                  Manufacture date: " << listArray[i].year << "-"
                << listArray[i].month << "-" << listArray[i].day << endl;
        }
    }

void findPos()                       //Find the position of the car
    {
        int n;
        cin >> n;
        if (n == 6) {
            int num;
            cout << "                  Please enter the number of the car you want to find: ";
            cin >> num;
            getCar(num - 1);
        }
        else if (n == 5) {
            int y;
            cout << "                  Please enter the year of the car you want to find: ";
            cin >> y;
            getPos(y);
        }
        else {
            char a[20];
            cout << "                  Please enter the parament of the car you want to find: ";
            cin >> a;
            getPos(a, n);
        }
    }

void getPos(char a[],int n)     //Get the position of the car
    {
        switch (n)
        {
        case 1:
            for (int i = 0; i < listSize; i++)
            {
                if (judge(a, listArray[i].brand))   //Compare a and brand of every car in list
                    getCar(i);
            }
            break;
        case 2:
            for (int i = 0; i < listSize; i++)
            {
                if (judge(a, listArray[i].type))     //Compare a and type of every car in list
                    getCar(i);
            }
            break;
        case 3:
            for (int i = 0; i < listSize; i++)
            {
                if (judge(a, listArray[i].color))    //Compare a and type of every car in list
                    getCar(i);
            }
            break;
        case 4:
            for (int i = 0; i < listSize; i++)
            {
                if (judge(a, listArray[i].car_number))   //Compare a and type of every car in list
                    getCar(i);
            }
            break;
        }
    }

void getPos(int y)      //Get the position of the car
    {
        for (int i = 0; i < listSize; i++)
        {
            if (y == listArray[i].year)
                getCar(i);
        }
    }

bool judge(char a[], char b[])     //Judge whether the two is the same
    {
        int n = 0;
        if (strlen(a) != strlen(b))
            return 0;
        else
        {
            while (a[n] != '\0'&&b[n] != '\0')
            {
                if (a[n] == b[n])
                    n++;
                else
                    return 0;
            }
        }
        return 1;
    }

#pragma warning(disable:4996)
    void read()                    //Read the information of the car into listArray
    {
        ifstream fin("Car.txt");   //Create an object of ifstream
        string str;         //Store the string read just now
        char sstr[20];      //Used to transfer string to an array of char
        int flag = 0;       //Help to judge how many string has been read
        Car c;            //Temporarily store the information of the car
        if (fin)         //Judge whether the file exist
        {
            while (fin >> str)  //Do the instructions when reading the string
            {
                flag++;
                switch (flag)
                {
                case 1:
                    strcpy(c.brand, str.c_str());    //Get the brand of the car
                    break;
                case 2:
                    strcpy(c.type, str.c_str());      //Get the type of the car
                    break;
                case 3:
                    strcpy(c.color, str.c_str());      //Get the color of the car
                    break;
                case 4:
                    strcpy(c.car_number, str.c_str());     //Get the car number of the car
                    break;
                case 5:
                    strcpy(sstr, str.c_str());     //Transfer the string to an array of char
                    c.year = atoi(sstr);       //Transfer an array of char to int and get year of manufacture of the car
                    break;
                case 6:
                    strcpy(sstr, str.c_str());     //Transfer the string to an array of char
                    c.month = atoi(sstr);       //Transfer an array of char to int and get month of manufacture of the car
                    break;
                case 7:
                    strcpy(sstr, str.c_str());     //Transfer the string to an array of char
                    c.day = atoi(sstr);       //Transfer an array of char to int and get day of manufacture of the car
                    insert(c);          //Insert the car after geting all the information
                    flag = 0;        //Reset the flag to 0 and collect the information of the car again
                    break;
                }
            }
            cout << sizeof(Car) << " " << sizeof(listArray[0]);
            fin.close();            //Close the file
        }
        else
            cout << "                  Not found!" << endl;
    }

void save()           //Save the information
    {
        ofstream fout("Car.txt");  //Create an object of ofstream
        if (fout)         //Judge whether the file exist
        {
            for (int i = 0; i < listSize; i++)          //Store the information of the car into the file
            {
                fout << listArray[i].brand << " ";
                fout << listArray[i].type << " ";
                fout << listArray[i].color << " ";
                fout << listArray[i].car_number << " ";
                fout << listArray[i].year << " " << listArray[i].month << " " << listArray[i].day << endl;
            }
        }
        else
            cout << "                  Not found!" << endl;
        fout.close();
    }
};

#endif

源文件

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<stdlib.h>
#include"AList.h"

using namespace std;

int main()
{
    int instru;
    Alist L;
    //Build the image for the user
    cout << "                  * * * * * *欢迎使用大松公司汽车信息管理* * * * * *" << endl << endl;
    cout << "                    对公司的汽车你享有信息分享权,但是你得妥善驾驶" << endl;
    cout << "                  =                                                =" << endl;
    cout << "                  =      1.输入汽车信息        2.显示汽车信息      =" << endl;
    cout << "                  =                                                =" << endl;
    cout << "                  =      3.查找汽车信息        4.删除汽车信息      =" << endl;
    cout << "                  =                                                =" << endl;
    cout << "                  =      5.修改汽车信息        0.退出管理系统      =" << endl;
    cout << "                  =                                                =" << endl;
    cout << "                               公司的车辆只允许工作之用             " << endl << endl;
    cout << "                                    请输入功能选项:" << endl;
    
    L.read();    //Read the file into the list
    //Do instructions when users enter the function number
    while (cin >> instru)
    {
        switch (instru)
        {
        case 1:
            L.inputNew();
            break;
        case 2:
            L.display();
            break;
        case 3:
            cout << "                  1.Brand  2.Type  3.Color  4.Car number  5.Manufacture year  6.Num" << endl;
            cout << "                  Please choose the parameter you want to rely on and enter: " << endl;
            L.findPos();
            break;
        case 4:
            cout << "                  Please enter the number of the car: ";
            int n4;
            cin >> n4;
            L.getCar(n4 - 1);
            L.remove(n4 - 1);
            break;
        case 5:
            cout << "                  Please enter the number of the car: ";
            int n5;
            cin >> n5;
            L.getCar(n5 - 1);
            L.modify(n5 - 1);
            break;
        case 0:
            cout << "                  Do you want to save the changes?" << endl;
            cout << "                  1.Save  2.Not to Save" << endl;
            int n0;
            cin >> n0;
            if (n0 == 1)
                L.save();
            cout << "                  Are you sure that you want to exit?" << endl;
            cout << "                  1.Exit  2.Stay" << endl;
            int m0;
            cin >> m0;
            if(m0==1)
                //Or exit(0);
                return 0;            //Exit the program
            break;
        }
    }

system("pause");
    return 0;
}

汽车管理程序(数据结构课程练习)相关推荐

  1. c语言数据结构课程设计停车场管理系统,数据结构课程设计报告停车场管理系统...

    <数据结构课程设计报告停车场管理系统>由会员分享,可在线阅读,更多相关<数据结构课程设计报告停车场管理系统(8页珍藏版)>请在人人文库网上搜索. 1.数据结构课程设计报告系 别 ...

  2. C/C++《数据结构课程设计》任务书[2022-12-27]

    C/C++<数据结构课程设计>任务书[2022-12-27] <数据结构课程设计>任务书 一.任务总体安排: 班级 设计时间 地点 指导老师 21软件开发 17周每周一至周五五 ...

  3. C/C++数据结构课程设计安排

    C/C++数据结构课程设计安排 数据结构课程设计安排 课程设计学时:32学时 课程设计目的:综合应用数据结构课程中所学的数据结构:线性表.栈.队列.数组.广义表.树.二叉树.图.查找表中的一种或多种数 ...

  4. “数据结构”课程设计题目

    "数据结构"课程设计题目 1.城市链表 [问题描述] 将若干城市的信息,存入一个带头结点的单链表.结点中的城市信息包括:城市名,城市的位置坐标.要求能够利用城市名和位置坐标进行有关 ...

  5. 山东大学数据结构课程设计实验五(低风险出行系统)

    数据结构课程设计(五)--低风险出行系统 前言 题目要点 ①生成数据 ②要给定两种最短路解法 ③创立文件 ④模拟时间流动并与用户交互 代码讲解 源代码 写在最后 前言 数据结构课程设计第五题是每一个同 ...

  6. 计算机课程设计收费管理系统,数据结构课程设计报告---收费停车场管理系统

    数据结构课程设计报告---收费停车场管理系统 (20页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 14.9 积分 XX大学计算机与电子 信息学院< ...

  7. 数据结构课程设计——机票售卖系统(C++)

    引言 这学期最后的数据结构课程设计需要我们完成一个简单的小程序,我选择了一个机票售卖系统,实现了一些基本的功能:因为时间给的比较短,又赶在复习周补课,所以并没有什么突出的地方,我就在这里聊聊我的代码实 ...

  8. php数据结构课程---2、链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的))...

    php数据结构课程---2.链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的)) 一.总结 一句话总结: php是弱类型语言,变量即可表示数值,也可表示对象:链表节点的数据域的值就 ...

  9. 数据结构课程设计---最长公共子串

    数据结构课程设计,由用户输入两个字符串串X和Y,再由用户输入一个任意的字符串Z,实现以下功能: ①如果字符串Z是字符串X的子串,则显示Z在X中的位置并记录,如果字符串Z是字符串Y的子串,则显示Z在Y中 ...

  10. 设树采用孩子兄弟表示法存放.用类c语言设计算法计算树的高度.,(数据结构课程设计分类题目.doc...

    (数据结构课程设计分类题目 线性表 顺序表: 1.设有一元素为整数的线性表L=(a1,a2,a3,-,an),存放在一维数组A[N]中,设计一个算法,以表中an作为参考元素,将该表分为左.右两部分,其 ...

最新文章

  1. TP3.2的删除缓存与引入第三方库的问题(二)
  2. Linux下的shell语言编程入门
  3. java 复合_【福利】java新手做的复合型计算器!
  4. mysql创建的是拉丁_mysql 拉丁1 转换成 utf8
  5. 零基础学python图文版-零基础学Minecraft编程(图文版)中文pdf_Python教程
  6. hdu2089 不要62
  7. 使用javascript 实现.net 验证控件功能
  8. 一些用JAVA实现的小题目
  9. Excel 相对引用 绝对引用 区别是什么 如何快速转换 快捷键 F4
  10. 汉字转拼音,并返回第一个字母
  11. vue2中监听watch的写法汇总
  12. VS2019无法启动程序 系统找不到指定文件解决办法
  13. 2012年部分节假日安排
  14. ACM Hrbeu OJ 1201 Simplest Task in Windows || ZOJ 2480
  15. Delphi编程中流的使用
  16. 新版openwrt配置vlan 拨号-局域网-iptv
  17. 2020-04-24--黑寡妇的复活
  18. qt creator在高分辨率笔记本上控件运行显示不全的问题解决方法
  19. Twitter账号总被关联封号? 如何解决?
  20. 声学参数-基频-Librosa标准: 基频的文字定义和用librosa提取wav文件基频

热门文章

  1. 有一个人说网址前面的www是主机名?主机?我懵了
  2. 大厂嫡系文化,养肥了谁?
  3. IT常见_项目管理_名词
  4. Linux 使用交流
  5. Pygame精灵和碰撞检测
  6. 基于Node.JS的SNL词法分析和语法分析
  7. 用户体验分析: 以 “南通大学教务管理系统微信公众号” 为例
  8. java把map值放入vector_java把map值放入vector
  9. 浅谈c语言中怎么让程序直接结束(待补充)
  10. 帝国CMS升级以及PHP版本为7.*空白问题解决