cannot get search to work properly.

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX 25
#define S_SIZE 30
#define NAME_SIZE 20
#define STATE_SIZE 3
#define CODE_SIZE 5

 struct customer{
	int cust_id;
	char customer_name[NAME_SIZE];
	char state[STATE_SIZE];
    char discount_code[CODE_SIZE];
	double balance_due;
	int outstanding_orders;
	//char a[20];
}cust_list[MAX]; 

void load_file();
int stripend(char *s);
void init_list();
void add_rec();
int binary_search();
int menu_select(); 
int find_free();
void list();
int compare(int i, int j);
void  swap(int i,int j);
void print_list_header();
void print_customer(int t);

	


int main() 
{
	load_file();
	char choice;
	char s[3];
	int i, j;

	
	list();/* initialize the structure array */
	for(;;)
	{
		choice = menu_select();
		
	    switch (choice)
		{
	   case 1: list();
       break;
       case 2: binary_search();
       break;
	   case 3: add_rec();
	   break;
       case 4: exit(0);
	   case 5:
	   case 6:
		  printf("i: ");
		  fgets(s, 3, stdin);
		  i = atoi(s);
		  if (choice == 5) compare(i,j);
		  else swap(i,j);
		  break;
	   case 7:
		  binary_search();
    }
  }
	return 0;
}

void init_list()
{
  register int t;

  for(t=0; t<MAX; ++t) cust_list[t].cust_id = '\0';
}
int menu_select()
{
      char s[80];
      int c;
      
      printf("1. List the file\n");
      printf("2. Search the file\n");
      printf("3. add a record\n");
      printf("4. Quit\n");
      printf("5. Compare\n");
      printf("6. Swap\n");
      printf("7. Sort\n");
      do {
        printf("\nEnter your choice: ");
        fgets(s,S_SIZE,stdin);
        c = atoi(s);
      } while(c<0 || c>7);
      
  return c;
}
void add_rec()
{

      int slot;
      char s[S_SIZE];
      
      slot = find_free();

      if(slot==-1) {
        printf("\nList Full");
        return ;
      
}

      printf("Enter customer ID: ");
      fgets(s,S_SIZE,stdin);
      cust_list[slot].cust_id = atoi(s);

      printf("Enter customer name: ");
      fgets(s,S_SIZE,stdin);
      stripend(s);
      strncpy(cust_list[slot].customer_name,s,NAME_SIZE);
      
      printf("Enter state: ");
      fgets(s,S_SIZE,stdin);
      stripend(s);
      strncpy(cust_list[slot].state,s,STATE_SIZE);

      printf("Enter discount code 'A', 'B', 'C': ");
      fgets(s,S_SIZE,stdin);
      stripend(s);
      strncpy(cust_list[slot].discount_code,s,CODE_SIZE);
      
      printf("Enter balance due: ");
      fgets(s,S_SIZE,stdin );
      cust_list[slot].balance_due= atof(s);

      printf("Enter outstanding orders: ");
      fgets(s,S_SIZE,stdin);
      cust_list[slot].outstanding_orders = atoi(s);

}
/* Find an unused structure. */
int find_free()
{
      register int t;

      for(t=0; cust_list[t].cust_id && t<MAX; ++t) ;

      if(t==MAX) return -1; /* no slots free */
      return t;
}


void load_file()
{
      FILE  *fp;
      char s[S_SIZE];
      int t=0;
      register int i;

      if((fp=fopen("ASSIGNV1.DAT", "r"))==NULL) 
      {
        printf("Cannot open file.\n");
        return;
      }
      
      init_list();
      int eof = 0;
      //print_list_header();
      printf("\n");
      for(i=0; i<MAX; i++)
      {
	      if(!fgets(s,S_SIZE,fp)) break;
	      cust_list[i].cust_id=atoi(s);

	      if(!fgets(s,S_SIZE,fp)) break;
	      stripend(s);
	      strncpy(cust_list[i].customer_name,s,NAME_SIZE);

	      if(!fgets(s,S_SIZE,fp)) break;
	      stripend(s);
	      strncpy(cust_list[i].state,s,STATE_SIZE);

	      if(!fgets(s,S_SIZE,fp)) break;
	      stripend(s);
          strncpy(cust_list[i].discount_code,s,CODE_SIZE);

	      if(!fgets(s,S_SIZE,fp)) break;
	      stripend(s);
          cust_list[i].balance_due=atof(s);

	      if(!fgets(s,S_SIZE,fp)) break;
	      stripend(s);
	      cust_list[i].outstanding_orders=atoi(s);
      }
	    {
		    //print_customer(t);
	    }
      
	      fclose(fp);

 }
void list()
{
    
       int t;
	    print_list_header();
      for(t=0; t<MAX; ++t) 
      {
        if(cust_list[t].customer_name[0]) 
	    {
		    print_customer(t);	
        }
      }
      printf("\n\n");
}

int stripend(char *s)
{
      int i;
      for (i = 0; s[i]; i++)
      {
        if (s[i] == '\r' || s[i] == '\n')
        {
          s[i] = '\0';
          return i;
        }
      }
}

void  swap(int i,int j)
{
   double temp;
   temp = cust_list[i];
   cust_list[i] = cust_list[j];
   cust_list[j] = temp;

}
 
int compare(int i, int j)
{
	    if (cust_list[i].cust_id > cust_list[j].cust_id)
	      return 1;
      else
	      return 0;
        
  }

int binary_search() 
{
	int t =0;
	char s[S_SIZE];
	int low=0;
	int high=0;
	int target=0;	
	printf("Enter a custome ID: ");
	fgets(s,20,stdin);
    
	if (high < low)
        return -1;
     print_customer(t);
    int middle = (low + high)/2;
    if (target < s[middle])
        return binary_search();
    else if (target > s[middle])
        return binary_search();
    else if (target == s[middle])
        //return middle;
        printf("RECORD NOT FOUND");
    
}


void print_list_header()
{
	printf("%-10s%-20s%-10s%-10s%-10s%-s\n", "CUSTOMER", "NAME", "STATE", "DISCOUNT", "BALANCE", "OUTSTANDING");
	printf("%-10s%-20s%-10s%-10s%-10s%-s\n", "ID",       "",     "",       "CODE",    "DUE",     "ORDERS");
}

void print_customer(int t)
{
	printf("%-10d%-20s%-10s%-10s%-10.2f%-d\n", cust_list[t].cust_id,cust_list[t].customer_name,cust_list[t].state, cust_list[t].discount_code, cust_list[t].balance_due, cust_list[t].outstanding_orders);
}


Last edited on
Topic archived. No new replies allowed.