1. 程式人生 > 實用技巧 >C語言結課課程設計

C語言結課課程設計

大一上學期C語言課結課課程設計:停車場管理系統

分了學習小組, 我們小組有我、YL(YKL)、MCL
三人分工完成
不過我比較閒所以就把整個專案自己悄咪咪寫完了
題目:

初始:
一小時之內免費
一小時後按每小時五元計費
系統功能
1.車輛入場登記
讀檔案中原有內容:車牌號,入場時間
螢幕輸入車牌號,同時寫入檔案
入場時間為當前時間
2.顯示目前場內車輛資訊(車牌號、入場時間)和剩餘車位數、已經用的車位數
螢幕輸出
3.管理收費價目表
預設一小時(分鐘)內免費
多於一小時(分鐘)按每小時x元計費
x由螢幕輸入
4.輸入某幾輛車出場時間
更新檔案內車輛資訊(把出場的車刪掉)
分別輸出結算金額到螢幕
5.統計當天車輛數及收費總金額
輸出到螢幕
6.根據車輛停車總時間從小到大排序
停車總時間為停車時間到當前時間
輸出排序結果到檔案
7.退出系統

資料(自己寫的)

Doinb	2020	12	14	18	50	
Clear	2020	12	14	18	53	
ClearLove	2020	12	14	19	21	
9E33F	2020	12	14	9	45	
X999	2020	12	14	18	32	
8NNXN	2020	12	14	18	40	
9N622	2020	12	14	18	47	
9N777	2020	12	14	18	49	
MLXG	2020	12	14	19	24	

ParkDefinations.h

#define N 200//車位
#define M 10
typedef struct Node{
    int time[5];
    //出入場時間
    long parktime;
    char num[M];//車牌號
}Car;
//從上到下依次為功能1~6
void CarsInput(Car car[], int *n);//車輛資訊,車輛總數
void CarsInformation(Car car[], int n);//車輛資訊
void Price(int *price);//收費價目
void Charge(Car car[], int *n, int price, int *sum);//車輛資訊,車輛總數,停車花費,
void Print(Car car[], int n, int sum);//車輛資訊
void Sort(Car car[], int n);//排序

main.c

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <windows.h>
#include "ParkDefinations.h"

int main(void)
{
	time_t t = time(NULL);
    struct tm *local;
    local = localtime(&t);

    int op, price=5, n=0, sum=0, lastday = 0;
    Car car[205];
    do
    {
        system("cls");
        fprintf(stdout, "1.Vehicle entry registration\n");
        fprintf(stdout, "2.List vehicle information\n");
        fprintf(stdout, "3.Change the price\n");
        fprintf(stdout, "4.Vehicle leave registration\n");
        fprintf(stdout, "5.Count the vehicles admitted today and the total amount charged\n");
        fprintf(stdout, "6.Rank the total parking time from smallest to largest\n");
        fprintf(stdout, "7.Exit\n");
        fprintf(stdout, "Please enter the choice:");

        fscanf(stdin, "%d", &op);
        switch(op)
        {
            case 1:
                CarsInput(car, &n);
                break;
            case 2:
                if(n == 0)
                {
                    fprintf(stdin, "There are no cars in the Park");
                    break;
                }
                CarsInformation(car, n);
                break;
            case 3:
                if(n == 0)
                {
                    fprintf(stdin, "There are no cars in the Park");
                    break;
                }
                Price(&price);
                break;
            case 4:
                if(n == 0)
                {
                    fprintf(stdin, "There are no cars in the Park");
                    break;
                }
                local = localtime(&t);
                if(!lastday && lastday != local->tm_mday)
                {
                    lastday = local->tm_mday;
                    sum = 0;
                }
                Charge(car, &n, price, &sum);
                break;
            case 5:
                if(n == 0)
                {
                    fprintf(stdin, "There are no cars in the Park");
                    break;
                }
                local = localtime(&t);
                if(!lastday && lastday != local->tm_mday)
                {
                    lastday = local->tm_mday;
                    sum = 0;
                }
                Print(car, n, sum);
                break;
            case 6:
                if(n == 0)
                {
                    fprintf(stdin, "There are no cars in the Park");
                    break;
                }
                Sort(car, n);
                break;
            case 7:
                break;
            default:puts("Error Inputs");
        }
        system("pause");
    }while(op != 7);
    return 0;
}

