In: Computer Science
Explain the code below in details.
void accountInfo()
{
system("cls");
int ch;
printf("1-Total Amount Claimed by LifeTime Claim Limit subscriber\n");
printf("2-Total number of Annual Claim Limit who have exhausted all their eligible amount\n");
scanf("%d", & ch);
if (ch == 1)
{
int totalAmountClaimedByLifeTimeSubs = 0;
for (int i = 0; i < patientCount; i++)
{
if (patients[i].annualClaim == false)
{
for (int j = 0; j < patientCount; j++)
{
if (claims[j].id == patients[i].id)
{
totalAmountClaimedByLifeTimeSubs += claims[j].amountClaimed;
}
}
}
}
printf("\nTotal amount Claimed By LifeTime Subscribers is: %d\n", totalAmountClaimedByLifeTimeSubs);
} else
{
int count = 0;
for (int i = 0; i < patientCount; i++)
{
if (claims[i].remaininigAmount <= 0 && patients[i].annualClaim == true)
count++;
}
printf("Total number of Annual Claim Limit Subcriber who have exhausted all their amount are: %d\n", count);
}
system("pause");
}
void searchingFunctionalities()
{
system("cls");
int ch;
printf("1-Search by ID\n2-Search by age\n");
scanf("%d", & ch);
if (ch == 1)
{
int id;
printf("Enter patient ID for which you want Search: ");
scanf("%d", & id);
int i;
for (i = 0; i < patientCount; i++)
{
if (patients[i].id == id)
{
printf("\nSubscriber Name: %s\nSubscriber Age: %d\nSubscriber Contact: %s\nSubscriber Address: %s\n", patients[i].name, patients[i].age, patients[i].contactNum, patients[i].address);
break;
}
}
printf("Subscriber Not Found");
} else
{
int age;
printf("Enter age for which you want Search: ");
scanf("%d", & age);
for (int i = 0; i < patientCount; i++)
{
if (patients[i].age == age)
{
printf("\nSubscriber Name: %s\nSubscriber ID: %d\nSubscriber Contact: %s\nSubscriber Address: %s\n", patients[i].name, patients[i].id, patients[i].contactNum, patients[i].address);
}
}
}
system("pause");
}
void loadData()
{
char line[MAX_LENGTH];
const char * delimiter = ",\n";
FILE * fp;
fp = fopen("patients.txt", "r");
char c;
if (fp == NULL)
{
printf("file not found");
return;
}
while (fp != NULL)
{
if (fgets(line, MAX_LENGTH - 1, fp) == NULL)
break;
if (line[1] == '\0')
break;
patients[patientCount].id = atoi(strtok(line, delimiter));
strcpy(patients[patientCount].name, strtok(NULL, delimiter));
patients[patientCount].age = atoi(strtok(NULL, delimiter));
patients[patientCount].annualClaim = strtok(NULL, delimiter);
patients[patientCount].plan = atoi(strtok(NULL, delimiter));
strcpy(patients[patientCount].contactNum, strtok(NULL, delimiter));
strcpy(patients[patientCount].address, strtok(NULL, delimiter));
strcpy(line, "\0");
patientCount++;
}
fclose(fp);
fp = fopen("claims.txt", "r");
int claimCount = 0;
while (fp != NULL)
{
if (fgets(line, MAX_LENGTH - 1, fp) == NULL)
break;
if (line == "\n")
break;
claims[claimCount].id = atoi(strtok(line, delimiter));
claims[claimCount].claimedYear = atoi(strtok(NULL, delimiter));
claims[claimCount].amountClaimed = atoi(strtok(NULL, delimiter));
claims[claimCount].remaininigAmount = atoi(strtok(NULL, delimiter));
strcpy(line, "\0");
claimCount++;
}
fclose(fp);
}
CODE:
Explanation is given in the comment wise.
// prints the account information of the subscribers
void accountInfo() {
system("cls"); // clears the output screen
int ch; // represents the choice
printf("1-Total Amount Claimed by LifeTime Claim Limit
subscriber\n");
printf("2-Total number of Annual Claim Limit who have exhausted all
their eligible amount\n");
scanf("%d", & ch); // can give input choice
if (ch == 1) // if choice is 1 then below statements executes
{
int totalAmountClaimedByLifeTimeSubs = 0;
for (int i = 0; i < patientCount; i++) // iterates till reaches
all the patient Counts
{
if (patients[i].annualClaim == false) // if patient fails to claim
then below code
// executes for claims array to check the patient claim done on
one-on-one basis.
{
for (int j = 0; j < patientCount; j++) {
if (claims[j].id == patients[i].id) // if the patient has a certain
then that will be added to
// total amount claimed by life time
{
totalAmountClaimedByLifeTimeSubs += claims[j].amountClaimed;
}
}
}
}
// total amount claimed is printed here.
printf("\nTotal amount Claimed By LifeTime Subscribers is: %d\n",
totalAmountClaimedByLifeTimeSubs);
} else // if the choice is other than the 1 then below code
executes
{
int count = 0;
for (int i = 0; i < patientCount; i++) {
if (claims[i].remaininigAmount <= 0 &&
patients[i].annualClaim == true)
count++;
}
printf("Total number of Annual Claim Limit Subcriber who have
exhausted all their amount are: %d\n", count);
}
system("pause");
// Whenever pause is used then the program waits to be terminated,
and halts the exceution of the parent program.
// Only after the pause program is terminated, will the original
program continue.
}
// function describing the searchingFunctionalities in the
program
// there are two functionalities given here.
// one by age and other by ID.
void searchingFunctionalities() {
system("cls"); // clears the output screen
int ch; // choice variable can choose either ID by choosing 1 or
Age by choosing 2
printf("1-Search by ID\n2-Search by age\n");
scanf("%d", & ch); // enters choice
// if choice is 1 then the search by ID is exceuted
if (ch == 1) {
int id;
printf("Enter patient ID for which you want Search: ");
scanf("%d", & id); // allows you to enter the patient ID.
int i;
for (i = 0; i < patientCount; i++) { // iterates till the number
of patients
// iterates for the patients array to check the ID with the ID
given.
// if matches prints the contents and then breaks the loop
if (patients[i].id == id) {
printf("\nSubscriber Name: %s\nSubscriber Age: %d\nSubscriber
Contact: %s\nSubscriber Address: %s\n", patients[i].name,
patients[i].age, patients[i].contactNum,
patients[i].address);
break;
}
}
printf("Subscriber Not Found");
}
else { // seach by age is executed.
int age;
printf("Enter age for which you want Search: ");
scanf("%d", & age); // can enter the age
for (int i = 0; i < patientCount; i++) { // iterates till the
number of patients
// iterates for the patients array to check the ID with the ID
given.
// if matches prints the contents and then breaks the loop
if (patients[i].age == age) {
printf("\nSubscriber Name: %s\nSubscriber ID: %d\nSubscriber
Contact: %s\nSubscriber Address: %s\n", patients[i].name,
patients[i].id, patients[i].contactNum, patients[i].address);
}
}
}
system("pause");
// Whenever pause is used then the program waits to be terminated,
and halts the exceution of the parent program.
// Only after the pause program is terminated, will the original
program continue.
}
// function describing the loading the data
// here loads two files one is patients.txt and other is
claims.txt
// the contents from the patients.txt is stored in patients of
structure type.
// the contents from the claims.txt is stored in claims of
structure type.
void loadData() {
char line[MAX_LENGTH]; // line of character array type of
MAX_LENGTH is created.
const char * delimiter = ",\n"; // delimiter is choosen an \n
FILE * fp; // file pointer created
// patients.txt is opened.
fp = fopen("patients.txt", "r");
char c;
// if the patients.txt doesnt exist then return NULL
// prints file not found.
if (fp == NULL) {
printf("file not found");
return;
}
// if the file is not NULL then contents are read line by
line
// using strtok the contents are broken by the delimiter
// atoi is the function converts alpha values to the integer
numeric type.
while (fp != NULL) {
if (fgets(line, MAX_LENGTH - 1, fp) == NULL)
break; // till the file reaches the end-pointer this loop is
executed.
if (line[1] == '\0')
break;
// the line is broken down using the delimiter and is stored in
the
// respective structure members.
patients[patientCount].id = atoi(strtok(line, delimiter));
strcpy(patients[patientCount].name, strtok(NULL, delimiter));
patients[patientCount].age = atoi(strtok(NULL, delimiter));
patients[patientCount].annualClaim = strtok(NULL, delimiter);
patients[patientCount].plan = atoi(strtok(NULL, delimiter));
strcpy(patients[patientCount].contactNum, strtok(NULL,
delimiter));
strcpy(patients[patientCount].address, strtok(NULL,
delimiter));
strcpy(line, "\0");
patientCount++; // patientCount is maintained.
}
fclose(fp); // file pointer is closed.
// claims.txt file is opened.
fp = fopen("claims.txt", "r");
int claimCount = 0; // claimCount is initialised to 0.
// if the file is not NULL then contents are read line by
line
// using strtok the contents are broken by the delimiter
// atoi is the function converts alpha values to the integer
numeric type.
while (fp != NULL) {
if (fgets(line, MAX_LENGTH - 1, fp) == NULL)
break;
if (line == "\n")
break;
claims[claimCount].id = atoi(strtok(line, delimiter));
claims[claimCount].claimedYear = atoi(strtok(NULL,
delimiter));
claims[claimCount].amountClaimed = atoi(strtok(NULL,
delimiter));
claims[claimCount].remaininigAmount = atoi(strtok(NULL,
delimiter));
strcpy(line, "\0");
claimCount++; // claimCount is incremented by 1.
}
fclose(fp); // file pointer is closed
}