Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your task is to develop a large hexadecimal integer calculator using C that works with hexadecimal integers of up to 100 digits (plus a sign).

Your task is to develop a large hexadecimal integer calculator using C 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 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 first 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. 2 Stage 1 - Reading in a Large Hexadecimal Integer (Up to 3 Marks) Your first task is to understand the skeleton code. Then start with implementing the `=' operator. Note the use of the type longhex_t in the skeleton code. Each of the longhex_t variables in the array vars stores one large hexadecimal integer, with 10 variables available in total, named 0", 1", ... 9", respectively. These variables should be initialised to 0. In this stage you do not need to consider negative numbers. You can just put a `+' character as the rst character in the array of any hexadecimal integer. You need to add code to the following functions: The init function to initialise the variable values to be 0. The assign function to enable assigning a constant value to a variable, or the value of a variable to another variable (both could be the same variable, e.g., 0=n0"). A sample execution session of this stage is shown below. > n0? 0x0 > n0=0x2147483647 > n0? 0x2147483647 > n1=n0 > n1? 0x2147483647 You should plan carefully, rather than just leaping in and starting to edit the skeleton code. Then, before moving through the rest of the stages, you should test your program thoroughly to make sure its correctness. Stage 2 - Adding up Two Large Hexadecimal Integers (Up to 8 Marks) Now add code to the add function to enable adding up two large hexadecimal integers. Note that the `+' operator always starts with a variable, e.g., 0", followed by `+' itself. After that there are two cases: a hexadecimal constant or another variable. Your code should be able to handle both cases: adding up a variable with a constant value, and adding up two variables (both could be the same variable). The sum should always be stored in the starting variable of the command. In this stage you do not need to consider negative numbers. You can just put a `+' character as the rst character in the array of any hexadecimal integer. A sample execution session of this stage is shown below. > n0=0x2147483647 > n0+0x1 > n0? 0x2147483648 > n0+0x2 > n0? 0x214748364A > n1=0x100000000000000000000000000000000000000000000000000 > n1+n0 > n1? 0x10000000000000000000000000000000000000000214748364A > n0? 0x214748364A Note that in this assignment we assume hexadecimal integers with up to 100 digits. They can still over flow if we are adding up really large numbers. In this case, we will simply ignore the over flowed digits and keep the remaining part in the sum. You should also remove any leading `0's in the sum, e.g., your program should not produce numbers like 0x0001 (should be 0x1).

SKELETON CODE IS AS FOLLOWS

/* Extended precision hexadecimal integer calculator * Implements +, -, and * operations * * */ #include #include #include #include #include #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] = '\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(" "); } /****************************************************************/ /* 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 */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxiv Special Issue On Database And Expert Systems Applications Lncs 9510

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Hendrik Decker ,Lenka Lhotska ,Sebastian Link

1st Edition

366249213X, 978-3662492130

More Books

Students also viewed these Databases questions

Question

What risks do you feel P&G will face going forward?

Answered: 1 week ago