Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task is to develop a large hexadecimal integer calculator that works with h

ID: 3690756 • Letter: Y

Question

Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of up to
100 digits (plus a sign). The calculator has a simple user interface, and 10 ariables" (n0, n1, ..., n9)
into which hexadecimal integers can be stored. For example a session with your calculator might look like:
mac: ./assmt1
> n0=0x2147483647
> n0+0x3
> n0?
0x214748364A
> n1=0x1000000000000000000
> n1+n0
> n1?
0x100000000214748364A
> n0?
0x214748364A
> exit
mac:
Note: mac: " is the terminal prompt and > " is the prompt of the calculator.
The calculator to be implemented has a very limited syntax: constants or other variables can be assigned to
variables using an `=' operator; variable values can be altered using a single operator and another variable
or constant; variable values can be printed using `?'. Each line of input command always starts with a vari-
able followed by a single operator, which is then followed by (optional depending on the operator) another
variable or a constant. The only exception is the exit" command, which has no additional parameters.
To allow storage of very large hexadecimal integers, your calculator needs to use arrays of characters, with
one hexadecimal digit stored per character in the array. Additional information is required in each integer",
including a count of the number of digits, and a sign. We assume a maximum of 100 digits (plus a sign)
are to be handled, and use a everse order" representation. For example, 0x123A will be represented by:
#define INT_SIZE 101
typedef char longhex_t[INT_SIZE];
longhex_t var_n0 = {'+', 'A', '3', '2', '1'}; /* Note the prefix ``0x'' is not stored. */
int var_len0 = 4;
Here the character array var_n0 starts with a sign character (`+' or `-'), followed by the digits which are
stored in a reversed way. The reverse order representation will make calculations such as addition and
multiplication much easier (why?). However, if you wish, you are free to change how the digits are stored
in the array. By using struct we can further simplify the representation, but you are not required to do so
as it has not been covered yet.
The expected input format for all stages is identical - a sequence of simple commands as shown above. You
may assume that the input is always valid, that is, you do not need to consider input errors.
Also, there will not be leading 0's in the input numbers, e.g., 0x001, and you do not need to
consider lower case letters a,b,...,f. Positive numbers (or zero) in the input will come with-
out the `+' sign. That is, there will not be 0=+0x0", 0=+0x123A", 0++0x123A", or 0*+0x123A"
in the input. It will just be 0=0x0", 0=0x123A", 0+0x123A", or 0*0x123A".
You will be given a skeleton code. The skeleton code
contains a main function where a loop is used to continuously read in user commands, and calls relevant
functions to process the commands. The exit function has been implemented already, which can handle
the exit command if you compile and run the skeleton code. The echo function for the `?' operator to
print out a hexadecimal integer has been given to you for reference as well, but you need to implement the
init function to initialise the variables rst before echo can work properly. All the other function bodies
are empty. Your task is to add code into them to process the other calculator commands. Note that
you should not change the main function, but you are free to modify any other parts of the
skeleton code (including adding more functions). You can change echo as well if you wish to change
how a hexadecimal integer is stored in an array.

Subtracting by a Large Hexadecimal Integer
In you are keen, add code to the subtract function to enable subtraction. Similar to `+', the `-' operator
also starts with a variable, e.g., 0", followed by `-' itself. After that there are two cases: a positive (or
zero) hexadecimal constant or another variable. Your code should handle both cases: subtracting by a
constant value, or by a variable (could be the same variable). The result should always be stored in the
starting variable of the command.
You should make use of the code you wrote for the previous stages to complete this stage. You should
remove any leading `0's in the result. A sample execution session of this stage is shown below.
> n0=0x214748364A
> n1=0x0
> n1-0x214748364A
> n0-n1
> n0?
0x428E906C94
> n1-n0
> n1?
-0x63D5D8A2DE
> n1-n1
> n1?
0x0

SKELETON CODE IS BELOW

/* Extended precision hexadecimal integer calculator
* Implements +, -, and * operations
*
*
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define INT_SIZE        101     /* max number of digits (and sign) per integer value */
#define LINE_LEN        106     /* maximum length of any input line */
#define NUM_VARS        10      /* number of different variables */
#define BASE_HEX        16      /* base of hexadecimal system */

#define CH_ZERO         '0'     /* character 0 */

#define POS_SGN         '+'     /* sign for positive number */
#define NEG_SGN         '-'     /* sign for negative number */

#define ASN_OP          '='     /* addition operator */
#define ECH_OP          '?'     /* addition operator */
#define ADD_OP          '+'     /* addition operator */
#define SUB_OP          '-'     /* addition operator */
#define MUL_OP          '*'     /* addition operator */

#define EXIT_CMD        "exit" /* command to exit */
#define PROMPT          "> " /* command prompt */
#define HEXA            "0x"    /* hexadecimal prefix */
#define CMT_FLAG        '%'             /* indicator for comment line */

/* add your own defined constants here */

typedef char longhex_t[INT_SIZE];       /* one large hexadecimal "variable" */

/****************************************************************/

/* function prototypes */
void read_line(char *line, int max_len);
void init(longhex_t vars[], int lens[]);
void echo(longhex_t var, int len);
void assign(longhex_t vars[], int lens[], int index, char *parameter);
void add(longhex_t vars[], int lens[], int index, char *parameter);
void subtract(longhex_t vars[], int lens[], int index, char *parameter);
void multiply(longhex_t vars[], int lens[], int index, char *parameter);

/* add your own function prototypes here */

/****************************************************************/

/* main program controls all the action */
int
main(int argc, char *argv[]) {
        /* DO NOT CHANGE THE MAIN FUNCTION */
      
        char line[LINE_LEN+1];          /* to hold the input line */
        longhex_t vars[NUM_VARS];       /* to hold 10 large hexadecimal integers */
        int lens[NUM_VARS];                     /* to hold the length of the 10 vars */
      
        int index;                                      /* index of the first variable in command */
        char op;                                        /* operator in command */
      
        init(vars, lens);
      
        while (1) {
                printf(PROMPT);                                         /* print prompt */
                read_line(line, LINE_LEN);                      /* read one line of command */

                if (line[0] == CMT_FLAG) {                      /* print comment in the test data */
                        printf("%s ", line);                   /* used to simplify marking */
                        continue;
                }

                if (strcmp(line, EXIT_CMD) == 0) {      /* see if command is "exit" */
                        return 0;
                }
              
                index = line[1] - CH_ZERO;                      /* first var number at line[1] */
                op = line[2];                                           /* operator at line[2] */

                if (op == ECH_OP) {                                     /* print out the variable */
                        echo(vars[index], lens[index]);
                        continue;
                }
              
                /* do the calculation, second operand starts at line[3] */
                if (op == ASN_OP) {   
                        assign(vars, lens, index, line+3);
                } else if (op == ADD_OP) {
                        add(vars, lens, index, line+3);
                } else if (op == MUL_OP) {
                        multiply(vars, lens, index, line+3);
                } else if (op == SUB_OP) {
                        subtract(vars, lens, index, line+3);
                }
        }
      
        /* all done; take some rest */
        return 0;
}

/* read a line of input into the array passed as argument;
* no need to change this function.
*/
void
read_line(char *line, int max_len) {
        int i = 0, c;
        while (((c = getchar()) != EOF) && (c != ' ')) {
                if (i < max_len) {
                        line[i++] = c;
                } else {
                        printf("Invalid input line, toooooooo long. ");
                        exit(0);
                }
        }
        line[i] = '';
}

/* print out a large hexadecimal integer;
* no need to change this function,
* unless you changed the hexadecimal representation.
*/
void
echo(longhex_t var, int len) {
        int i;
      
        /* print sign */
        if (var[0] == NEG_SGN) {
                printf("%c", NEG_SGN);
        }
      
        /* print "0x" prefix */
        printf(HEXA);
      
        /* print the digits in a reverse order */
        for (i = len; i > 0; i--) {
                putchar(var[i]);
        }
      
        printf(" ");
}

/****************************************************************/

/* set the vars array to all zero values */
void
init(longhex_t vars[], int lens[]) {
        /* add your code here */
}

/* process the '=' operator */
void
assign(longhex_t vars[], int lens[], int index, char *parameter) {
        /* add your code here */
}
              
/* process the '+' operator */
void
add(longhex_t vars[], int lens[], int index, char *parameter) {
        /* add your code here */
}

/* process the '*' operator */
void
multiply(longhex_t vars[], int lens[], int index, char *parameter) {
        /* add your code here */
}

/* process the '-' operator */
void
subtract(longhex_t vars[], int lens[], int index, char *parameter) {
        /* add your code here */
}

/****************************************************************/

/* add supporting functions here */

Explanation / Answer

ANS;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/*
* File:   main.cpp
* Author: Dhrub
*
* Created on 16 April, 2016, 3:33 AM
*/

/* Extended precision hexadecimal integer calculator
* Implements +, -, and * operations
*
*
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define INT_SIZE        101     /* max number of digits (and sign) per integer value */
#define LINE_LEN        106     /* maximum length of any input line */
#define NUM_VARS        10      /* number of different variables */
#define BASE_HEX        16      /* base of hexadecimal system */

#define CH_ZERO         '0'     /* character 0 */

#define POS_SGN         '+'     /* sign for positive number */
#define NEG_SGN         '-'     /* sign for negative number */

#define ASN_OP          '='     /* addition operator */
#define ECH_OP          '?'     /* addition operator */
#define ADD_OP          '+'     /* addition operator */
#define SUB_OP          '-'     /* addition operator */
#define MUL_OP          '*'     /* addition operator */

#define EXIT_CMD        "exit" /* command to exit */
#define PROMPT          "> " /* command prompt */
#define HEXA            "0x"    /* hexadecimal prefix */
#define CMT_FLAG        '%'             /* indicator for comment line */

/* add your own defined constants here */

typedef char longhex_t[INT_SIZE];       /* one large hexadecimal "variable" */

/****************************************************************/

/* function prototypes */
void read_line(char *line, int max_len);
void init(longhex_t vars[], int lens[]);
void echo(longhex_t var, int len);
void assign(longhex_t vars[], int lens[], int index, char *parameter);
void add(longhex_t vars[], int lens[], int index, char *parameter);
void subtract(longhex_t vars[], int lens[], int index, char *parameter);
void multiply(longhex_t vars[], int lens[], int index, char *parameter);

/* add your own function prototypes here */

/****************************************************************/

/* main program controls all the action */
int
main(int argc, char *argv[]) {
        /* DO NOT CHANGE THE MAIN FUNCTION */
      
        char line[LINE_LEN+1];          /* to hold the input line */
        longhex_t vars[NUM_VARS];       /* to hold 10 large hexadecimal integers */
        int lens[NUM_VARS];                     /* to hold the length of the 10 vars */
      
        int index;                                      /* index of the first variable in command */
        char op;                                        /* operator in command */
      
        init(vars, lens);
      
        while (1) {
                printf(PROMPT);                                         /* print prompt */
                read_line(line, LINE_LEN);                      /* read one line of command */

                if (line[0] == CMT_FLAG) {                      /* print comment in the test data */
                        printf("%s ", line);                   /* used to simplify marking */
                        continue;
                }

                if (strcmp(line, EXIT_CMD) == 0) {      /* see if command is "exit" */
                        return 0;
                }
              
                index = line[1] - CH_ZERO;                      /* first var number at line[1] */
                op = line[2];                                           /* operator at line[2] */

                if (op == ECH_OP) {                                     /* print out the variable */
                        echo(vars[index], lens[index]);
                        continue;
                }
              
                /* do the calculation, second operand starts at line[3] */
                if (op == ASN_OP) {   
                        assign(vars, lens, index, line+3);
                } else if (op == ADD_OP) {
                        add(vars, lens, index, line+3);
                } else if (op == MUL_OP) {
                        multiply(vars, lens, index, line+3);
                } else if (op == SUB_OP) {
                        subtract(vars, lens, index, line+3);
                }
        }
      
        /* all done; take some rest */
        return 0;
}

/* read a line of input into the array passed as argument;
* no need to change this function.
*/
void
read_line(char *line, int max_len) {
        int i = 0, c;
        while (((c = getchar()) != EOF) && (c != ' ')) {
                if (i < max_len) {
                        line[i++] = c;
                } else {
                        printf("Invalid input line, toooooooo long. ");
                        exit(0);
                }
        }
        line[i] = '';
}

/* print out a large hexadecimal integer;
* no need to change this function,
* unless you changed the hexadecimal representation.
*/
void
echo(longhex_t var, int len) {
        int i;
      
        /* print sign */
        if (var[0] == NEG_SGN) {
                printf("%c", NEG_SGN);
        }
      
        /* print "0x" prefix */
        printf(HEXA);
      
        /* print the digits in a reverse order */
        for (i = len; i > 0; i--) {
                putchar(var[i]);
        }
      
        printf(" ");
}

/****************************************************************/

/* set the vars array to all zero values */
void
init(longhex_t vars[], int lens[]) {
        /* add your code here */
}

/* process the '=' operator */
void
assign(longhex_t vars[], int lens[], int index, char *parameter) {
        /* add your code here */
}
              
/* process the '+' operator */
void
add(longhex_t vars[], int lens[], int index, char *parameter) {
        /* add your code here */
}

/* process the '*' operator */
void
multiply(longhex_t vars[], int lens[], int index, char *parameter) {
        /* add your code here */
}

/* process the '-' operator */
void
subtract(longhex_t vars[], int lens[], int index, char *parameter) {
        /* add your code here */
}

/****************************************************************/

/* add supporting functions here */