+0  
 
-2
238
0
avatar+234 

Problem:

In Part 2 of this assignment, you will augment your previous implementation for first. Rather
than have all binary integers, the integers can be in hexadecimal, octal, base 10, or binary. All
input sequences will remain 4 bit patterns. Thus, this will only effect the Delimiter and Number
input sequences. We also remove the NaN input sequence and replace it with the Parenthesis input
sequence. Let us now redefine these input sequences:
•0001 Delimiter: This input sequence denotes the end of the current equation and the
start of a new equation. The sequence will be followed by a 2 character pattern signifying
what number system is to be used [”0x” for hexadecimal, ”0h” for octal”, ”00” for base ten,
and ”0b” for binary”]. Upon receiving this input, the program should print the result of the
previous equation in the given number system. The result should be preceeded by the symbol
designating the base being used. An equation will always end with a delimiter.
•0010 Number: This input sequence denotes that the following sequence is a number x.
This input sequence will be followed by a 4 character sequence represting a number. This
number will signify the amount of binary digits that the number x is comprised of. The 4
character number will be preceeded by a 2 character pattern signifying what number system
is being used. [”0x” for hexadecimal, ”0h” for octal”, ”00” for base ten, and ”0b” for binary”].
The 4 bit number will be followed by another 2 character pattern once again signifying the
number system being used for the number x. Lastly, the characters for the number x will
follow.
•1010 Open Parenthesis: This input sequence denotes the beginning of a ”sub equation”
or an equation enclosed in parenthesis. It will always be followed, at some point, by a closed
parenthesis. The ”sub equation” enclosed within the sub parenthesis should be resolved in
its entirety before interacting with the remaining equation on the outside of the parenthesis.
A set of parenthesis may exist within another pair of parenthesis.
•1001 Closed Parenthesis: This input sequence denotes the end of a ”sub equation” or
the end of an equation enclosed in parenthesis. It will always be preceded, at some point, by
a closed parenthesis. The ”sub equation” enclosed within the parenthesis should be resolved
before the remaining equation on the outside of the parenthesis. There will always be at least
one value inbetween an open and closed parenthesis.
Input format: This program takes one sequence from the command line. The sequence will be a
binary sequence. The sequence should then be read using the previous rules in order to determine
the underlying equation(s).
Output format: Your program should output the result of each of the equations in the designated
base. Each equation will result in some output. The output will be either a numeric solution to
the equation or the value ”NaN”. The solution to each equation should be printed on a different
line.

 

Example Execution:
Some sample runs and results are:


test 1:
$./second 00100000010xA010000101000100b00110011101001000100b00100h50100100010x
0x1162


test 2:
$./second 00100b01000b100001001000100b00100xAA000100
001360


To break down each of the examples:
test 1: 0010 00 0001 0x A 0100 00 1010 0010 0b 0011 00111 0100 10 0010 0b0010 0h50
1001 00010x


can be interpreted as: [Number, base 10-1 digit long, hexidecimal A, Symbol, addition, Open
parenthesis, Number, binary-3 digit long, base 10-111, Symbol, multiplication, Number, binary-2
digits long, octal 50, End parenthesis, Delimiter-hexadecimal] or
0xA + (111 * 0h50)
which is:
10 + (111 * 40) = 4450
test 2: 0010 0b0100 0b1000 0100 10 0010 0b0010 0xAA 000100
can be interpreted as: [Number, binary-4 digits long, binary 1000, Symbol, multiplication, Number,
binary-2 digit long, hexidecimal AA, Delimiter-base 10] or
0b1000 * 0xAA
which is:
8 * 170 = 1360

 

My current code: 

 

#include
#include
#include
#include

void main(int argc, char* argv[]) {
    int val = 0;
    int nextop = -1;
    int nan = 0;

    for ( int i = 1; i < argc; i++ ){
        char* ops = malloc(sizeof(char) * (1 + strlen(argv [i])));
        strncpy(ops, argv [i], strlen(argv [i]));
        *(ops+strlen(argv [i])) = '\0';

        while ( strcmp(ops, "") != 0 ) {
            char* sym = malloc(sizeof(char) * 5);
            strncpy(sym, ops, 4);
            *(sym+4) = '\0';
            ops += 4;

            if ( strcmp(sym, "1000") == 0 ){
                nan = 1;
            } 
            
            else if ( strcmp(sym, "0001") == 0 ) {
                if ( nan == 1 )printf("NaN\n");
                else printf("%d\n", val);
                val = 0;
                nextop = -1;
                nan = 0;
            } 
            
            else if ( strcmp(sym, "0100") == 0 ) {
                char* op = malloc(sizeof(char) * 3);
                strncpy(op, ops, 2);
                *(op+2) = '\0';
                ops += 2;

                if ( strcmp(op, "00") == 0 )nextop = 0;
                else if ( strcmp(op, "01") == 0 )nextop = 1;
                else nextop = 2;

                free(op);
            } 
            
            else {
                char* len = malloc(sizeof(char) * 5);
                strncpy(len, ops, 4);
                *(len+4) = '\0';
                ops += 4;

                int bits = (int)strtol(len, NULL, 2);

                char* numstring = malloc(sizeof(char) * (1 + bits));
                strncpy(numstring, ops, bits);
                *(numstring+bits) = '\0';
                ops += bits;

                int num = (int)strtol(numstring, NULL, 2);

                if ( nextop == -1 )val = num;
                
                else if ( nextop == 0 ){
                    if ( val < INT_MAX - num ) val += num;
                    else nan = 1;
                } 
                
                else if ( nextop == 1 ) val -= num;
                
                else {
                    if ( val < INT_MAX / num ) val *= num;
                    else nan = 1;
                }

                free(len);
                free(numstring);
            }

            free(sym);
        }

        free(ops-strlen(argv [i]));
    }
}

 
 Jul 20, 2022

2 Online Users

avatar