Code:
#include <stdio.h>
#include <stdlib.h>
struct database{
char fname[50]; #include <stdio.h>
#include <stdlib.h>
struct database{
char fname[50];
char lname[50];
int age[];
char gender[50];
int group;
int position;
};//structure named database
void menu(struct database);
void input(struct database);
void files(struct database);
void output(struct database);
void sort(struct database);
void sort_age(struct database);
int count;
int main()
{
struct database ind;//ind_Individual
menu(ind);
}
void menu(struct database ind)
{
int opt;
do{
printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n");
printf("Option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
input(ind);
break;
case 2:
output(ind);
break;
case 3:
sort(ind);
break;
case 4:
exit(0);
break;
default:
printf("INVALID\n\n");
break;
}
}while(opt!=4);
}
void input(struct database ind)
{
printf("Enter first name: ");
scanf("%s", ind.fname);
printf("Enter last name: ");
scanf("%s", ind.lname);
printf("Enter age: ");
scanf("%d", &ind.age);
printf("Enter gender: ");
scanf("%s", ind.gender);
printf("Enter group[1-4]: ");
scanf("%d", &ind.group);
printf("Enter position[1-3]: ");
scanf("%d", &ind.position);
files(ind);
count++;
}
void files(struct database ind)
{
FILE*fp=fopen("REG.txt", "a");
fprintf(fp,"%s\n", ind.fname);
fprintf(fp,"%s\n", ind.lname);
fprintf(fp,"%d\n", ind.age);
fprintf(fp,"%s\n", ind.gender);
fprintf(fp,"%d\n", ind.group);
fprintf(fp,"%d\n", ind.position);
fclose(fp);
}
void output(struct database ind)
{
int inc;//increment
FILE*fp=fopen("REG.txt", "r");
for(inc=0; inc<count; inc++)
{
fscanf(fp,"%s", ind.fname);
fscanf(fp, "%s", ind.lname);
printf("NAME: %s %s\n", ind.fname, ind.lname);
fscanf(fp,"%d", &ind.age);
printf("AGE: %d\n", ind.age);
fscanf(fp,"%s", ind.gender);
printf("GENDER: %s\n", ind.gender);
fscanf(fp,"%d", &ind.group);
printf("GROUP: %d\n", ind.group);
fscanf(fp,"%d", &ind.position);
printf("POSITION: %d\n\n", ind.position);
}
fclose(fp);
}
void sort(struct database ind)
{
int choice;
do{
printf("How would you like to sort the data?\n\n");
printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n");
printf("Choice: ");
scanf("%d", & choice);
switch(choice)
{
case 1:
sort_age(ind);
break;
case 2:
printf("By Group");
break;
case 3:
printf("By Position\n");
break;
case 4:
menu(ind);
break;
default:
printf("INVALID OPTION\n\n");
break;
}
}while( choice != 4);
}
void sort_age(struct database ind)
{
int i, j, temp;
for (i=0; i<= count-2; i++)
{
for (j=0; j<= (count-2) - i; j++)
{
if (ind.age[j] > ind.age[j + 1] )
{
temp = ind.age[j];
ind.age[j] = ind.age[j + 1];
ind.age[j + 1] = temp;
}
}
}#include <stdio.h>
#include <stdlib.h>
struct database{
char fname[50];
char lname[50];
int age[];
char gender[50];
int group;
int position;
};//structure named database
void menu(struct database);
void input(struct database);
void files(struct database);
void output(struct database);
void sort(struct database);
void sort_age(struct database);
int count;
int main()
{
struct database ind;//ind_Individual
menu(ind);
}
void menu(struct database ind)
{
int opt;
do{
printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n");
printf("Option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
input(ind);
break;
case 2:
output(ind);
break;
case 3:
sort(ind);
break;
case 4:
exit(0);
break;
default:
printf("INVALID\n\n");
break;
}
}while(opt!=4);
}
void input(struct database ind)
{
printf("Enter first name: ");
scanf("%s", ind.fname);
printf("Enter last name: ");
scanf("%s", ind.lname);
printf("Enter age: ");
scanf("%d", &ind.age);
printf("Enter gender: ");
scanf("%s", ind.gender);
printf("Enter group[1-4]: ");
scanf("%d", &ind.group);
printf("Enter position[1-3]: ");
scanf("%d", &ind.position);
files(ind);
count++;
}
void files(struct database ind)
{
FILE*fp=fopen("REG.txt", "a");
fprintf(fp,"%s\n", ind.fname);
fprintf(fp,"%s\n", ind.lname);
fprintf(fp,"%d\n", ind.age);
fprintf(fp,"%s\n", ind.gender);
fprintf(fp,"%d\n", ind.group);
fprintf(fp,"%d\n", ind.position);
fclose(fp);
}
void output(struct database ind)
{
int inc;//increment
FILE*fp=fopen("REG.txt", "r");
for(inc=0; inc<count; inc++)
{
fscanf(fp,"%s", ind.fname);
fscanf(fp, "%s", ind.lname);
printf("NAME: %s %s\n", ind.fname, ind.lname);
fscanf(fp,"%d", &ind.age);
printf("AGE: %d\n", ind.age);
fscanf(fp,"%s", ind.gender);
printf("GENDER: %s\n", ind.gender);
fscanf(fp,"%d", &ind.group);
printf("GROUP: %d\n", ind.group);
fscanf(fp,"%d", &ind.position);
printf("POSITION: %d\n\n", ind.position);
}
fclose(fp);
}
void sort(struct database ind)
{
int choice;
do{
printf("How would you like to sort the data?\n\n");
printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n");
printf("Choice: ");
scanf("%d", & choice);
switch(choice)
{
case 1:
sort_age(ind);
break;
case 2:
printf("By Group");
break;
case 3:
printf("By Position\n");
break;
case 4:
menu(ind);
break;
default:
printf("INVALID OPTION\n\n");
break;
}
}while( choice != 4);
}
void sort_age(struct database ind)
{
int i, j, temp;
for (i=0; i<= count-2; i++)
{
for (j=0; j<= (count-2) - i; j++)
{
if (ind.age[j] > ind.age[j + 1] )
{
temp = ind.age[j];
ind.age[j] = ind.age[j + 1];
ind.age[j + 1] = temp;
}
}
}
printf ("\n\nData Sorted by Age:\n");
for (i=0; i<= count-1; i++)
printf("%d\t", ind.age[i]);
getch();
}
printf ("\n\nData Sorted by Age:\n");
for (i=0; i<= count-1; i++)
printf("%d\t", ind.age[i]);
getch();
}
char lname[50];
int age;
char gender[50];
int group;
int position;
};//structure named database
void menu(struct database);
void input(struct database);
void files(struct database);
void output(struct database);
void sort(struct database);
void sort_age(struct database);
int count;
int main()
{
struct database ind;//ind_Individual
menu(ind);
}
void menu(struct database ind)
{
int opt;
do{
printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n");
printf("Option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
input(ind);
break;
case 2:
output(ind);
break;
case 3:
sort(ind);
break;
case 4:
exit(0);
break;
default:
printf("INVALID\n\n");
break;
}
}while(opt!=4);
}
void input(struct database ind)
{
printf("Enter first name: ");
scanf("%s", ind.fname);
printf("Enter last name: ");
scanf("%s", ind.lname);
printf("Enter age: ");
scanf("%d", &ind.age);
printf("Enter gender: ");
scanf("%s", ind.gender);
printf("Enter group[1-4]: ");
scanf("%d", &ind.group);
printf("Enter position[1-3]: ");
scanf("%d", &ind.position);
files(ind);
count++;
}
void files(struct database ind)
{
FILE*fp=fopen("REG.txt", "a");
fprintf(fp,"%s\n", ind.fname);
fprintf(fp,"%s\n", ind.lname);
fprintf(fp,"%d\n", ind.age);
fprintf(fp,"%s\n", ind.gender);
fprintf(fp,"%d\n", ind.group);
fprintf(fp,"%d\n", ind.position);
fclose(fp);
}
void output(struct database ind)
{
int inc;//increment
FILE*fp=fopen("REG.txt", "r");
for(inc=0; inc<count; inc++)
{
fscanf(fp,"%s", ind.fname);
fscanf(fp, "%s", ind.lname);
printf("NAME: %s %s\n", ind.fname, ind.lname);
fscanf(fp,"%d", &ind.age);
printf("AGE: %d\n", ind.age);
fscanf(fp,"%s", ind.gender);
printf("GENDER: %s\n", ind.gender);
fscanf(fp,"%d", &ind.group);
printf("GROUP: %d\n", ind.group);
fscanf(fp,"%d", &ind.position);
printf("POSITION: %d\n\n", ind.position);
}
fclose(fp);
}
void sort(struct database ind)
{
int choice;
do{
printf("How would you like to sort the data?\n\n");
printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n");
printf("Choice: ");
scanf("%d", & choice);
switch(choice)
{
case 1:
sort_age(ind);
break;
case 2:
printf("By Group");
break;
case 3:
printf("By Position\n");
break;
case 4:
menu(ind);
break;
default:
printf("INVALID OPTION\n\n");
break;
}
}while( choice != 4);
}
void sort_age(struct database ind)
{
int i, j, temp;
for (i=0; i<= count-2; i++)
{
for (j=0; j<= (count-2) - i; j++)
{
if (ind.age[j] > ind.age[j + 1] )
{
temp = ind.age[j];
ind.age[j] = ind.age[j + 1];
ind.age[j + 1] = temp;
}
}
}
printf ("\n\nData Sorted by Age:\n");
for (i=0; i<= count-1; i++)
printf("%d\t", ind.age[i]);
getch();
}