00001
00007 #include "at_startup.H"
00008
00009
00010 #ifdef IS_CLIENT
00011 #error "ieval is not a client!"
00012 #endif
00013
00014 #ifdef IS_SERVER
00015 #error "ieval is not a server!"
00016 #endif
00017
00018 #ifdef USE_NETWORK
00019 #error "ieval uses no network features!"
00020 #endif
00021
00022 #ifndef IS_STANDALONE
00023 #error "ieval is a standalone application!"
00024 #endif
00025
00026
00027
00028 #include <cstdio>
00029 #include <cstdlib>
00030 #include <cmath>
00031 #include <gmp.h>
00032
00033
00034 #ifdef NOTIFY_PARENT
00035 #warning "undefining NOTIFY_PARENT, since this is not a networked server"
00036 #undef NOTIFY_PARENT
00037 #endif
00038
00039
00040 #undef VERBOSE_WARN
00041 #undef VERBOSE_INFO
00042 #undef VERBOSE_NOTICE
00043 #include "parse_term.cc"
00044
00045 int main(const int argc, const char* const argv[])
00046 {
00047 bool showhelp = (argc!=2);
00048
00049 if (argc==2)
00050 {
00051 const string s = argv[1];
00052 showhelp = (s=="-h" || s=="--help");
00053 }
00054
00055 if (showhelp)
00056 {
00057 PrintHeader("ieval expression evaluator");
00058 cout << "ieval is a small tool to evaluate integer expressions." << endl
00059 << "Terms consisting of 0123456789 as decimal digits," << endl
00060 << " +,-,*,^,/,% and :,** as operators, () for parenthesis," << endl
00061 << "and the function symbols" << endl
00062 << " F [Fibonacci number], L [Lucas number], P [partition number]," << endl
00063 << " f [factorial], i [increment], d [decrement]," << endl
00064 << " p [next prime number (if not already prime)]," << endl
00065 << " r [(truncated) square root and" << endl
00066 << " s [next safeprime (if not already safeprime)]" << endl
00067 << "are allowed. The result is printed to stdout." << endl
00068 << "Examples: \"i10*d10\", \"P100\", \"F9*L9\", \"3^3^3\"" << endl;
00069 if (argc<3) return 0; else return 1;
00070 }
00071
00072 mpz_t n;
00073 mpz_init(n);
00074
00075
00076 char* const neuer_str = strdup(argv[1]);
00077 char* str = neuer_str;
00078 if (!parse_term::get_number(n, str))
00079 {
00080 cerr << "integer term for evaluation expected!" << endl;
00081 cerr << "Wrong input at: '" << str << "'" << endl;
00082 exit(1);
00083 }
00084 else
00085 if (str[0]!='\0')
00086 {
00087 cerr << "integer term for evaluation expected!" << endl;
00088 cerr << "Syntax error in input term. (parenthesis?)" << endl;
00089 exit(1);
00090 }
00091
00092
00093
00094 mpz_out_str(NULL,10,n);
00095 cout << endl;
00096
00097 free(neuer_str);
00098 mpz_clear(n);
00099
00100 return 0;
00101 }