[c/c++] Tablica struktur

0

Witam! Mam do zrobienia następujące zadanie:
Napisz program, który wylosuje n dat i zapisze je w tablicy struktur, a następnie posortuje te daty od najwcześniejszej lub od ostatniej.

Jak na razie napisałem tyle, może da się coś z tego wykorzystać:

#include "stdafx.h"
#include "stdlib.h"
#include <ctime>

struct Data{
		short d,m,r;
};
struct Miesiac{
	int dni;
	char *tekst;
};
struct Data D;
struct Miesiac Tab[12]={
	{31,"styczen"},
	{28,"luty"},
	{31,"marzec"},
	{30,"kwiecień"},
	{31,"maj"},
	{30,"czerwiec"},
	{31,"lipiec"},
	{31,"sierpien"},
	{30,"wrzesien"},
	{31,"pazdziernik"},
	{30,"listopad"},
	{31,"grudzien"}
};

/*
struct Dat{
	int p;
	int q;
	int s;
};
struct Dat (int n){
	int *Ata;
	Ata=(int(*)) calloc(n+1,sizeof(*Ata));
	if (tab == NULL) goto brak_pamieci;

	for(k=0, k<n, k++)
};
*/

struct Dat{
	int *p, *q, *s;
	p=(int(*)) calloc(n,sizeof(*p));
	q=(int(*)) calloc(n,sizeof(*q));
	s=(int(*)) calloc(n,sizeof(*s));

	if (p == NULL){
		free(p);
		free(q);
		free(s);
	}
	else if (q == NULL){
		free(p);
		free(q);
		free(s);
	}
	else if (s == NULL){
		free(p);
		free(q);
		free(s);
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	int n,w;
	fprintf(stderr, "Podaj ilosc dat do wylosowania: ");
	scanf_s("%d", &n);

	for (w=0, w<n, w++){
		srand(time(0));

		//a=zakres_dolny+rand()%(zakres_gorny-zakres_dolny+1)

		D.r=(-5000)+rand()%(5000-(-5000)+1);
		D.m=1+rand()%(12-1+1);
		D.d=1+rand()%(Tab[D.m-1].dni-1+1);

		printf("Data wprowazona: %hd %hd %hdr.\n", D.d, D.m, D.r);
		printf(" = %hd %s %hd r.\n", D.d, Tab[D.m-1].tekst, D.r);

		Dat.p[w]=D.d;
		Dat.q[w]=D.m;
		Dat.s[w]=D.r;

	}


		free(p);
		free(q);
		free(s);

	return 0;
}

Największy problem mam z tablicą struktur.

0

Napisałem od nowa. Teraz mam coś takiego i wyskakują mi dwa błędy.

#include "stdafx.h"
#include "stdlib.h"
#include <ctime>

struct Data{
	int d,m,r;
};

struct Data D[100];

struct Miesiac{
	int dni;
	char *tekst;
};

struct Miesiac Tab[12]={
	{31,"styczen"},
	{28,"luty"},
	{31,"marzec"},
	{30,"kwiecień"},
	{31,"maj"},
	{30,"czerwiec"},
	{31,"lipiec"},
	{31,"sierpien"},
	{30,"wrzesien"},
	{31,"pazdziernik"},
	{30,"listopad"},
	{31,"grudzien"}
};


int _tmain(int argc, _TCHAR* argv[])
{
	int n,w=0;
	fprintf(stderr, "Podaj ilosc dat do wylosowania: ");
	scanf_s("%d", &n);

	for (w; w<n; w++){
		srand(time(0));

		//a=zakres_dolny+rand()%(zakres_gorny-zakres_dolny+1)

		D[w].r=(-5000)+rand()%(5000-(-5000)+1);
		D[w].m=1+rand()%(12-1+1);
		D[w].d=1+rand()%(Tab[D.m-1].dni-1+1);

		printf("Data wprowazona: %hd %hd %hdr.\n", D[w].d, D[w].m, D[w].r);
	}

	return 0;
}
0

Nie łaska napisać JAKIE BŁĘDY? CO MY TU WRÓŻKI?

0

Już sobie poradziłem z tym problemem. Pojawił się kolejny przy sortowaniu:

#include "stdafx.h"
#include "stdlib.h"
#include <ctime>

struct Data{
	int d,m,r;
};

struct Data D[100], H;

struct Miesiac{
	int dni;
	char *tekst;
};

struct Miesiac Tab[12]={
	{31,"styczen"},
	{28,"luty"},
	{31,"marzec"},
	{30,"kwiecień"},
	{31,"maj"},
	{30,"czerwiec"},
	{31,"lipiec"},
	{31,"sierpien"},
	{30,"wrzesien"},
	{31,"pazdziernik"},
	{30,"listopad"},
	{31,"grudzien"}
};


int _tmain(int argc, _TCHAR* argv[])
{
	int n,w=0,x=0,z=1,q=0;
	fprintf(stderr, "Podaj ilosc dat do wylosowania: ");
	scanf_s("%d", &n);
	srand(time(0));
	printf("\n");
	


	//losowanie


	for (w; w<n; w++){
		

		//a=zakres_dolny+rand()%(zakres_gorny-zakres_dolny+1)

		D[w].r=(-5000)+rand()%(5000-(-5000)+1);
		D[w].m=1+rand()%(12-1+1);
		D[w].d=1+rand()%(Tab[D[w].m-1].dni-1+1);

		printf("Data wygenerowana: %hd %hd %hdr.\n", D[w].d, D[w].m, D[w].r);
	}



	//sortowanie

	
	for (x; x<n; x++){
		for (z; z<n; z++){
			if (D[x] > D[z]){
				H=D[z];
				D[z]=D[x];
				D[x]=H;
			}
		}
	}

	//wyswietlanie

	for (q; q<n; q++){

		printf("Data wygenerowana: %hd %hd %hdr.\n", D[q].d, D[q].m, D[q].r);
	}



	return 0;
}

**Dla osób nie posiadających kompilatora: **
error C2676: binary '>' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator

0
#include "stdlib.h"
#include <ctime>

Zdecydujmy, czy to ma być C czy C++. Bo jak na razie z wyjątkiem linijki z <ctime> jest to C.

0

Powiedzmy że C z elementami C++. Ale głównie C

1 użytkowników online, w tym zalogowanych: 0, gości: 1