Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CourseWork3.cpp : Defines the entry point for the console application.
- // Calculate Hire Purchase by input of car price, dp, years of instalment and interest rate
- // Create a calculator for Hire Purchase & Roadtax
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char txt_line[50];
- int records=0;
- int flag=0;
- int addfromlast=0;
- struct customerEnquiry
- {
- char fullname[30];
- char mobile[15];
- char date[10];
- char enquiry[100];
- };
- void hirePurchase()
- {
- float car_price, interest, down_payment, year, instalment, total_interest;
- printf("Enter car price: RM ");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%f", &car_price);
- printf("Enter down payment: RM ");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%f", &down_payment);
- printf("Enter year of instalment: ");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%f", &year);
- printf("Enter interest rate: ");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%f", &interest);
- total_interest = (car_price-down_payment)*(interest/100)*year;
- instalment = (car_price-down_payment+total_interest)/(year*12);
- printf("The monthly instalment is RM %.2f \n", instalment);
- system("pause");
- system("cls");
- }
- void roadtax()
- {
- int cc;
- float rtax;
- printf("Enter your enginee CC[example: 1500, 1600]\n");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%i", &cc);
- if( cc>0 && cc<1001 )
- {
- rtax = 20;
- }
- else if( cc>1000 && cc<1201 )
- {
- rtax = 55;
- }
- else if( cc>1200 && cc<1401 )
- {
- rtax = 70;
- }
- else if( cc>1400 && cc<1601 )
- {
- rtax = 90;
- }
- else if( cc>1600 && cc<1801 )
- {
- rtax = (cc-1600)*0.4+200;
- }
- else if( cc>1800 && cc<2001 )
- {
- rtax = (cc-1800)*0.5+280;
- }
- else if( cc>2000 && cc<2501 )
- {
- rtax = (cc-2000)+380;
- }
- else if( cc>2500 && cc<3001 )
- {
- rtax = (cc-2500)*2.5+880;
- }
- else if( cc>3000 )
- {
- rtax = (cc-3000)*4.5+2130;
- }
- else
- {
- printf("Invalid number !!");
- }
- printf("\n Your payable amount of roadtax is RM %.2f \n", rtax);
- system("pause");
- system("cls");
- }
- void drawShape()
- {
- char txt_line[50];
- int height, width, n_width;
- n_width = 0;
- printf("Enter height: \n");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%i", &height);
- printf("Enter width: \n");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%i", &width);
- for( ; height>0; height-- )
- {
- if(n_width>=width) n_width=0;
- for( ; width>0; width-- )
- {
- printf("*");
- n_width++;
- }
- width = n_width;
- printf("\n");
- }
- system("pause");
- system("cls");
- }
- void addCustomerEnquiries( struct customerEnquiry *enquiries )
- {
- printf("Enter fullname(without space): \n");
- fgets(txt_line, sizeof(txt_line), stdin);
- txt_line[strcspn(txt_line, "\n")]='\0'; //remove auto trailing newline
- strcpy_s(enquiries[records].fullname, txt_line);
- printf("Enter mobile number: \n");
- fgets(txt_line, sizeof(txt_line), stdin);
- txt_line[strcspn(txt_line, "\n")]='\0';
- strcpy_s(enquiries[records].mobile, txt_line);
- printf("Enter date in [dd/mm/yyyy]: \n");
- fgets(txt_line, sizeof(txt_line), stdin);
- txt_line[strcspn(txt_line, "\n")]='\0';
- strcpy_s(enquiries[records].date, txt_line);
- printf("Enter enquiry: \n");
- fgets(txt_line, sizeof(txt_line), stdin);
- txt_line[strcspn(txt_line, "\n")]='\0';
- strcpy_s(enquiries[records].enquiry, txt_line);
- records++;
- flag=1;
- }
- void displayCustomerEnquiries( struct customerEnquiry *enquiries )
- {
- for( int i=0; i<records; i++ )
- {
- printf(" Customer Records %i\n", i+1);
- printf("Fullname: %s", enquiries[i].fullname);
- printf("Mobile number: %s", enquiries[i].mobile);
- printf("Date: %s", enquiries[i].date);
- printf("Enquiries: %s", enquiries[i].enquiry);
- }
- system("pause");
- }
- void searchCustomerEnquiries( struct customerEnquiry *enquiries )
- {
- char searchString[50];
- printf("Enter fullname: \n");
- fgets(searchString, sizeof(searchString), stdin);
- for( int i=0; i<records; i++ )
- {
- if(strcmp(enquiries[i].fullname, searchString)==0)
- {
- printf("Enquiries found\n");
- printf("%s", enquiries[i].fullname);
- printf("%s", enquiries[i].mobile);
- printf("%s", enquiries[i].date);
- printf("%s", enquiries[i].enquiry);
- }
- else
- {
- printf("Enquiries not available\n");
- }
- }
- system("pause");
- }
- void writeToFile( struct customerEnquiry *enquiries )
- {
- FILE *fptr;
- fptr=fopen("data.txt", "a");
- char searchString[50];
- if(fptr==NULL)
- {
- printf("File error\n");
- exit(1);
- }
- for( int i=addfromlast; i<records; i++ )
- {
- fprintf(fptr, "%s,%s,%s,%s\n", enquiries[i].fullname, enquiries[i].mobile, enquiries[i].date, enquiries[i].enquiry);
- }
- fclose(fptr);
- addfromlast=records;
- flag=2;
- system("pause");
- }
- void readFromFile( struct customerEnquiry *enquiries )
- {
- FILE *fptr;
- int c;
- char buff[300];
- if(flag==0 || flag==2)
- {
- fptr=fopen("data.txt", "r");
- while(fscanf(fptr, "%s", buff) != EOF)
- {
- printf("%s\n", buff);
- }
- fclose(fptr);
- system("pause");
- }
- else if(flag==1)
- {
- printf("You have data not saved in file\n");
- system("pause");
- }
- }
- void searchFromFile( struct customerEnquiry *enquiries )
- {
- if(flag==2 || flag==0)
- {
- char searchString[15];
- printf("Enter mobile\n");
- fgets(searchString, sizeof(searchString), stdin);
- searchString[strcspn(searchString, "\n")]='\0';
- FILE *fptr;
- char buff[300];
- int c;
- fptr=fopen("data.txt", "r");
- while(fscanf(fptr, "%s", &buff) != EOF)
- {
- char buff2[300];
- size_t destinationsize=sizeof(buff2);
- strncpy(buff2, buff, destinationsize);
- buff2[destinationsize-1] ='\0';
- const char s[2]=",";
- char *token;
- token=strtok(buff2, s);
- while(token != NULL)
- {
- if(strcmp(searchString, token)==0)
- {
- printf("%s\n", buff);
- }
- token=strtok(NULL, s);
- }
- }
- fclose(fptr);
- system("pause");
- }
- else
- {
- printf("You have data not saved to file\n");
- system("pause");
- }
- }
- void main()
- {
- // for(;;) // continuous loop
- // {
- int choice, option;
- do
- {
- printf("1. Hire purchase calculator\n");
- printf("2. Roadtax calculator\n");
- printf("3. Draw shape\n");
- printf("4. Customer enquiries\n");
- printf("Enter option: \n");
- printf("Enter 9 to Exit\n");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%i", &choice);
- if(choice == 1)
- {
- hirePurchase();
- }
- else if(choice == 2)
- {
- roadtax();
- }
- else if(choice == 3)
- {
- drawShape();
- }
- else if(choice == 4)
- {
- option=0;
- do
- {
- system("cls");
- struct customerEnquiry enquiries[100];
- printf("1. Add customer enquiries\n");
- printf("2. Display customer enquiries\n");
- // printf("3. Search customer enquiries\n");
- printf("3. Search enquiries from file\n");
- printf("4. Write records in file\n");
- printf("5. Read records from file\n");
- printf("Enter option:\n");
- printf("Enter 9 to return Main\n");
- fgets(txt_line, sizeof(txt_line), stdin);
- sscanf_s(txt_line, "%i", &option);
- if(option == 1)
- {
- addCustomerEnquiries(enquiries);
- }
- else if(option == 2)
- {
- displayCustomerEnquiries(enquiries);
- }
- else if(option == 3)
- {
- // searchCustomerEnquiries(enquiries);
- searchFromFile(enquiries);
- }
- else if(option == 4)
- {
- writeToFile(enquiries);
- }
- else if(option==5)
- {
- readFromFile(enquiries);
- }
- else if(option == 9)
- {
- system("cls");
- main();
- }
- else if(option!=1 || option!=2 || option!=3 || option!=4 || option !=5)
- {
- printf("Invalid option !!\n");
- system("pause");
- system("cls");
- }
- }
- while(option==1 || option==2 || option==3 || option==4 || option == 5);
- }
- else if(choice==9)
- {
- printf("Goodbye");
- }
- else if(choice!=1 || choice!=2 || choice !=3 || choice!=4)
- {
- printf("Invalid choice !! \n");
- system("pause");
- system("cls");
- }
- }
- while(choice==1 || choice==2 || choice==3 || choice==4);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement