Added more tests, more parameters, fixed interval handling

This commit is contained in:
Yessiest 2022-05-27 23:44:10 +04:00
parent 26aef364d1
commit 79cd4c0a7f
2 changed files with 21 additions and 6 deletions

View File

@ -4,13 +4,14 @@
#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 { struct Output_struct {
char * result; char * result;
std::vector<char *> messages; std::vector<char *> messages;
}; };
Output_struct qalculate(const char* input, bool exact_mode, bool interval_mode) { Output_struct qalculate(const char* input, bool exact_mode, bool interval_mode, bool structuring_mode) {
// Create and prepare calculator // Create and prepare calculator
Calculator* c = new Calculator(); Calculator* c = new Calculator();
c->loadExchangeRates(); c->loadExchangeRates();
@ -20,17 +21,26 @@ Output_struct qalculate(const char* input, bool exact_mode, bool interval_mode)
std::string Input = input; std::string Input = input;
std::string Output; std::string Output;
// Check evaluation options // Check evaluation options
user_evaluation_options.structuring = (structuring_mode ?
STRUCTURING_FACTORIZE :
STRUCTURING_EXPAND);
user_evaluation_options.interval_calculation = (interval_mode ? user_evaluation_options.interval_calculation = (interval_mode ?
INTERVAL_CALCULATION_NONE : INTERVAL_CALCULATION_VARIANCE_FORMULA :
INTERVAL_CALCULATION_VARIANCE_FORMULA); INTERVAL_CALCULATION_NONE);
user_evaluation_options.approximation = (exact_mode ? user_evaluation_options.approximation = (exact_mode ?
APPROXIMATION_EXACT : APPROXIMATION_EXACT :
APPROXIMATION_APPROXIMATE); 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 // Prepare output struct
struct Output_struct output; struct Output_struct output;
memset(&output,0,sizeof(Output_struct)); memset(&output,0,sizeof(Output_struct));
// Evaluate and record result in Output // Evaluate and record result in Output
Output = c->calculateAndPrint(Input,2000,user_evaluation_options); Output = c->calculateAndPrint(Input,2000,user_evaluation_options,user_print_options);
// record messages // record messages
std::string current_msg; std::string current_msg;
while (c->message() != NULL) { while (c->message() != NULL) {
@ -53,7 +63,8 @@ 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); bool interval_mode = lua_toboolean(L, 3);
Output_struct output = qalculate(input, exact_mode, !interval_mode); bool struct_mode = lua_toboolean(L, 4);
Output_struct output = qalculate(input, exact_mode, interval_mode, struct_mode);
lua_pushstring(L, output.result); lua_pushstring(L, output.result);
lua_newtable(L); lua_newtable(L);
for (int i = 0; i < output.messages.size(); i++) { for (int i = 0; i < output.messages.size(); i++) {

View File

@ -1,5 +1,9 @@
qalculate = require("libqalculator") qalculate = require("libqalculator")
local result,messages = qalculate.qalc("sin(3 rad)") local result,messages = qalculate.qalc("sin(3 rad)",true,false,true)
print(result,table.concat(messages))
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)) print(result,table.concat(messages))
local result,messages = qalculate.qalc("fibonacci()") local result,messages = qalculate.qalc("fibonacci()")
print(result,table.concat(messages)) print(result,table.concat(messages))