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 *top_scores);
char* new_player(void);
void print_array(struct player *top_scores);
int setpt(struct player *player,struct player *top_scores, int Gnd_it);
void load_array(struct player *player,int size);
void save_array(struct player *player,int size);
char make_choice();
void display_menu();
void count_change();
int guess_number(struct player *player, struct player *top_scores, char gn[30]);
void printsummary(int ct, int Gnt, int t);
int main(void)
{
int Gn_Total= 0;
int Gn_Tries=0;
int CC_Total= 0;
int tries= 0;
char choice;
char Gnd_name[30];
struct player top_scores[MAX2];
struct player player[MAX2];
load_array(top_scores,sizeof(top_scores));
print_array(top_scores);
new_player();
do
{
display_menu();
choice = make_choice();
switch(choice)
{
case 'A':
count_change();
CC_Total++;
break;
case 'B':
Gn_Tries += guess_number(player, top_scores, Gnd_name);
Gn_Total++;
break;
case 'C':
printsummary(CC_Total, Gn_Total, Gn_Tries);
break;
case 'N':
new_player();
break;
case 'S':
print_array(top_scores);
break;
}
}while(choice != 'Q');
save_array(top_scores,sizeof(top_scores));
return 0;
}
void init_array(struct player *player)
{
int cur_row;
for(cur_row=0;cur_row<MAX2;cur_row++)
{
player[cur_row].score=0;
strcpy(player[cur_row].name,"init");
}
}
void print_array(struct player *player)
{
int cur_row;
printf("\n");
for(cur_row=0;cur_row<MAX2;cur_row++)
{
printf("Element #%d is:%30s %6d\n"
,cur_row,player[cur_row].name,player[cur_row].score);
}
printf("\n\nPress any key");
getch();
}
char* new_player(void)
{
char Gnd_name[30];
printf("\nEnter a name\n");
flushall();
gets(Gnd_name);
return Gnd_name;
}
int setpt(struct player *player, struct player *top_scores, int Gnd_it)
{
int set_pt,incr;
for(set_pt=0;set_pt < MAX2 && Gnd_it > player[set_pt].score
&& player[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
{
player[incr] = player[incr-1];
player[incr].score=player[incr-1].score;
strcpy(player[incr].name,player[incr-1].name);
top_scores[incr] = top_scores[incr-1];
top_scores[incr].score = top_scores[incr-1].score;
strcpy(top_scores[incr].name,top_scores[incr-1].name);
}
return set_pt;
}
void save_array(struct player *player,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(player,size,1,fptr);
fclose(fptr);
}
}
void load_array(struct player *player,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(player);
}
else
{
fread(player,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 count_change()
{
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 guess_number(struct player *player, struct player *top_scores, char gn[30])
{
int magic,
guess,
Gnd_row=0,
cur_row,
tries=0;
char Gnd_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);
Gnd_row=setpt(player, top_scores, tries);
player[Gnd_row].score=tries;
strcpy(player[Gnd_row].name, Gnd_name);
printf("%s", Gnd_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 Gnt, int t)
{
char* n = new_player();
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", Gnt);
printf("Your average tries for High / Low are %.2f\n", (float) t/Gnt);
getch();
}