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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct orderSlip
{
char source[64];
char destination[64];
char item[64];
int position;
};
typedef int (*compfn)(const void*, const void*);
struct orderSlip data[100];
void decision();
void read();
void moveSlip();
void deleteSlip();
void print();
int compare(struct orderSlip *, struct orderSlip *);
int main()
{
//choice();
decision();
//read();
//pause
return 0;
}
void decision()
{
int choice;
do
{
printf("1 - Add Delivery Slip\n");
printf("2 - Move Delivery Slip\n");
printf("3 - Delete Delivery Slip\n");
printf("4 - View Delivery Slips\n");
printf("5 - Exit\n");
printf("Please enter your choice: \n");
scanf("%i", &choice);
switch(choice)
{
case 1:
read();
break;
case 2:
case 3:
case 4:
print();
qsort((void *) &data, // Beginning address of array
100, // Number of elements in array
sizeof(struct orderSlip), // Size of each element
(compfn)compare ); // Pointer to compare function
print();
break;
default:
if(choice !=5)
printf("Input Not Recognized! Please Try Again!\n");
break;
}
}
while(choice !=5);
if(choice == 5)
printf("Goodbye!");
}
void read()
{
static const char filename[] = "datafile.txt";
FILE *file = fopen(filename, "r");
if(file != NULL)
{
char line[64];
int n = 0;
int accumulator = 0;
while (fgets(line, sizeof line, file) != NULL)
{
//remove /n
for (int i = 0; i < sizeof line; i++ )
{
if ( line[i] == '\n' )
{
line[i] = '\0';
break;
}
}
//fputs(line, stdout);
//if first line
if(accumulator == 0 || accumulator % 4 == 0)
{
strcpy(data[n].source, line);
accumulator++;
}
//if 2nd line
else if(accumulator == 1)
{
strcpy(data[n].destination, line);
accumulator++;
}
//if 3rd line
else if(accumulator % 2 == 0)
{
strcpy(data[n].item, line);
accumulator++;
}
//if the line contains only 1 element, must be the
//position
else
{
int a = line[0] - '0';
data[n].position = a;
accumulator=0;
n++;
}
};
fclose(file);
}
else
{
perror("You goofed!");
exit(1);
}
}
int compare(struct orderSlip *elem1, struct orderSlip *elem2)
{
if ( elem1->position < elem2->position)
return -1;
else if (elem1->position > elem2->position)
return 1;
else
return 0;
}
void print()
{
printf("%s\n",data[0].source);
printf("%s\n",data[0].destination);
printf("%s\n",data[0].item);
printf("%i\n\n", data[0].position);
printf("%s\n",data[1].source);
printf("%s\n",data[1].destination);
printf("%s\n",data[1].item);
printf("%i\n", data[1].position);
}
|