1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
/*
This programs takes input through ioredirection with the structure of
name
address
citystate
zip
and arranges them by zipcode
MAKNELTEK
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int TOTAL = 18;
struct info {
char name[30];
char address[30];
char citystate[30];
char zip[30];
}a;
void get_info(struct info *a);
void print_out(struct info *p);
void sortdata( struct info *a);
int main()
{
//struct info *strptr;
//strptr=&a;
info a[TOTAL];
info *spc; // need to be used sometime
spc = (info*)malloc (sizeof(info)); //i need to use this at some point, not sure where yet.
get_info(a);
sortdata(a);
print_out(a);
return 0;
}
void get_info(info *a)
{
char *p;
for ( int i = 0; i <TOTAL; i++){
fgets(a[i].name, sizeof(a[i].name), stdin);
if ((p = strchr(a[i].name, '\n')) != (NULL))
*p = '\0';
fgets (a[i].address, sizeof(a[i].address), stdin);
if ((p = strchr(a[i].address, '\n')) != (NULL))
*p = '\0';
fgets (a[i].citystate, sizeof(a[i].citystate), stdin);
if ((p = strchr(a[i].citystate, '\n')) != (NULL))
*p = '\0';
fgets (a[i].zip, sizeof(a[i].zip),stdin);
if ((p = strchr(a[i].zip, '\n')) != (NULL))
*p = '\0';
}
}
void print_out(struct info *p)
{
int i, total=TOTAL;
for ( i = 0; i < total; i++)
{
printf("%s", p[i].name);// folowing 4 lines are added to display the data.
printf("\n");
printf ("%s", p[i].address);
printf("\n");
printf ("%s", p[i].citystate);
printf("\n");
printf ("%s", p[i].zip);
printf ("\n");
}
}
void sortdata(info *a)
{ int t;
for (t=0; t<TOTAL; t++)
{
int i, total=TOTAL;
for ( i = t; i < total; i++)
{
if (-1==(strcmp(a[i].zip,a[t].zip)))
{
char temp[30];
memcpy(temp, a[i].zip, sizeof(a[i].zip));
memcpy(a[i].zip, a[t].zip, sizeof(a[t].zip));
memcpy(a[t].zip, temp, sizeof(temp));
memcpy(temp, a[i].name, sizeof(a[i].name));
memcpy(a[i].name, a[t].name, sizeof(a[t].name));
memcpy(a[t].name, temp, sizeof(temp));
memcpy(temp, a[i].address, sizeof(a[i].address));
memcpy(a[i].address, a[t].address, sizeof(a[t].address));
memcpy(a[t].address, temp, sizeof(temp));
memcpy(temp, a[i].citystate, sizeof(a[i].citystate));
memcpy(a[i].citystate, a[t].citystate, sizeof(a[t].citystate));
memcpy(a[t].citystate, temp, sizeof(temp));
}
}
}
}
|