78 lines
1.6 KiB
C
78 lines
1.6 KiB
C
#include "calcop.h"
|
|
|
|
#include "num.h"
|
|
#include "pile.h"
|
|
|
|
int addi(int i, int j) {
|
|
return i + j;
|
|
}
|
|
float addf(float i, float j) {
|
|
return i + j;
|
|
}
|
|
int subi(int i, int j) {
|
|
return i - j;
|
|
}
|
|
float subf(float i, float j) {
|
|
return i - j;
|
|
}
|
|
int multi(int i, int j) {
|
|
return i * j;
|
|
}
|
|
float multf(float i, float j) {
|
|
return i * j;
|
|
}
|
|
int divi(int i, int j) {
|
|
return i/j;
|
|
}
|
|
float divf(float i, float j) {
|
|
return i/j;
|
|
}
|
|
|
|
void calcop( int (*opint)(int,int), float (*opfloat)(float,float), struct Pile* pile ) {
|
|
struct NumContainer res;
|
|
struct NumContainer num1;
|
|
struct NumContainer num2;
|
|
getlastnums(&num1, &num2, pile);
|
|
|
|
if ( num1.t == INT ) {
|
|
if ( num2.t == INT ) {
|
|
res.t = INT;
|
|
res.n.vali = opint( num1.n.vali, num2.n.vali );
|
|
} else {
|
|
res.t = FLOAT;
|
|
res.n.valf = opfloat((float)num1.n.vali, num2.n.valf);
|
|
}
|
|
} else {
|
|
res.t = FLOAT;
|
|
if ( num2.t == INT ) {
|
|
res.n.valf = opfloat(num1.n.valf, (float)num2.n.vali);
|
|
} else {
|
|
res.n.valf = opfloat(num1.n.valf, num2.n.valf);
|
|
}
|
|
}
|
|
push(pile, res);
|
|
}
|
|
|
|
void add(struct State* state) {
|
|
if (state->mode == EXECUTE) {
|
|
calcop(addi, addf, &(state->pile));
|
|
}
|
|
}
|
|
|
|
void sub(struct State* state) {
|
|
if (state->mode == EXECUTE) {
|
|
calcop(subi, subf, &(state->pile));
|
|
}
|
|
}
|
|
|
|
void mult(struct State* state) {
|
|
if (state->mode == EXECUTE) {
|
|
calcop(multi, multf, &(state->pile));
|
|
}
|
|
}
|
|
|
|
void divide(struct State* state) {
|
|
if (state->mode == EXECUTE) {
|
|
calcop(divi, divf, &(state->pile));
|
|
}
|
|
}
|