Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab9 #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Lab9 #60

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
calc
Binary file removed calc
Binary file not shown.
45 changes: 30 additions & 15 deletions calc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,46 @@ int main(){
FILE *fp = NULL;
int operand1, operand2;
char operator = ' ';
int result, line = 0;
float result, line = 0;

fp = fopen("read.txt","r");
if(fp!=NULL){
fscanf(fp, "%d", &line);
fscanf(fp, "%f", &line);

for(int i=0; i<line; i++) {
fscanf(fp, "%d %c %d",&operand1, &operator, &operand2);
switch(operator) {
case '+':
result = add(operand1, operator);
fscanf(fp, "%d %c %d",&operand1, &operator, &operand2);
float (*cal)(float, float);
if (operator == '+')
cal = add;
else if (operator == '-')
cal = minus;
else if (operator == '*')
cal = mul;
else if (operator == '/')
cal = div;

result = cal(operand1, operand2);
printf("%d %c %d = %f\n", operand1, operator, operand2, result);
}
/*case '+':
result = add(operand1, operand2);
break;
case '-':
result = minus(operand1, operator);
result = minus(operand1, operand2);
break;
case '*':
result = mul(operand1, operator);
case '/':
result = div(operand1, operator);
result = mul(operand1, operand2);
break;
}
printf("%d %c %d = %d\n",
operand1, operator, operand2, result);
}
}
case '/':
result = div(operand1, operand2);
break; */

/* printf("%d %c %d = %f\n",
operand1, operator, operand2, result); */

return 0;
}

}


Binary file removed calc.o
Binary file not shown.
10 changes: 5 additions & 5 deletions operators.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "operators.h"

int add(int op1, int op2) {
float add(float op1, float op2) {
return op1+op2;
}
int minus(int op1, int op2) {
float minus(float op1, float op2) {
return op1-op2;
}
int mul(int op1, int op2) {
float mul(float op1, float op2) {
return op1*op2;
}

int div(int op1, int op2) {
return op1%op2;
float div(float op1, float op2) {
return op1/op2;
}

8 changes: 4 additions & 4 deletions operators.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int add(int op1, int op2);
int mul(int op1, int op2);
int div(int op1, int op2);
int minus(int op1, int op2);
float add(float op1, float op2);
float mul(float op1, float op2);
float div(float op1, float op2);
float minus(float op1, float op2);
Binary file removed operators.o
Binary file not shown.