This simple program enable LPC11U24 MBED board to act as a simple integer calculator. Just type the operation you want to execute from the serial terminal. Admitted operations: +, -, * /, ^ (only positive exponent). Multiple operations in raw are supported as well as negative integer.

Dependencies:   mbed

Fork of Serial_HelloWorld_Mbed by mbed official

Revision:
2:0baf535ed8f8
Parent:
1:247161a5c5d6
Child:
3:3bef1c4e42d4
--- a/main.cpp	Mon Sep 22 03:12:09 2014 +0000
+++ b/main.cpp	Mon Sep 22 04:29:44 2014 +0000
@@ -42,7 +42,7 @@
     }
 }
 
- 
+
 
 int main()
 {
@@ -52,12 +52,15 @@
     char buf[10];
     int result =0;
     int index = 0;
+    int ioperand =0;
     bool first = TRUE;
+    bool sign_check = TRUE;
+    bool neg_operand = FALSE;
 
     pc.printf("LPC Integer Calculator!\n");
-    
+
     while(1) {
- 
+
         c=pc.getc();  //get serial input
 
         if (!si_check_valid(c)) { // Invalid Input
@@ -67,38 +70,63 @@
             pc.putc(c);
             operand[index] = c;
             index++;
+            sign_check = FALSE;  //sign check disactivated
         } else { //Operator has been entered
-        
-            pc.putc('\n');
-            pc.putc(c);
-            pc.putc('\n');
+
+            if(!sign_check) {
+                pc.putc('\n');
+                pc.putc(c);
+                pc.putc('\n');
 
-            if (first) {  //First Operator
-                result = char_to_int(operand,index);
-                first = FALSE;
-            } else { //Do Calculation
+                // sign adjustment
+                if(neg_operand) {
+                    ioperand = -(char_to_int(operand,index));
+                } else {
+                    ioperand = char_to_int(operand,index);
+                }
+                neg_operand=FALSE;
+                
+                if (first) {  //First Operator
+                    result = ioperand;
+                    first = FALSE;
+                } else { //Do Calculation
 
-                if(op == '+') {
-                    result = int (result + char_to_int(operand,index));
-                } else if (op == '-') {
-                    result = int (result - char_to_int(operand,index));
-                } else if (op  == '/') {
-                    result = int (result / char_to_int(operand,index));
-                } else if (op == '*') {
-                    result = int (result * char_to_int(operand,index));
+                    if(op == '+') {
+                        result = int (result + ioperand);
+                    } else if (op == '-') {
+                        result = int (result - ioperand);
+                    } else if (op  == '/') {
+                        result = int (result / ioperand);
+                    } else if (op == '*') {
+                        result = int (result * ioperand);
+                    }
+
+                    if (c == '=') { // Operator = has been entered--> Print result and reset calculator
+                        for (int j =9; j>=0; j--) {
+                            buf[j]='\0';
+                        }
+                        sprintf(buf, "%d", result);
+                        pc.printf("%s", buf);
+                        pc.printf("\n\n");
+                        pc.printf("Please enter a new operation\n");
+                        first = TRUE;
+                    }
                 }
-
-                if (c == '=') { // Operator = has been entered--> Print result and reset calculator
-                    for (int j =9; j>=0; j--){buf[j]='\0';}
-                    sprintf(buf, "%d", result);
-                    pc.printf("%s", buf);
-                    pc.printf("\n\n");
-                    pc.printf("Please enter a new operation\n");
-                    first = TRUE;
+                op = c;  // assign the Operator
+                index=0; // reset the operand
+                sign_check = TRUE; // sign check activated
+            } else {
+                pc.putc(c);
+                if (c=='-') {
+                    neg_operand=TRUE;
+                    sign_check = FALSE;
+                } else if (c=='+') {
+                    neg_operand=FALSE;
+                    sign_check = FALSE;
+                } else { 
+                pc.printf("ERROR:Two consecutive Operators entered");               
                 }
             }
-            op = c;  // assign the Operator
-            index=0; // reset the operand
         }
     }
 }