【C语言做题系列】Basically Speaking

The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:
It will have a 7-digit display.

Its buttons will include the capital letters A through F in addition to the digits 0 through 9.

It will support bases 2 through 16.
Input
The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the end of file is reached.
Output
The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is to large to appear on the display, then print "ERROR’’ (without the quotes) right justified in the display.
Sample Input
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15
Sample Output
120
78
1765
7CA
ERROR
11001
12D687
D071
题意:
每行输入三个数字。第一个数字将是您要转换的基数中的数字。第二个数字是你要转换的基数。第三个数字是要转换成的基数。然后根据样例输出转换过后的数。

注意:
题目有特别说明,当位数大于7的时候,要输出“ERROR”。

算法思想:
其实题目大意应该都能明白,不过操作起来可能确实有点麻烦。有很多小细节需要注意。下面放的代码会详细说明。

上代码:

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 1e5+5;
char a[maxn];          //首先我们定义了两个字符数组,一个装输入的,一个装转换后的
char b[maxn];          //有题目我们知道,输入的数字中可能包含字母,所以我们定义的是字符类型而不是整型
int change(char c)     //定义一个将字符转换成数字的函数
{
    if(c=='A')
        return 10;
    else if(c=='B')
        return 11;
    else if(c=='C')
        return 12;
    else if(c=='D')
        return 13;
    else if(c=='E')
        return 14;
    else if(c=='F')
        return 15;
    else
        return c-'0';
}
int change1(int n)    //定义一个将数字转换成字符的函数
{
    if(n==10)
        return 'A';
    else if(n==11)
        return 'B';
    else if(n==12)
        return 'C';
    else if(n==13)
        return 'D';
    else if(n==14)
        return 'E';
    else if(n==15)
        return 'F';
    else
        return n+'0';
}
int pow(int n, int len)    //此处是阶乘函数
{
    int i;
    int sum = 1;
    for(i=1; i<=len; i++)
    {
        sum*=n;
    }
    return sum;
}

int main()
{
    int basc1,basc2;           //定义两个基数
    while(scanf("%s", a)!=EOF)
    {
        scanf("%d%d", &basc1, &basc2);
        int len = strlen(a);   //找出输入字符数组a的长度,方便后续操作
        int i;
        int sum = 0;           //通过转换后的数字可以计算出当 为十进制时 的和

        for(i=0; i<len; i++)
        {
            sum+=change(a[i])*pow(basc1, len-1-i);    //此处需要好好理解
        }
        int r;
        int k = 0;
        while(sum)                    //通过不断取余得出转换后的结果
        {
            r=sum%basc2;
            b[k++]=change1(r);
            sum/=basc2;
        }
        if(k>7)                       //判断宽度是否大于7,如果大了便输出ERROR
        {
            printf("  ERROR\n");
            continue;                 //此处需要continue
        }

        for(i=1; i<=7-k; i++)          //样例输出中为右对齐,所以先要输出空格
            printf(" ");
        for(i=k-1; i>=0; i--)           //通过倒叙输出便是最终结果
            printf("%c", b[i]);
        printf("\n");
    }
}

此处再次强调几个复杂的点:
**1、
change函数里“return c -‘0’ ”返回的是int类型
change1函数里“return c +‘0’ ”返回的是char类型
2、

for(i=0; i<len; i++)
        {
            sum+=change(a[i])*pow(basc1, len-1-i);   
        }

此处将a[i]经过change函数转换后再通过阶乘计算,把总和加入到sum中
3、
我们把十进制数转换成其他进制数的时候,都是不断除以这个基数然后取余,最后倒叙一遍。所以要好好理解如下代码:

 		int r;
        int k = 0;
        while(sum)                    //通过不断取余得出转换后的结果
        {
            r=sum%basc2;
            b[k++]=change1(r);
            sum/=basc2;
        }

心得:
遇到此类问题切莫慌张,细节处理需要仔细处理,同时要记得易错点1的字符与数字的转换方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that! Basically, to convert an infix expression to postfix expression using C language, we can use the stack data structure. Here's the basic algorithm: 1. Create a stack and push a left parenthesis '(' onto it. 2. Scan the infix expression from left to right. 3. If the current token is an operand, append it to the postfix expression. 4. If the current token is a left parenthesis, push it onto the stack. 5. If the current token is a right parenthesis, pop operators from the stack and append them to the postfix expression until a left parenthesis is found. Discard the left parenthesis. 6. If the current token is an operator, pop operators from the stack and append them to the postfix expression until an operator with lower precedence or a left parenthesis is found. Push the current operator onto the stack. 7. After the infix expression is scanned, pop any remaining operators from the stack and append them to the postfix expression. And here's an example C program that implements this algorithm: #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_EXPR_LEN 100 #define MAX_STACK_SIZE 100 int is_operator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } int get_precedence(char c) { switch (c) { case '+': case '-': return 1; case '*': case '/': return 2; default: return 0; } } void infix_to_postfix(char *infix_expr, char *postfix_expr) { int len = strlen(infix_expr); char *stack = malloc(MAX_STACK_SIZE * sizeof(char)); int top = 0; int i, j; stack[top] = '('; j = 0; for (i = 0; i < len; i++) { char c = infix_expr[i]; if (c == ' ') { continue; } else if (isdigit(c)) { postfix_expr[j++] = c; } else if (is_operator(c)) { while (get_precedence(c) <= get_precedence(stack[top]) && stack[top] != '(') { postfix_expr[j++] = stack[top--]; } stack[++top] = c; } else if (c == '(') { stack[++top] = c; } else if (c == ')') { while (stack[top] != '(') { postfix_expr[j++] = stack[top--]; } top--; } } while (top > 0) { postfix_expr[j++] = stack[top--]; } postfix_expr[j] = '\0'; free(stack); } int main() { char infix_expr[MAX_EXPR_LEN]; printf("Please enter the infix expression: "); fgets(infix_expr, MAX_EXPR_LEN, stdin); infix_to_postfix(infix_expr, infix_expr); printf("The postfix expression is: %s\n", infix_expr); return 0; } And here's an example of how to use the program: Please enter the infix expression: (1+2)*3-4/5 The postfix expression is: 12+3*45/-

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值