📄 qcalc-cli.c
字号:
#include <string.h>#include <stdio.h>#include <stdlib.h>#include <readline/readline.h>#include <readline/history.h>#include <math.h>#include <malloc.h>#include <sys/param.h>#include "calculator.h"voidcalculator_exit(void){ FILE *config; char npath[PATH_MAX]; char *env; env = getenv("HOME"); if (env) strcpy(npath, env); else strcpy(npath, "/tmp"); strcat(npath, "/.kde/share/apps/qcalc/qcalc.conf"); config = fopen (npath, "wt"); if (config) { WriteHistory(config); fclose(config); } exit(0);}intmain (void){ FILE *config; char result[40], exp[10], ausg[40], *text, *thist; double res, man, expo; int iexpo, i, k, j; char npath[PATH_MAX], syspath[PATH_MAX]; char *env; char *line_read = (char*) NULL; env = getenv("HOME"); if (env) strcpy(npath, env); else strcpy(npath, "/tmp"); strcat(npath, "/.kde/share/apps/qcalc"); // create a subdirectory for the configuration file; // there is a error message, if the directory already // exists; this is ignored at the moment strcpy(syspath, "mkdir "); strcat(syspath, npath); system(syspath); strcat(npath, "/qcalc.conf"); config = fopen (npath, "rt"); if (config) { ReadHistory(config); fclose(config); } using_history (); for (i = 0; i < CalculatorCmdHistoryAnz; i++) add_history(CalculatorCmdHistory[i]); while (1) { res = 0; CalculatorError = ERR_NOERROR; line_read = (char*) readline("qcalc > "); text = malloc(strlen(line_read)+1); strcpy(text, line_read); // remember the formula, because "Calculator" destroys the string thist = (char*) malloc(strlen(text)+1); strcpy(thist, text); if (strstr(text, "quit")) calculator_exit(); if (strstr(text, "exit")) calculator_exit(); if (strstr(text, "list fkt")) { printf ("Functions:\n"); for (i = 1; i <= CalculatorFktAnz; i++) { printf (" %s (", CalculatorFkt[i].Name); for (j = 0; j < CalculatorFkt[i].Vars; j++) { printf("%s", CalculatorFkt[i].Var[j].Name); if (j < CalculatorFkt[i].Vars-1) printf (";"); } printf (")\n"); } } else if (strstr(text, "list var")) { printf ("Variables:\n"); for (i = 0; i < CalculatorFkt[0].Vars; i++) printf ("%20s = %e\n", CalculatorFkt[0].Var[i].Name, CalculatorFkt[0].Var[i].Valu); } else { // evaluate the formula res = Calculator(text, &CalculatorFkt[0]); if ((!CalculatorError) && ((CalculatorCmdHistoryAnz == 0) || (strcmp(thist, CalculatorCmdHistory[CalculatorCmdHistoryAnz-1])))) { // if there is no mistake in the formula and the formula is not // already the last element of the history list, put the formual // to the history list add_history (thist); if (CalculatorCmdHistoryAnz == 100) { // the history list is full, remove the oldes entry for (i = 0; i < 99; i++) CalculatorCmdHistory[i] = CalculatorCmdHistory[i+1]; CalculatorCmdHistoryAnz--; } CalculatorCmdHistory[CalculatorCmdHistoryAnz] = thist; CalculatorCmdCurhist = CalculatorCmdHistoryAnz; CalculatorCmdHistoryAnz++; free(text); } else { free(text); free(thist); } if (CalculatorError) { // got an error, display Errormessage printf("Error: %s\n", calculator_errtxt[CalculatorError-1]); } else { // don't display garbage values if (fabs(res) > 1.e-301) expo = log10(fabs(res)); else expo = 0; // "normalize" the exponent for display in engineering format if (expo < -2) expo -= 3; iexpo = (int)(expo/3)*3; // check for floating point errors, which were not signaled by // the variable CalculatorError if ((iexpo > 1000) || (iexpo < -1000)){ printf("Floating Point Error\n"); } else { // calculate the mantissa for the choosen exponent man = res / pow(10, (double)iexpo); sprintf (result, "%14.12f", man); k = -100; j = 0; // insert blankes after group of 3 digits to make display // more readable for (i = 0; (unsigned int)i < strlen(result); i++){ ausg[j++] = result[i]; k++; if (result[i] == '.') k = 0; if (k == 3){ ausg[j++] = ' '; k = 0; } } ausg[j++] = 0; // remove '0' at end of string for (i = strlen(ausg)-1; i > 0; i--) { if ((ausg[i] == '0') || (ausg[i] == ' ')) ausg[i] = 0; else break; } sprintf (exp, "%+04d", iexpo); // display the result printf("\n Result: %s E%s\n\n", ausg, exp); CalculatorCmdCurhist = CalculatorCmdHistoryAnz; } } } }}// Local Variables: ***// mode:c++ ***// End: ***
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -