This example illustrates the concept of structures
discussed in previous post. Here structure variable bill is declared
which has structure template Bill and members firstName, lastName,
Address, previousUnit and presentUnit. The structure represents the
information of a customer and the program calculates the cost by
checking previous and present unit. After you understand the code you
will have the knowledge of following things:
- Defining Structure
- Declaring Structure variable
- Accessing members of structure
- Passing structure in function
- Returning structure from function
The source code and output is given here…
Source Code//generating electricity bill using structure in C
#include<stdio.h>/** This block defines the structure Bill having
fields for first name, last name, address,previous unit and present unit. The structuremust be defined before main function */struct Bill{
char firstName[10];
char lastName[10];
char Address[20];
float previousUnit;
float presentUnit;
};/** This is the function definition that
calculates the cost. Here structure is passedas an argument */float generateBill(struct Bill temp){float diff;
diff = temp.presentUnit - temp.previousUnit;if(diff > 20){
return diff*4.75;
}else{
return 20*4.75+(diff-20)*7.75;
}}int main(){
struct Bill bill; /*Declaration of structure variable*/printf("Fill up the following: \n");printf("First Name: ");gets(bill.firstName); //accessing member
printf("Last Name: ");gets(bill.lastName);printf("Address: ");gets(bill.Address);printf("Previous Unit: ");scanf("%f",&bill.previousUnit);printf("Present Unit: ");scanf("%f",&bill.presentUnit);printf("\a\n\n***********Electricity Bill***************\n\n\a");printf("Name: %s %s",bill.firstName,bill.lastName);printf("\nAddress: %s",bill.Address);printf("\nPrevious Unit: %.3f Current Unit: %.3f",bill.previousUnit,bill.presentUnit);printf("\nCost: %.3f\n\n", generateBill(bill));return 0;
}
Output
It is not working
ReplyDelete