Ошибка компиляции - C (СИ)
Формулировка задачи:
#include<windows.h> //подключаем библиотеки #include<stdlib.h> #include<time.h> #include<process.h> #include<stdio.h> HANDLE hstdin, hstdout, hthread1, hthread2, hthread3, hthread4, hthread5, hthread6, hthread7, hthread8, em, adw, dw1, dw2, dw3; //хэндлы для работы нитей, семафоров и ввода вывода int imo = 0, dmo = 0, gas = 0, m1 = 0, m2 = 0, m3 = 0;//для очереди машин, водителей int N; //число N unsigned __stdcall gas1( void* pArguments)// Нить 1й колонки { int i; COORD pos; while (1) { WaitForSingleObject(dw1, INFINITE); int randomize(); // выполняется до завершения основной программы Sleep(rand()% 500); WaitForSingleObject(em, INFINITE); m1 = 0; SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); pos.X = 17; pos.Y = 1; SetConsoleCursorPosition(hstdout, pos); printf(" "); if (imo>0) { m1 = 1; imo--; pos.Y = 5;//перемещение машины по Х for (pos.X = 39; pos.X>16; pos.X--) { SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); SetConsoleCursorPosition(hstdout, pos); printf("M"); ReleaseMutex(em); Sleep(50); WaitForSingleObject(em, INFINITE); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED); SetConsoleCursorPosition(hstdout, pos); printf(" "); }//перемещение машины по Y pos.X = 17; for (pos.Y = 5; pos.Y>1; pos.Y--) { SetConsoleCursorPosition(hstdout, pos); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("M"); ReleaseMutex(em); Sleep(50); WaitForSingleObject(em, INFINITE); SetConsoleCursorPosition(hstdout, pos); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED); printf(" "); } pos.X = 17; pos.Y = 1; SetConsoleCursorPosition(hstdout, pos); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("M"); dmo++; }//водитель+ ReleaseMutex(em); ReleaseMutex(dw1); Sleep(rand()% 1500); gas = 1; } } unsigned __stdcall gas2(void* pArguments) { int i; COORD pos; while (TRUE) { WaitForSingleObject(dw2, INFINITE); int randomize(); Sleep(rand()% 500); WaitForSingleObject(em, INFINITE); m2 = 0; SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); pos.X = 25; pos.Y = 1; SetConsoleCursorPosition(hstdout, pos); printf(" "); if (imo>0) { m2 = 1; imo--; pos.Y = 5;//перемещение машины по Х for (pos.X = 39; pos.X>24; pos.X--) { SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); SetConsoleCursorPosition(hstdout, pos); printf("M"); ReleaseMutex(em); Sleep(50); WaitForSingleObject(em, INFINITE); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED); SetConsoleCursorPosition(hstdout, pos); printf(" "); }//перемещение машины по Y pos.X = 25; for (pos.Y = 5; pos.Y>1; pos.Y--) { SetConsoleCursorPosition(hstdout, pos); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("M"); ReleaseMutex(em); Sleep(50); WaitForSingleObject(em, INFINITE); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED); SetConsoleCursorPosition(hstdout, pos); printf(" "); } pos.X = 25; pos.Y = 1; SetConsoleCursorPosition(hstdout, pos); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("M"); dmo++; }//водитель+ ReleaseMutex(em); ReleaseMutex(dw2); Sleep(rand()% 15000); gas = 2; } } unsigned __stdcall gas3(void* pArguments) { int i; COORD pos; while (TRUE) { WaitForSingleObject(dw3, INFINITE); int randomize(); Sleep(rand()% 500); WaitForSingleObject(em, INFINITE); m3 = 0; SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); pos.X = 33; pos.Y = 1; SetConsoleCursorPosition(hstdout, pos); printf(" "); if (imo>0) { m3 = 1; imo--; pos.Y = 5;//перемещение машины по Х for (pos.X = 39; pos.X>32; pos.X--) { SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); SetConsoleCursorPosition(hstdout, pos); printf("M"); ReleaseMutex(em); Sleep(50); WaitForSingleObject(em, INFINITE); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED); SetConsoleCursorPosition(hstdout, pos); printf(" "); }//перемещение машины по Y pos.X = 33; for (pos.Y = 5; pos.Y>1; pos.Y--) { SetConsoleCursorPosition(hstdout, pos); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("M"); ReleaseMutex(em); Sleep(50); WaitForSingleObject(em, INFINITE); SetConsoleCursorPosition(hstdout, pos); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED); printf(" "); } pos.X = 33; pos.Y = 1; SetConsoleCursorPosition(hstdout, pos); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("M"); dmo++; }//водитель+ ReleaseMutex(em); ReleaseMutex(dw3); Sleep(rand()% 1500); gas = 3; } } unsigned __stdcall drv1(void* pArguments)// водители прорисовка { int i; COORD pos; while (TRUE) { int randomize(); WaitForSingleObject(em, INFINITE); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED); pos.X = 13; pos.Y = 7; SetConsoleCursorPosition(hstdout, pos); for (i = -3; i<dmo; i++) { printf(" "); } if (dmo>0) { SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); SetConsoleCursorPosition(hstdout, pos); for (i = 0; i<dmo; i++) { printf("D"); }//очередь водителей в кассу //машин-1 } SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED); pos.X = 40; pos.Y = 5; SetConsoleCursorPosition(hstdout, pos); for (i = -3; i<imo; i++) { printf(" "); } if (imo>0) { SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); SetConsoleCursorPosition(hstdout, pos); for (i = 0; i<imo; i++) { printf("M"); }//очередь водителей в кассу //машин-1 } SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); pos.X = 0; pos.Y = 9; SetConsoleCursorPosition(hstdout, pos); printf(" \n \n "); SetConsoleCursorPosition(hstdout, pos); printf(" Cars total = %d\n Cars here = %d\n Cars somewhere else = %d", N, (imo + m1 + m2 + m3), (N - imo - m1 - m2 - m3)); ReleaseMutex(em); Sleep(50); } } unsigned __stdcall drv2_1(void* pArguments)// водители счетчик очереди { int i; COORD pos; while (TRUE) { int randomize(); WaitForSingleObject(dw1, INFINITE); WaitForSingleObject(adw, INFINITE); if (dmo>0) { Sleep(500 + rand()% 1500); dmo--; } ReleaseMutex(dw1); ReleaseMutex(adw); } } unsigned __stdcall drv2_2(void* pArguments)// водители счетчик очереди { int i; COORD pos; while (TRUE) { int randomize(); WaitForSingleObject(dw2, INFINITE); WaitForSingleObject(adw, INFINITE); if (dmo>0) { Sleep(500 + rand()% 1500); dmo--; } ReleaseMutex(dw2); ReleaseMutex(adw); } } unsigned __stdcall drv2_3(void* pArguments)// водители счетчик очереди { int i; COORD pos; while (TRUE) { int randomize(); WaitForSingleObject(dw3, INFINITE); WaitForSingleObject(adw, INFINITE); if (dmo>0) { Sleep(500 + rand()%1500); dmo--; } ReleaseMutex(dw3); ReleaseMutex(adw); } } void main() //основная программа { //обозначаем что нити будут расположены после основной части программы unsigned int threadid1, threadid2, threadid3, threadid4, threadid5, threadid6, threadid7, threadid8; //идентификатор для нитей COORD pos;//координатная переменная int i, j = 0;//переменные для циклов hstdout = GetStdHandle(STD_OUTPUT_HANDLE); // инициализация хэндлов ввода, ввывода hstdin = GetStdHandle(STD_INPUT_HANDLE); em = CreateMutex(NULL, TRUE, NULL);//инициализация семафоров Mutex if (em == NULL) { printf("Mutex=%d\n", em); getchar(); } dw1 = CreateMutex(NULL, TRUE, NULL); if (dw1 == NULL) { printf("Mutex=%d\n", dw1); getchar(); } dw2 = CreateMutex(NULL, TRUE, NULL); if (dw2 == NULL) { printf("Mutex=%d\n", dw2); getchar(); } dw3 = CreateMutex(NULL, TRUE, NULL); if (dw3 == NULL) { printf("Mutex=%d\n", dw3); getchar(); } adw = CreateMutex(NULL, TRUE, NULL); if (adw == NULL) { printf("Mutex=%d\n", adw); getchar(); } SetConsoleTextAttribute(hstdout, FOREGROUND_BLUE); for (i = 0; i<10; i++) { printf("\n\n\n\n\n\n\n"); }// очистка экрана printf("input N> "); scanf("%d", &N); pos.X = 0; //установка координат курсора вывода pos.Y = 0; //рисование заправки SetConsoleCursorPosition(hstdout, pos); printf(" "); SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf(""); SetConsoleTextAttribute(hstdout, FOREGROUND_RED); printf(" "); SetConsoleTextAttribute(hstdout, FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("Gas #1"); SetConsoleTextAttribute(hstdout, FOREGROUND_RED); printf(" "); SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("Gas #2"); SetConsoleTextAttribute(hstdout, FOREGROUND_RED); printf(" "); SetConsoleTextAttribute(hstdout, FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("Gas #3\n"); SetConsoleTextAttribute(hstdout, FOREGROUND_RED); printf(" "); SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); printf("\n\n\n\n\n\n Cash\n"); SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); printf("\n Anaai iaoei = %d\n N = %d\n", (imo + m1 + m2 + m3), N); ReleaseMutex(em); ReleaseMutex(dw1); ReleaseMutex(dw2); ReleaseMutex(dw3); ReleaseMutex(adw); hthread1 = (HANDLE)_beginthreadex(NULL, 4096, &gas1, NULL, 0, &threadid1); //Запуск нитей if (hthread1 == NULL) { printf("hthread1=%d\n", hthread1); getchar(); } hthread2 = (HANDLE)_beginthreadex(NULL, 4096, &gas2, NULL, 0, &threadid2); if (hthread2 == NULL) { printf("hthread2=%d\n", hthread2); getchar(); } hthread3 = (HANDLE)_beginthreadex(NULL, 4096, &gas3, NULL, 0, &threadid3); if (hthread3 == NULL) { printf("hthread3=%d\n", hthread3); getchar(); } hthread4 = (HANDLE)_beginthreadex(NULL, 4096, &drv1, NULL, 0, &threadid4); if (hthread4 == NULL) { printf("hthread4=%d\n", hthread4); getchar(); } hthread5 = (HANDLE)_beginthreadex(NULL, 4096, &drv2_1, NULL, 0, &threadid5); if (hthread5 == NULL) { printf("hthread5=%d\n", hthread5); getchar(); } hthread7 = (HANDLE)_beginthreadex(NULL, 4096, &drv2_2, NULL, 0, &threadid7); if (hthread7 == NULL) { printf("hthread7=%d\n", hthread7); getchar(); } hthread8 = (HANDLE)_beginthreadex(NULL, 4096, &drv2_3, NULL, 0, &threadid8); if (hthread8 == NULL) { printf("hthread8=%d\n", hthread8); getchar(); } do { for (i = 5; i>0; i--) { if (imo<N - i - m1 - m2 - m3) { imo = imo + rand()% i; break; } } Sleep(rand()% 2500); j++; } while (j<150); CloseHandle(hthread1); CloseHandle(hthread2); CloseHandle(hthread3); CloseHandle(hthread4); CloseHandle(hthread5); CloseHandle(hthread6); CloseHandle(hthread7); CloseHandle(hthread8); CloseHandle(hstdout); CloseHandle(em); CloseHandle(dw1); CloseHandle(dw2); CloseHandle(dw3); CloseHandle(adw); CloseHandle(hstdin); }
Решение задачи: «Ошибка компиляции»
textual
Листинг программы
int main(){ // тут программа return 0; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д