YKL.h

#define N 200//車位
#define M 10
typedef struct Node{
    int time[5];
    //出入場時間
    long parktime;
    char num[M];//車牌號
    struct Node *next;
}Car;
void CarsInput(Car car[], int *n);//車輛資訊,車輛總數
void CarsInformation(Car car[], int n);//車輛資訊

YKL.c

#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "YKL.h"


int day_diff(int year, int month, int day)
{
    int i, ans = 0;
    int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    for(i=1970; i<year; ++i)
    {
        ans += 365;
        if(i%400 == 0 || (i%4 == 0 && i%100 != 0))
            ++ans;
    }
    if(year%400 == 0 || (year%4 == 0 && year%100 != 0))
        ++days[2];
    for(i=1; i<month; ++i)
        ans += days[i];
    ans += day-1;
    return ans;
}
void CarsInput(Car car[], int *n)
{
//初始化
    FILE *fp;
    if((fp = fopen("E:\\程式\\c\\作業\\停車場管理系統\\data.txt", "r"))==NULL)
        if((fp = fopen("E:\\程式\\c\\作業\\停車場管理系統\\data.txt", "w"))==NULL)
        {
            puts("Failure to open data.txt");
            exit(0);
        }
	time_t t=time(NULL);
	struct tm *local = localtime(&t);
	memset(car, 0, *n);
	*n = 0;
	int i, j, diffday;
//檔案輸入
	while(fscanf(fp, "%s", car[*n].num) != EOF)
    {
        for(i=0; i<5; ++i)
            fscanf(fp, "%d", &car[*n].time[i]);
        diffday = day_diff(car[*n].time[0], car[*n].time[1], car[*n].time[2]);
        car[*n].parktime = (diffday * 24 + car[*n].time[3] - 8) * 3600 + car[*n].time[4] * 60;
        //local是中國時間,time()是國際時間,差八個小時
        ++*n;
    }
    fprintf(stdout, "Data has been entered\n");
	fclose(fp);
//螢幕輸入
	if((fp = fopen("E:\\程式\\c\\作業\\停車場管理系統\\data.txt", "a"))==NULL)
    {
        puts("Failure to open data.txt");
        exit(0);
    }
	int x;
	fprintf(stdout, "Please enter the total number of new cars entering the parking lot:");
	fscanf(stdin, "%d", &x);
	for(i=0; i<x; ++i)
    {
        fprintf(stdout, "Please enter the car's license plate number:");
        fscanf(stdin, "%s", car[*n].num);

        t = time(NULL);
        local = localtime(&t);
        car[*n].time[0] = local->tm_year + 1900;
        car[*n].time[1] = local->tm_mon + 1;
        car[*n].time[2] = local->tm_mday;
        car[*n].time[3] = local->tm_hour;
        car[*n].time[4] = local->tm_min;

        fprintf(fp, "%s\t", car[*n].num);
        for(j=0; j<5; ++j)
            fprintf(fp, "%d\t",car[*n].time[j]);
        fprintf(fp, "\n");

        ++*n;
        if(*n == N)
        {
            fprintf(stdout, "The park is full!\n");
            break;
        }
    }
    fflush(stdin);
	fclose(fp);
    return ;
}
void CarsInformation(Car car[], int n)
{
    int i, j;
    fprintf(stdout, "There are %d cars in the park, and there are %d empty places.\n", n, N-n);
    for(i=0; i<n; ++i)
    {
        fprintf(stdout, "%s\t", car[i].num);
        for(j=0; j<5; ++j)
            fprintf(stdout, "%d\t", car[i].time[j]);
        fprintf(stdout, "\n");
    }
    return ;
}

MCL.h

#ifndef MCL_H_INCLUDED
#define MCL_H_INCLUDED

