簡體   English   中英

C:在 for 循環中創建結構數組

[英]C: Creating an array of structs in a for loop

我正在嘗試在循環中創建一個由結構組成的數組(或者可能創建結構數組?),該代碼適用於單個結構,但我需要使用 losuj_liczbe 函數在循環中創建其中的 5 個,以分配隨機數. 每次我收到此錯誤時:可能未初始化可變大小的對象。 請幫忙!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define n 5

typedef enum
{
    Bialy,
    Czarny,
    Czerwony,
    Niebieski
}Kolor;

typedef enum
{
    Podstawowe,
    Standard,
    Premium
}Wyposazenie;

typedef struct Samochod
{
    int waga;
    int cena;
    Kolor kolor;
    Wyposazenie wyposazenie;
}Samochod;

int losuj_liczbe(int min, int max)
{
    int rolex;
    time_t tt;
    rolex=time(tt);
    srand(rolex);

    int temp;
    if (max >=min)
        max -= min;
    else{
        temp = min - max;
        min = max;
        max = temp;
    }
    return ((rand()% (max-min) + min));
}


int main()
{
    int i;
    Samochod automobil[n];
    for(i=0; i<n; i++){
    Samochod automobil[i] = {{.waga = losuj_liczbe(500,1500),
                .cena = losuj_liczbe(20000,30000),
                .kolor = ((0 == losuj_liczbe(0,3)) ? Bialy :(1 == losuj_liczbe(0,3)) ? Czarny :(2 == losuj_liczbe(0,3)) ? Czerwony : Niebieski),
                .wyposazenie = ((0 == losuj_liczbe(0,2)) ? Podstawowe :(1 == losuj_liczbe(0,2)) ? Standard : Premium)}};
    }
    printf("%d | %d | %s | %s",
           automobil.waga,
           automobil.cena,
           (0 == automobil.kolor) ? "Bialy" : (1 == automobil.kolor) ? "Czarny" : (2 == automobil.kolor) ? "Czerwony" : "Niebieski",
           (0 == automobil.wyposazenie) ? "Podstawowe" : (1 == automobil.wyposazenie) ? "Standard" : "Premium");

    return 0;
}

有很多問題:

僅舉幾個:

  • srand應該在程序開始時只調用一次。 還有一個srand(time(NULL)); 足夠。 閱讀srandtime函數的文檔
  • 第一個循環中automobil[i]的賦值都是錯誤的
  • 您的printf錯誤且毫無意義,您需要執行另一個for循環來顯示所有汽車。
  • 您需要在printf格式字符串的末尾放置一個\\n
  • 按照慣例,所有宏名稱都應該大寫,因此您應該使用#define NUMBEROFCARS 5而不是#define n 5 #define NUMBEROFCARS 5 並且您應該絕對避免使用短宏名稱,例如NAX 這只是可讀性的問題。

你可能想要這個:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUMBEROFCARS 5

typedef enum
{
  Bialy,
  Czarny,
  Czerwony,
  Niebieski
}Kolor;

typedef enum
{
  Podstawowe,
  Standard,
  Premium
}Wyposazenie;

typedef struct Samochod
{
  int waga;
  int cena;
  Kolor kolor;
  Wyposazenie wyposazenie;
}Samochod;

int losuj_liczbe(int min, int max)
{
  int temp;
  if (max >= min)
    max -= min;
  else {
    temp = min - max;
    min = max;
    max = temp;
  }
  return ((rand() % (max - min) + min));
}


int main()
{
  srand(time(NULL));

  int i;
  Samochod automobil[NUMBEROFCARS];

  for (i = 0; i < NUMBEROFCARS; i++) {
    automobil[i].waga = losuj_liczbe(500, 1500);
    automobil[i].cena = losuj_liczbe(20000, 30000);
    automobil[i].kolor = ((0 == losuj_liczbe(0, 3)) ? Bialy : (1 == losuj_liczbe(0, 3)) ? Czarny : (2 == losuj_liczbe(0, 3)) ? Czerwony : Niebieski);
    automobil[i].wyposazenie = ((0 == losuj_liczbe(0,2)) ? Podstawowe : (1 == losuj_liczbe(0,2)) ? Standard : Premium);
  }

  for (i = 0; i < NUMBEROFCARS; i++) {
    printf("%d | %d | %s | %s\n",
      automobil[i].waga,
      automobil[i].cena,
      (0 == automobil[i].kolor) ? "Bialy" : (1 == automobil[i].kolor) ? "Czarny" : (2 == automobil[i].kolor) ? "Czerwony" : "Niebieski",
      (0 == automobil[i].wyposazenie) ? "Podstawowe" : (1 == automobil[i].wyposazenie) ? "Standard" : "Premium");
  }

  return 0;
}

這個程序還是別扭的,尤其是把顏色轉換成顏色名稱的可怕的三元表達式應該重構。

查看正確代碼版本的內嵌注釋:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>

/* Use readable names */
#define NUM_OF_AUTOMOBIL 5

typedef enum {
    Bialy = 0,
    Czarny,
    Czerwony,
    Niebieski,
} Kolor;

/* Use predefined array of string.
 * Look at printf() to see how to use it to simplify your code
 */
const char *KolorStr[] = {
    "Bialy",
    "Czarny",
    "Czerwony",
    "Niebieski"
};

typedef enum {
    Podstawowe = 0,
    Standard,
    Premium
} Wyposazenie;

const char *WyposazenieStr[] = {
    "Podstawowe",
    "Standard",
    "Premium"
};

typedef struct Samochod {
    int waga;
    int cena;
    Kolor kolor;
    Wyposazenie wyposazenie;
} Samochod;

int losuj_liczbe(int min, int max)
{
    assert(max != min);

    if (max < min) {
        int temp = min - max;
        min = max;
        max = temp;
    }

    /* Use parenthes even if you completely sure of precedence, this allows
     * to avoid mistakes during code modification
     */
    return ((rand() % (max - min)) + min);
}


int main(int argc, char *argv[])
{
    Samochod automobil[NUM_OF_AUTOMOBIL];

    /* Init random generator once over all your program
     */
    srand(time(NULL));

    for (int i = 0; i < NUM_OF_AUTOMOBIL; i++) {
        automobil[i].waga = losuj_liczbe(500, 1500);
        automobil[i].cena = losuj_liczbe(20000, 30000);
        /* You can convert int into enum directly if
         * limit int with enum values only */
        automobil[i].kolor = losuj_liczbe(Bialy, Niebieski);
        automobil[i].wyposazenie = losuj_liczbe(Podstawowe, Premium);
    }

    /* Use loop over your array to show values
     */
    for (int i = 0; i < NUM_OF_AUTOMOBIL; i++) {
        /* Use \n to terminate your lines
         */
        printf("%d | %d | %s | %s\n",
                automobil[i].waga,
                automobil[i].cena,
                KolorStr[automobil[i].kolor],
                WyposazenieStr[automobil[i].wyposazenie]);
    }

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM