In: Computer Science
Hello sir below is my code can you plz explain it line by line in details
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include<time.h>
struct patient {
int id;
int age;
bool annualClaim;
int plan;
char name[30];
char contactNum[15];
char address[50];
};
#define MAX_LENGTH 500
struct patient patients[100];
int patientCount = 0;
struct claim {
int id;
int claimedYear;
int amountClaimed;
int remaininigAmount;
};
struct claim claims[100];
void subscribe()
{
system("cls");
patients[patientCount].id = patientCount + 1;
printf("Enter age: ");
scanf("%d", & patients[patientCount].age);
printf("\n\n%-25sHealth Insurence Plan\n\n", " ");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20d|%-20d|%-20d|\n", "Monthly Premium", 120, 150, 200);
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20d|%-20d|%-20d|\n", "Annual Claim Limit", 120000, 150000, 200000);
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20d|%-20d|%-20d|\n", "Lifetime Claim Limit", 600000, 750000, 1000000);
printf("-----------------------------------------------------------------------------------------------\n");
printf("\n\n%-25sAge Group and Health Insurence Plan\n\n", " ");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-25s%-30s\n", "Types of Claim", " ", "Eligibility Amount");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", "Room Charges", "120/day", "150/day", "200/day");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", "Intensive Care Unit", "250/day", "400/day", "700/day");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", "Hospital Supplies and Services", " ", " ", " ");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-60s|\n", "Surgical Fees", "As charged Sbject to approval by ZeeMediLife");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", "Other Fees", " ", " ", " ");
printf("-----------------------------------------------------------------------------------------------\n\n");
int ch;
do {
printf("Select a Claim Limit Type\n1-Annual Claim Limit 2-Lifetime Claim Limit: ");
scanf("%d", & ch);
} while (ch < 1 || ch > 2);
if (ch == 1)
patients[patientCount].annualClaim = true;
else
patients[patientCount].annualClaim = false;
do {
printf("Select a plan\n1-Plan120 2-Plan150 3-Plan200: ");
scanf("%d", & ch);
} while (ch < 1 || ch > 3);
patients[patientCount].plan = ch;
printf("Enter Name: ");
scanf("%s", & patients[patientCount].name);
printf("Contact Number: ");
scanf("%s", & patients[patientCount].contactNum);
printf("Enter Address: ");
scanf("%s", & patients[patientCount].address);
FILE * fp;
fp = fopen("patients.txt", "a");
fprintf(fp, "%d,%s,%d,%d,%d,%s,%s\n", patients[patientCount].id, patients[patientCount].name, patients[patientCount].age, patients[patientCount].annualClaim, patients[patientCount].plan, patients[patientCount].contactNum, patients[patientCount].address);
fclose(fp);
time_t s;
struct tm * currentTime;
s = time(NULL);
currentTime = localtime( & s);
claims[patientCount].id = patients[patientCount].id;
claims[patientCount].amountClaimed = 0;
claims[patientCount].claimedYear = currentTime -> tm_hour + 1900;
if (patients[patientCount].annualClaim == true)
{
if (patients[patientCount].plan == 1)
claims[patientCount].remaininigAmount = 120000;
else if (patients[patientCount].plan == 2)
claims[patientCount].remaininigAmount = 150000;
else
claims[patientCount].remaininigAmount = 200000;
} else
{
if (patients[patientCount].plan == 1)
claims[patientCount].remaininigAmount = 600000;
else if (patients[patientCount].plan == 2)
claims[patientCount].remaininigAmount = 750000;
else
claims[patientCount].remaininigAmount = 1000000;
}
patientCount++;
fp = fopen("claims.txt", "a");
if (fp == NULL)
printf("File Dont exist");
fprintf(fp, "%d,%d,%d,%d\n", claims[patientCount].id, claims[patientCount].claimedYear, claims[patientCount].amountClaimed, claims[patientCount].remaininigAmount);
fclose(fp);
system("pause");
}
I HAVE COMMENTED EACH LINE EXPLANATION AGIANTS EACH LINE YOU CAN EXECUTE IT
#include<stdio.h>// including stabdard input output
#include<stdlib.h> // including standard library
#include<stdbool.h>
#include<string.h>
#include<time.h>
struct patient { //defining a structure patient with following fields
int id; // an id of integer type
int age; // age of integertpye
bool annualClaim; //annualvlaim of boolean type
int plan; // plan of integer type
char name[30]; //an name array of char type with size =30
char contactNum[15]; //a contactnum array of char type with size =15
char address[50]; //an address array of char type with size =50
};
#define MAX_LENGTH 500 // defining max_lenght tp 500 global
struct patient patients[100]; // defining a object of structure patient type with name patients and size =100
int patientCount = 0; //intialising patient count to 0
struct claim { //defining a structure claim with following fields
int id; // an id of integer type
int claimedYear; // a claimedyear of integer type
int amountClaimed; // aa amountclaimed of integer type
int remaininigAmount; // a remainingAmount of integer type
};
struct claim claims[100]; // defining a object of structure claim type with name claims and size =100
void subscribe() // defining a function of subscribe name
{
system("cls"); // clearing tha system
patients[patientCount].id = patientCount + 1; // entering the value to id of structure patient's patients object
printf("Enter age: "); //taking input for age from user/keyboard
scanf("%d", & patients[patientCount].age); // entering the value to age of structure patient's patients object
printf("\n\n%-25sHealth Insurence Plan\n\n", " "); // dislaying message to screen Health Insurence Plan
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)"); //dislaying message to screen |Plan 120(RM) |Plan 150(RM) |Plan 200(RM) |
printf("-----------------------------------------------------------------------------------------------\n");// these all printf statements are used to display a table like structure as i have shown it below these are just a printf statements
printf("|%-30s|%-20d|%-20d|%-20d|\n", "Monthly Premium", 120, 150, 200);
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20d|%-20d|%-20d|\n", "Annual Claim Limit", 120000, 150000, 200000);
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20d|%-20d|%-20d|\n", "Lifetime Claim Limit", 600000, 750000, 1000000);
printf("-----------------------------------------------------------------------------------------------\n");
printf("\n\n%-25sAge Group and Health Insurence Plan\n\n", " ");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-25s%-30s\n", "Types of Claim", " ", "Eligibility Amount");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", "Room Charges", "120/day", "150/day", "200/day");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", "Intensive Care Unit", "250/day", "400/day", "700/day");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", "Hospital Supplies and Services", " ", " ", " ");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-60s|\n", "Surgical Fees", "As charged Sbject to approval by ZeeMediLife");
printf("-----------------------------------------------------------------------------------------------\n");
printf("|%-30s|%-20s|%-20s|%-20s|\n", "Other Fees", " ", " ", " ");
printf("-----------------------------------------------------------------------------------------------\n\n");
/*
-----------------------------------------------------------------------------------------------
| |Plan 120(RM) |Plan 150(RM) |Plan 200(RM) |
-----------------------------------------------------------------------------------------------
|Monthly Premium |120 |150 |200 |
-----------------------------------------------------------------------------------------------
|Annual Claim Limit |120000 |150000 |200000 |
-----------------------------------------------------------------------------------------------
|Lifetime Claim Limit |600000 |750000 |1000000 |
-----------------------------------------------------------------------------------------------
Age Group and Health Insurence Plan
-----------------------------------------------------------------------------------------------
|Types of Claim | Eligibility Amount
-----------------------------------------------------------------------------------------------
| |Plan 120(RM) |Plan 150(RM) |Plan 200(RM) |
-----------------------------------------------------------------------------------------------
|Room Charges |120/day |150/day |200/day |
-----------------------------------------------------------------------------------------------
|Intensive Care Unit |250/day |400/day |700/day |
-----------------------------------------------------------------------------------------------
|Hospital Supplies and Services| | | |
-----------------------------------------------------------------------------------------------
|Surgical Fees |As charged Sbject to approval by ZeeMediLife
|
-----------------------------------------------------------------------------------------------
|Other Fees | | | |
-----------------------------------------------------------------------------------------------
*/
int ch; // defining a ch variable of integer type
do { // startin do loop
printf("Select a Claim Limit Type\n1-Annual Claim Limit
2-Lifetime Claim Limit: ");
/* display this message
Select a Claim Limit Type
1-Annual Claim Limit 2-Lifetime Claim Limit:
*/
scanf("%d", & ch); // storing to ch variable
} while (ch < 1 || ch > 2); // while conditioning statement checking only for 1 and 2 if user enter any value other than 1 and 2 loop will terminate
if (ch == 1) // if condition checking if value entered by user is 1 then annualclaims become true
patients[patientCount].annualClaim = true;
else
patients[patientCount].annualClaim = false; // otherwise annualcaims become false
do { // starting do loop
printf("Select a plan\n1-Plan120 2-Plan150 3-Plan200: "); //dipaying this message 1-Plan120 2-Plan150 3-Plan200:
scanf("%d", & ch); ///storing user input value to ch variable
} while (ch < 1 || ch > 3); // while conditioning statement checking only for 1 and 2 if user enter any value other less than 1 or greater than 3, loop will terminate
patients[patientCount].plan = ch; // assigneing ch value to plan field of patient structure
printf("Enter Name: "); // to enter name
scanf("%s", & patients[patientCount].name); // stores the entered name on name field of structure patient's patients object
printf("Contact Number: ");// to enter contact number
scanf("%s", & patients[patientCount].contactNum); //stores the entered contact number on contactnum field of structure patient's patients object
printf("Enter Address: "); // to enter address
scanf("%s", & patients[patientCount].address); //stores the entered address on address field of structure patient's patients object
FILE * fp; // declare a pointer of type file
fp = fopen("patients.txt", "a"); // opening a file in append mode i.e Data is added to the end of the file.If the file does not exist, it will be created.
fprintf(fp, "%d,%s,%d,%d,%d,%s,%s\n", patients[patientCount].id,
patients[patientCount].name, patients[patientCount].age,
patients[patientCount].annualClaim, patients[patientCount].plan,
patients[patientCount].contactNum,
patients[patientCount].address);
// data is getting append on file
fclose(fp); // closing the file
time_t s; // declaration of time object
struct tm * currentTime;
s = time(NULL); // Get the system time
currentTime = localtime( & s); // set the local time to current time
claims[patientCount].id = patients[patientCount].id; // assigning structure patient's id to structure claims's id
claims[patientCount].amountClaimed = 0; // initialising amountclaimed to 0
claims[patientCount].claimedYear = currentTime -> tm_hour + 1900; // assigning current time + 1900 hours to claimedyear
if (patients[patientCount].annualClaim == true) // ckecking condition foor annualclaim which we have already defined on line no 156 in if statement
{
if (patients[patientCount].plan == 1) // nested if i.e if annualclaim == true then this will execute and structure patients plan value == 1 then
claims[patientCount].remaininigAmount = 120000; //assigning 120000 to reamining amount
else if (patients[patientCount].plan == 2) // else contition i.e structure patients plan value == 2 then
claims[patientCount].remaininigAmount = 150000; //assigning 150000 to reamining amount
else
claims[patientCount].remaininigAmount = 200000; //assigning 200000 to reamining amount
}
else // outer else
// this will execute when annualclaim == false
{
if (patients[patientCount].plan == 1) // when structure patients plan value == 1 then
claims[patientCount].remaininigAmount = 600000; //assigning 600000 to reamining amount
else if (patients[patientCount].plan == 2) //else contition i.e structure patients plan value == 2 then
claims[patientCount].remaininigAmount = 750000; //assigning 750000 to reamining amount
else
claims[patientCount].remaininigAmount = 1000000; ////assigning 1000000 to reamining amount
}
patientCount++; // post increment in patientcount
fp = fopen("claims.txt", "a"); // opening a file in append mode i.e Data is added to the end of the file.
if (fp == NULL) //If the file does not exist
printf("File Dont exist");// file does not exist
fprintf(fp, "%d,%d,%d,%d\n", claims[patientCount].id,
claims[patientCount].claimedYear,
claims[patientCount].amountClaimed,
claims[patientCount].remaininigAmount);
// data is getting append on file
fclose(fp); // closing the file
system("pause");//This is a Windows-specific command, which
tells the OS to run the pause program.
//This program waits to be terminated, and halts the exceution of
the parent C program.
//Only after the pause program is terminated, will the original program continue.
}