Hi once again.
As the title implies, the goal of the following code is to create calculator for operations +,-,*,/ and ^. However, the operations are to be executed on values in the files I arbitrarily named a.txt and b.txt.
a.txt contains following data:
a1 era1
a2 era2
.
.
.
an eran
and b.txt:
b1 erb1
b2 erb2
.
.
.
bn erbn
era and erb are belonging errors in measurement for a and b (in case you are wondering)
The result should be in the standard output and look like:
a1(operation)b1 resultingerror1(whose value depends on operation, but nevermind)
.
.
.
an(-||-)bn resultingerrorn(-||-)
If you don't get it from the description just create two .txt files, enter some values and run the program in terminal like this
gcc name.c -lm -ocalc
./calc a.txt + b.txt
And so... The following code works perfectly for + and -, but for some reason in case of other operations the program prints absolutely nothing.
Why is that? How would I correct it?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main(int argc, char** argv)
{
FILE* fa = fopen(argv[1],"r"); if(!fa)exit(0);
FILE* fb = fopen(argv[3],"r"); if(!fb)exit(0);
double a,b,pa,pb,rez,prez;
if( argv[2][0] == '+' ){
while((fscanf(fa,"%lf %lf\n", &a, &pa))>0){
fscanf(fb,"%lf %lf\n", &b, &pb);
rez=a+b;
prez=sqrt(pow(pa,2) + pow(pb,2));
printf("%8.2lf %8.2lf\n",rez,prez);}
}
else if( argv[2][0] == '-' ){
while((fscanf(fa,"%lf %lf\n", &a, &pa))>0){
fscanf(fb,"%lf %lf\n", &b, &pb);
rez=a-b;
prez=sqrt(pow(pa,2) + pow(pb,2));
printf("%8.2lf %8.2lf\n",rez,prez);}
}
else if( argv[2][0] == '*' ){
while((fscanf(fa,"%lf %lf\n", &a, &pa))>0){
fscanf(fb,"%lf %lf\n", &b, &pb);
rez=a*b;
prez=sqrt(pow(a*pb,2) + pow(pa*b,2));
printf("%8.2lf %8.2lf\n",rez,prez);}
}
else if( argv[2][0] == '/' ){
while((fscanf(fa,"%lf %lf\n", &a, &pa))>0){
fscanf(fb,"%lf %lf\n", &b, &pb);
rez=a/b;
prez=sqrt(pow(((a*pb)/(b*b)),2) + pow((pa/b),2));
printf("%8.2lf %8.2lf\n",rez,prez);}
}
else if( argv[2][0] == '^' ){
while((fscanf(fa,"%lf %lf\n", &a, &pa))>0){
fscanf(fb,"%lf %lf\n", &b, &pb);
rez=pow(a,b);
prez=sqrt(pow((b*(pow(a,(b-1)))*pa),2) + pow(((pow(a,b))*(log(a))*pb),2));
printf("%8.2lf %8.2lf\n",rez,prez);}
}
else
exit(0);
fclose(fa);
fclose(fb);
return 0;
}