⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_num.cpp

📁 一个不错
💻 CPP
字号:
/* Copyright is licensed under GNU LGPL.                 by I.J.Wang 2004 Demonstrate use of Wy::_strnum by implementing a simple calculaor Build: 1. typedef NumType to long long or double (default long long)        2. make demo_num*/#include "../src/wy_uty.h"#include "../src/wynum.h"#include "../src/wymath.h"// Pick one type for NumType for the number//typedef double NumType;typedef long long NumType;static const char titmsg[]=   "\nSimple expr calculator, Cntrl-C to exit.\n"   "  Usage> num(+-*/)num  (No blank in the expr. e.g. '243-0')\n"   "  num= number (C number literal)\n\n";static void print_bad_expr(const WyStr& str){ Wy::cout << "Invalid expr: " << str; };static void read_expr_eval(void){ WyRet r; WyStr rbuf; // print prompt '>' and read input into rbuf Wy::cout << ">"; Wy::cin >> rbuf; // It is easier to operate with char*. Get the parameters of rbuf // And we won't use rbuf untill the next call, these fetches are ok // size_t rb_size=rbuf.size(); const char *rb_aptr(rbuf.c_str()); const char *rb_zptr(rb_aptr+rb_size); // check that string in rbuf is exactly the format dddd[+-*/]ddd if((rb_size<4)&&(rb_aptr[rb_size-1]!='\n')) {   print_bad_expr(rbuf);     // not enough chararacters to parse, or   return;                   // not expected string format } NumType lft;        // left side number of expr NumType rit;        // right side number of expr char opch;          // ( +| -| *| /) const char* endptr; // convert and get the first number into lft r=Wy::_strnum(lft,&endptr,WyCSeg(rb_aptr,rb_size),0); if(r!=Wym_EBADMSG) {   print_bad_expr(rbuf);   // no number/expr identified   return; } if((rb_zptr-endptr)<2) {   print_bad_expr(rbuf);   // not enough left to parse   return; } // get the operator into opch opch=*endptr; ++endptr; // convert and get the second number into rit r=Wy::_strnum(rit,&endptr,WyCSeg(endptr,rb_zptr),0); if(r!=Wym_EBADMSG) {   print_bad_expr(rbuf);   return; } if((rb_zptr-endptr!=1)&&(*endptr!='\n')) {   print_bad_expr(rbuf);   return; }  // calulate: res=lft op rit NumType res(lft); switch(opch) {   case '+': res+=rit; break;   case '-': res-=rit; break;   case '*': res*=rit; break;   case '/': res/=rit; break;   // on my machine, divide 0 produces garbage   default:        print_bad_expr(rbuf);        return; }; // print the result in radix 10 Wy::cout << " " << rb_aptr << " = " << res; // if NumType is integer, print it also in radix 8 and 16 if(std::numeric_limits<NumType>::is_integer) {   Wy::cout << " (dec)\n";   WyStr str(rb_size+1,' ');   str+=" = ";   if((r=Wy::_catstr(str,res,8))!=Ok) {     WY_THROW(r);   }   Wy::cout << str << " (oct)\n";   if((r=str.reset(rb_size+1,' '))!=Ok) {     WY_THROW(r);   }   str+=" = ";   if((r=Wy::_catstr(str,res,16))!=Ok) {     WY_THROW(r);   }   Wy::cout << str << " (hex)\n"; } else {   Wy::cout << "\n"; }};int main(int,char**)try { Wy::cout << titmsg; Wy::cout << (std::numeric_limits<NumType>::is_integer?              "Compiled for integer type\n" : "Compiled for float type\n"); for(;;) {   read_expr_eval(); } return(0);}catch(const WyRet& e) { if(e!=Ok) {   Wy::cerr << Wy::wrd(e) << '\n'; } return e->c_repcode();}catch(...) { Wy::cerr << "main() caught(...)\n"; return(-1);};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -