C语言基于栈实现二进制转十进制
程序员文章站
2022-07-15 10:39:59
...
#include <stdio.h>
char stack[100];
int top = 0;
int cf(int n)
{
int t=1;
for (int i = 0; i < n; i++)
{
t *= 2;
}
return t;
}
int main()
{
char s[100];
scanf("%s",&s);
int i = 0;
while (s[i])
{
stack[top++] = s[i];
i++;
}
int sum=0;
int t;
i = 0;
while(top>0)
{
t = (int)stack[--top] - 48;
printf("%d\n", t);
sum += t*cf(i);
i++;
}
printf("%d\n", sum);
return 0;
}
上一篇: 40、数组中只出现一次的数字
下一篇: 【学亮IT手记】枚举类入门实例讲解