Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct customer
- {
- char name[100];
- char accNo[10];
- char mobileNo[11];
- double balance;
- };
- int findAcc(struct customer *c,char *acc,int n)
- {
- for(int i=0; i<n; i++)
- {
- if(strcmp((c+i)->accNo,acc)==0)
- return i;
- }
- }
- int findmobile(struct customer *c,char *acc,int n)
- {
- for(int i=0; i<n; i++)
- {
- if(strcmp((c+i)->mobileNo,acc)==0)
- return i;
- }
- return n;
- }
- void printcustomer(struct customer *c,int n)
- {
- for(int i=0; i<n; i++)
- {
- printf("Name: %s ",(c+i)->name);
- printf("Account No: <%s> ",(c+i)->accNo);
- printf("Mobile No: %s ",(c+i)->mobileNo);
- printf("Balance: %lf\n",(c+i)->balance);
- }
- }
- double balanceSum(struct customer *c,int n,double sum)
- {
- for(int i=0; i<n; i++)
- {
- sum+=(c+i)->balance;
- }
- return sum;
- }
- void sortcustomer(int n,struct customer *c)
- {
- printf("Sorted successfully\n");
- for(int i=0;i<n-1;i++)
- {
- for(int j=0;j<n-1-i;j++)
- {
- if((c+i)->balance< (c+i+1)->balance){
- float temp=(c+i)->balance;
- (c+i)->balance=(c+i+1)->balance;
- (c+i+1)->balance=temp;
- char *p=(c+i)->name;
- strcpy((c+i)->name,(c+i+1)->name);
- strcpy((c+i+1)->name,p);
- char *q=(c+i)->mobileNo;
- strcpy((c+i)->mobileNo,(c+i+1)->mobileNo);
- strcpy((c+i+1)->mobileNo,p);
- char *r=(c+i)->accNo;
- strcpy((c+i)->accNo,(c+i+1)->accNo);
- strcpy((c+i+1)->accNo,p);
- }
- }
- }
- return;
- }
- int main()
- {
- int n;
- printf("enter number of customers:");
- scanf("%d",&n);
- //char a[2];
- if(n<0 || n>100)printf("invalid input");
- else
- {
- struct customer *c=(struct customer*)malloc(n*sizeof(struct customer));
- for(int i=0; i<n; i++)
- {
- printf("enter customer name:");
- scanf("%s",(c+i)->name);
- printf("enter account no:");
- scanf("%s",(c+i)->accNo);
- printf("enter mobile no:");
- scanf("%s",(c+i)->mobileNo);
- fflush(stdout);
- printf("enter balance: ");
- scanf("%lf",&(c+i)->balance);
- //gets(a);
- }
- while(1)
- {
- fflush(stdout);
- printf("\nPlease select an option\n");
- printf("Choose 1 to deposit money.\n");
- printf("Choose 2 to withdraw money.\n");
- printf("Choose 3 to sort all customers by using their salary.\n");
- printf("Choose 4 to search customers by using their mobileNo.\n");
- printf("Choose 5 to print all the customers.\n");
- printf("Choose 6 to print the sum of all customer balances.\n");
- printf("Choose 0 to exit.");
- printf("Enter you option:\n");
- int x;
- scanf("%d",&x);
- if(x==0)break;
- else if(x==1)
- {
- char *acc;
- double money;
- printf("enter account no: ");
- scanf("%s",acc);
- printf("enter amount to deposit:");
- scanf("%lf",&money);
- int y=findAcc(c,acc,n);
- (c+y)->balance=(c+y)->balance+money;
- //printf("%lf",(c+y)->balance);
- }
- else if(x==2)
- {
- char acc2[10];
- double withdrawmoney;
- printf("enter account no: ");
- scanf("%s",acc2);
- printf("enter amount to withdraw:");
- scanf("%lf",&withdrawmoney);
- int y=findAcc(c,acc2,n);
- (c+y)->balance=(c+y)->balance-withdrawmoney;
- //printf("%lf",(c+y)->balance);
- }
- else if(x==3)
- {
- sortcustomer(n,c);
- printcustomer(c,n);
- }
- else if(x==4)
- {
- char mobile[12];
- scanf("%s",mobile);
- int y=findmobile(c,mobile,n);
- if(y==n)printf("customer not found");
- else
- printf("customer name %s",(c+y)->name);
- }
- else if(x==5)
- {
- printcustomer(c,n);
- }
- else if(x==6)
- {
- printf("sum of balance of customers: %lf",balanceSum(c,n,0));
- }
- else{
- printf("invalid input");
- }
- }
- free(c);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement