Question
Edit the file exp10to2.c and implement exp10ToExp2. Your code should take the struct Number pointed by parameter pn and preserve the value v=mantissa2exp210exp10. The restrictions
Edit the file exp10to2.c and implement exp10ToExp2.
Your code should take the struct Number pointed by parameter "pn" and preserve the value v=mantissa2exp210exp10. The restrictions are as follows:
- mantissa should end up as a value that has bit 63 set. mantissa is a 64-bit unsigned integer.
- exp10 should be 0 when your algorithm is done.
- exp2 can be any integer (signed)
- the represented value v should be preserved as much as possible
File code:
#include
#include
#include
#include
struct Number
{
int sign;
uint64_t mantissa;
int16_t exp10;
int16_t exp2;
};
struct ParseState
{
const char *ptr;
struct Number *pn;
};
int parse(const char *str, struct Number *pn);
int parseSign(struct ParseState *pps)
{
int retValue = 0;
switch (*pps->ptr)
{
case '+':
++pps->ptr; // skip, default sign is fine
break;
case '-':
++pps->ptr; // advance
retValue = 1;
break;
default:
break;
}
return retValue;
}
int isDigit(const char p)
{
return (p >= '0') && (p <= '9');
}
uint64_t parseUnsignedInt(uint64_t n, struct ParseState *pps)
{
while (isDigit(*pps->ptr))
{
n = n * 10 + (*pps->ptr - '0');
++pps->ptr;
}
return n;
}
void parseMantissa(struct ParseState *pps)
{
// after sign, parse the main part
pps->pn->mantissa = 0;
pps->pn->exp10 = 0;
{
const char *oldPtr = pps->ptr;
pps->pn->mantissa = parseUnsignedInt(0,pps);
if (oldPtr != pps->ptr) // pointer advanced
{
if (*pps->ptr == '.')
{
++pps->ptr;
const char *oldPtr = pps->ptr;
pps->pn->mantissa =
parseUnsignedInt(pps->pn->mantissa, pps);
pps->pn->exp10 -= (pps->ptr - oldPtr);
}
}
}
}
int parse(const char *str, struct Number *pn)
{
struct ParseState ps;
int error = 0;
ps.ptr = str;
ps.pn = pn;
pn->exp10 = 0;
pn->exp2=0;
pn->sign = parseSign(&ps);
{
const char *oldPtr = ps.ptr;
parseMantissa(&ps);
if (oldPtr != ps.ptr)
{
if (*ps.ptr == 'e')
{
int expSign;
uint64_t exp = 0;
++ps.ptr;
expSign = parseSign(&ps);
exp = parseUnsignedInt(exp, &ps);
pn->exp10 += (expSign ? -exp : exp);
}
}
else
{
error = 1;
}
}
return error;
}
void exp10ToExp2(struct Number *pn);
void exp10ToExp2(struct Number *pn)
{
// in this function, convert the number pointed to b
// pn from a base-10 mantissa and exponent to a base-2
// mantissa and exponent
// divide into two main cases: when exp10 > 0, and when
// exp10 < 0
}
int main(int argc, const char **argv)
{
const char *numberStr = NULL;
struct Number n;
int error = 0;
--argc; ++argv; // skip name of executable itself
while (!error && argc)
{
if (strcmp(*argv,"-n") == 0)
{
--argc; ++argv;
if (argc)
{
numberStr = *argv;
--argc; ++argv;
}
else
{
fprintf(stderr,"-n should be followed by a number ");
error = 1;
}
}
}
if (!error && numberStr)
{
// no errors encountered
parse(numberStr, &n);
exp10ToExp2(&n);
}
return 0;
}
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