Question

Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of...

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 \variables" (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 \reverse 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 \n0=+0x0", \n0=+0x123A", \n0++0x123A", or \n0*+0x123A"
in the input. It will just be \n0=0x0", \n0=0x123A", \n0+0x123A", or \n0*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., \n0", 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\n", 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 != '\n')) {
                if (i < max_len) {
                        line[i++] = c;
                } else {
                        printf("Invalid input line, toooooooo long.\n");
                        exit(0);
                }
        }
        line[i] = '\0';
}

/* 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("\n");
}

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

/* 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 */

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • I have a question on an assignment for C++. I have my code available here and...

    I have a question on an assignment for C++. I have my code available here and just want someone to look over it using MS VS 2010 express, for that is what my instructor is using. Write a program that mimics a calculator. The program should take as input two integers and the opreation to be performed. It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message.) //**This...

  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • In this assignment, you must complete the 5 functons below. Each function's body must consist of...

    In this assignment, you must complete the 5 functons below. Each function's body must consist of just a single C statement. Furthermore, the statement must not be a control statement, such as if, switch, or any kind of loop. For each problem, you are also restricted to the operators and constants as specified. */ /* 1. Write a function which zeros a variable x. A pointer to x is passed as a parameter. YOU MAY USE NO CONSTANTS in this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT