1. Code formatting and layout.
It's a lot easier to find things if code is layed out in a consistent manner, not randomly distributed.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* #defines */
/* ======== */
#define HASHSIZE 997
/* Types */
/* ===== */
typedef struct Node {
char code_name[15];
int total_points;
struct Node *next;
} listNode;
typedef struct {
int low_limit;
int hi_limit;
int points;
} item1;
typedef struct {
int money;
char codename[15];
int earned_points;
} item2;
/* Prototypes */
/* ========== */
void menu(void);
void insert_points();
void menu_choice();
void insert_clients();
int hash(char *key);
int hash_insert(char *key, int key_value);
char *hash_find(char *key);
void hash_destroy();
/* Global variables */
/* ================ */
item1 *array1;
item2 *array2;
listNode *HashTable[HASHSIZE]; /*to * antiprosopevi dieythinsi domis gia na perasti se sinartisi */
/* Functions */
/* ========= */
int hash(char *key)
{
unsigned int hash_val = 0;
while (*key != '\0')
hash_val += *(key++);
return (hash_val % HASHSIZE);
}
int hash_insert(char *key, int key_value)
{
listNode *newptr, *curr;
int pos;
pos = hash(key);
/* make sure it's not already in the hash table */
curr = HashTable[pos];
while (curr != NULL) {
if (!strcmp(curr->code_name, key))
curr->total_points = curr->total_points + key_value;
return 1;
curr = curr->next;
}
newptr = malloc(sizeof(listNode));
if (newptr == NULL) {
printf("Not Enough Memory!\n");
exit(EXIT_SUCCESS);
}
strcpy(newptr->code_name, key);
newptr->total_points = key_value;
newptr->next = NULL;
if (HashTable[pos] == NULL)
HashTable[pos] = newptr;
else {
listNode *headptr = HashTable[pos];
HashTable[pos] = newptr;
HashTable[pos]->next = headptr;
}
return 1;
}
char *hash_find(char *key)
{
listNode *curr;
int pos;
pos = hash(key);
curr = HashTable[pos];
while (curr != NULL) {
if (!strcmp(curr->code_name, key))
printf("%s %d", curr->code_name, curr->total_points);
return key;
curr = curr->next;
}
return NULL;
}
int main(int argc, char *argv[])
{
printf("\n<<<<Welcome to Super Market Program>>>\n");
arxi:
menu();
menu_choice();
goto arxi;
system("PAUSE");
return 0;
}
void menu(void)
{
printf("\nPlease choose one of the following:\n\n");
printf("1. Insert Points File\n");
printf("2. Insert Shopping File\n");
printf("3. Best Clients\n");
printf("4. Client's Data\n\n");
printf(">");
}
void menu_choice()
{
char kodikos[15] = "ABCDEFGHIGK";
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
insert_points();
break;
case 2:
insert_clients();
break;
case 3:
hash_find(kodikos);
break;
case 4:
break;
default:
printf("\nERROR: The number must be between 1-4\n\n");
break;
}
}
void insert_clients()
{
int s, n;
int i = 0;
FILE *File2;
File2 = fopen("clients.txt", "r");
array2 = calloc(1000000, sizeof(item2));
while (!feof(File2)) {
fscanf(File2, "%[^;];%d", array2[i].codename, &array2[i].money);
if ((array2[i].money > array1[0].low_limit)
&& (array2[i].money < array1[0].hi_limit)) {
array2[i].earned_points = array1[0].points;
}
if ((array2[i].money > array1[1].low_limit)
&& (array2[i].money < array1[1].hi_limit)) {
array2[i].earned_points = array1[1].points;
}
if ((array2[i].money > array1[2].low_limit)
&& (array2[i].money < array1[2].hi_limit)) {
array2[i].earned_points = array1[2].points;
}
if ((array2[i].money > array1[3].low_limit)
&& (array2[i].money < array1[3].hi_limit)) {
array2[i].earned_points = array1[3].points;
}
if ((array2[i].money > array1[4].low_limit)
&& (array2[i].money < array1[4].hi_limit)) {
array2[i].earned_points = array1[4].points;
}
printf("%s %d %d", array2[i].codename, array2[i].money,
array2[i].earned_points);
hash_insert(array2[i].codename, array2[i].earned_points);
}
fclose(File2);
}
void insert_points()
{
int s, n;
int i = 0;
FILE *File1;
File1 = fopen("points.txt", "r");
fscanf(File1, "%d", &n);
printf("%d\n", n); /*This is for the first line that shows the line number */
array1 = calloc(n, sizeof(item1)); /*Don't cast the return value of malloc or calloc */
for (i = 0; i < n; i++) { /*You're reading n values, not n+1, so the condition shouldn't be i<=n */
fscanf(File1, "%d;%d;%d", &array1[i].low_limit,
&array1[i].hi_limit, &array1[i].points);
printf("%d %d %d\n", array1[i].low_limit, array1[i].hi_limit,
array1[i].points);
}
free(array1);
fclose(File1);
}
2. None of the malloc or file reading functions bother to check for errors.
3. Is this a typo?
char kodikos[15]="ABCDEFGHIGK";
4. Reduce the size
#define HASHSIZE 997
Debug the program with a 7 entry hash table and a 2 line input file.
Then go through your code one statement at a time to make sure it's doing the right thing.
When all seems well, then you can scale up.