首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

数制变换 (九度oj题目44)

2013-03-06 
数制转换(九度oj题目44)题目#include stdio.h#include stdlib.h#include string.h#define MAX 200st

数制转换 (九度oj题目44)
题目

#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX 200struct stack{char data[MAX];int top;};long int switchToTen(int a, char *s);int main(){long int a, b, n;char str[MAX];while(scanf("%ld %s %ld", &a, str, &b) != EOF){n = switchToTen(a, str);if(n == 0){printf("0");}else{int temp;struct stack *s = (struct stack *)malloc(sizeof(struct stack));s->top = 0;while(n){temp = n % b;if(temp >= 0 && temp <= 9){s->data[s->top ++] = temp + '0';}else{s->data[s->top ++] = temp - 10 + 'A';}n /= b;}while(s->top){printf("%c", s->data[-- s->top]);}}printf("\n");}return 0;}long int switchToTen(int a, char *s){int i, b;long int n, c;for(n = 0, c = 1, i = strlen(s) - 1; i >= 0; i --){if(s[i] >= 'A' && s[i] <= 'Z')b = s[i] - 55;else if(s[i] >= 'a' && s[i] <= 'z')b = s[i] - 87;elseb = s[i] - '0';n += b * c;c *= a;}return n;}


热点排行