Question: Instructions (in C language) Edit the file exp10to2.c and implement exp10ToExp2. Your code should take the struct Number pointed by parameter pn and preserve the

Instructions (in C language)

Edit the file exp10to2.c and implement exp10ToExp2.

Your code should take the struct Number pointed by parameter "pn" and preserve the value . 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

In the following editor area, indicates the following when your code completes

  • mantissa (in base-10)
  • exp2 (in base-10)

To run the program with your specific test input, do the following in CodeBlocks:

  • put a break point at the end of exp10ToExp2, you can click slightly to the right of the line number to toggle breakpoint on a line.
  • In the "Project" menu, click "set programs' arguments"
  • In Program Arguments, enter the following without quotes:"-n 1.23e-10" (obviously change the floating point number to the one that you should use), click OK
  • when the program stops at the break point, you can examine the expressing *pn
  • In the Debugger pane, you can type the following command without the quotes "print *pn"

-------------------------------------------------------------------------

number.h

#ifndef NUMBER_H #define NUMBER_H #include struct Number { int sign; uint64_t mantissa; int16_t exp10; int16_t exp2; };

#endif

-------------------------------------------------------------------------

parse.h

#ifndef PARSE_H #define PARSE_H #include #include "number.h"

struct ParseState { const char *ptr; struct Number *pn; };

int parse(const char *str, struct Number *pn);

#endif

-------------------------------------------------------------------------

exp10to2.h

#ifndef EXP10TO2 #define EXP10TO2 #include "number.h"

void exp10ToExp2(struct Number *pn);

#endif

-------------------------------------------------------------------------

main.c

#include #include #include #include

#include "parse.h" #include "exp10to2.h"

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; }

-------------------------------------------------------------------------

parse.c

#include "parse.h"

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; }

-------------------------------------------------------------------------

exp10to2.c

#include "number.h" #include "exp10to2.h"

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 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!