建议读者先自行思考,代码仅供参考:
#include<stdio.h>
#include<math.h>
typedef struct node {
char data[1000];
int tptr;
}stack;
void push(stack* a, int tptr, char str)
{
a->data[tptr] = str;
}
void printstack(stack* a)
{
while (a->tptr >= 0)
{
printf("%c", a->data[a->tptr]);
a->tptr--;
}
}
int main()
{
stack two_stack;
stack eight_stack;
two_stack.tptr = 0;
eight_stack.tptr = 0;
char t;
while ((t = getchar()) != '#')
{
push(&two_stack, two_stack.tptr, t);
two_stack.tptr++;
}
int cent = 3;
int number = 0;
two_stack.tptr--;
while (two_stack.tptr >= 2)
{
cent = 3;
number = 0;
while (cent > 0)
{
number = number + (two_stack.data[two_stack.tptr]-48) * pow(2, 3 - cent);
cent--;
two_stack.tptr--;
}
push(&eight_stack, eight_stack.tptr, number % 8 + 48);
eight_stack.tptr++;
}
number = 0;
cent = 3;
if (two_stack.tptr > 0)
{
while (two_stack.tptr >= 0)
{
number = number + (two_stack.data[two_stack.tptr] - 48) * pow(2, 3 - cent);
two_stack.tptr--;
cent--;
}
push(&eight_stack, eight_stack.tptr, number % 8 + 48);
eight_stack.tptr++;
}
eight_stack.tptr--;
printstack(&eight_stack);
return 0;
}