B
You're very strange trying to separate functionality between functions. If the function is transform() the symbols shall be converted intermediately to number, but the data entry function shall not be attached. Structural data entry is well managed scanf()♪ It's not enough that she's got the opportunity to celebrate her memory under the line.%a) If you don't want to have a memorial (also with help) scanf()(d) for sedative processing getchar() may be summoned from an external function, giving the result inwards transform()♪ Leave the functions transform() It's just her actions.For some reason, instead of being in a classroom. decimal() Transfer the indicator to the current position in the area being processed, you transfer the address to the beginning of the mass, and the index (in fact, the index) goes to your global variable. Yeah, and actually, it's not really necessary, but it's just running out of the glass.Global variables are evil. Data coupling between functions should be zero, unless vital. The non-resident input parameters of the function are evil. All data transmission should be made through arguments.
Otherwise, in the future (with a more serious code and a larger scale of the project), the huge problems of the class of haemorrho are inevitable.It's not enough in your code that these problems are present right now, that's the function. transform() Based on the assumption that j At the beginning, it's 1, but if someone calls transform() several times, especially after the challenge decimal()- Categoryfractional()at the entrance. tranform() variable j It'll have a waste.And you don't have NIGDE's control way out of the range. Array_F! It guarantees problems. It's, in fact, the previous one - probably the root of your problems. In addition, no printed but non-white symbols (problems, prepinning signs other than point, etc.) shall be processed.It's not clear why you need a function. decimal()if it actually supplements functionality transform()♪ It does not translate into a decimal place, but simply transforms a set of separate figures from the arbitrary computation system created by the function. transform() in normal type float♪ It's not decimal. IEE754. It would be logical to combine all these three functions into one.In general, I would have completely rewritten this code around:#include <stdio.h>
#include <ctype.h>
#include <math.h>
double transform_char(double in, char c, int base, int fractional_position)
{
int i;
if(isalnum(c)) {
if(isdigit(c)) {
i = c - '0';
}
else if(isalpha(c)) {
if(isupper(c)) {
i = c - 'A' + 10;
}
else {
i = c - 'a' + 10;
}
}
if(i > base - 1) {
return -NAN;
}
if(!fractional_position) {
in *= base;
in += i;
}
else {
double frac = in - (int)in;
in -= frac;
frac += (double)i / pow(base, (double)fractional_position);
in += frac;
}
}
return in;
}
This function is synonymous with the line. For the whole part of the parameter fractional_position must be zero. Once the point symbol in the flow is detected, a parameter shall be set in value 1 and further incremented after each of the following design symbols:#include <stdbool.h>
#include <stdlib.h>
...
int input_base;
double res = 0.0;
char c = 0;
bool fraction = false;
int pos = 0;
... // Инициализация input_base где-то тут
while((c = getchar()) != '\n') {
int i;
printf("%c: %g -> ", c, res);
if(isalnum(c)) {
res = transform_char(res, c, input_base, pos);
if(isnan(res)) {
fprintf(stderr, "Invalid digit %c for base %d\n", c, input_base);
break;
}
if(pos) pos++;
}
else if('.' == c) {
fraction = true;
pos = 1;
}
else {
fprintf(stderr, "Invalid character %c\n", c);
return 1;
}
printf("%g\n", res);
}
I draw your attention to the fact that the reverse transformation is a more complex task. Although your restriction of 13 symbols makes it very easier. Decisions are set out, for example, here: https://stackoverflow.com/questions/7228438/convert-double-float-to-string Please also note that my code (as yours) cannot handle negative numbers, but it's easy to correct.