Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <cstdlib>
- #ifndef __cplusplus
- typedef bool unsigned char;
- #define true 1
- #define false 0
- #endif
- const char OPERATOR_OR = '|';
- const char OPERATOR_AND = '&';
- const char OPERATOR_NOT = '!';
- const char OPERATOR_XOR = '^';
- const char ASCII_A = 0x41;
- const char ASCII_Z = 0x5A;
- static unsigned char *variables;
- static unsigned char varsCount;
- /*
- * Decide how many variables are in string
- *
- * @param string
- */
- unsigned char getVarsCount(const char *string)
- {
- unsigned char i;
- if ( string == NULL )
- return 0;
- for ( i = ASCII_A; i < ASCII_Z; i++ )
- {
- if ( strchr(string, i) == NULL )
- return ( i - ASCII_A );
- }
- return 0;
- }
- /*
- * Get var's current value by char represent.
- *
- * @param var
- */
- unsigned char getValue(const char var)
- {
- return ( variables[var - ASCII_A - 1] );
- }
- /*
- * Parses the sequence of strings and returns
- * Stack, packed in char array (zero-ended)
- *
- * @param string - input string
- */
- unsigned char parseString(char *string)
- {
- size_t length;
- int i;
- int value_total;
- if ( string == NULL )
- return NULL;
- // Get stack length
- length = strlen(string);
- if ( length < 1 )
- return 0;
- value_total = getValue(string[0]);
- // Parse the string
- for ( i = 1; i < length; i++ )
- {
- if ( string[i] == OPERATOR_AND )
- value_total &= getValue(string[i + 1]);
- else if ( string[i] == OPERATOR_OR )
- value_total |= getValue(string[i + 1]);
- else if ( string[i] == OPERATOR_XOR )
- value_total ^= value_total;
- else if ( string[i] == OPERATOR_NOT )
- value_total = ! value_total;
- }
- return value_total;
- }
- /*
- * The entry point
- *
- * @param argc - cl arguments count
- * @param argv - cl arguments 2d array
- */
- int main(int argc, char **argv)
- {
- FILE *fp = NULL;
- char string[256];
- char tmp_string[256];
- unsigned char i;
- size_t count;
- fp = fopen("expression.txt", "r");
- if ( fp == NULL )
- return 1;
- count = fread(string, 1, 255, fp);
- fclose(fp);
- string[count] = 0x00;
- printf("Expression: %s\n\n", string);
- fopen("output.txt", "w");
- for ( i = 0; i < varsCount; i++ )
- {
- }
- /////////////////////////////////////////////////////////////////
- // Вот тут надо переберать переменные и вызывать parseString
- /////////////////////////////////////////////////////////////
- // fwrite(tmp_string, 1, strlen(tmp_string), fp);
- varsCount = getVarsCount(string);
- printf("%d\n", varsCount);
- fclose(fp);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement