Madmouse

the beginnings to a prefix notation calculator

Oct 18th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.26 KB | None | 0 0
  1. #!/usr/bin/env rdmd
  2.  
  3. import std.stdio;
  4. import std.conv;
  5. import std.c.stdlib;
  6. import std.string;
  7. import std.array;
  8.  
  9. void main(string[] args)
  10. {
  11.     void usage(){writeln("Example: ^ 2 (+ 1 (+ 2 (* 2 2)))");}
  12.    
  13.     while(true)
  14.     {
  15.         stdout.write("> "); // print prompt
  16.         string input = chop(stdin.readln());    // chop the new line character
  17.        
  18.         string[] temp = input.split(" ");
  19.         real answer=0;
  20.         bool parsing_error = false;
  21.         while(temp.length >= 3 && !parsing_error)
  22.         {
  23.             auto oper = temp[$-3].removechars("()");
  24.             //stdout.writeln("chunk: ", oper);
  25.            
  26.             real a,b;
  27.             auto bite = temp[$-2].removechars("()");
  28.             if((bite[0]<='9'&&bite[0]>='0')||((bite[0]=='-'||bite[0]=='.')&&bite[0]<='9'&&bite[1]>'0'))
  29.                 a = to!real(bite);
  30.             else
  31.             {
  32.                 parsing_error = true;
  33.                 break;
  34.             }
  35.             //stdout.writeln("chunk: ", bite);
  36.            
  37.             bite = temp[$-1].removechars("()");
  38.             if((bite[0]<='9'&&bite[0]>='0')||((bite[0]=='-'||bite[0]=='.')&&bite[0]<='9'&&bite[1]>'0'))
  39.                 b = to!real(bite);
  40.             else
  41.             {
  42.                 parsing_error = true;
  43.                 break;
  44.             }
  45.             //stdout.writeln("chunk: ", bite);
  46.            
  47.             switch(oper)
  48.             {
  49.                 case "+":
  50.                     answer = a+b;
  51.                     stdout.writeln("(+ ", a, " ", b, ") = ", answer);
  52.                     break;
  53.                 case "-":
  54.                     answer = a-b;
  55.                     stdout.writeln("(- ", a, " ", b, ") = ", answer);
  56.                     break;
  57.                 case "*":
  58.                     answer = a*b;
  59.                     stdout.writeln("(* ", a, " ", b, ") = ", answer);
  60.                     break;
  61.                 case "/":
  62.                     answer = a && b ? a/b : 0;
  63.                     a && b ? stdout.writeln("(/ ", a, " ", b, ") = ", answer) : writeln("Cannot divide by zero...");
  64.                     break;
  65.                 case "^":
  66.                     answer = a^^b;
  67.                     stdout.writeln("(^ ", a, " ", b, ") = ", answer);
  68.                     break;
  69.                 case "%":
  70.                     answer = a && b ? a%b : 0;
  71.                     a && b ? stdout.writeln("(% ", a, " ", b, ") = ", answer) : writeln("Cannot divide by zero...");
  72.                     break;
  73.                 default:
  74.                     parsing_error = true;
  75.                     break;
  76.             }
  77.             temp.popBack();
  78.             temp.popBack();
  79.             temp.popBack();
  80.             temp ~= join([to!string(answer), ")"]);
  81.             //stdout.writeln(temp);
  82.             if(temp.length == 1)
  83.             {
  84.                 temp.popBack();
  85.                 break;
  86.             }
  87.         }
  88.         //stdout.writeln(temp," -> ",temp.length);
  89.         if(parsing_error || temp.length > 0)
  90.         {
  91.             stdout.writeln("Invalid syntax...");
  92.             usage();
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment