代码:
int main()
{
srand((size_t)time(NULL));
FILE * fp = fopen("/home/michael/test_malloc_pp/count.txt","w");
enum ch
{
add,sub,mlt,dive
};
if(!fp) {
printf("create file fail\n");
return -1;
}
for(int i=0; i<10; i++)
{
int a = rand()%100;
int b = rand()%100;
int c = rand()%3;
switch (c)
{
case add:
fprintf(fp,"%d%c%d=\n",a,'+',b);
break;
case sub:
fprintf(fp,"%d%c%d=\n",a,'-',b);
break;
case mlt:
fprintf(fp,"%d%c%d=\n",a,'*',b);
break;
case dive:
fprintf(fp,"%d%c%d=\n",a,'/',b);
break;
default:
break;
}
}
fclose(fp);
FILE * fp2 = fopen("/home/michael/test_malloc_pp/count.txt","r");
FILE * fp1 = fopen("/home/michael/test_malloc_pp/count_sum.txt","w");
if(!fp1 || !fp2)
{
printf("create file fail\n");
return -1;
}
for (int i = 0; i < 10; i++)
{
int a, b;
char c;
fscanf(fp2,"%d%c%d=\n",&a,&c,&b);
switch (c)
{
case '+':
fprintf(fp1,"%d%c%d=%d\n",a,'+',b,a+b);
break;
case '-':
fprintf(fp1,"%d%c%d=%d\n",a,'-',b,a-b);
break;
case '*':
fprintf(fp1,"%d%c%d=%d\n",a,'*',b,a*b);
break;
case '/':
fprintf(fp1,"%d%c%d=%d\n",a,'/',b,a/b);
break;
default:
break;
}
}
fclose(fp2);
fclose(fp1);
}