O’ZBEKISTON RESPUBLIKASI
AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI
RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
QARSHI FILIALI
KOMPYUTER INJINIRINGI FAKULTETI
AXBOROT XAVFSIZLIGI YO’NALISHI
I-BOSQICH AX-11-22 GURUH TALABASI
ESANOV ASLIDDINNING
“DASTURLASH”
FANIDAN TAYYORLAGAN
3-AMALIY MASHG`ULOT
Bajardi: ESANOV ASLIDDIN
Qabul qildi: LAZIZ XUDOYOROV
QARSHI-2023
7
|
Haydovchi yo‘lovchini o‘z manziliga yetkazib olib borishi kerak. Lekin yo‘l triband. Sitaforning Yashil rangi yongunicha 1 dan N sekund o‘tmoqda. N gacha bo‘lgan sekundda qizil mashinalar ketma - ket kelishsa sitaforning qizil rangi yonmasdan yana Yashil rang 1 dan N gacha yonib turaverar ekan. Yo‘lning boshlanishiga shuning uchun ham bir xil rangli mashinalar ketma ket ket kelsin deb yozib qo‘yilgan ekan. Mashinalar ranglarini o‘zgartirish imkoniyati mavjudligidan foydalanib, yo‘lovchini qisqa vaqt ichida manziliga yetkazuvchi dastur tuzing.
|
#include
#include
#include
#include
using namespace std;
// Function to simulate car movement on the road
void moveCar(int id, int start, int end, int greenDuration) {
int current = start;
int delta = 1; // Initial direction: forward
while (true) {
cout << "Car " << id << " is at position " << current << endl;
if (current == end) {
cout << "Car " << id << " has arrived at destination!" << endl;
break;
}
current += delta;
if (current == start || current == end) {
delta = -delta; // Change direction
}
// Change traffic light color when necessary
if (current == start + greenDuration + 1 || current == end - greenDuration - 1) {
cout << "Changing traffic light!" << endl;
this_thread::sleep_for(chrono::seconds(1)); // Wait 1 second to change traffic light
cout << "Traffic light changed!" << endl;
}
// Wait 1 second to simulate car movement
this_thread::sleep_for(chrono::seconds(1));
}
}
int main() {
// Set road length and green duration
int roadLength = 10;
int greenDuration = 4;
// Create a vector to store the cars
vector carIds = {1, 2, 3, 4};
// Simulate car movement on the road
for (int id : carIds) {
thread t(moveCar, id, 0, roadLength, greenDuration);
t.join();
}
return 0;
}
Do'stlaringiz bilan baham: |