C:在for循环中创建一个结构数组

时间:2017-10-09 10:00:19

标签: c arrays struct

我试图在循环中创建一个由结构(或者可能创建结构数组?)创建的数组,该代码适用于单个结构,但我需要在循环中创建其中的5个usig losuj_liczbe函数,asign随机数。每次我收到此错误: 可能无法初始化可变大小的对象。 请帮忙!

#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;
}

2 个答案:

答案 0 :(得分:1)

有很多问题:

仅举几例:

  • srand应该只在程序开始时调用一次。一个srand(time(NULL));就够了。阅读srandtime函数
  • 的文档
  • 第一个循环中automobil[i]的分配都是错误的
  • 您的printf错误且无意义,您需要执行另一个for循环来显示所有汽车。
  • 您需要在\n格式字符串的末尾添加printf
  • 按照惯例,所有宏名称都应该是大写字母,因此您应该#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;
}

这个程序仍然很尴尬,特别是将颜色转换为颜色名称的可怕三元表达式应该重构。

答案 1 :(得分:0)

查看正确代码版本的内嵌注释:

Cars automobil[5]; 
Cars mobilOne={};

for(i=0; i<5; i++)
{
    if((i+1)==5)
    {
        break;
    }else
    {
        if (automobil[i].weight> automobil[i+1].weight)
        {
            mobilOne = automobil[i];
            automobil[i] = automobil[i+1];
            automobil[i+1] = mobilOne;
        }
    }
}