Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Serial_HelloWorld_Mbed by
main.cpp@2:0baf535ed8f8, 2014-09-22 (annotated)
- Committer:
- andrelongo85
- Date:
- Mon Sep 22 04:29:44 2014 +0000
- Revision:
- 2:0baf535ed8f8
- Parent:
- 1:247161a5c5d6
- Child:
- 3:3bef1c4e42d4
Added Negative Number Support
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 0:879aa9d0247b | 1 | #include "mbed.h" |
| andrelongo85 | 1:247161a5c5d6 | 2 | |
| andrelongo85 | 1:247161a5c5d6 | 3 | #define FALSE 0 |
| andrelongo85 | 1:247161a5c5d6 | 4 | #define TRUE 1 |
| andrelongo85 | 1:247161a5c5d6 | 5 | |
| mbed_official | 0:879aa9d0247b | 6 | Serial pc(USBTX, USBRX); // tx, rx |
| andrelongo85 | 1:247161a5c5d6 | 7 | |
| andrelongo85 | 1:247161a5c5d6 | 8 | // function to convert a char buffer to decimal inetger |
| andrelongo85 | 1:247161a5c5d6 | 9 | int char_to_int (const char *char_p, const int size) |
| andrelongo85 | 1:247161a5c5d6 | 10 | { |
| andrelongo85 | 1:247161a5c5d6 | 11 | int tmp = 0; |
| andrelongo85 | 1:247161a5c5d6 | 12 | int pot_ten=1; |
| andrelongo85 | 1:247161a5c5d6 | 13 | for ( int i= (size -1); i >= 0 ; i-- ) { |
| andrelongo85 | 1:247161a5c5d6 | 14 | tmp = int( tmp + ( ( int(char_p[i]) - 48 ) * pot_ten )); |
| andrelongo85 | 1:247161a5c5d6 | 15 | pot_ten*=10; |
| andrelongo85 | 1:247161a5c5d6 | 16 | //pc.printf("\n"); |
| andrelongo85 | 1:247161a5c5d6 | 17 | //print_num(tmp); |
| andrelongo85 | 1:247161a5c5d6 | 18 | //pc.printf("\n"); |
| andrelongo85 | 1:247161a5c5d6 | 19 | //print_num(pot_ten); |
| andrelongo85 | 1:247161a5c5d6 | 20 | } |
| andrelongo85 | 1:247161a5c5d6 | 21 | return tmp; |
| andrelongo85 | 1:247161a5c5d6 | 22 | } |
| andrelongo85 | 1:247161a5c5d6 | 23 | |
| andrelongo85 | 1:247161a5c5d6 | 24 | |
| andrelongo85 | 1:247161a5c5d6 | 25 | // check if the input is valid |
| andrelongo85 | 1:247161a5c5d6 | 26 | bool si_check_valid(const char & input) |
| andrelongo85 | 1:247161a5c5d6 | 27 | { |
| andrelongo85 | 1:247161a5c5d6 | 28 | if ( ( ( int(input) > 57) || ( int(input) < 48) ) && (input != '+') && (input != '-') && (input != '*') && (input != '/') && (input != '=') ) { |
| andrelongo85 | 1:247161a5c5d6 | 29 | return FALSE; |
| andrelongo85 | 1:247161a5c5d6 | 30 | } else { |
| andrelongo85 | 1:247161a5c5d6 | 31 | return TRUE; |
| andrelongo85 | 1:247161a5c5d6 | 32 | } |
| andrelongo85 | 1:247161a5c5d6 | 33 | } |
| andrelongo85 | 1:247161a5c5d6 | 34 | |
| andrelongo85 | 1:247161a5c5d6 | 35 | //check if the input is a number |
| andrelongo85 | 1:247161a5c5d6 | 36 | bool si_check_number(const char & input) |
| andrelongo85 | 1:247161a5c5d6 | 37 | { |
| andrelongo85 | 1:247161a5c5d6 | 38 | if ( (int(input) >= 48) && ( int(input) <= 57) ) { |
| andrelongo85 | 1:247161a5c5d6 | 39 | return TRUE; |
| andrelongo85 | 1:247161a5c5d6 | 40 | } else { |
| andrelongo85 | 1:247161a5c5d6 | 41 | return FALSE; |
| andrelongo85 | 1:247161a5c5d6 | 42 | } |
| andrelongo85 | 1:247161a5c5d6 | 43 | } |
| andrelongo85 | 1:247161a5c5d6 | 44 | |
| andrelongo85 | 2:0baf535ed8f8 | 45 | |
| andrelongo85 | 1:247161a5c5d6 | 46 | |
| andrelongo85 | 1:247161a5c5d6 | 47 | int main() |
| andrelongo85 | 1:247161a5c5d6 | 48 | { |
| andrelongo85 | 1:247161a5c5d6 | 49 | char c; |
| andrelongo85 | 1:247161a5c5d6 | 50 | char op = '='; |
| andrelongo85 | 1:247161a5c5d6 | 51 | char operand [100]; |
| andrelongo85 | 1:247161a5c5d6 | 52 | char buf[10]; |
| andrelongo85 | 1:247161a5c5d6 | 53 | int result =0; |
| andrelongo85 | 1:247161a5c5d6 | 54 | int index = 0; |
| andrelongo85 | 2:0baf535ed8f8 | 55 | int ioperand =0; |
| andrelongo85 | 1:247161a5c5d6 | 56 | bool first = TRUE; |
| andrelongo85 | 2:0baf535ed8f8 | 57 | bool sign_check = TRUE; |
| andrelongo85 | 2:0baf535ed8f8 | 58 | bool neg_operand = FALSE; |
| andrelongo85 | 1:247161a5c5d6 | 59 | |
| andrelongo85 | 1:247161a5c5d6 | 60 | pc.printf("LPC Integer Calculator!\n"); |
| andrelongo85 | 2:0baf535ed8f8 | 61 | |
| mbed_official | 0:879aa9d0247b | 62 | while(1) { |
| andrelongo85 | 2:0baf535ed8f8 | 63 | |
| andrelongo85 | 1:247161a5c5d6 | 64 | c=pc.getc(); //get serial input |
| andrelongo85 | 1:247161a5c5d6 | 65 | |
| andrelongo85 | 1:247161a5c5d6 | 66 | if (!si_check_valid(c)) { // Invalid Input |
| andrelongo85 | 1:247161a5c5d6 | 67 | pc.putc(c); |
| andrelongo85 | 1:247161a5c5d6 | 68 | printf ("Invalid Input!\n"); |
| andrelongo85 | 1:247161a5c5d6 | 69 | } else if (si_check_number(c)) { // Operand has been entered |
| andrelongo85 | 1:247161a5c5d6 | 70 | pc.putc(c); |
| andrelongo85 | 1:247161a5c5d6 | 71 | operand[index] = c; |
| andrelongo85 | 1:247161a5c5d6 | 72 | index++; |
| andrelongo85 | 2:0baf535ed8f8 | 73 | sign_check = FALSE; //sign check disactivated |
| andrelongo85 | 1:247161a5c5d6 | 74 | } else { //Operator has been entered |
| andrelongo85 | 2:0baf535ed8f8 | 75 | |
| andrelongo85 | 2:0baf535ed8f8 | 76 | if(!sign_check) { |
| andrelongo85 | 2:0baf535ed8f8 | 77 | pc.putc('\n'); |
| andrelongo85 | 2:0baf535ed8f8 | 78 | pc.putc(c); |
| andrelongo85 | 2:0baf535ed8f8 | 79 | pc.putc('\n'); |
| andrelongo85 | 1:247161a5c5d6 | 80 | |
| andrelongo85 | 2:0baf535ed8f8 | 81 | // sign adjustment |
| andrelongo85 | 2:0baf535ed8f8 | 82 | if(neg_operand) { |
| andrelongo85 | 2:0baf535ed8f8 | 83 | ioperand = -(char_to_int(operand,index)); |
| andrelongo85 | 2:0baf535ed8f8 | 84 | } else { |
| andrelongo85 | 2:0baf535ed8f8 | 85 | ioperand = char_to_int(operand,index); |
| andrelongo85 | 2:0baf535ed8f8 | 86 | } |
| andrelongo85 | 2:0baf535ed8f8 | 87 | neg_operand=FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 88 | |
| andrelongo85 | 2:0baf535ed8f8 | 89 | if (first) { //First Operator |
| andrelongo85 | 2:0baf535ed8f8 | 90 | result = ioperand; |
| andrelongo85 | 2:0baf535ed8f8 | 91 | first = FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 92 | } else { //Do Calculation |
| andrelongo85 | 1:247161a5c5d6 | 93 | |
| andrelongo85 | 2:0baf535ed8f8 | 94 | if(op == '+') { |
| andrelongo85 | 2:0baf535ed8f8 | 95 | result = int (result + ioperand); |
| andrelongo85 | 2:0baf535ed8f8 | 96 | } else if (op == '-') { |
| andrelongo85 | 2:0baf535ed8f8 | 97 | result = int (result - ioperand); |
| andrelongo85 | 2:0baf535ed8f8 | 98 | } else if (op == '/') { |
| andrelongo85 | 2:0baf535ed8f8 | 99 | result = int (result / ioperand); |
| andrelongo85 | 2:0baf535ed8f8 | 100 | } else if (op == '*') { |
| andrelongo85 | 2:0baf535ed8f8 | 101 | result = int (result * ioperand); |
| andrelongo85 | 2:0baf535ed8f8 | 102 | } |
| andrelongo85 | 2:0baf535ed8f8 | 103 | |
| andrelongo85 | 2:0baf535ed8f8 | 104 | if (c == '=') { // Operator = has been entered--> Print result and reset calculator |
| andrelongo85 | 2:0baf535ed8f8 | 105 | for (int j =9; j>=0; j--) { |
| andrelongo85 | 2:0baf535ed8f8 | 106 | buf[j]='\0'; |
| andrelongo85 | 2:0baf535ed8f8 | 107 | } |
| andrelongo85 | 2:0baf535ed8f8 | 108 | sprintf(buf, "%d", result); |
| andrelongo85 | 2:0baf535ed8f8 | 109 | pc.printf("%s", buf); |
| andrelongo85 | 2:0baf535ed8f8 | 110 | pc.printf("\n\n"); |
| andrelongo85 | 2:0baf535ed8f8 | 111 | pc.printf("Please enter a new operation\n"); |
| andrelongo85 | 2:0baf535ed8f8 | 112 | first = TRUE; |
| andrelongo85 | 2:0baf535ed8f8 | 113 | } |
| andrelongo85 | 1:247161a5c5d6 | 114 | } |
| andrelongo85 | 2:0baf535ed8f8 | 115 | op = c; // assign the Operator |
| andrelongo85 | 2:0baf535ed8f8 | 116 | index=0; // reset the operand |
| andrelongo85 | 2:0baf535ed8f8 | 117 | sign_check = TRUE; // sign check activated |
| andrelongo85 | 2:0baf535ed8f8 | 118 | } else { |
| andrelongo85 | 2:0baf535ed8f8 | 119 | pc.putc(c); |
| andrelongo85 | 2:0baf535ed8f8 | 120 | if (c=='-') { |
| andrelongo85 | 2:0baf535ed8f8 | 121 | neg_operand=TRUE; |
| andrelongo85 | 2:0baf535ed8f8 | 122 | sign_check = FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 123 | } else if (c=='+') { |
| andrelongo85 | 2:0baf535ed8f8 | 124 | neg_operand=FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 125 | sign_check = FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 126 | } else { |
| andrelongo85 | 2:0baf535ed8f8 | 127 | pc.printf("ERROR:Two consecutive Operators entered"); |
| andrelongo85 | 1:247161a5c5d6 | 128 | } |
| andrelongo85 | 1:247161a5c5d6 | 129 | } |
| andrelongo85 | 1:247161a5c5d6 | 130 | } |
| mbed_official | 0:879aa9d0247b | 131 | } |
| andrelongo85 | 1:247161a5c5d6 | 132 | } |
| andrelongo85 | 1:247161a5c5d6 | 133 |
