Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#define MAX 5
#define MAX2 10
struct player
{
char name[30];
int score;
};
void init_array(struct player *top5);
void get_name(struct player *local);
void print_array(struct player *top5);
int setpt(struct player *local,int hld_it);
void load_array(struct player *local,int size);
void save_array(struct player *local,int size);
char make_choice();
void display_menu();
void choice_a();
int choice_b(struct player *local, struct player *top5);
void printsummary(int ct, int hlt, int t, char n[30]);
void main()
{
int HL_Total= 0;
int HL_Tries=0;
int CC_Total= 0;
int tries= 0;
char choice;
char hld_name[30];
struct player top5[MAX2];
struct player local[MAX2];
load_array(top5,sizeof(top5));
print_array(top5);
get_name(top5);
do
{
display_menu();
choice = make_choice();
switch(choice)
{
case 'A':
choice_a();
CC_Total++;
break;
case 'B':
HL_Tries += choice_b(local, top5);
HL_Total++;
break;
case 'C':
printsummary(CC_Total, HL_Total, HL_Tries, hld_name);
break;
case 'N':
get_name(top5);
break;
case 'S':
print_array(top5);
break;
}
}while(choice != 'Q');
save_array(top5,sizeof(top5));
}
void init_array(struct player *local)
{
int cur_row;
for(cur_row=0;cur_row<MAX2;cur_row++)
{
local[cur_row].score=0;
strcpy(local[cur_row].name,"init");
}
}
void print_array(struct player *local)
{
int cur_row;
printf("\n");
for(cur_row=0;cur_row<MAX2;cur_row++)
{
printf("Element #%d is:%30s %6d\n"
,cur_row,local[cur_row].name,local[cur_row].score);
}
printf("\n\nPress any key");
getch();
}
void get_name(struct player *local)
{
int cur_row;
int hld_row=0;
char hld_name[30];
printf("\nEnter a name\n");
flushall();
gets(hld_name);
}
int setpt(struct player *local,int hld_it)
{
int set_pt,incr;
for(set_pt=0;set_pt < MAX2 && hld_it > local[set_pt].score
&& local[set_pt].score!=0;set_pt++); //this for loop will locate the row that the new data is to be inserted
for(incr=MAX2 -1;incr >set_pt;incr--) //this for loop will make room in the array for the new data insertion
{
local[incr] = local[incr-1];
local[incr].score=local[incr-1].score;
strcpy(local[incr].name,local[incr-1].name);
}
return set_pt;
}
void save_array(struct player *local,int size)
{
FILE *fptr;
if ((fptr = fopen("topten.dat","wb"))==NULL)
{
system("CLS");
printf("File write error...Press any key to terminate");
getch();
exit(0);
}
else
{
fwrite(local,size,1,fptr);
fclose(fptr);
}
}
void load_array(struct player *local,int size)
{
FILE *fptr;
if ((fptr = fopen("topten.dat","rb"))==NULL)
{
system("CLS");
printf("File does not exists...initializing new array...Press any key to continue");
getch();
init_array(local);
}
else
{
fread(local,size,1,fptr);
fclose(fptr);
}
}
char make_choice()
{
char ch;
do{
ch = toupper(_getch());
}while (!strchr("ABCNSQ",ch));
return ch;
}
void display_menu()
{
system("cls");
printf( "Please select one of these options. \n");
printf( "A. Change Count \n");
printf( "B. Play High / Low Game \n");
printf( "C. Print Summary \n");
printf( "N. Change Name \n");
printf( "S. High Scores \n");
printf( "Q. Quit Program \n");
}
void choice_a()
{
int hd,
q,
d,
n,
p;
system("cls");
printf("You have chosen Change Count \n\nEnter number of half dollars: ");
scanf_s("%i", &hd);
printf("Enter number of quarters: ");
scanf_s("%i", &q);
printf("Enter number of dimes: ");
scanf_s("%i", &d);
printf("Enter number of nickles: ");
scanf_s("%i", &n);
printf("Enter number of pennies: ");
scanf_s("%i", &p);
int tc= hd+q+d+n+p;
double hd_v= hd*0.5,
q_v= q*0.25,
d_v= d*0.10,
n_v= n*0.05,
p_v= p*0.01;
printf("\nYou have entered %i half dollars: $", hd);
printf("%.2f",hd * 0.5);
printf("\nYou have entered %i quarters: $", q);
printf("%.2f", q * .25);
printf("\nYou have entered %i dimes: $", d);
printf("%.2f", d * .10);
printf("\nYou have entered %i nickles: $", n);
printf("%.2f", n * .05);
printf("\nYou have entered %i pennies: $", p);
printf("%.2f", p * .01);
double tm= hd_v+q_v+d_v+n_v+p_v;
printf("\n\nYou have entered %i coins worth a total of %.2f", tc, tm);
printf("\n\nPress any key to return to the main menu");
_getch();
}
int choice_b(struct player *local, struct player *top5)
{
int magic,
guess,
hld_row=0,
cur_row,
tries=0;
char hld_name[30];
srand((unsigned)time(NULL));
magic = rand()%MAX;
system("cls");
printf("\n\nGuess the random number from 0 to %d\n\n",MAX);
do
{
printf("guess: ");
scanf_s("%d",&guess);
if(guess == magic)
{
printf("** Right **");
printf(" %d is the magic number\n", magic);
}
else
if (guess > magic)
printf(".. Wrong .. Too High\n");
else
printf(".. Wrong .. Too Low\n");
tries++;
}while (guess != magic);
hld_row=setpt(local,tries);
local[hld_row].score=tries;
strcpy(local[hld_row].name, hld_name);
getch();
printf("You took %d tries.\n", tries);
printf("\n\n\n\nPress any key to return to the main menu");
getch();
return tries;
}
void printsummary(int ct, int hlt, int t, char n[30])
{
system("cls");
printf("Hello %10s\n\n", n);
printf("You have counted change %d times\n", ct);
printf("You have played High / Low %d times\n", hlt);
printf("Your average tries for High / Low are %.2f\n", (float) t/hlt);
getch();
}