Hello. I train in with ++ (not pure), I write the program. Must translate words into numbers (and a-1, b-2, z-26, aa-27 ...) and vice versa. In one direction it works perfectly, and in the other it bangs on any combination of numbers, when translating which into letters the letter z is present. What is the mistake and how can it be fixed?

/* Numerically Speaking */ #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct Big{ int len; int num[50]; }; Big number; Big letter; Big sumn(Big *a, int n, int base) { Big res = *a; int pos=0; int i=0; res.num[pos]+=n; for(i=0; i<=res.len; i++) { if(res.num[i]>base) { res.num[i+1]+=res.num[i]/base; res.num[i]%=base; } } if (res.num[res.len]>0) res.len++; return res; } Big multi(Big *a, int b, int base) { Big res=*a; int r = 0, i; for (i=0;i<res.len || r != 0;i++) { res.num[i] = res.num[i] * b + r; r = res.num[i]/base; res.num[i]%=base; } if (i > res.len) res.len = i; return res; } void let2num() { for(int i=letter.len-1; i>=0; i--) { number=multi(&number, 26, 10); number=sumn(&number, letter.num[i],10); number=multi(&number, 1, 10); } } void num2let() { for(int i=number.len-1; i>=0; i--) { letter=multi(&letter, 10, 26); letter=sumn(&letter, number.num[i], 26); } } int main() { char buf[1024]; int x; while(scanf("%s", buf)==1) { if (!strcmp(buf, "*")) break; if (buf[0]>='0' && buf[0]<='9') { number.len = strlen(buf); for(int i=0; i<number.len; i++) { number.num[i] = buf[number.len-i-1]-'0'; } num2let(); } else { letter.len = strlen(buf); for(int i=0; i<letter.len; i++) { letter.num[i] = buf[letter.len-i-1]-'a'+1; } number.len = 1; let2num(); } for(int i=letter.len-1; i>=0; i--) { printf("%c", ('a'+letter.num[i])-1); } printf("\t"); for(int i=number.len-1; i>=0; i--) { printf("%c", '0'+number.num[i]); if (i && (i%3==0)) { printf(","); } } printf("\n"); memset(&number, 0, sizeof(Big)); memset(&letter, 0, sizeof(Big)); } } 
  • Have you tried to run your program under a debugger? Why? Visual studio has a nice debugger built in. - VladD
  • Naturally tried. And more than once. But I just can’t understand why the meaning of the letter "z" -26 does not want to remain. - Xerocry
  • I have at z outputs 26. Can you give an example of input, output and the expected correct output? By the way, you have no s ++. And almost pure C (from c ++ it is used only what can be written in a loop for(int i and not to write a struct when declaring variables). - KoVadim
  • Any number of z is greater than 2. 18278 (zzz), 702 (zz) and so on, as well as 35779 (azxc) and similar. - Xerocry 2:21 pm
  • Well, if you debugged, in which line is the computed value not what you expect? What are the other variables equal in the same line? (By the way: sscce.org ) - VladD

1 answer 1

Most likely in this line in vain you add one:

 letter.num[i] = buf[letter.len-i-1]-'a'+1; 

This is indirectly confirmed by the fact that the program gives the wrong numerical result. It is easy to calculate that zz should be equivalent to 676 (25 * 26 + 25), and you get 702 (26 * 26 + 26). Your letters are, in fact, numbers in the 26-number system. Obviously, they should be in the range from 0 to 25. If you want the resulting numbers to start with one (a = 1, aa = 27), then one should be added to the result.