Compare commits
No commits in common. "79cd4c0a7fe8e00cd87bea6053bc8abbfe52bc8e" and "23fcea17c2eb380b6a6b1ba848d5bc7ebdc38b05" have entirely different histories.
79cd4c0a7f
...
23fcea17c2
|
@ -1,60 +1,26 @@
|
||||||
#include <lua.hpp>
|
#include <lua.hpp>
|
||||||
#include <libqalculate/qalculate.h>
|
#include <libqalculate/qalculate.h>
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
EvaluationOptions user_evaluation_options = default_evaluation_options;
|
EvaluationOptions user_evaluation_options = default_evaluation_options;
|
||||||
PrintOptions user_print_options = default_print_options;
|
|
||||||
|
|
||||||
struct Output_struct {
|
const char* qalculate(const char* input, bool exact_mode) {
|
||||||
char * result;
|
|
||||||
std::vector<char *> messages;
|
|
||||||
};
|
|
||||||
|
|
||||||
Output_struct qalculate(const char* input, bool exact_mode, bool interval_mode, bool structuring_mode) {
|
|
||||||
// Create and prepare calculator
|
|
||||||
Calculator* c = new Calculator();
|
Calculator* c = new Calculator();
|
||||||
c->loadExchangeRates();
|
c->loadExchangeRates();
|
||||||
c->loadGlobalDefinitions();
|
c->loadGlobalDefinitions();
|
||||||
c->loadLocalDefinitions();
|
c->loadLocalDefinitions();
|
||||||
// Create input and output string buffers
|
|
||||||
std::string Input = input;
|
std::string Input = input;
|
||||||
std::string Output;
|
std::string Output;
|
||||||
// Check evaluation options
|
if (exact_mode) {
|
||||||
user_evaluation_options.structuring = (structuring_mode ?
|
user_evaluation_options.approximation = APPROXIMATION_EXACT;
|
||||||
STRUCTURING_FACTORIZE :
|
} else {
|
||||||
STRUCTURING_EXPAND);
|
user_evaluation_options.approximation = APPROXIMATION_APPROXIMATE;
|
||||||
user_evaluation_options.interval_calculation = (interval_mode ?
|
|
||||||
INTERVAL_CALCULATION_VARIANCE_FORMULA :
|
|
||||||
INTERVAL_CALCULATION_NONE);
|
|
||||||
user_evaluation_options.approximation = (exact_mode ?
|
|
||||||
APPROXIMATION_EXACT :
|
|
||||||
APPROXIMATION_APPROXIMATE);
|
|
||||||
user_print_options.number_fraction_format = (exact_mode ?
|
|
||||||
FRACTION_DECIMAL_EXACT :
|
|
||||||
FRACTION_DECIMAL);
|
|
||||||
user_print_options.interval_display = (interval_mode ?
|
|
||||||
INTERVAL_DISPLAY_INTERVAL :
|
|
||||||
INTERVAL_DISPLAY_MIDPOINT);
|
|
||||||
// Prepare output struct
|
|
||||||
struct Output_struct output;
|
|
||||||
memset(&output,0,sizeof(Output_struct));
|
|
||||||
// Evaluate and record result in Output
|
|
||||||
Output = c->calculateAndPrint(Input,2000,user_evaluation_options,user_print_options);
|
|
||||||
// record messages
|
|
||||||
std::string current_msg;
|
|
||||||
while (c->message() != NULL) {
|
|
||||||
current_msg = c->message()->message();
|
|
||||||
output.messages.push_back(new char[current_msg.size()+1]);
|
|
||||||
current_msg.copy(output.messages.back(),current_msg.size());
|
|
||||||
output.messages.back()[current_msg.size()] = "\0"[0];
|
|
||||||
c->nextMessage();
|
|
||||||
}
|
}
|
||||||
|
Output = c->calculateAndPrint(Input,2000,user_evaluation_options);
|
||||||
delete c;
|
delete c;
|
||||||
// Copy result to the output structure
|
char * output = new char [Output.length()+1];
|
||||||
output.result = new char [Output.length()+1];
|
Output.copy(output,Output.length());
|
||||||
Output.copy(output.result,Output.length());
|
output[Output.length()] = "\0"[0];
|
||||||
output.result[Output.length()] = "\0"[0];
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,16 +28,9 @@ extern "C"
|
||||||
int l_qalc(lua_State* L) {
|
int l_qalc(lua_State* L) {
|
||||||
const char * input = luaL_checkstring(L, 1);
|
const char * input = luaL_checkstring(L, 1);
|
||||||
bool exact_mode = lua_toboolean(L, 2);
|
bool exact_mode = lua_toboolean(L, 2);
|
||||||
bool interval_mode = lua_toboolean(L, 3);
|
const char * output = qalculate(input, exact_mode);
|
||||||
bool struct_mode = lua_toboolean(L, 4);
|
lua_pushstring(L, output);
|
||||||
Output_struct output = qalculate(input, exact_mode, interval_mode, struct_mode);
|
return 1;
|
||||||
lua_pushstring(L, output.result);
|
|
||||||
lua_newtable(L);
|
|
||||||
for (int i = 0; i < output.messages.size(); i++) {
|
|
||||||
lua_pushstring(L, output.messages[i]);
|
|
||||||
lua_rawseti(L, -2, i+1);
|
|
||||||
}
|
|
||||||
return 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
|
|
12
test.lua
12
test.lua
|
@ -1,11 +1,3 @@
|
||||||
qalculate = require("libqalculator")
|
qalculate = require("libqalculator")
|
||||||
local result,messages = qalculate.qalc("sin(3 rad)",true,false,true)
|
print(qalculate.qalc("sin(3 rad)"))
|
||||||
print(result,table.concat(messages))
|
print(qalculate.qalc("sin(3 rad)",true))
|
||||||
local result,messages = qalculate.qalc("4/3",true)
|
|
||||||
print(result,table.concat(messages))
|
|
||||||
local result,messages = qalculate.qalc("4/3",false)
|
|
||||||
print(result,table.concat(messages))
|
|
||||||
local result,messages = qalculate.qalc("fibonacci()")
|
|
||||||
print(result,table.concat(messages))
|
|
||||||
local result,messages = qalculate.qalc("sin(3 rad)",true)
|
|
||||||
print(result,table.concat(messages))
|
|
||||||
|
|
Loading…
Reference in New Issue