Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.23 KB | None | 0 0
  1. // CourseWork3.cpp : Defines the entry point for the console application.
  2. // Calculate Hire Purchase by input of car price, dp, years of instalment and interest rate
  3. // Create a calculator for Hire Purchase & Roadtax
  4.  
  5. #include "stdafx.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. char txt_line[50];
  11. int  records=0;
  12. int  flag=0;
  13. int  addfromlast=0;
  14.  
  15. struct customerEnquiry
  16. {
  17.     char fullname[30];
  18.     char mobile[15];
  19.     char date[10];
  20.     char enquiry[100];
  21. };
  22.  
  23. void hirePurchase()
  24. {
  25.     float car_price, interest, down_payment, year, instalment, total_interest;
  26.  
  27.     printf("Enter car price: RM ");
  28.     fgets(txt_line, sizeof(txt_line), stdin);
  29.     sscanf_s(txt_line, "%f", &car_price);
  30.  
  31.     printf("Enter down payment: RM ");
  32.     fgets(txt_line, sizeof(txt_line), stdin);
  33.     sscanf_s(txt_line, "%f", &down_payment);
  34.  
  35.     printf("Enter year of instalment: ");
  36.     fgets(txt_line, sizeof(txt_line), stdin);
  37.     sscanf_s(txt_line, "%f", &year);
  38.  
  39.     printf("Enter interest rate: ");
  40.     fgets(txt_line, sizeof(txt_line), stdin);
  41.     sscanf_s(txt_line, "%f", &interest);
  42.  
  43.     total_interest = (car_price-down_payment)*(interest/100)*year;
  44.     instalment = (car_price-down_payment+total_interest)/(year*12);
  45.  
  46.     printf("The monthly instalment is RM %.2f \n", instalment);
  47.     system("pause");
  48.     system("cls");
  49. }
  50.  
  51. void roadtax()
  52. {
  53.     int cc;
  54.     float rtax;
  55.  
  56.     printf("Enter your enginee CC[example: 1500, 1600]\n");
  57.     fgets(txt_line, sizeof(txt_line), stdin);
  58.     sscanf_s(txt_line, "%i", &cc);
  59.  
  60.     if( cc>0 && cc<1001 )
  61.     {
  62.         rtax = 20;
  63.     }
  64.     else if( cc>1000 && cc<1201 )
  65.     {
  66.         rtax = 55;
  67.     }
  68.     else if( cc>1200 && cc<1401 )
  69.     {
  70.         rtax = 70;
  71.     }
  72.     else if( cc>1400 && cc<1601 )
  73.     {
  74.         rtax = 90;
  75.     }
  76.     else if( cc>1600 && cc<1801 )
  77.     {
  78.         rtax = (cc-1600)*0.4+200;
  79.     }
  80.     else if( cc>1800 && cc<2001 )
  81.     {
  82.         rtax = (cc-1800)*0.5+280;
  83.     }
  84.     else if( cc>2000 && cc<2501 )
  85.     {
  86.         rtax = (cc-2000)+380;
  87.     }
  88.     else if( cc>2500 && cc<3001 )
  89.     {
  90.         rtax = (cc-2500)*2.5+880;
  91.     }
  92.     else if( cc>3000 )
  93.     {
  94.         rtax = (cc-3000)*4.5+2130;
  95.     }
  96.     else
  97.     {
  98.         printf("Invalid number !!");
  99.     }
  100.     printf("\n Your payable amount of roadtax is RM %.2f \n", rtax);
  101.     system("pause");
  102.     system("cls");
  103. }
  104.  
  105. void drawShape()
  106. {
  107.     char txt_line[50];
  108.     int height, width, n_width;
  109.     n_width = 0;
  110.  
  111.     printf("Enter height: \n");
  112.     fgets(txt_line, sizeof(txt_line), stdin);
  113.     sscanf_s(txt_line, "%i", &height);
  114.  
  115.     printf("Enter width: \n");
  116.     fgets(txt_line, sizeof(txt_line), stdin);
  117.     sscanf_s(txt_line, "%i", &width);
  118.  
  119.     for( ; height>0; height-- )  
  120.     {
  121.         if(n_width>=width)  n_width=0;
  122.  
  123.         for( ; width>0; width-- )  
  124.         {
  125.             printf("*");
  126.             n_width++;
  127.         }
  128.         width = n_width;
  129.         printf("\n");
  130.     }
  131.     system("pause");
  132.     system("cls");
  133. }
  134.  
  135. void addCustomerEnquiries( struct customerEnquiry *enquiries )
  136. {
  137.     printf("Enter fullname(without space): \n");
  138.     fgets(txt_line, sizeof(txt_line), stdin);
  139.     txt_line[strcspn(txt_line, "\n")]='\0'; //remove auto trailing newline
  140.     strcpy_s(enquiries[records].fullname, txt_line);
  141.    
  142.     printf("Enter mobile number: \n");
  143.     fgets(txt_line, sizeof(txt_line), stdin);
  144.     txt_line[strcspn(txt_line, "\n")]='\0';
  145.     strcpy_s(enquiries[records].mobile, txt_line);
  146.    
  147.     printf("Enter date in [dd/mm/yyyy]: \n");
  148.     fgets(txt_line, sizeof(txt_line), stdin);
  149.     txt_line[strcspn(txt_line, "\n")]='\0';
  150.     strcpy_s(enquiries[records].date, txt_line);
  151.    
  152.     printf("Enter enquiry: \n");
  153.     fgets(txt_line, sizeof(txt_line), stdin);
  154.     txt_line[strcspn(txt_line, "\n")]='\0';
  155.     strcpy_s(enquiries[records].enquiry, txt_line);
  156.     records++;
  157.     flag=1;
  158. }
  159.  
  160. void displayCustomerEnquiries( struct customerEnquiry *enquiries )
  161. {
  162.     for( int i=0; i<records; i++ )
  163.     {
  164.         printf("    Customer Records %i\n", i+1);
  165.         printf("Fullname: %s",      enquiries[i].fullname);
  166.         printf("Mobile number: %s", enquiries[i].mobile);
  167.         printf("Date: %s",          enquiries[i].date);
  168.         printf("Enquiries: %s",     enquiries[i].enquiry);
  169.     }
  170.     system("pause");
  171. }
  172.  
  173. void searchCustomerEnquiries( struct customerEnquiry *enquiries )
  174. {
  175.     char searchString[50];
  176.  
  177.     printf("Enter fullname: \n");
  178.     fgets(searchString, sizeof(searchString), stdin);
  179.  
  180.     for( int i=0; i<records; i++ )
  181.     {
  182.         if(strcmp(enquiries[i].fullname, searchString)==0)
  183.         {
  184.             printf("Enquiries found\n");
  185.             printf("%s", enquiries[i].fullname);
  186.             printf("%s", enquiries[i].mobile);
  187.             printf("%s", enquiries[i].date);
  188.             printf("%s", enquiries[i].enquiry);
  189.         }
  190.         else
  191.         {
  192.             printf("Enquiries not available\n");
  193.         }
  194.     }
  195.     system("pause");
  196. }
  197.  
  198. void writeToFile( struct customerEnquiry *enquiries )
  199. {
  200.     FILE *fptr;
  201.     fptr=fopen("data.txt", "a");
  202.     char searchString[50];
  203.  
  204.     if(fptr==NULL)
  205.     {
  206.         printf("File error\n");
  207.         exit(1);
  208.     }
  209.  
  210.     for( int i=addfromlast; i<records; i++ )
  211.     {
  212.         fprintf(fptr, "%s,%s,%s,%s\n", enquiries[i].fullname, enquiries[i].mobile, enquiries[i].date, enquiries[i].enquiry);
  213.     }
  214.     fclose(fptr);
  215.     addfromlast=records;
  216.     flag=2;
  217.     system("pause");
  218. }
  219.  
  220. void readFromFile( struct customerEnquiry *enquiries )
  221. {
  222.     FILE *fptr;
  223.     int c;
  224.     char buff[300];
  225.  
  226.     if(flag==0 || flag==2)
  227.     {
  228.         fptr=fopen("data.txt", "r");
  229.         while(fscanf(fptr, "%s", buff) != EOF)
  230.         {
  231.             printf("%s\n", buff);
  232.         }
  233.         fclose(fptr);
  234.         system("pause");
  235.     }
  236.     else if(flag==1)
  237.     {
  238.         printf("You have data not saved in file\n");
  239.         system("pause");
  240.     }
  241. }
  242.  
  243. void searchFromFile( struct customerEnquiry *enquiries )
  244. {
  245.     if(flag==2 || flag==0)
  246.     {
  247.         char searchString[15];
  248.         printf("Enter mobile\n");
  249.         fgets(searchString, sizeof(searchString), stdin);
  250.         searchString[strcspn(searchString, "\n")]='\0';
  251.         FILE *fptr;
  252.         char buff[300];
  253.         int c;
  254.         fptr=fopen("data.txt", "r");
  255.         while(fscanf(fptr, "%s", &buff) != EOF)
  256.         {
  257.             char buff2[300];
  258.             size_t destinationsize=sizeof(buff2);
  259.             strncpy(buff2, buff, destinationsize);
  260.             buff2[destinationsize-1] ='\0';
  261.             const char s[2]=",";
  262.             char *token;
  263.             token=strtok(buff2, s);
  264.             while(token != NULL)
  265.             {
  266.                 if(strcmp(searchString, token)==0)
  267.                 {
  268.                     printf("%s\n", buff);
  269.                 }
  270.                 token=strtok(NULL, s);
  271.             }
  272.         }
  273.         fclose(fptr);
  274.         system("pause");
  275.     }
  276.     else
  277.     {
  278.         printf("You have data not saved to file\n");
  279.         system("pause");
  280.     }
  281. }
  282.  
  283. void main()
  284. {
  285. //  for(;;) // continuous loop
  286. //  {
  287.         int choice, option;
  288.  
  289.         do
  290.         {
  291.             printf("1. Hire purchase calculator\n");
  292.             printf("2. Roadtax calculator\n");
  293.             printf("3. Draw shape\n");
  294.             printf("4. Customer enquiries\n");
  295.             printf("Enter option: \n");
  296.             printf("Enter 9 to Exit\n");
  297.             fgets(txt_line, sizeof(txt_line), stdin);
  298.             sscanf_s(txt_line, "%i", &choice);
  299.  
  300.             if(choice == 1)
  301.             {
  302.                 hirePurchase();
  303.             }
  304.             else if(choice == 2)
  305.             {
  306.                 roadtax();
  307.             }
  308.             else if(choice == 3)
  309.             {
  310.                 drawShape();
  311.             }
  312.             else if(choice == 4)
  313.             {
  314.                 option=0;
  315.  
  316.                 do
  317.                 {
  318.                     system("cls");
  319.                     struct customerEnquiry enquiries[100];
  320.  
  321.                     printf("1. Add customer enquiries\n");
  322.                     printf("2. Display customer enquiries\n");
  323. //                  printf("3. Search customer enquiries\n");
  324.                     printf("3. Search enquiries from file\n");
  325.                     printf("4. Write records in file\n");
  326.                     printf("5. Read records from file\n");
  327.                     printf("Enter option:\n");
  328.                     printf("Enter 9 to return Main\n");
  329.                     fgets(txt_line, sizeof(txt_line), stdin);
  330.                     sscanf_s(txt_line, "%i", &option);
  331.                
  332.                     if(option == 1)
  333.                     {
  334.                         addCustomerEnquiries(enquiries);
  335.                     }
  336.                     else if(option == 2)
  337.                     {
  338.                         displayCustomerEnquiries(enquiries);
  339.                     }
  340.                     else if(option == 3)
  341.                     {
  342. //                      searchCustomerEnquiries(enquiries);
  343.                         searchFromFile(enquiries);
  344.                     }
  345.                     else if(option == 4)
  346.                     {
  347.                         writeToFile(enquiries);
  348.                     }
  349.                     else if(option==5)
  350.                     {
  351.                         readFromFile(enquiries);
  352.                     }
  353.                     else if(option == 9)
  354.                     {
  355.                         system("cls");
  356.                         main();
  357.                     }
  358.                     else if(option!=1 || option!=2 || option!=3 || option!=4 || option !=5)
  359.                     {
  360.                         printf("Invalid option !!\n");
  361.                         system("pause");
  362.                         system("cls");
  363.                     }
  364.                 }
  365.                 while(option==1 || option==2 || option==3 || option==4 || option == 5);
  366.             }
  367.             else if(choice==9)
  368.             {
  369.                 printf("Goodbye");
  370.             }
  371.             else if(choice!=1 || choice!=2 || choice !=3 || choice!=4)
  372.             {
  373.                 printf("Invalid choice !! \n");
  374.                 system("pause");
  375.                 system("cls");
  376.             }
  377.         }
  378.         while(choice==1 || choice==2 || choice==3 || choice==4);   
  379. //  }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement