С 2-3 дней у меня небольшая проблема с проектом, на самом деле мой проект - сделать круговую развязку с автомобилями. Я новичок в C, поэтому я попытался инициализировать структуру самостоятельно.Как инициализировать структуру в C
Моя структура является:
typedef struct Car Car;
struct Car
{
char vehicule; /* N, S, E, W */
int speed; /* 1 => slow ou 2 => fast */
int pos_i; /* Position courante 'i' du véhicule sur la route */
int pos_j; /* Position courante 'j' du véhicule sur la route */
char alig; /* 'l' for left and 'r' for right */
};
Затем я создаю функцию под названием initCar для того, чтобы поставить две случайные машины на (23,1) в красном круге, (21,1) оранжевый один, как это:
Моя initCar функция:
Car initCar() {
Car car [43][90] = {{0}};
char direction [] = {'N', 'S', 'E', 'W'};
int position [2][2] = {{0}};
position [0][0] = 23;
position [0][1] = 1;
position [1][0] = 21;
position [1][1] = 1;
int i = 0, j = 0;
srand (time (NULL));
for (i = 0; i < 43; i++) {
for (j = 0; j < 90; j++) {
car [i][j].vehicule = direction [rand() % 4];
car [i][j].speed = 1;
car [i][j].pos_i = position [i][j];
car [i][j].pos_j = position [i][j];
car [i][j].alignement = 'r';
}
printf("\n");
}
roadMap(car);
}
Но у меня есть это, когда я выполняю:
RoadMap моя окольными функция,
else if (j < 33) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 33);
}
основе:
(мой mcve)
function.h:
#ifndef FONCTIONS_H_INCLUDED
#define FONCTIONS_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NB_LIN 43
#define NB_COL 90
#define clrscr() printf("\033[H\033[2J")
#define couleur(param) printf("\033[%sm",param)
typedef struct Car Car;
struct Car
{
char vehicule; /* N, S, E, W */
int speed; /* 1 => slow ou 2 => fast */
int pos_i; /* Position courante 'i' du véhicule sur la route */
int pos_j; /* Position courante 'j' du véhicule sur la route */
char alig; /* 'l' for left and 'r' for right */
};
void roadMap (char matrice [NB_LIN][NB_COL]);
void initCarFromEst();
main.c:
#include "function.h"
int main() {
initCarFromEst();
return 0;
}
И function.c:
void myDelay (float i) { // Fais une pause de l'app durant i secondes
clock_t start, end;
start = clock();
while (((end = clock()) - start) <= i * CLOCKS_PER_SEC);
}
void roadMap (Car matrice [NB_LIN][NB_COL]) {
int i = 0, j = 0;
//NB_LIN de NB_COL
for (i = 0; i < NB_LIN; i++) {
for (j = 0; j < NB_COL; j++) {
switch (i) {
case 20: {
if (j < 19) {
do {
printf("■");
j++;
} while (j < 19);
}
else if (j < 33) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 33);
}
else if (j < 53) {
do {
printf("╬");
j++;
} while (j < 53);
}
else if (j < 67) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 67);
}
else if (j < 87) {
do {
printf("■");
j++;
} while (j < 87);
}
}
break;
case 18:
case 22: {
if (j < 10) {
do {
printf("─ ");
j++;
} while (j < 10);
}
else if (j < 23) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 23);
}
else if (j < 43) {
do {
printf("╬");
j++;
} while (j < 43);
}
else if (j < 57) {
do {
printf("%c", matrice[i][j].vehicule);
j++;
} while (j < 57);
}
else if (j < 68) {
do {
printf("─ ");
j++;
} while (j < 68);
}
}
break;
} printf("\n");
}
}
Car initCarFromWest() {
Car car [43][90] = {{0}};
char direction [] = {'N', 'S', 'E', 'W'};
int position [2][2] = {{0}};
position [0][0] = 23;
position [0][1] = 1;
position [1][0] = 21;
position [1][1] = 1;
int i = 0, j = 0;
srand (time (NULL));
for (i = 0; i < 43; i++) {
for (j = 0; j < 90; j++) {
car [i][j].vehicule = direction [rand() % 4];
car [i][j].speed = 1;
car [i][j].pos_i = position [i][j];
car [i][j].pos_j = position [i][j];
car [i][j].alignement = 'r';
}
printf("\n");
}
roadMap(car);
}
Voiture initCarFromEst() {
}
Voiture initCarFromSouth() {
}
Voiture initCarFromNorth() {
}
Моя функция RoadMap почти 400 строк, поэтому я дал вам пример
Если вы хотите разместить только две машины, почему вы создаете 3870 автомобилей? – Guffa
Можете ли вы отправить сообщение [mcve] (http://stackoverflow.com/help/mcve) – Guillaume
Что такое mcve? –