Question
this this the format code for part 2: /* Safe integer calculator - warns if an overflow or underflow error occurs. Menu Negation Safe unsigned
this this the format code for part 2:
/*
Safe integer calculator - warns if an overflow or
underflow error occurs.
Menu
Negation
Safe unsigned ops
Safe signed ops
Arithmetic without + - * / % ++ or --
Code the functions below and uncomment the code in main()
when completed.
Code the functions in order as they appear. Subsequent
functions depend on previous functions. You cannot use
any math operators (+ - * / % ++ or --) except simple
assignment (=). However, you may use relational and
logical operators.
The _add() function should only use bitwise operators. All
other functions can call functions necessary to complete the
required operation. I added some hints above functions.
*/
#include
// Prototypes
int _add(int a, int b);
int add(int a, int b);
int sub(int a, int b);
int neg(int a);
int mul(int a, int b);
int div(int a, int b);
int mod(int a, int b);
int pow(int a, int b);
int convert(char *input);
// Main
int main(int argc, char *argv[]){
int res = 0; // Cumulative result - running total
int n = 0; // For number conversion from input string
char input[50]; // Input string
input[0] = '\0'; // Put null in operator char so loop works
// Write code here to test your functions
// Uncomment code below when done
// Loop until quit is selected
/* while(input[0] != 'q' && input[0] != 'Q'){
// Show menu choices
menu();
// Print prompt with running total
printf(" res = %d > ", res);
// Get input string
gets(input);
// Clear screen
system("cls");
// Switch on operator char input[0]
switch (input[0]){
case '+':
res = add(res, convert(input));
break;
case '-':
res = sub(res, convert(input));
break;
case '*':
res = mul(res, convert(input));
break;
case '/':
res = div(res, convert(input));
break;
case '%':
res = mod(res, convert(input));
break;
case '~':
res = neg(res);
break;
case '^':
res = pow(res, convert(input));
break;
case 'c':
case 'C':
res = 0;
break;
case 'q':
case 'Q':
printf("Good-bye! ");
break;
default:
printf("Enter a valid operator and operand ");
}
}
*/
return 0;
}
// Show menu choices
void menu(){
printf(" Safe Integer Calculator ");
printf("+ x to add ");
printf("- x to subtract ");
printf("* x to multiply ");
printf("/ x to divide ");
printf("%% x to modulus ");
printf("~ x to negate ");
printf("^ x to raise by power x ");
printf("c x to clear result ");
printf("q x to quit ");
return;
}
/*
This function should only use bitwise operators and
relational operators
*/
// Add operation using only bitwise operators
int _add(int a, int b){
// Loop until b is zero
// Find carry 1 bits - a AND b assign to carry
// Find non carry 1 bits - a XOR b assign to a
// Multiply carry by 2 by shift and assign to b
return a;
}
/*
Safe add() should call _add() and check for both
overflow and underflow errors.
*/
// Safe add operation
int add(int a, int b){
// Declare int for result
int res = 0;
// Call to _add() a and b and assign to result
// Check for overflow - look at page 90 in book
// Check for underflow - look at page 90 in book
return res;
}
/*
Negate a by using a bitwise operator and safe add().
Look on page 95 in book.
Replace the zero with an expression that solves this.
*/
// Define negation with ~ and safe add
int neg(int a){
// Return negation of a and add 1
return 0; // Replace 0 with code
}
/*
Remember that subtraction is the same as addition
if you negate one of the operands.
Replace the zero with an expression that solves this.
*/
// Define safe subtract by safe add - negate b
int sub(int a, int b){
return 0; // Replace 0 with code
}
/*
Safe mul() uses an iterative call to safe add()
to calculate a product. Remember that
5 x 4 = 5 + 5 + 5 + 5 = 20
*/
// Define safe multiply by calling safe add b times
int mul(int a, int b){
// Declare and initialize cumulative result
int res = 0;
// Declare sign of product - initially assume positive
// For efficiency - smaller number should be multiplier
// Absolute value of a and flip sign
// Absolute value of b and flip sign
// Accumulate result
// Set sign to output
return res;
}
/*
Safe div() repeatedly subtracts b from a, counting the
number of subtractions until a
*/
// Define safe divide by calling safe subtract b times
int div(int a, int b){
// Declare int to count how many times can b be subtracted from a
int cnt = 0;
// Declare sign
// Absolute value of a and flip sign
// Absolute value of b and flip sign
// loop to calculate how many times can b be subtracted from a
// Set sign to output
return cnt;
}
/*
Safe mod() repeatedly subtracts b from a until a
*/
// Define safe modulus by calling safe subtract
int mod(int a, int b){
// Absolute value of a
// Absolute value of b
// Find remainder by repeated subtraction a - b
return a;
}
/*
Safe pow() calculates as the math pow function but
only uses the safe operations.
res = n^exp
Loop until exp is zero
res = res * n
exp = exp - 1
Remember the special case for n^0
*/
// Define safe pow by calling safe multiply exp times
int pow(int n, int exp){
// Declare int for result of n^exp
int res = 0;
// Loop and multiply to calculate n^exp
return res;
}
/*
This function extracts the integer value from the input string.
If input = "+ -123", res = -123.
If input = "* 987654", res = 987654.
The best way to solve complicated problems is to work them out
on paper first.
*/
// Extract the integer from the input string and convert to int
int convert(char *input){
// Declare int for result extracted from input
int res = 0;
// Declare int for sign of result
// Declare two iterators
// Declare a buffer for numeric chars
// Set error to zero - no error found yet
// Check for space in element 1
// Check for negative integer at element 2
// Loop to copy all numeric chars to buffer
// i is iterator for input string and should start at first numeric char
// j is iterator for buffer where numeric chars are copied
// This must test for chars between 0 and 9
// i gets position of last numeric char in buffer
// j is now used for pow function - start at zero
// Construct integer from buffer using pow j increases and i decreases
// Set sign for output
return res;
}
please. complete format of the code i have given and please just do the part 2 and show the output, thank you
this is C
Part 1: Converting data types (50 points) In this project, you will write code to convert strings containing hexadecimal. Decimal, octal and binary strings to an unsigned integer. Some of the code is completed for you. Open the Convert source code file and look at the completed code and comments. The description and comments in each function should help you complete this project. To convert from hex string to unsigned int, accumulate the position-wise values of each character in the string. Remember that you can perform math operations on char data because they are numbers and have specific positions in the ASCII table. This method is like the one shown in lecture. Convert 0xFFFF to a unsigned char, considering that F = 15 and each hex char a multiple of 16: 15*1+15*16+15*256+15*4096=65,535 This process is similar with decimal, octal and binary. To from an unsigned int 16,535 convert to hex: 1. 65,535 / 16 = 4095 and 65,535 % 16 = 15 2. 4,095 / 16 = 255 and 4095 % 16 = 15 3. 255 / 16 = 15 and 255 % 16 = 15 4. 15/16 = 0 and 15 % 16 = 15 Collect remainders for the hex values in reverse of these steps. 15 15 15 15 -> 0xFFFF This is a similar process for base-10, base-8 and base-2 conversions. Part 2: A safe calculator (100 points) In this project, you will write a safe signed integer calculator. As with Part 1, some of the code is completed for you and there are descriptions and comments to help. You should not use any arithmetic operators, except simple assignment, or increment and decrement operators in your code. The _add(), an add function that only uses bitwise operators, and add(), a safe add function that detects and reports overflow and underflow errors, are crucial to the other functions. The seven remaining functions either directly or indirectly depend on _add() and add(). Code these functions to in their order to maintain functional dependencies. Use the main function to test each function before continuing to the next function. 1. Negation is defined using a bitwise operator and the add() function 2. Subtraction is defined using Negation and add() functions 3. Multiplication is defined with successive add() function and Subtraction function calls a. Should perform with positive integers and keep track of which sign the product has a. 4. Division is defined with successive Subtraction and add() function calls Should perform with positive integers and keep track of which sign the result has 5. Modulus is defined as successive calls to Subtraction to return a remainder a. Should perform with positive integers 6. Power is defined as successive calls to Multiply and Subtraction a. Only use positive exponents 7. Convert converts an input string to an integer value a. Pay attention to comments - This is the longest function b. You may be able to modify your dec_to_uint() from
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