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.