Advertisement
daniil_mironoff

Console Application - Variable Calculator (Lab4)

Nov 28th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. bool this_letter (char c) {
  4.     // FUNCTION DESCRIPTION:
  5.     // Return true if the incoming character is a letter or underscore (_).
  6.     // Need to define a variable.
  7.     // Determined by comparing a character using an ASCII table.
  8.    
  9.     if ((c > 64 && 91 > c) || (c > 96 && 123 > c) || c == '_') {
  10.         return true;
  11.     } else {
  12.         return false;
  13.     }
  14. }
  15.  
  16. bool this_digit (char c) {
  17.     // FUNCTION DESCRIPTION:
  18.     // Return true if the incoming character is a digit.
  19.     // Need to define a number.
  20.     // Determined by comparing a character using an ASCII table.
  21.    
  22.     if (c > 47 && 58 > c) {
  23.         return true;
  24.     } else {
  25.         return false;
  26.     }
  27. }
  28.  
  29. int define_operator (char c) {
  30.     // FUNCTION DESCRIPTION:
  31.     // Return identification if the incoming character is a operator
  32.     // else return 0 (false)
  33.     // Need to define a operator.
  34.     // Determined by comparing a operator using an ASCII table.
  35.    
  36.     switch (c) {
  37.         // if operator '='
  38.         case 61:
  39.             return 1;
  40.             break;
  41.         // if operator '+'
  42.         case 43:
  43.             return 2;
  44.             break;
  45.         // if operator '-'
  46.         case 45:
  47.             return 3;
  48.             break;
  49.         // if operator '*'
  50.         case 42:
  51.             return 4;
  52.             break;
  53.         // if operator '/'
  54.         case 47:
  55.             return 5;
  56.             break;
  57.         // if operator ':'
  58.         case 58:
  59.             return 6;
  60.             break;
  61.            
  62.         default:
  63.             return 0;
  64.             break;
  65.     }
  66. }
  67.  
  68. int size_string(char * string) {
  69.     // FUNCTION DESCRIPTION:
  70.     // Return the size of the incoming string
  71.     // Counts character quantity to character equal zero (last character)
  72.    
  73.     int size = 0;
  74.     while (string[size] != 0) {
  75.         size++;
  76.     }
  77.    
  78.     return size;
  79. }
  80.  
  81. char * separates_name_var(char * str, int index) {
  82.     // FUNCTION DESCRIPTION:
  83.     // Returns a string whose the index of the first character
  84.     // which is string to the incoming character.
  85.     // Need to determine the variable name.
  86.    
  87.     char * name_var = new char[1];
  88.    
  89.     for (int i = 0; this_digit(str[index + i]) || this_letter(str[index + i]); i++ ) {
  90.         name_var[i] = str[index + i];
  91.     }
  92.    
  93.     return name_var;
  94. }
  95.  
  96. int separates_number(char * str, int index) {
  97.     // FUNCTION DESCRIPTION:
  98.     // Returns a number whose the index of the first character
  99.     // which is number to the incoming character.
  100.     // Need to determine the number.
  101.    
  102.     int number = 0;
  103.    
  104.     for(int i = 0; this_digit(str[index + i]); i++) {
  105.         number = number * 10 + (str[index + i] - 48);
  106.     }
  107.    
  108.     return number;
  109. }
  110.  
  111. int define_index_var(char ** arr_name_var, int size_arr_name_var, char * name_var) {
  112.     // FUNCTION DESCRIPTION:
  113.     // Returns index in array name variable incoming variable.
  114.     // If the variable is not found, returns -1.
  115.    
  116.     int index = -1;
  117.    
  118.     // Array for storing variable name matching statuses
  119.     // 0 = Don't appropriate name
  120.     // 1 = Appropriate name (default)
  121.     int arr[size_arr_name_var];
  122.     for (int i = 0; size_arr_name_var > i; i++) {
  123.         arr[i] = 1;
  124.     }
  125.    
  126.     // Per-character comparison of variables
  127.     int size_name_var = size_string(name_var);
  128.     for (int i = 0; size_name_var > i; i++) {
  129.         for(int j = 0; size_arr_name_var > j; j++) {
  130.             if (arr[j] == 1) {
  131.                 if (arr_name_var[j][i] != name_var[i]) {
  132.                     arr[j] = 0;
  133.                 }
  134.             }
  135.         }
  136.     }
  137.    
  138.     // Cycle to eliminate variables equal to the first characters
  139.     // Example: name_varable and name_varable_blablbla
  140.     for(int i = 0; size_arr_name_var > i; i++) {
  141.         if (arr[i] == 1) {
  142.             // If the appropriate variable names have the same size
  143.             if (size_name_var == size_string(arr_name_var[i])) {
  144.                 index = i;
  145.                 break;
  146.             }
  147.         }
  148.     }
  149.    
  150.     return index;
  151. }
  152.  
  153. int define_val_id(int * arr_var, int * arr_num, int index) {
  154.     // FUNCTION DESCRIPTION:
  155.     // Returns the number based on the incoming identifier.
  156.     // Needed for the calculation step.
  157.    
  158.     if (index >= 10) {
  159.         // if this variabled
  160.         return arr_var[index - 10];
  161.     } else {
  162.         // if this number
  163.         return arr_num[index * -1 - 1];
  164.     }
  165. }
  166.  
  167. void output_error(int code) {
  168.     // FUNCTION DESCRIPTION:
  169.     // Output to the output stream a code and description of the error
  170.     // and a warning about the end of the program.
  171.    
  172.     printf("PROGRAM ERROR [18092000x%d]: ", code);
  173.     switch (code) {
  174.         case 0:
  175.             printf("Cycle-translater met undeclared symbol\n");
  176.             break;
  177.         case 1:
  178.             printf("Unary minus it applies to operator or variabled\n");
  179.             break;
  180.         case 2:
  181.             printf("Assignment operator was expected\n");
  182.             break;
  183.         case 3:
  184.             printf("Variable or number was expected\n");
  185.             break;
  186.         case 4:
  187.             printf("Operator was expected\n");
  188.             break;
  189.         case 5:
  190.             printf("Operator was expected\n");
  191.             break;
  192.         case 7:
  193.             printf("at assignment variable was expected");
  194.             break;
  195.         case 8:
  196.             printf("at assignment variable was expected");
  197.             break;
  198.         case 9:
  199.             printf("at assignment variable was expected");
  200.             break;
  201.     }
  202. }
  203.  
  204. int main() {
  205.    
  206.     // Interface Program
  207.     printf("THE PROGRAM IS RUNNING\n\n");
  208.     printf("THE PROGRAM CAN:\n");
  209.     printf("  1) Calculate and display the result\n");
  210.     printf("     of the simplest algebraic expressions (+, -, *, /).\n");
  211.     printf("     Example(output):\n");
  212.     printf("      128 * 64 + -124;\n");
  213.     printf("  2) Save any number of named user variables\n");
  214.     printf("     and use them to calculate.\n");
  215.     printf("     Example(initialization, output and used variabled in initialization):\n");
  216.     printf("      score = 16;\n");
  217.     printf("      score * 10;\n");
  218.     printf("      result = score - 60;\n");
  219.    
  220.     // String to keep user input
  221.     char * str_command;
  222.    
  223.     // Array to variabled's
  224.     char ** arr_name_var = new char * [1];
  225.     int * arr_val_var = new int [1];
  226.     int qt_var = 0;
  227.    
  228.     // Array to uninitialized variabled's
  229.     char ** arr_name_unvar = new char * [1];
  230.     int qt_unvar = 0;
  231.    
  232.     // Array to number's
  233.     int * arr_num;
  234.     int qt_num = 0;
  235.    
  236.     // Array to ID's
  237.     // ID's - index's variabled's, number's and operator's of str_command
  238.     int * arr_id;
  239.     int qt_id = 0;
  240.  
  241.     bool last_operator_end = false;
  242.     int last_id;
  243.    
  244.     while(true) {
  245.         str_command = new char[256];
  246.         arr_num = new int[1];
  247.         arr_id = new int[1];
  248.         arr_name_unvar = new char * [1];
  249.        
  250.         last_id = 0;
  251.         while(true) {
  252.             // Input user string in str_command
  253.             scanf("%s", str_command);
  254.             start_cycle_translator: ;
  255.             // Cycle for translate operator's, variabled's and operator's in ID's (Cycle Translator)
  256.             for (int i = last_id; size_string(str_command) > i; i++) {
  257.                 char symbol = str_command[i];
  258.                
  259.                 // Translater for variabled
  260.                 if (this_letter(symbol)) {
  261.                     char * name_var = separates_name_var(str_command, i);
  262.                     int index = define_index_var(arr_name_var, qt_var, name_var);
  263.                    
  264.                     // IF variabled not initialized
  265.                     if (index == -1) {
  266.                         index = define_index_var(arr_name_unvar, qt_unvar, name_var);
  267.                         if (index == -1) {
  268.                             arr_name_unvar[qt_unvar] = name_var;
  269.                             qt_unvar++;
  270.                         }
  271.                        
  272.                         arr_id[qt_id] = 0;
  273.                     }
  274.                    
  275.                     // IF variabled initialized
  276.                     else {
  277.                         arr_id[qt_id] = index + 10;
  278.                     }
  279.                    
  280.                     // Cycle for skip symbol's name variabled parent-cycle
  281.                     while (this_letter(symbol) || this_digit(symbol)) {
  282.                         i++;
  283.                         symbol = str_command[i];
  284.                     }
  285.                    
  286.                     qt_id++;
  287.                     i--;
  288.                    
  289.                 } else
  290.                    
  291.                 // Translater for number
  292.                 if (this_digit(symbol)) {
  293.                     int num = separates_number(str_command, i);
  294.                    
  295.                     arr_num[qt_num] = num;
  296.                     qt_num++;
  297.                    
  298.                     arr_id[qt_id] = qt_num * -1;
  299.                     qt_id++;
  300.                    
  301.                     // Cycle for skip symbol's number parent-cycle
  302.                     while (this_digit(symbol)) {
  303.                         i++;
  304.                         symbol = str_command[i];
  305.                     }
  306.                    
  307.                     i--;
  308.                 } else
  309.                    
  310.                 // Translater operator
  311.                 if (define_operator(symbol)) {
  312.                     arr_id[qt_id] = define_operator(symbol);
  313.                     qt_id++;
  314.                 } else
  315.                
  316.                 // End string command;
  317.                 if (symbol == ';') {
  318.                     i + 1 == size_string(str_command) ? last_operator_end = true : last_operator_end = false;
  319.                     last_id = i + 1;
  320.                     goto exit_translater;
  321.                 } else
  322.                
  323.                 // Start special command
  324.                 if (symbol == '!') {
  325.                     goto exit_program;
  326.                 } else
  327.                
  328.                 if (symbol != 0) {
  329.                     output_error(0);
  330.                     goto exit_program;
  331.                 }
  332.             }
  333.         }
  334.        
  335.         exit_translater: ;
  336.        
  337.         int id_assignment = 0;
  338.         bool wait_var = true;
  339.         bool minus = false;
  340.        
  341.         // Cycle for finding error-construction and del unary minus (Сycle Analyzer)
  342.         for (int i = 0; qt_id > i; i++) {
  343.            
  344.             // ERROR DETECTOR
  345.             // IF (current ID == unary minus) AND ((right ID == variabled) OR (right ID == operator))
  346.             if (wait_var && arr_id[i] == 3 && arr_id[i + 1] >= 0) {
  347.                 output_error(1);
  348.                 goto exit_program;
  349.             }
  350.            
  351.             // IF (current ID == unary minus) AND (right ID == number)
  352.             if (wait_var && arr_id[i] == 3 && 0 > arr_id[i + 1]) {
  353.                 arr_num[arr_id[i + 1] * -1 - 1] *= -1;
  354.                 minus = false;
  355.                
  356.                 // Deleted minus in array identifier's
  357.                 int new_arr_id[qt_id - 1];
  358.                 for (int j = 0; i > j; j++) {
  359.                     new_arr_id[j] = arr_id[j];
  360.                 }
  361.  
  362.                 for (int j = i + 1; qt_id > j; j++) {
  363.                     new_arr_id[j - 1] = arr_id[j];
  364.                 }
  365.  
  366.                 delete [] arr_id;
  367.                 qt_id--;
  368.                 arr_id = new int(qt_id);
  369.                
  370.                 for (int j = 0; qt_id > j; j++) {
  371.                     arr_id[j] = new_arr_id[j];
  372.                 }
  373.             }
  374.            
  375.             // ERROR DETECTOR
  376.             // IF (current ID == uninitialized variable) AND (right identifier =/= '=' OR left identifier =/= '=')
  377.             if (arr_id[i] == 0 && (arr_id[i + 1] != 1 || (i > 0 && arr_id[i - 1] != 1))) {
  378.                 output_error(2);
  379.                 goto exit_program;
  380.             }
  381.            
  382.             // ERROR DETECTOR
  383.             // IF (wait_var == yes) AND (current ID == operator);
  384.             if (wait_var && arr_id[i] > 0 && 10 > arr_id[i]) {
  385.                 output_error(3);
  386.                 goto exit_program;
  387.             }
  388.  
  389.             // ERROR
  390.             // IF (wait_var == no) AND ((current ID == number) OR (current ID == variabled))
  391.             if (wait_var == false && (arr_id[i] >= 10 || 0 >= arr_id[i]) ) {
  392. //                printf("[%d] %d\n", i, define_val_id(arr_val_var, arr_num, arr_id[i]));
  393.                 output_error(4);
  394.                 goto exit_program;
  395.             }
  396.            
  397.             // ERROR
  398.             // IF (current ID == '=') AND ((current ID == 0) OR (left ID == operator OR number) OR (right ID == operator OR number))
  399.             if (arr_id[i] == 1 && (i == 0 || (arr_id[i - 1] > 0 && 10 > arr_id[i - 1]) || 0 > arr_id[i - 1])) {
  400.                 output_error(5);
  401.                 goto exit_program;
  402.             }
  403.            
  404.             // IF current ID == operator
  405.             if (arr_id[i] > 0 && 10 > arr_id[i]) {
  406.                 wait_var = true;
  407.             }
  408.            
  409.             // IF current ID == variabled
  410.             if (arr_id[i] >= 10 || 0 >= arr_id[i]) {
  411.                 wait_var = false;
  412.             }
  413.  
  414.             // Find index last assignment
  415.             arr_id[i] == 1 ? id_assignment = i : id_assignment += 0;
  416.         }
  417.        
  418.         // ERROR
  419.         // IF (wait variabled == yes) (End Сycle Analyzer)
  420.         if (wait_var) {
  421.             output_error(0);
  422.             goto exit_program;
  423.         }
  424.        
  425.         // Calculations of the right side | Top priority ( * and / )
  426.         for (int i = 0; qt_id > i; i++) {
  427.             // IF current ID == '*' OR '/' OR ':'
  428.             if (arr_id[i] == 4 || arr_id[i] == 5 || arr_id[i] == 6) {
  429.                 // Initialize the left and right value
  430.                 int x = define_val_id(arr_val_var, arr_num, arr_id[i - 1]);
  431.                 int y = define_val_id(arr_val_var, arr_num, arr_id[i + 1]);
  432.                
  433.                 // Deletion of operator and lateral values and insert result calculation
  434.                 int new_arr_id[qt_id - 2];
  435.                 for (int j = 0; i - 1 > j; j++) {
  436.                     new_arr_id[j] = arr_id[j];
  437.                 }
  438.                
  439.                 new_arr_id[i - 1] = qt_num * -1 - 1;
  440.                 arr_id[i] == 4 ? arr_num[qt_num++] = x * y : arr_num[qt_num++] = x / y;
  441.                
  442.                 for (int j = i; qt_id > j + 2; j++) {
  443.                     new_arr_id[j] = arr_id[j + 2];
  444.                 }
  445.                
  446.                 delete [] arr_id;
  447.                 qt_id -= 2;
  448.                 arr_id = new int(qt_id);
  449.                
  450.                 for (int j = 0; qt_id > j; j++) {
  451.                     arr_id[j] = new_arr_id[j];
  452.                 }
  453.                
  454.                 i--;
  455.             }
  456.         }
  457.        
  458.         // Calculations of the right side | Lowest priority ( + and - )
  459.         for (int i = 0; qt_id > i; i++) {
  460.             // IF current ID == '+' OR '-'
  461.             if (arr_id[i] == 2 || arr_id[i] == 3) {
  462.                 // Initialize the left and right value
  463.                 int x = define_val_id(arr_val_var, arr_num, arr_id[i - 1]);
  464.                 int y = define_val_id(arr_val_var, arr_num, arr_id[i + 1]);
  465.                
  466.                 // Deletion of operator and lateral values and insert result calculation
  467.                 int new_arr_id[qt_id - 2];
  468.                 for (int j = 0; i - 1 > j; j++) {
  469.                     new_arr_id[j] = arr_id[j];
  470.                 }
  471.                
  472.                 new_arr_id[i - 1] = qt_num * -1 - 1;
  473.                 arr_id[i] == 2 ? arr_num[qt_num++] = x + y : arr_num[qt_num++] = x - y;
  474.                
  475.                 for (int j = i; qt_id > j + 2; j++) {
  476.                     new_arr_id[j] = arr_id[j + 2];
  477.                 }
  478.                
  479.                 delete [] arr_id;
  480.                 qt_id -= 2;
  481.                 arr_id = new int(qt_id);
  482.                
  483.                 for (int j = 0; qt_id > j; j++) {
  484.                     arr_id[j] = new_arr_id[j];
  485.                 }
  486.                
  487.                 i--;
  488.             }
  489.         }
  490.        
  491.         int result = define_val_id(arr_val_var, arr_num, arr_id[qt_id - 1]);
  492.        
  493.         // IF index last assignment == 0 (that's assignment don't find)
  494.         if (id_assignment == 0) {
  495.             // This operation output
  496.             printf("RESULT OUTPUT: %d\n", result);
  497.         } else {
  498.             // This operation assignment
  499.             for (int i = 0; qt_unvar > i; i++) {
  500.                 arr_name_var[qt_var] = arr_name_unvar[i];
  501.                 arr_val_var[qt_var] = result;
  502.                 qt_var++;
  503.             }
  504.            
  505.             for (int i = 0; id_assignment / 2 + 1 > i; i++) {
  506.                 int id = arr_id[2 * i];
  507.                
  508.                 if (id == 0) {
  509.                     continue;
  510.                 }
  511.                
  512.                 arr_val_var[id - 10] = result;
  513.             }
  514.         }
  515.        
  516.         delete [] arr_num;
  517.         qt_num = 0;
  518.        
  519.         delete [] arr_id;
  520.         qt_id = 0;
  521.        
  522.         qt_unvar = 0;
  523.        
  524.         if (last_operator_end == false) {
  525.             arr_num = new int[1];
  526.             arr_id = new int[1];
  527.            
  528.             goto start_cycle_translator;
  529.         }
  530.        
  531.         delete [] str_command;
  532.     }
  533.    
  534.     exit_program: ;
  535.    
  536.     printf("PROGRAM WILL BE CLOSED");
  537.    
  538.     return 0;
  539. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement