Question
YOU NEED TO Use C program to create a hexadecimal calculator to do the equality and addition The Code you need to add things to
YOU NEED TO Use C program to create a hexadecimal calculator to do the equality and addition
The Code you need to add things to run!!!
#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);
}
}
/* 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 != ' ') && (c != ' ')) {
if (i
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 */
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started