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;
}

So, there's no one to help?

Your code has no formatting so it's hard to follow.
Your explanation makes no sense. All that "data" may be understandable to you, but it means nothing to us. I have no idea what

a1(operation)b1 resultingerror1(whose value depends on operation, but nevermind)
.
.
.
an(-||-)bn resultingerrorn(-||-)

even means.

Ok,

you have a.txt and b.txt which both contain two collumns: the first column is the value of a parameter.

the goal would be to use the values inside the files and add, subtract, multiply them and etc.

for example:
a.txt:
2 0.1
3 0.1
4 0.1

b.txt:
1 0.1
1 0.1
1 0.1

and in case a.txt + b.txt the result in output should be:

3 0.14
4 0.14
5 0.14

the first column is simply adding the values while the second is calculated from an expression derived from calculus, but really, the numerical values are not the issue here. So don't worry about the second column - it's not meant to be 0.2 as you can see in the code.

I apologize for the first code, I forgot to change the names of the variables into something english-like.

Just go through the first if to understand what the program is about. Else ifs are just a variation.
However, while I get good results for + and - I don't get anything printed out in case of other operations.

Why am I not getting any output for */^ ?

#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,errora,errorb,res,reserror;                              
 
if( argv[2][0] == '+' ){
while((fscanf(fa,"%lf %lf\n", &a, &errora))>0){
fscanf(fb,"%lf %lf\n", &b, &errorb);
res=a+b;
reserror=sqrt(pow(errora,2) + pow(errorb,2));
printf("%8.2lf  %8.2lf\n",res,reserror);}
}
 
else if( argv[2][0] == '-' ){
while((fscanf(fa,"%lf %lf\n", &a, &errora))>0){
fscanf(fb,"%lf %lf\n", &b, &errorb);
res=a-b;
prez=sqrt(pow(errora,2) + pow(errorb,2));
printf("%8.2lf  %8.2lf\n",res,reserror);}
}
 
else if( argv[2][0] == '*' ){
while((fscanf(fa,"%lf %lf\n", &a, &errora))>0){
fscanf(fb,"%lf %lf\n", &b, &errorb);
res=a*b;
reserror=sqrt(pow(a*errorb,2) + pow(errora*b,2));
printf("%8.2lf  %8.2lf\n",res,reserror);}
}
 
else if( argv[2][0] == '/' ){
while((fscanf(fa,"%lf %lf\n", &a, &errora))>0){
fscanf(fb,"%lf %lf\n", &b, &errorb);
res=a/b;
reserror=sqrt(pow(((a*errorb)/(b*b)),2) + pow((errora/b),2));
printf("%8.2lf  %8.2lf\n",res,reserror);}
}
 
else if( argv[2][0] == '^' ){
while((fscanf(fa,"%lf %lf\n", &a, &errora))>0){
fscanf(fb,"%lf %lf\n", &b, &errorb);
res=pow(a,b);
reserror=sqrt(pow((b*(pow(a,(b-1)))*errora),2) + pow(((pow(a,b))*(log(a))*errorb),2));
printf("%8.2lf  %8.2lf\n",rez,reserror);}
}
else
exit(0);
fclose(fa);
fclose(fb);
return 0;
}

Your code still has no formatting so it's still hard to follow.
If you really want us to "Just go through the first if to understand what the program is about" I suggest readable code. See this. I also suggest since you don't wish to explain your code to us, comment it heavily. Reading someone else's code is not a task we take lightly. It's work.

For the love of god, how difficult can it be? Can't you just run the program instead of complaining about formatting? I wrote that you'll need 2 .txt files and i gave an example of their content. I also wrote how to run the program. And as far as I can see the formatting is not bad at all. You have 5 cases all of which are separated by an empty line and inside those 5 blocks you have one command per line. I really don't see what the issue is. Unless you don't want to help and in that case, I don't understand the point of you posting in this thread.

For the love of god, why can't you just format the friggin' code properly? It's not that hard! And why are you the only one that requires us to create data files, create projects, and run code just to understand what you should be able to explain in 3 lines of text?

We've got 900,000 other members that want our help and they follow the Member Rules when asked. What's so special about you? Why are you above the Rules?

You're right. I will stop posting in this thread. And since I'm the only one that has, good luck...

First of all, I don't even know what do you mean by "format the code properly". I used tabs, enters and spaces as much as I can see. What is so improper about it? I could have answered if you had asked a question about the code which is precisely what you didn't do. I don't think I'm above rules but I'm not really aware of the fact that I have broken any. I asked you to create 2 .txt files of 6 numbers, which isn't exactly the twelve tasks of Hercules, because that is what I am supposed to do. And by the way, don't you think that your answers were a bit too generic, effortless and arrogant?

First of all, I don't even know what do you mean by "format the code properly". I used tabs, enters and spaces as much as I can see. What is so improper about it? I could have answered if you had asked a question about the code which is precisely what you didn't do. I don't think I'm above rules but I'm not really aware of the fact that I have broken any. I asked you to create 2 .txt files of 6 numbers, which isn't exactly the twelve tasks of Hercules, because that is what I am supposed to do. And by the way, don't you think that your answers were a bit too generic, effortless and arrogant?

If you checked your code that you posted(meaning as seen in this forum) you can see that there's not much tabs, enters and spaces as you stated and it's better to use comments on every line if your code is like that

Anyway..while "trying" to understand your code(just a single glance) I found this

printf("%8.2lf %8.2lf\n",rez,reserror);}

in line 47 rez is not defined... shoudn't it be res?

P.S. Like WaitP stated we don't have the time to make the files required to run this program... this is as much help as you can get if can't narrow down the problem.. simply tracing another persons code is very,very much time consuming while the only reward we can get from it is your satisfaction.. Not much motivation is there?

Edited 4 Years Ago by zeroliken: n/a

This article has been dead for over six months. Start a new discussion instead.