數據結構習題學習筆記(The Fourth Day)

 

 Q1:假設一個算術表達式中包含圓括弧、方括弧和花括弧三種類型的括弧,編寫一個判別表達式中括弧是否正確配對的函數 correct(exptag);其中,exp 爲字符串類型的變量,表示被判別的表達式,tag 爲布爾型變量。

使用棧st進行判斷,將"(","["或"{"入棧,當遇到")","]"或"}"時,檢查當前棧頂元素是否是對應的"(","["或"{"若是則退棧,否則返回表示不配對.

當棧爲空時,表示整個表達式檢查完畢.括號正確配對.

CODE:

// correct.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Maxsize 100 //Maxsize爲算術表達式中最多字符個數

int correct(char exp[]){
   char st[Maxsize];
   int top=0,i=0;
   int tag=1;
   while(exp[i]!='/0' && tag){
    if(exp[i]=='(' || exp[i]=='[' || exp[i]=='{'){//遇到'(','[','{',則將其入棧
       top++;
    st[top]=exp[i];
    }
    if(exp[i]==')'){//遇到')',若棧頂是'(',則繼續,否則不配對返回
     if(st[top]=='(') top--;
     else tag=0;
    }
    if(exp[i]==']'){//遇到']',若棧頂是'[',則繼續,否則不配對返回
     if(st[top]=='[') top--;
     else tag=0;
    }
    if(exp[i]=='}'){//遇到'}',若棧頂是'{',則繼續,否則不配對返回
     if(st[top]=='{') top--;
     else tag=0;
    }
    i++;
   }
   if(top>0)tag=0;//若棧不空,則不配對
   return tag;
}

void main(){
   int tag;
   char exp[] = "{1+2)*[(3+6)*9]}";
   printf("表達式爲:%s/n",exp);
   tag = correct(exp);
   if(tag==0)
    printf("表達式括弧不配對/n");
   else
       printf("表達式括弧配對/n");
}

 

RUN:

Q2:編寫一個函數求逆波蘭表達式的值,其中波蘭表達式是在該函數中輸入的。

使用一個數棧stack,逆波蘭表達式存放在字符型數組exp中,依次掃描逆波蘭表達式,

當遇到運算對象時,就把它壓入數棧stack;

當遇到運算符時,就執行兩次彈出熟棧stack中的數進行運算,再把結果壓入棧;

重複,直到掃描到終止符"#",在棧頂得到表達式的值.

My View:根據前天的轉逆波蘭式的方法改進函數.一般表達式->逆波蘭表達式->求表達式的值.

CODE:

#include "stdafx.h"
#define Maxsize 100/*Maxsize爲算術表達式中最多字符個數*/
char str[Maxsize];//存儲原算術表達式
char exp[Maxsize];//存儲轉換後的波蘭表達式


void trans()
{
 char stack[Maxsize];//作爲棧使用

 char ch;
 int i,j,t,top=0;//t作爲exp下標,top作爲stack下標,i作爲str下標
 i=0;//獲取用戶輸入的表達式

 do
 {
  i++;
  scanf("%c",&str[i]);
 }while((str[i]!='#')&&(i<Maxsize));
 t=0;i=0;
 ch=str[i];i++;
 while(ch!='#')
 {
  if((ch>='0')&&(ch<='9'))//判定爲數字
  {
   exp[t]=ch;t++;
  }else
   if(ch=='(')//判定爲左括號
   {
    top++;
    stack[top]=ch;
   }else
    if(ch==')')//判定爲右括號
    {
     while(stack[top]!='(')
     {
                         exp[t]=stack[top];
       top--;
       t++;
     }
     top--;//將左括號彈出
    }else
     if((ch=='+')||(ch=='-'))//判定爲加減號
     {
      while((top!=0) && (stack[top]!='('))
      {
       exp[t]=stack[top];
       top--;
       t++;
      }
      top++;
      stack[top]=ch;
     }else
      if((ch=='*')||(ch=='/'))//判定爲乘除
      {
       while((stack[top]=='*')||(stack[top]=='/'))
       {
        exp[t]=stack[top];
        top--;
        t++;
       }
       top++;
       stack[top]=ch;
      }
      ch=str[i];//繼續判斷字符
      i++;
 }
 while(top!=0)
 {
  exp[t]=stack[top];
  t++;
  top--;
 }
 exp[t]='#';
    exp[t+1]='/0';
 printf("逆波蘭表達式爲:");
 for(j=0;j<=t;j++)
  printf("%c",exp[j]);
 printf("/n");
}

void compvalue()
{
    //char expCopy[Maxsize];//存儲轉換後的波蘭表達式
    float stack[Maxsize];//作爲棧使用 
 
 char c;
 int i=0,t=0,top=0;//t作爲exp下標,top作爲stack下標,i作爲str下標

 /*do//獲取逆波蘭表達式
 {
  i++;
  expCopy[i]=exp[i];
 }
 while((exp[i]!='#') && (i<Maxsize));*/

 
 c=exp[t];
 t++;
 while(c!='#')
 {
  if((c>='0') && (c<='9'))//判定爲數字字符
  {
   float d=c-'0';//將數字字符轉換爲對應的數值
   top++;
   stack[top]=d;
  }
  else//判定爲運算符
  {
   switch(c)
   {
    case'+':
                 stack[top-1]=stack[top-1]+stack[top];break;
    case'-':
                 stack[top-1]=stack[top-1]-stack[top];break;
    case'*':
                 stack[top-1]=stack[top-1]*stack[top];break;
    case'/':
     if(stack[top]!=0)
                     stack[top-1]=stack[top-1]/stack[top];
     else printf("除零錯誤!/n");
         break;
   }
   top--;
  }
  c=exp[t];
  t++;
 }
    printf("計算結果是:%g/n",stack[top]);
}

void main()
{
 trans();
 compvalue();
}

RUN:

哈哈,閃………………

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章