Code:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
struct consumer {
int number;
int n;
};
main () {
int count,i,f1;
count=0;
char c;
float charge,fee;
struct consumer consumer,fileconsumer;
consumer.n=-1;
printf("give me the number of the charge \n");
fflush(stdin);
scanf("%f",&charge);
FILE *fp;
while(1)
{
f1=0;
scanf("%s",&c);
if(c=='0')
break;
printf("give me the number of the calling cellphone ");
scanf("%d",&consumer.number);
fp=fopen("consumer.txt","+ab");
assert(fp==NULL);
for(i=0;i<count;i++)
{
rewind(fp);
fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
fread(&fileconsumer,sizeof(struct consumer),1,fp);
if(consumer.number==fileconsumer.number)
{
fileconsumer.n++;
rewind(fp);
fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
fwrite(&fileconsumer,sizeof(struct consumer),1,fp);
f1=1;
break;
}
}
if(!f1)
{
consumer.n++;
rewind(fp);
fseek(fp,sizeof(struct consumer)*count,SEEK_SET);
fwrite(&consumer,sizeof(struct consumer),1,fp);
fclose(fp);
count++;
}
}
fp=fopen("consumer.txt","+ab");
assert(fp==NULL);
for(i=0;i<count;i++)
{
fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
fread(&fileconsumer,sizeof(struct consumer),1,fp);
if(fileconsumer.n==0)
{
rewind(fp);
fseek(fp,sizeof(struct consumer)*count,SEEK_SET);
fread(&fileconsumer,sizeof(struct consumer),1,fp);
rewind(fp);
fseek(fp,sizeof(struct consumer)*(count-i),SEEK_END);
fwrite(&fileconsumer,sizeof(struct consumer),1,fp);
count--;
}
}
for(i=0;i<count;i++)
{
rewind(fp);
fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
fread(&fileconsumer,sizeof(struct consumer)*i,1,fp);
if(fileconsumer.n>=10)
{
fee=charge*fileconsumer.n;
fee=charge*fileconsumer.n-charge*fileconsumer.n/10;
printf("the charge for the number %d it is %f \n",fileconsumer.number,fee);
}
else
{
fee=charge*fileconsumer.n;
printf("the charge for the number %d it is %f \n ",fileconsumer.number,fee);
}
fileconsumer.n=0;
fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
fwrite(&fileconsumer,sizeof(struct consumer)*i,1,fp);
}
fclose(fp);
}