代碼如下,僅實(shí)現(xiàn)功能,未考慮安全等情況.
創(chuàng)新互聯(lián)建站專注于梁山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供梁山營銷型網(wǎng)站建設(shè),梁山網(wǎng)站制作、梁山網(wǎng)頁設(shè)計(jì)、梁山網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造梁山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供梁山網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
//ParkingTest.java
/**
*?停車程序入口.本示例為考慮數(shù)據(jù)安全等.僅作為示例,未經(jīng)調(diào)試.
*?@author?menglg
*
*/
public?class?ParkingTest?{
public?static?void?main(String[]?args)?{
//?程序入口,可以安排停車出車
for?(int?i?=?0;?i??100;?i++)?{
if?(ParkingManage.hasPosition())?{
ParkingManage.enterParking(i?+?"");
continue;
}
break;
}
for?(int?i?=?0;?i??100;?i++)?{
ParkingManage.outParking(i?+?"");
}
System.out.println(ParkingManage.totalCost());
}
}
//ParkingManage.java
import?java.util.ArrayList;
import?java.util.Date;
import?java.util.List;
public?class?ParkingManage?{
private?static?ListParking?parkings?=?new?ArrayList();
private?static?final?int?maxNumber?=?100;
private?static?int?currentNumber;
public?static?void?enterParking(String?lpn)?{
if?(lpn?==?null?||?lpn.length()?==?0)?{
return;
}
Parking?p?=?new?Parking(lpn);
p.setEnterDate(new?Date());
parkings.add(p);
}
public?static?void?outParking(String?lpn)?{
if?(lpn?==?null?||?lpn.length()?==?0)?{
return;
}
for?(Parking?p?:?parkings)?{
if?(!lpn.equals(p.getLpn()))
continue;
Date?now?=?new?Date();
p.setOutDate(now);
parkings.remove(p);
currentNumber?+=?p.getCost();
break;
}
}
public?static?int?totalCost()?{
return?currentNumber;
}
public?static?boolean?hasPosition()?{
return?parkings.size()??maxNumber;
}
}
//Parking.java
import?java.util.Date;
public?class?Parking?{
private?String?lpn;
private?Date?enterDate;
private?Date?outDate;
private?int?cost;
public?Parking(String?lpn)?{
this.lpn?=?lpn;
}
public?String?getLpn()?{
return?lpn;
}
public?Date?getEnterDate()?{
return?enterDate;
}
public?Date?getOutDate()?{
return?outDate;
}
public?int?getCost()?{
if(outDate?==?null?||?enterDate?==?null){
return?0;
}
//停車時(shí)間,單位毫秒
long?time?=?outDate.getTime()?-?enterDate.getTime();
long?hour?=?60*60*1000;
if(time??hour){
cost?=?3;
}else?if(time%hour?==?0){
cost?=?(int)(time/hour?*?2);
}else{
cost?=?(int)((time/hour+1)?*?2);
}
return?cost;
}
public?void?setLpn(String?lpn)?{
this.lpn?=?lpn;
}
public?void?setEnterDate(Date?enterDate)?{
this.enterDate?=?enterDate;
}
public?void?setOutDate(Date?outDate)?{
this.outDate?=?outDate;
}
public?void?setCost(int?cost)?{
this.cost?=?cost;
}
}
importjava.text.SimpleDateFormat;\x0d\x0aimportjava.util.Date;\x0d\x0aimportjava.util.Scanner;\x0d\x0a\x0d\x0a/*********************************\x0d\x0a*停車場管理\x0d\x0a*authorzhang\x0d\x0a*2013-12-13\x0d\x0a********************************/\x0d\x0apublicclassCarStopManager{\x0d\x0a\x0d\x0apublicstaticvoidmain(String[]args){\x0d\x0aScannersc=newScanner(System.in);\x0d\x0a\x0d\x0aSystem.out.println("請入車牌號:");\x0d\x0aStringcarno=sc.next();\x0d\x0aCarStopManagercarStopManager=newCarStopManager();\x0d\x0acarStopManager.setCarNo(carno);//設(shè)置車牌號\x0d\x0a\x0d\x0aSimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");\x0d\x0aStringsdate=format.format(newDate());\x0d\x0aSystem.out.println("當(dāng)前時(shí)間(入場時(shí)間)是:"+sdate);\x0d\x0a\x0d\x0aSystem.out.println("需要開出車場嗎?yes/no:");\x0d\x0aStringyesno=sc.next();\x0d\x0a\x0d\x0aif(yesno.equals("yes")){\x0d\x0aStringedate=format.format(newDate());\x0d\x0aSystem.out.println("出場時(shí)間是:"+edate);\x0d\x0a//計(jì)算方法\x0d\x0acarManager(2,sdate,edate,carStopManager);\x0d\x0a}\x0d\x0a}\x0d\x0a/**\x0d\x0a*計(jì)算方法\x0d\x0a*/\x0d\x0apublicstaticvoidcarManager(inttype,StringstarTime,\x0d\x0aStringendTime,CarStopManagercarStopManager){\x0d\x0a\x0d\x0aif(type==1){//按月收費(fèi)\x0d\x0aSystem.out.println("如若沒有繳納月費(fèi)請繳納800元,如若繳納將不再提示!");\x0d\x0a}else{\x0d\x0a/**\x0d\x0a*一般不會有停車幾個(gè)月的吧?先不考慮停車幾年或者幾個(gè)月的\x0d\x0a*/\x0d\x0aStringsDay=starTime.substring(8,10);//入場日期(天)\x0d\x0aStringsHour=starTime.substring(11,13);//入場小時(shí)\x0d\x0aStringsMM=starTime.substring(14,16);//入場分鐘\x0d\x0a\x0d\x0aStringeDay=starTime.substring(8,10);//出場日期(天)\x0d\x0aStringeHour=endTime.substring(11,13);//出廠小時(shí)\x0d\x0aStringeMM=endTime.substring(14,16);//出廠分鐘\x0d\x0a\x0d\x0afloatmoney=0;//需繳納的費(fèi)用\x0d\x0aintshour=Integer.parseInt(sHour);\x0d\x0aintehour=Integer.parseInt(eHour);\x0d\x0aintsmm=Integer.parseInt(sMM);\x0d\x0aintemm=Integer.parseInt(eMM);\x0d\x0aintrehour=0;//停車幾個(gè)小時(shí)\x0d\x0a\x0d\x0aif(sDay.equals(eDay)){//同一天\x0d\x0a//當(dāng)天6點(diǎn)到20點(diǎn)之間\x0d\x0aif((shour=6shour
回答于?2022-12-14
#includestdio.h
#includestdlib.h
#includestring.h
#define MAX 3 // 停車場最大容量
#define price 5 //每小時(shí)5元
typedef struct time {
int hour;
int min;
} Time;// 定義時(shí)間結(jié)構(gòu)體
typedef struct node {
char num[10];
Time reach;
Time leave;
} CarNode;// 定義車輛信息結(jié)構(gòu)體
typedef struct NODE {
CarNode * stack[MAX + 1];
int top;
} SeqStackCar;
typedef struct car {
CarNode *data;
struct car *next;
} QueueNode;
typedef struct Node {
QueueNode *head;
QueueNode *rear;
} LinkQueueCar;
void InitStack(SeqStackCar *s) {
int i;
s-top = 0;
for (i = 0; i = MAX; i++)
s-stack[s-top] = NULL;
}// 棧的初始化
int InitQueue(LinkQueueCar *Q) {
Q-head = (QueueNode *) malloc(sizeof (QueueNode));
if (Q-head != NULL) {
Q-head-next = NULL;
Q-rear = Q-head;
return (1);
} else return (-1);
}// 隊(duì)列的初始化
void PRINT(CarNode *p, int room) { // 車輛收費(fèi)
int A1, A2, B1, B2;
printf("\n車輛離開的時(shí)間:");
scanf("%d:%d", (p-leave.hour), (p-leave.min));
printf("\n離開車輛的車牌號為:");
puts(p-num);
printf("\n其到達(dá)時(shí)間為: %d:%d", p-reach.hour, p-reach.min);
printf("\n離開時(shí)間為: %d:%d", p-leave.hour, p-leave.min);
A1 = p-reach.hour;
A2 = p-reach.min;
B1 = p-leave.hour;
B2 = p-leave.min;
printf("\n應(yīng)交費(fèi)用為: %2.1f元", ((B1 - A1)*60 + (B2 - A2)) * price);
free(p);
}
int Arrival(SeqStackCar *Enter, LinkQueueCar *W) {
CarNode *p;
QueueNode *t;
p = (CarNode *) malloc(sizeof (CarNode));
printf("\n請輸入車牌號(例:豫B1234):");
scanf("%d",p-num);
if (Enter-top MAX) {
Enter-top++;
printf("\n車輛在車場第%d位置.", Enter-top);
printf("\n車輛到達(dá)時(shí)間:");
scanf("%d:%d", (p-reach.hour), (p-reach.min));
Enter-stack[Enter-top] = p;
return (1);
} else {
printf("\n該車須在便道等待!有車位時(shí)進(jìn)入車場");
t = (QueueNode *) malloc(sizeof (QueueNode));
t-data = p;
t-next = NULL;
W-rear-next = t;
W-rear = t;
return (1);
}
}// 車輛的到達(dá)登記
void Leave(SeqStackCar *Enter, SeqStackCar *Temp, LinkQueueCar *W) { //車輛的離開
int room;
CarNode *p, *t;
QueueNode *q;
if (Enter-top 0) // 判斷車場是否為空
{
while (1) {
printf("\n請輸入車在車場的位置/1--%d/:", Enter-top);
scanf("%d", room);
if (room = 1 room = Enter-top) break;
else printf("\n 輸入有誤,請重輸: ");
}
while (Enter-top room) // 把要?jiǎng)h除的車輛的前面的車開出來,進(jìn)臨時(shí)棧。
{
Temp-top++;
Temp-stack[Temp-top] = Enter-stack[Enter-top];
Enter-stack[Enter-top] = NULL;
Enter-top--;
}
p = Enter-stack[Enter-top]; // 把要?jiǎng)h除的車輛節(jié)點(diǎn)賦給p。
Enter-stack[Enter-top] = NULL;
Enter-top--;
while (Temp-top = 1) // 再把臨時(shí)棧里德車輛進(jìn)停車場
{
Enter-top++;
Enter-stack[Enter-top] = Temp-stack[Temp-top];
Temp-stack[Temp-top] = NULL;
Temp-top--;
}
PRINT(p, room); // 調(diào)用計(jì)費(fèi)函數(shù)計(jì)費(fèi)
if ((W-head != W-rear) Enter-top MAX) //如果便道上有車,則再開進(jìn)停車場。
{
q = W-head-next;
t = q-data;
Enter-top++;
printf("\n便道的%s號車進(jìn)入車場第%d位置.", t-num, Enter-top);
printf("\n請輸入%s號車進(jìn)入車場的時(shí)間:", t-num);
scanf("%d:%d", (t-reach.hour), (t-reach.min));
W-head-next = q-next;
if (q == W-rear) W-rear = W-head;
Enter-stack[Enter-top] = t;
free(q);
} else printf("\n便道里沒有車.\n");
} else printf("\n車場里沒有車.");
}
void List1(SeqStackCar *S){ int i;
if (S-top 0) {
printf("\n車場:");
printf("\n 位置 到達(dá)時(shí)間 車牌號\n");
for (i = 1; i = S-top; i++) {
printf(" %d ", i);
printf(" %d:%d ", S-stack[i]-reach.hour, S-stack[i]-reach.min);
puts(S-stack[i]-num);
}
} else printf("\n車場里沒有車");
}//顯示車場里的車輛情況
void List2(LinkQueueCar *W) {
QueueNode *p;
int i;
p = W-head-next;
if (W-head != W-rear) {
printf("\n等待車輛的號碼為:");
for (i = 1; (p != NULL); i++) {
printf("\n第 %d 車輛.", i);
puts(p-data-num);
p = p-next;
}
} else printf("\n便道里沒有車.");
printf("\n");
}//顯示便道上的車輛情況
void List(SeqStackCar S, LinkQueueCar W) {
int flag, tag;
flag = 1;
while (flag) {
printf(" 查看車輛列表顯示: ");
printf("\n 1.車場列表\n 2.便道列表\n 3.返回主菜單\n");
printf("\n請選擇 1~3:");
while (1) {
scanf("%d", tag);
if (tag = 1 tag = 3) break;
else printf("\n 輸入有誤,請重新選擇 1~3:");
}
switch (tag) {
case 1:List1(S);
break;
case 2:List2(W);
break;
case 3:flag = 0;
system("cls");
break;
default: break;
}
}
}//顯示,遍歷
int main() {
SeqStackCar Enter, Temp;
LinkQueueCar Wait;
int ch;
InitStack(Enter);
InitStack(Temp);
InitQueue(Wait);
while (1) {
printf("\n ############ 歡迎使用停車場系統(tǒng).############\t\n\n");
printf("\n\t * 1. 車輛到達(dá)登記.\t\n");
printf("\n\t * 2. 車輛離開登記.\t\n");
printf("\n\t * 3. 車輛列表顯示.\t\n");
printf("\n\t * 4. 退出系統(tǒng).\t\n\n");
while (1) {
printf(" 請選擇: ");
scanf("%d", ch);
if (ch = 1 ch = 4)break;
else printf("\n 輸入有誤,請重新選擇: 1~4: ");
}
switch (ch) {
case 1:Arrival(Enter, Wait);
break;
case 2:Leave(Enter, Temp, Wait);
break;
case 3:List(Enter, Wait);
break;
case 4:exit(0);
default: break;
}
}
}// 自定義函數(shù)
停車的時(shí)候 new 一個(gè)data 放入數(shù)據(jù)庫 取車的時(shí)候 也new一個(gè) 放入數(shù)據(jù)庫
相減~
import java.util.ArrayList;
public class Tcat {
//假設(shè)停車場有100個(gè)停車位
private int carport=100;
//記錄空的車位
private int noullcat=100;
//初始化類時(shí)傳入空余車位數(shù)量
public Tcat(int mou) {
noullcat=mou;
}
public void setNoullcat(int noullcat) {
this.noullcat = noullcat;
}
public int getNoullcat() {
return noullcat;
}
//記錄進(jìn)來的車的停車時(shí)間
private ArrayList lis=new ArrayList();
//車輛進(jìn)來,空余車位減一
public int catin(){
if(noullcat1){
noullcat--;
lis.add(1);//停車時(shí)間
System.out.print("剩余的車位數(shù)量:"+noullcat);
return noullcat;//返回剩余的車位數(shù)量
}else{
return -1;//返回值-1,表示以及沒有空余的車位
}
}
//返回停車費(fèi)用
public int catvalue(int id){
return (int)lis.get(id)*3;
}
//車輛出去,空余車位加一,返回空余車位,刪除車輛停留的列表
public int catto(int id){
if(noullcatcarport){//判斷空余車位是否小于最大車位
lis.remove(id);
noullcat++;
}
System.out.println("停車費(fèi)用:"+catvalue(id));
System.out.println("剩余的車位數(shù)量:"+noullcat);
return noullcat;
}
}
可以參考一下這個(gè)