C++實(shí)現(xiàn)路口交通燈模擬系統(tǒng)
交通燈信號控制是交通工具現(xiàn)代化的產(chǎn)物,在平面交叉口,為了把可能發(fā)生沖突的車流從時(shí)空上分離,必須通過交通信號對交通流進(jìn)行有效的引導(dǎo)和調(diào)度。
設(shè)計(jì)要求:
(1) 設(shè)計(jì)一個(gè)十字路口的交通燈控制電路,要求南北方向和東西方向兩個(gè)交叉路口的車輛交替運(yùn)行,每個(gè)方向綠燈亮30秒,兩個(gè)方向能根據(jù)車流量的大小自動調(diào)節(jié)通行時(shí)間,車流量大,通行時(shí)間增加30秒,車流量小,通行時(shí)間仍然是30秒。
(2) 在路燈轉(zhuǎn)為紅燈時(shí),要求黃燈先亮3秒鐘,才能變換運(yùn)行車道。
系統(tǒng)算法設(shè)計(jì)圖:

#include<iostream> ?
#include<windows.h> ?
using namespace std; ?
??
void way1(int a,int y); ? ? //交通燈的函數(shù)
void way2(int b);
?
int green1, green2; ? ?//定義交通燈的紅,黃,綠燈的變量和賦初值
int yellow1, yellow2;
int red1, red2;
int green[2] = {31,61};
int yellow = 4;
int red[3] = {34,64};
?
int main()
{
?? ?int car1, car2, car3, car4; ? ? //車輛數(shù)變量
?? ?int i = 0, j;
?? ?char d;
?? ?cout<<endl<<"***開始模擬智能交通燈系統(tǒng)***"<<endl<<endl;
?? ?while(1)
?? ?{
?? ??? ?cout<<"請?jiān)O(shè)置東車道車輛數(shù):"; ? ? ?//設(shè)置車輛數(shù)
?? ??? ?cin>>car1;
?? ??? ?cout<<"請?jiān)O(shè)置西車道車輛數(shù):";
?? ??? ?cin>>car2;
?? ??? ?cout<<"請?jiān)O(shè)置北車道車輛數(shù):";
?? ??? ?cin>>car3;
?? ??? ?cout<<"請?jiān)O(shè)置南車道車輛數(shù):";
?? ??? ?cin>>car4;
?? ??? ?cout<<endl<<endl;
?? ??? ?cout<<"******開始模擬******"<<endl;
?? ??? ?if((car1+car2)-(car3+car4)>10) ? ? //根據(jù)車輛數(shù)來選擇交通燈計(jì)時(shí)函數(shù)
?? ??? ?{
?? ??? ??? ?j = i+1;
?? ??? ??? ?way1(j, i);
?? ??? ?}
?? ??? ?else if(((car1+car2)-(car3+car4)>=0)&&((car1+car2)-(car3+car4)<=10))
?? ??? ?{
?? ??? ??? ?way2(i);
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?j = i+1;
?? ??? ??? ?way1(i, j);
?? ??? ?}
?? ??? ?cout<<"若想重新設(shè)置車輛數(shù)請按Y,若退出請按N。";
?? ??? ?cin>>d;
?? ??? ?if(d == 'Y'||d == 'y')
?? ??? ?{
?? ??? ??? ?cout<<endl;
?? ??? ??? ?continue;
?? ??? ?}
?? ??? ?else if(d == 'N'||d == 'n')
?? ??? ??? ?break;
?? ?}
?
?? ?return 0;
}
?
void way1(int a,int y)
{
?? ?green1=green[a]; ? ? ?//先給交通燈賦初值
?? ?green2=0;
?? ?yellow1=yellow2=0;
?? ?red1=0;
?? ?red2=red[a];
?? ?int c;
?? ?char d;
?? ?while(1) ? ? ?//東西車道的交通燈
?? ?{
?? ??? ?for(c=99;c>0;c--)
?? ??? ?{ ?
?? ??? ??? ?cout<<endl<<"------東西車道------"<<endl;?
?? ??? ??? ?if(green1>0)
?? ??? ??? ?{
?? ??? ??? ??? ?if(green1==green[a])
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?green1--;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout<<" ? >>> 綠燈 <<< "<<green1<<" 秒\n"; ?
?? ??? ??? ??? ??? ?green1--;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if(green1==0) ?
?? ??? ??? ??? ??? ?yellow1=yellow; ?
?? ??? ??? ?} ?
?? ??? ??? ?if(yellow1>0) ?
?? ??? ??? ?{ ?
?? ??? ??? ??? ?if(yellow1==4) ?
?? ??? ??? ??? ??? ?yellow1--; ? ?
?? ??? ??? ??? ?else ?
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?cout<<" ? >>> 黃燈 <<< "<<yellow1<<" 秒\n"; ?
?? ??? ??? ??? ??? ?yellow1--; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(yellow1==0) ?
?? ??? ??? ??? ?{ ? ? ??
?? ??? ??? ??? ??? ?red1=red[y]; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ?} ?
?? ??? ??? ?if(red1>0) ?
?? ??? ??? ?{ ?
?? ??? ??? ??? ?if(red1==red[y]) ?
?? ??? ??? ??? ??? ?red1--; ?
?? ??? ??? ??? ?else ??
?? ??? ??? ??? ?{?
?? ??? ??? ??? ??? ?cout<<" ? >>> 紅燈 <<< "<<red1<<" 秒\n"; ?
?? ??? ??? ??? ??? ?red1--; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(red1==0) ?
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?green1=green[a]; ? ?
?? ??? ??? ??? ?} ?
?? ??? ??? ?}
?? ??? ??? ?/***********************************************************************************/
?? ??? ??? ?cout<<endl<<"------南北車道------"<<endl; ? ? ? ? ? ? ? ? ? //南北車道的交通燈
?? ??? ??? ?if(red2>0)
?? ??? ??? ?{
?? ??? ??? ??? ?if(red2==red[a])
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?red2--;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout<<" ? >>> 紅燈 <<< "<<red2<<" 秒\n"; ?
?? ??? ??? ??? ??? ?red2--;
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(red2==0) ?
?? ??? ??? ??? ??? ?green2=green[y]; ?
?? ??? ??? ?} ?
?? ??? ??? ?if(green2>0) ?
?? ??? ??? ?{ ?
?? ??? ??? ??? ?if(green2==green[y]) ?
?? ??? ??? ??? ??? ?green2--; ? ?
?? ??? ??? ??? ?else ?
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?cout<<" ? >>> 綠燈 <<< "<<green2<<" 秒\n"; ?
?? ??? ??? ??? ??? ?green2--; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(green2==0) ?
?? ??? ??? ??? ?{ ? ? ??
?? ??? ??? ??? ??? ?yellow2=yellow; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ?} ?
?? ??? ??? ?if(yellow2>0) ?
?? ??? ??? ?{ ?
?? ??? ??? ??? ?if(yellow2==4) ?
?? ??? ??? ??? ??? ?yellow2--; ?
?? ??? ??? ??? ?else ??
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?cout<<" ? >>> 黃燈 <<< "<<yellow2<<" 秒\n"; ?
?? ??? ??? ??? ??? ?yellow2--; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(yellow2==0) ?
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?red2=red[a]; ? ?
?? ??? ??? ??? ?} ?
?? ??? ??? ?} ?
?
?
?? ??? ??? ?Sleep(1000);
?? ??? ??? ?system("cls");
?? ??? ??? ?
?? ??? ?}
?? ??? ?cout<<"若想繼續(xù)請按C,若想返回上一級請按R。";
?? ??? ?cin>>d;
?? ??? ?if(d == 'C'||d == 'c')
?? ??? ?{
?? ??? ??? ?cout<<endl;
?? ??? ??? ?continue;
?? ??? ?}
?? ??? ?else if(d == 'R'||d == 'r')
?? ??? ?{
?? ??? ??? ?cout<<endl<<endl;
?? ??? ??? ?break;
?? ??? ?}
?? ?}
}
?
?
void way2(int b)
{
?? ?green1=green[b];
?? ?green2=0;
?? ?yellow1=yellow2=0;
?? ?red1=0;
?? ?red2=red[b];
?? ?int c;
?? ?char d;
?? ?while(1)
?? ?{
?? ??? ?for(c=69;c>0;c--)
?? ??? ?{ ?
?? ??? ??? ?cout<<endl<<"------東西車道------"<<endl;?
?? ??? ??? ?if(green1>0)
?? ??? ??? ?{
?? ??? ??? ??? ?if(green1==green[b])
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?green1--;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout<<" ? >>> 綠燈 <<< "<<green1<<" 秒"<<endl; ?
?? ??? ??? ??? ??? ?green1--;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if(green1==0) ?
?? ??? ??? ??? ??? ?yellow1=yellow; ?
?? ??? ??? ?} ?
?? ??? ??? ?if(yellow1>0) ?
?? ??? ??? ?{?? ??
?? ??? ??? ??? ?if(yellow1==4) ?
?? ??? ??? ??? ??? ?yellow1--; ? ?
?? ??? ??? ??? ?else ?
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?cout<<" ? >>> 黃燈 <<< "<<yellow1<<" 秒"<<endl; ?
?? ??? ??? ??? ??? ?yellow1--; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(yellow1==0) ?
?? ??? ??? ??? ?{ ? ? ??
?? ??? ??? ??? ??? ?red1=red[b]; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ?} ?
?? ??? ??? ?if(red1>0) ?
?? ??? ??? ?{ ?
?? ??? ??? ??? ?if(red1==red[b]) ?
?? ??? ??? ??? ??? ?red1--; ?
?? ??? ??? ??? ?else ??
?? ??? ??? ??? ?{?
?? ??? ??? ??? ??? ?cout<<" ? >>> 紅燈 <<< "<<red1<<" 秒"<<endl; ?
?? ??? ??? ??? ??? ?red1--; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(red1==0) ?
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?green1=green[b]; ? ?
?? ??? ??? ??? ?}?? ?
?? ??? ??? ?}
?? ??? ??? ?/***********************************************************************************/
?? ??? ??? ?cout<<endl<<"------南北車道------"<<endl;?
?? ??? ??? ?if(red2>0)
?? ??? ??? ?{
?? ??? ??? ??? ?if(red2==red[b])
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?red2--;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout<<" ? >>> 紅燈 <<< "<<red2<<" 秒"<<endl; ?
?? ??? ??? ??? ??? ?red2--;
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(red2==0) ?
?? ??? ??? ??? ??? ?green2=green[b]; ?
?? ??? ??? ?} ?
?? ??? ??? ?if(green2>0) ?
?? ??? ??? ?{ ?
?? ??? ??? ??? ?if(green2==green[b]) ?
?? ??? ??? ??? ??? ?green2--; ? ?
?? ??? ??? ??? ?else ?
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?cout<<" ? >>> 綠燈 <<< "<<green2<<" 秒"<<endl; ?
?? ??? ??? ??? ??? ?green2--; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(green2==0) ?
?? ??? ??? ??? ?{ ? ? ??
?? ??? ??? ??? ??? ?yellow2=yellow; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ?} ?
?? ??? ??? ?if(yellow2>0) ?
?? ??? ??? ?{ ?
?? ??? ??? ??? ?if(yellow2==4) ?
?? ??? ??? ??? ??? ?yellow2--; ?
?? ??? ??? ??? ?else ??
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?cout<<" ? >>> 黃燈 <<< "<<yellow2<<" 秒"<<endl; ?
?? ??? ??? ??? ??? ?yellow2--; ?
?? ??? ??? ??? ?} ?
?? ??? ??? ??? ?if(yellow2==0) ?
?? ??? ??? ??? ?{ ?
?? ??? ??? ??? ??? ?red2=red[b]; ? ?
?? ??? ??? ??? ?}?? ?
?? ??? ??? ?} ?
?
?
?? ??? ??? ?Sleep(1000);
?? ??? ??? ?system("cls");
?? ??? ??? ?
?? ??? ?}
?? ??? ?cout<<"若想繼續(xù)請按C,若想返回上一級請按R。";
?? ??? ?cin>>d;
?? ??? ?if(d == 'C'||d == 'c')
?? ??? ?{
?? ??? ??? ?cout<<endl;
?? ??? ??? ?continue;
?? ??? ?}
?? ??? ?else if(d == 'R'||d == 'r')
?? ??? ?{
?? ??? ??? ?cout<<endl<<endl;
?? ??? ??? ?break;
?? ??? ?}
?? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言使用stdlib.h庫函數(shù)的二分查找和快速排序的實(shí)現(xiàn)代碼
以下是對C語言使用stdlib.h庫函數(shù)的二分查找和快速排序的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下。希望對大家有所幫助2013-10-10
C++ Boost MultiIndex使用詳細(xì)介紹
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個(gè)可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱2022-11-11
C語言鏈表實(shí)現(xiàn)銷售管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言鏈表實(shí)現(xiàn)銷售管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
C++實(shí)現(xiàn)LeetCode(159.最多有兩個(gè)不同字符的最長子串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(159.最多有兩個(gè)不同字符的最長子串),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

