Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- bool this_letter (char c) {
- // FUNCTION DESCRIPTION:
- // Return true if the incoming character is a letter or underscore (_).
- // Need to define a variable.
- // Determined by comparing a character using an ASCII table.
- if ((c > 64 && 91 > c) || (c > 96 && 123 > c) || c == '_') {
- return true;
- } else {
- return false;
- }
- }
- bool this_digit (char c) {
- // FUNCTION DESCRIPTION:
- // Return true if the incoming character is a digit.
- // Need to define a number.
- // Determined by comparing a character using an ASCII table.
- if (c > 47 && 58 > c) {
- return true;
- } else {
- return false;
- }
- }
- int define_operator (char c) {
- // FUNCTION DESCRIPTION:
- // Return identification if the incoming character is a operator
- // else return 0 (false)
- // Need to define a operator.
- // Determined by comparing a operator using an ASCII table.
- switch (c) {
- // if operator '='
- case 61:
- return 1;
- break;
- // if operator '+'
- case 43:
- return 2;
- break;
- // if operator '-'
- case 45:
- return 3;
- break;
- // if operator '*'
- case 42:
- return 4;
- break;
- // if operator '/'
- case 47:
- return 5;
- break;
- // if operator ':'
- case 58:
- return 6;
- break;
- default:
- return 0;
- break;
- }
- }
- int size_string(char * string) {
- // FUNCTION DESCRIPTION:
- // Return the size of the incoming string
- // Counts character quantity to character equal zero (last character)
- int size = 0;
- while (string[size] != 0) {
- size++;
- }
- return size;
- }
- char * separates_name_var(char * str, int index) {
- // FUNCTION DESCRIPTION:
- // Returns a string whose the index of the first character
- // which is string to the incoming character.
- // Need to determine the variable name.
- char * name_var = new char[1];
- for (int i = 0; this_digit(str[index + i]) || this_letter(str[index + i]); i++ ) {
- name_var[i] = str[index + i];
- }
- return name_var;
- }
- int separates_number(char * str, int index) {
- // FUNCTION DESCRIPTION:
- // Returns a number whose the index of the first character
- // which is number to the incoming character.
- // Need to determine the number.
- int number = 0;
- for(int i = 0; this_digit(str[index + i]); i++) {
- number = number * 10 + (str[index + i] - 48);
- }
- return number;
- }
- int define_index_var(char ** arr_name_var, int size_arr_name_var, char * name_var) {
- // FUNCTION DESCRIPTION:
- // Returns index in array name variable incoming variable.
- // If the variable is not found, returns -1.
- int index = -1;
- // Array for storing variable name matching statuses
- // 0 = Don't appropriate name
- // 1 = Appropriate name (default)
- int arr[size_arr_name_var];
- for (int i = 0; size_arr_name_var > i; i++) {
- arr[i] = 1;
- }
- // Per-character comparison of variables
- int size_name_var = size_string(name_var);
- for (int i = 0; size_name_var > i; i++) {
- for(int j = 0; size_arr_name_var > j; j++) {
- if (arr[j] == 1) {
- if (arr_name_var[j][i] != name_var[i]) {
- arr[j] = 0;
- }
- }
- }
- }
- // Cycle to eliminate variables equal to the first characters
- // Example: name_varable and name_varable_blablbla
- for(int i = 0; size_arr_name_var > i; i++) {
- if (arr[i] == 1) {
- // If the appropriate variable names have the same size
- if (size_name_var == size_string(arr_name_var[i])) {
- index = i;
- break;
- }
- }
- }
- return index;
- }
- int define_val_id(int * arr_var, int * arr_num, int index) {
- // FUNCTION DESCRIPTION:
- // Returns the number based on the incoming identifier.
- // Needed for the calculation step.
- if (index >= 10) {
- // if this variabled
- return arr_var[index - 10];
- } else {
- // if this number
- return arr_num[index * -1 - 1];
- }
- }
- void output_error(int code) {
- // FUNCTION DESCRIPTION:
- // Output to the output stream a code and description of the error
- // and a warning about the end of the program.
- printf("PROGRAM ERROR [18092000x%d]: ", code);
- switch (code) {
- case 0:
- printf("Cycle-translater met undeclared symbol\n");
- break;
- case 1:
- printf("Unary minus it applies to operator or variabled\n");
- break;
- case 2:
- printf("Assignment operator was expected\n");
- break;
- case 3:
- printf("Variable or number was expected\n");
- break;
- case 4:
- printf("Operator was expected\n");
- break;
- case 5:
- printf("Operator was expected\n");
- break;
- case 7:
- printf("at assignment variable was expected");
- break;
- case 8:
- printf("at assignment variable was expected");
- break;
- case 9:
- printf("at assignment variable was expected");
- break;
- }
- }
- int main() {
- // Interface Program
- printf("THE PROGRAM IS RUNNING\n\n");
- printf("THE PROGRAM CAN:\n");
- printf(" 1) Calculate and display the result\n");
- printf(" of the simplest algebraic expressions (+, -, *, /).\n");
- printf(" Example(output):\n");
- printf(" 128 * 64 + -124;\n");
- printf(" 2) Save any number of named user variables\n");
- printf(" and use them to calculate.\n");
- printf(" Example(initialization, output and used variabled in initialization):\n");
- printf(" score = 16;\n");
- printf(" score * 10;\n");
- printf(" result = score - 60;\n");
- // String to keep user input
- char * str_command;
- // Array to variabled's
- char ** arr_name_var = new char * [1];
- int * arr_val_var = new int [1];
- int qt_var = 0;
- // Array to uninitialized variabled's
- char ** arr_name_unvar = new char * [1];
- int qt_unvar = 0;
- // Array to number's
- int * arr_num;
- int qt_num = 0;
- // Array to ID's
- // ID's - index's variabled's, number's and operator's of str_command
- int * arr_id;
- int qt_id = 0;
- bool last_operator_end = false;
- int last_id;
- while(true) {
- str_command = new char[256];
- arr_num = new int[1];
- arr_id = new int[1];
- arr_name_unvar = new char * [1];
- last_id = 0;
- while(true) {
- // Input user string in str_command
- scanf("%s", str_command);
- start_cycle_translator: ;
- // Cycle for translate operator's, variabled's and operator's in ID's (Cycle Translator)
- for (int i = last_id; size_string(str_command) > i; i++) {
- char symbol = str_command[i];
- // Translater for variabled
- if (this_letter(symbol)) {
- char * name_var = separates_name_var(str_command, i);
- int index = define_index_var(arr_name_var, qt_var, name_var);
- // IF variabled not initialized
- if (index == -1) {
- index = define_index_var(arr_name_unvar, qt_unvar, name_var);
- if (index == -1) {
- arr_name_unvar[qt_unvar] = name_var;
- qt_unvar++;
- }
- arr_id[qt_id] = 0;
- }
- // IF variabled initialized
- else {
- arr_id[qt_id] = index + 10;
- }
- // Cycle for skip symbol's name variabled parent-cycle
- while (this_letter(symbol) || this_digit(symbol)) {
- i++;
- symbol = str_command[i];
- }
- qt_id++;
- i--;
- } else
- // Translater for number
- if (this_digit(symbol)) {
- int num = separates_number(str_command, i);
- arr_num[qt_num] = num;
- qt_num++;
- arr_id[qt_id] = qt_num * -1;
- qt_id++;
- // Cycle for skip symbol's number parent-cycle
- while (this_digit(symbol)) {
- i++;
- symbol = str_command[i];
- }
- i--;
- } else
- // Translater operator
- if (define_operator(symbol)) {
- arr_id[qt_id] = define_operator(symbol);
- qt_id++;
- } else
- // End string command;
- if (symbol == ';') {
- i + 1 == size_string(str_command) ? last_operator_end = true : last_operator_end = false;
- last_id = i + 1;
- goto exit_translater;
- } else
- // Start special command
- if (symbol == '!') {
- goto exit_program;
- } else
- if (symbol != 0) {
- output_error(0);
- goto exit_program;
- }
- }
- }
- exit_translater: ;
- int id_assignment = 0;
- bool wait_var = true;
- bool minus = false;
- // Cycle for finding error-construction and del unary minus (Сycle Analyzer)
- for (int i = 0; qt_id > i; i++) {
- // ERROR DETECTOR
- // IF (current ID == unary minus) AND ((right ID == variabled) OR (right ID == operator))
- if (wait_var && arr_id[i] == 3 && arr_id[i + 1] >= 0) {
- output_error(1);
- goto exit_program;
- }
- // IF (current ID == unary minus) AND (right ID == number)
- if (wait_var && arr_id[i] == 3 && 0 > arr_id[i + 1]) {
- arr_num[arr_id[i + 1] * -1 - 1] *= -1;
- minus = false;
- // Deleted minus in array identifier's
- int new_arr_id[qt_id - 1];
- for (int j = 0; i > j; j++) {
- new_arr_id[j] = arr_id[j];
- }
- for (int j = i + 1; qt_id > j; j++) {
- new_arr_id[j - 1] = arr_id[j];
- }
- delete [] arr_id;
- qt_id--;
- arr_id = new int(qt_id);
- for (int j = 0; qt_id > j; j++) {
- arr_id[j] = new_arr_id[j];
- }
- }
- // ERROR DETECTOR
- // IF (current ID == uninitialized variable) AND (right identifier =/= '=' OR left identifier =/= '=')
- if (arr_id[i] == 0 && (arr_id[i + 1] != 1 || (i > 0 && arr_id[i - 1] != 1))) {
- output_error(2);
- goto exit_program;
- }
- // ERROR DETECTOR
- // IF (wait_var == yes) AND (current ID == operator);
- if (wait_var && arr_id[i] > 0 && 10 > arr_id[i]) {
- output_error(3);
- goto exit_program;
- }
- // ERROR
- // IF (wait_var == no) AND ((current ID == number) OR (current ID == variabled))
- if (wait_var == false && (arr_id[i] >= 10 || 0 >= arr_id[i]) ) {
- // printf("[%d] %d\n", i, define_val_id(arr_val_var, arr_num, arr_id[i]));
- output_error(4);
- goto exit_program;
- }
- // ERROR
- // IF (current ID == '=') AND ((current ID == 0) OR (left ID == operator OR number) OR (right ID == operator OR number))
- if (arr_id[i] == 1 && (i == 0 || (arr_id[i - 1] > 0 && 10 > arr_id[i - 1]) || 0 > arr_id[i - 1])) {
- output_error(5);
- goto exit_program;
- }
- // IF current ID == operator
- if (arr_id[i] > 0 && 10 > arr_id[i]) {
- wait_var = true;
- }
- // IF current ID == variabled
- if (arr_id[i] >= 10 || 0 >= arr_id[i]) {
- wait_var = false;
- }
- // Find index last assignment
- arr_id[i] == 1 ? id_assignment = i : id_assignment += 0;
- }
- // ERROR
- // IF (wait variabled == yes) (End Сycle Analyzer)
- if (wait_var) {
- output_error(0);
- goto exit_program;
- }
- // Calculations of the right side | Top priority ( * and / )
- for (int i = 0; qt_id > i; i++) {
- // IF current ID == '*' OR '/' OR ':'
- if (arr_id[i] == 4 || arr_id[i] == 5 || arr_id[i] == 6) {
- // Initialize the left and right value
- int x = define_val_id(arr_val_var, arr_num, arr_id[i - 1]);
- int y = define_val_id(arr_val_var, arr_num, arr_id[i + 1]);
- // Deletion of operator and lateral values and insert result calculation
- int new_arr_id[qt_id - 2];
- for (int j = 0; i - 1 > j; j++) {
- new_arr_id[j] = arr_id[j];
- }
- new_arr_id[i - 1] = qt_num * -1 - 1;
- arr_id[i] == 4 ? arr_num[qt_num++] = x * y : arr_num[qt_num++] = x / y;
- for (int j = i; qt_id > j + 2; j++) {
- new_arr_id[j] = arr_id[j + 2];
- }
- delete [] arr_id;
- qt_id -= 2;
- arr_id = new int(qt_id);
- for (int j = 0; qt_id > j; j++) {
- arr_id[j] = new_arr_id[j];
- }
- i--;
- }
- }
- // Calculations of the right side | Lowest priority ( + and - )
- for (int i = 0; qt_id > i; i++) {
- // IF current ID == '+' OR '-'
- if (arr_id[i] == 2 || arr_id[i] == 3) {
- // Initialize the left and right value
- int x = define_val_id(arr_val_var, arr_num, arr_id[i - 1]);
- int y = define_val_id(arr_val_var, arr_num, arr_id[i + 1]);
- // Deletion of operator and lateral values and insert result calculation
- int new_arr_id[qt_id - 2];
- for (int j = 0; i - 1 > j; j++) {
- new_arr_id[j] = arr_id[j];
- }
- new_arr_id[i - 1] = qt_num * -1 - 1;
- arr_id[i] == 2 ? arr_num[qt_num++] = x + y : arr_num[qt_num++] = x - y;
- for (int j = i; qt_id > j + 2; j++) {
- new_arr_id[j] = arr_id[j + 2];
- }
- delete [] arr_id;
- qt_id -= 2;
- arr_id = new int(qt_id);
- for (int j = 0; qt_id > j; j++) {
- arr_id[j] = new_arr_id[j];
- }
- i--;
- }
- }
- int result = define_val_id(arr_val_var, arr_num, arr_id[qt_id - 1]);
- // IF index last assignment == 0 (that's assignment don't find)
- if (id_assignment == 0) {
- // This operation output
- printf("RESULT OUTPUT: %d\n", result);
- } else {
- // This operation assignment
- for (int i = 0; qt_unvar > i; i++) {
- arr_name_var[qt_var] = arr_name_unvar[i];
- arr_val_var[qt_var] = result;
- qt_var++;
- }
- for (int i = 0; id_assignment / 2 + 1 > i; i++) {
- int id = arr_id[2 * i];
- if (id == 0) {
- continue;
- }
- arr_val_var[id - 10] = result;
- }
- }
- delete [] arr_num;
- qt_num = 0;
- delete [] arr_id;
- qt_id = 0;
- qt_unvar = 0;
- if (last_operator_end == false) {
- arr_num = new int[1];
- arr_id = new int[1];
- goto start_cycle_translator;
- }
- delete [] str_command;
- }
- exit_program: ;
- printf("PROGRAM WILL BE CLOSED");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement