Advertisement
Aritra15

code_offline

Feb 2nd, 2023
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct customer
  5. {
  6.     char name[100];
  7.     char accNo[10];
  8.     char mobileNo[11];
  9.     double balance;
  10.  
  11. };
  12.  
  13. int findAcc(struct customer *c,char *acc,int n)
  14. {
  15.  
  16.     for(int i=0; i<n; i++)
  17.     {
  18.         if(strcmp((c+i)->accNo,acc)==0)
  19.             return i;
  20.     }
  21. }
  22.  
  23. int findmobile(struct customer *c,char *acc,int n)
  24. {
  25.  
  26.     for(int i=0; i<n; i++)
  27.     {
  28.         if(strcmp((c+i)->mobileNo,acc)==0)
  29.             return i;
  30.     }
  31.     return n;
  32.  
  33. }
  34. void printcustomer(struct customer *c,int n)
  35. {
  36.     for(int i=0; i<n; i++)
  37.     {
  38.         printf("Name: %s ",(c+i)->name);
  39.         printf("Account No: <%s> ",(c+i)->accNo);
  40.         printf("Mobile No: %s ",(c+i)->mobileNo);
  41.         printf("Balance: %lf\n",(c+i)->balance);
  42.     }
  43. }
  44. double balanceSum(struct customer *c,int n,double sum)
  45. {
  46.     for(int i=0; i<n; i++)
  47.     {
  48.         sum+=(c+i)->balance;
  49.     }
  50.     return sum;
  51. }
  52.  
  53. void sortcustomer(int n,struct customer *c)
  54. {
  55.     printf("Sorted successfully\n");
  56.     for(int i=0;i<n-1;i++)
  57.     {
  58.         for(int j=0;j<n-1-i;j++)
  59.         {
  60.             if((c+i)->balance< (c+i+1)->balance){
  61.                 float temp=(c+i)->balance;
  62.                 (c+i)->balance=(c+i+1)->balance;
  63.                 (c+i+1)->balance=temp;
  64.                 char *p=(c+i)->name;
  65.                 strcpy((c+i)->name,(c+i+1)->name);
  66.                 strcpy((c+i+1)->name,p);
  67.                 char *q=(c+i)->mobileNo;
  68.                 strcpy((c+i)->mobileNo,(c+i+1)->mobileNo);
  69.                 strcpy((c+i+1)->mobileNo,p);
  70.                 char *r=(c+i)->accNo;
  71.                 strcpy((c+i)->accNo,(c+i+1)->accNo);
  72.                 strcpy((c+i+1)->accNo,p);
  73.  
  74.             }
  75.         }
  76.     }
  77.     return;
  78. }
  79.  
  80. int main()
  81. {
  82.     int n;
  83.     printf("enter number of customers:");
  84.     scanf("%d",&n);
  85.     //char a[2];
  86.     if(n<0 || n>100)printf("invalid input");
  87.     else
  88.     {
  89.  
  90.         struct customer *c=(struct customer*)malloc(n*sizeof(struct customer));
  91.         for(int i=0; i<n; i++)
  92.         {
  93.  
  94.  
  95.             printf("enter customer name:");
  96.             scanf("%s",(c+i)->name);
  97.             printf("enter account no:");
  98.             scanf("%s",(c+i)->accNo);
  99.             printf("enter mobile no:");
  100.             scanf("%s",(c+i)->mobileNo);
  101.             fflush(stdout);
  102.             printf("enter balance: ");
  103.             scanf("%lf",&(c+i)->balance);
  104.             //gets(a);
  105.         }
  106.  
  107.  
  108.  
  109.         while(1)
  110.         {
  111.  
  112.             fflush(stdout);
  113.             printf("\nPlease select an option\n");
  114.             printf("Choose 1 to deposit money.\n");
  115.             printf("Choose 2 to withdraw money.\n");
  116.             printf("Choose 3 to sort all customers by using their salary.\n");
  117.             printf("Choose 4 to search customers by using their mobileNo.\n");
  118.             printf("Choose 5 to print all the customers.\n");
  119.             printf("Choose 6 to print the sum of all customer balances.\n");
  120.             printf("Choose 0 to exit.");
  121.             printf("Enter you option:\n");
  122.  
  123.             int x;
  124.             scanf("%d",&x);
  125.             if(x==0)break;
  126.             else if(x==1)
  127.             {
  128.                 char *acc;
  129.                 double money;
  130.                 printf("enter account no: ");
  131.                 scanf("%s",acc);
  132.                 printf("enter amount to deposit:");
  133.                 scanf("%lf",&money);
  134.                 int y=findAcc(c,acc,n);
  135.                 (c+y)->balance=(c+y)->balance+money;
  136.                 //printf("%lf",(c+y)->balance);
  137.             }
  138.             else if(x==2)
  139.             {
  140.                 char acc2[10];
  141.                 double withdrawmoney;
  142.                 printf("enter account no: ");
  143.                 scanf("%s",acc2);
  144.                 printf("enter amount to withdraw:");
  145.                 scanf("%lf",&withdrawmoney);
  146.                 int y=findAcc(c,acc2,n);
  147.                 (c+y)->balance=(c+y)->balance-withdrawmoney;
  148.                 //printf("%lf",(c+y)->balance);
  149.  
  150.  
  151.             }
  152.             else if(x==3)
  153.             {
  154.                 sortcustomer(n,c);
  155.                 printcustomer(c,n);
  156.             }
  157.             else if(x==4)
  158.             {
  159.                 char mobile[12];
  160.                 scanf("%s",mobile);
  161.                 int y=findmobile(c,mobile,n);
  162.                 if(y==n)printf("customer not found");
  163.                 else
  164.                     printf("customer name %s",(c+y)->name);
  165.             }
  166.             else if(x==5)
  167.             {
  168.                 printcustomer(c,n);
  169.             }
  170.             else if(x==6)
  171.             {
  172.                 printf("sum of balance of customers: %lf",balanceSum(c,n,0));
  173.             }
  174.             else{
  175.                 printf("invalid input");
  176.             }
  177.         }
  178.  
  179.         free(c);
  180.  
  181.     }
  182.     return 0;
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement