zoukankan      html  css  js  c++  java
  • PTA数据结构与算法题目集(中文) 7-20

    PTA数据结构与算法题目集(中文)  7-20

    7-20 表达式转换 (25 分)
     

    算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。

    输入格式:

    输入在一行中给出不含空格的中缀表达式,可包含+-*以及左右括号(),表达式不超过20个字符。

    输出格式:

    在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

    输入样例:

    2+3*(7-4)+8/4
    

    输出样例:

    2 3 7 4 - * + 8 4 / +
    题目分析:一开始没思路 百度一下就有了。。看了大佬的思路
    一种方法是利用树的想法 将中缀表达式写成树的形式 利用先序遍历可以输出前缀表达式 利用后续遍历可以输出后缀表达式
    另一种方法是利用栈的想法 可以利用栈将中缀表达式转换为前缀 后缀表达式
    https://www.cnblogs.com/zxcjj/p/7793329.html
    先把我写的这个放在这 这个一个测试点都没过 。。
     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<ctype.h>
     5 #define INIFITY -65535
     6 char Stack[100];
     7 int topOfStack = 0;
     8 
     9 void Push(char op)
    10 {
    11     Stack[topOfStack++] = op;
    12 }
    13 
    14 char Pop()
    15 {
    16     char op = Stack[--topOfStack];
    17     return op;
    18 }
    19 
    20 char Top()
    21 {
    22     if (!topOfStack)
    23         return 'I'; //当栈顶无元素时候返回I元素
    24     else
    25         return Stack[topOfStack - 1];
    26 }
    27 
    28 int IsEmpty()
    29 {
    30     return topOfStack == 0;
    31 }
    32 
    33 int Priority(char op)
    34 {
    35     switch (op)
    36     {
    37     case '(':return -1; break;
    38     case')':return 0; break;
    39     case'+':
    40     case'-':return 1; break;
    41     case'*':
    42     case'/':return 2; break;
    43     case'I':return INIFITY; break; //处理栈顶无元素的情况
    44     }
    45 }
    46 
    47 int postFix(char Begin[],char End[],int Length)
    48 {
    49     int i = 0;
    50     int TrueLength = Length;
    51     for (int j = 0; j < Length; j++)
    52     {
    53         if (isdigit(Begin[j])||Begin[j]=='.')
    54             End[i++] = Begin[j];
    55         else if (Begin[j] == '(')
    56             Push(Begin[j]);
    57         else if (Begin[j] == ')')
    58         {
    59             while (Top() != '(')
    60                 End[i++] = Pop();
    61             Pop();
    62             TrueLength = TrueLength - 2;
    63         }
    64         else
    65         {
    66             while (1)
    67             {
    68                 if (Priority(Begin[j]) > Priority(Top()))
    69                 {
    70                     Push(Begin[j]);
    71                     break;
    72                 }
    73                 else
    74                     End[i++] = Pop();
    75             }
    76         }
    77     }
    78     while (!IsEmpty())
    79     {
    80         End[i++] = Pop();
    81     }
    82     return TrueLength;
    83 }
    84 
    85 int main()
    86 {
    87     char Begin[21] = { 0 }, End[21] = { 0 };
    88     scanf("%s", Begin);
    89     int Length=strlen(Begin);
    90     int TrueLength=postFix(Begin, End, Length);
    91     for (int i = 0; i < TrueLength - 1; i++)
    92             printf("%c ", End[i]);
    93     printf("%c", End[TrueLength - 1]);
    94     return 0;
    95 }
    View Code

    接下来是别人的写法

    https://www.cnblogs.com/yuxiaoba/p/8399934.html

  • 相关阅读:
    Bootstrap3系列:按钮式下拉菜单
    Bootstrap3系列:按钮组
    Bootstrap3系列:下拉菜单
    CSS系列:CSS常用样式
    Entity Framework中使用IEnumerable<T>、IQueryable<T>及IList<T>的区别
    ASP.NET中Session的sessionState 4种mode模式
    ASP.NET MVC系列:Area
    Sql Server系列:SQL语句查询数据库中表、视图、存储过程等组成
    ASP.NET MVC系列:Model
    jQuery LigerUI系列:ligerComboBox
  • 原文地址:https://www.cnblogs.com/57one/p/11628894.html
Copyright © 2011-2022 走看看