#define N 200//車位
#define M 10
typedef struct Node{
    int time[5];
    //出入場時間
    long parktime;
    char num[M];//車牌號
    struct Node *next;
}Car;
void Price(int *price);//收費價目
void Charge(Car car[], int *n, int price, int *sum);//車輛資訊,車輛總數,停車花費,

#endif // MCL_H_INCLUDED

MCL.c

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "MCL.h"

void Price(int *price)
{
    fprintf(stdout, "Now the cost is %d yuan per hour.\n", *price);
    fprintf(stdout, "Are you going to change it?(y/n)\n");
    fflush(stdin);
    char c = getchar();
    while(c!='y' && c!='n' && c!='N' && c!='Y'){puts("Input Error!");c=getchar();}
    if(c=='n' || c=='N')return ;
    fprintf(stdout, "Please enter the new cost:(an integer):");
    fscanf(stdin, "%d", price);
    fprintf(stdout, "Now the cost is %d yuan per hour.\n", *price);
    return ;
}
void Charge(Car car[], int *n, int price, int *sum)
{
    FILE *fp;
    time_t t=time(NULL);
    if((fp = fopen("E:\\程式\\c\\作業\\停車場管理系統\\data.txt", "r+"))==NULL)
    {
        puts("ERROR!!!!");
        exit(0);
    }
    fflush(stdin);
    int x, i, j, k, flag;
    char emp[M];
    fprintf(stdout, "Please input the number of the leaved cars:");
    fscanf(stdin, "%d", &x);
    for(i=0; i<x; ++i)
    {
        printf("Please enter the car's license plate number:  ");
        fscanf(stdin, "%s", emp);
        flag = 0;
        for(j=0; !flag && j<*n; ++j)
            if(strcmp(emp, car[j].num) == 0)
            {
                fprintf(stdout, "%s has left, the charge is %ld yuan\n", emp, (t-car[j].parktime)/3600 * price);
                *sum += (t-car[j].parktime)/3600 * price;
                --*n;
                for(k=j; k<*n; ++k)
                    car[k] = car[k+1];
                flag = 1;
            }
        if(!flag)
            fprintf(stdout, "%s is not in the Park!\n", emp);
    }
    if((fp = fopen("E:\\程式\\c\\作業\\停車場管理系統\\data.txt", "w"))==NULL)
	{
		fputs("Failure to open data.txt", stdout);
		exit(0);
	}
    for(i=0; i<*n; ++i)
    {
        fprintf(fp, "%s\t", car[i].num);
        for(j=0; j<5; ++j)
            fprintf(fp, "%d\t", car[i].time[j]);
        fprintf(fp, "\n");
    }
    fclose(fp);
    return ;
}

NLH.h

#ifndef NLH_H_INCLUDED
#define NLH_H_INCLUDED

#define N 200//車位
#define M 10
typedef struct Node{
    int time[5];
    //出入場時間
    long parktime;
    char num[M];//車牌號
    struct Node *next;
}Car;
void Print(Car car[], int n, int sum);//車輛資訊
void Sort(Car car[], int n);//排序

#endif // NLH_H_INCLUDED

NLH.c

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "NLH.h"

void Print(Car car[], int n, int sum)
{
    fprintf(stdout, "There are %d cars parking today.\n", n);
    fprintf(stdout, "The income is %d yuan today.\n", sum);
    return ;
}
void Sort(Car car[], int n)
{
//初始化
    FILE *fp;
    if((fp = fopen("E:\\程式\\c\\作業\\停車場管理系統\\data.txt", "w"))==NULL)
	{
		puts("Failure to open data.txt");
		exit(0);
	}
	Car emp;
	int i, j, k;
//選擇排序
	for(k, i=0; i<n; ++i)
	{
	    k=i;
        for(j=k+1; j<n; ++j)
            if(car[j].parktime < car[k].parktime)
                k=j;
        if(i != k)
        {
            emp = car[i];
            car[i] = car[k];
            car[k] = emp;
        }
	}
//輸出
	for(i=0; i<n; ++i)
    {
        fprintf(fp, "%s\t", car[i].num);
        for(j=0; j<5; ++j)
            fprintf(fp, "%d\t", car[i].time[j]);
        fprintf(fp, "\n");
    }
    return ;
}