日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构课程设计之简单计算器的实现

發布時間:2023/12/9 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构课程设计之简单计算器的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、問題陳述

從鍵盤上輸入一算術表達式(中綴白大師),包括圓括號,計算出表達式的值。
要求:

  • 程序對所輸入的表達式作簡單判斷,如有錯給出提示;
  • 實現算術四則運算(+、-、*、/)和平方(^)運算,能處理雙目運算符:+和-;
  • 能將中綴算術表達式轉換成后綴表達式并輸出,并輸出運算結果。
  • 二、概要設計

    2.1 概要簡述

    中綴表達式轉為后綴表達式,從左到右遍歷中綴表達式的每個數字和符號,若是數字就輸出,即成為后綴表達式的一部分;若是符號,則判斷其與棧頂符號的優先級,是右括號或優先級低于找頂符號(乘除優先加減)則棧頂元素依次出找并輸出,并將當前符號進棧,一直到最終輸出后綴表達式為止。
    后綴表達式也稱為逆波蘭表達式,計算方法為:新建一個表達式,如果當前字符為變量或者為數字,則壓棧,如果是運算符,則將棧頂兩個元素彈出作相應運算,結果再入棧,最后當表達式掃描完后,棧里的就是結果。

    2.2 存儲結構表示

    typedef struct {//數字棧結構體double data[MAXSIZE];int top; } opstack;typedef struct {//符號棧結構體char data[MAXSIZE];int top; } stack;

    2.3 詳細設計

    2.3.1 判斷中綴表達式是否合理
    //判斷中綴表達式是否合理 bool judge_infix(char str[]){int temp=0;if(str[0]=='/'||str[0]=='*')return false;if(str[strlen(str)-1]<'0'&&str[strlen(str)-1]>'9')return false;for(int i=0; i<strlen(str); i++) {if(str[i]=='(') {if(i==0&&(str[i+1]=='*'||str[i+1]=='/'))return false;else if(str[i-1]>='0'&&str[i-1]<='9')return false;temp++;} else if(str[i]==')') {if(i==0)return false;else if(str[i-1]=='+'||str[i-1]=='*'||str[i-1]=='-'||str[i-1]=='/')return false;else if(str[i+1]>='0'&&str[i+1]<='9')return false;temp--;}}if(temp==0)return true;return false; }
    2.3.2 中綴表達式轉換為后綴表達式
  • 創建棧

  • 從左向右順序遍歷中綴表達式:

    • 數字直接輸出
    • 運算符:
      遇到‘(’直接入棧,遇到‘)’將棧中‘(’之后入棧的全部輸出,同時‘(’出棧但是不輸出。其他符號將符號棧中的元素依次出棧并輸出,直到遇到比當前符號優先級更低的符號或者‘(’,將當前符號入棧。
  • 將棧中剩余的符號依次輸出。

  • //中綴表達式轉后綴表達式 void TranslateExpress(char str[], char exp[]) {stack s;char ch, e;int i=0, j=0;InitStack(&s);ch=str[i++];while(ch!='\0') {switch(ch) {case '(':Push(&s,ch);break;case ')':while (GetTop(s,&e)&&e!='(') {Pop(&s,&e);exp[j++]=e;}Pop(&s,&e);break;case '+':case '-':while (!StackEmpty(s)&&GetTop(s,&e)&&e!='(') {Pop(&s,&e);exp[j++]=e;}Push(&s,ch);break;case '*':case '/':while(!StackEmpty(s)&&GetTop(s,&e)&&e=='/'||e=='*'||e=='^') {Pop(&s,&e);exp[j++]=e;}Push(&s,ch);break;case '^':while(!StackEmpty(s)&&GetTop(s,&e)&&e=='^') {Pop(&s,&e);exp[j++]=e;}Push(&s,ch);break;case ' ':break;default:while(ch>='0'&&ch<='9') {exp[j++]=ch;ch=str[i++];}i--;exp[j++]=' ';}ch=str[i++];}while(!StackEmpty(s)) {Pop(&s,&e);exp[j++]=e;}exp[j]='\0'; }

    2.3.3 計算后綴表達式值

  • 依次遍歷棧
  • 檢測是否是數字,是數字就壓棧,是操作符從棧中依次取出當前操作數的右操作數和左操作數與當前操作符進行運算,結果壓棧。
  • 遍歷結束,運行結果就是棧頂元素。
  • //計算表達式值 double ComputeExpress(char a[]) {opstack s;int i=0, value;float x1, x2, result;s.top=-1;while (a[i]!='\0') {if(a[i]!=' '&&a[i]>='0'&&a[i]<='9') {value=0;while (a[i]!=' ') {value=10*value+a[i]-'0';i++;}s.top++;s.data[s.top]=value;} else {switch(a[i]) {case '+':x1=s.data[s.top--];x2=s.data[s.top--];result=x1+x2;s.data[++s.top]=result;break;case '-':x1=s.data[s.top--];x2=s.data[s.top--];result=x2-x1;s.data[++s.top]=result;break;case '*':x1=s.data[s.top--];x2=s.data[s.top--];result=x1*x2;s.data[++s.top]=result;break;case '/':x1=s.data[s.top--];x2=s.data[s.top--];result=x2/x1;s.data[++s.top]=result;break;case '^':x1=s.data[s.top--];x2=s.data[s.top--];result=pow(x2,x1);s.data[++s.top]=result;break;}i++;}}if(!s.top!=-1) {result=s.data[s.top];s.top--;if(s.top==-1)return result;else {printf("表達式錯誤!");//exit(-1);}} }

    三、測試與運行

    3.1 判斷表達式是否正確

    3.2 簡單四則運算

    3.3 含平方運算

    四、代碼實現

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAXSIZE 50typedef struct {//數字棧結構體double data[MAXSIZE];int top; } opstack;typedef struct {//符號棧結構體char data[MAXSIZE];int top; } stack;//初始化棧 void InitStack(stack *s) {s->top=0; }//取棧頭 int GetTop(stack s, char *e) {if(s.top<=0)return 0;else {*e=s.data[s.top-1];return 1;} }//出棧操作 void Pop(stack *s, char *e) {if(s->top<=0)printf("棧空!");else*e=s->data[--s->top]; }//入棧操作 void Push(stack *s, char e) {if(s->top>=MAXSIZE)printf("棧滿!");elses->data[s->top++]=e; }//判斷棧空 int StackEmpty(stack s) {if(s.top==0)return 1;else return 0; }//計算表達式值 double ComputeExpress(char a[]) {opstack s;int i=0, value;float x1, x2, result;s.top=-1;while (a[i]!='\0') {if(a[i]!=' '&&a[i]>='0'&&a[i]<='9') {value=0;while (a[i]!=' ') {value=10*value+a[i]-'0';i++;}s.top++;s.data[s.top]=value;} else {switch(a[i]) {case '+':x1=s.data[s.top--];x2=s.data[s.top--];result=x1+x2;s.data[++s.top]=result;break;case '-':x1=s.data[s.top--];x2=s.data[s.top--];result=x2-x1;s.data[++s.top]=result;break;case '*':x1=s.data[s.top--];x2=s.data[s.top--];result=x1*x2;s.data[++s.top]=result;break;case '/':x1=s.data[s.top--];x2=s.data[s.top--];result=x2/x1;s.data[++s.top]=result;break;case '^':x1=s.data[s.top--];x2=s.data[s.top--];result=pow(x2,x1);s.data[++s.top]=result;break;}i++;}}if(!s.top!=-1) {result=s.data[s.top];s.top--;if(s.top==-1)return result;else {printf("表達式錯誤!");//exit(-1);}} }//中綴表達式轉后綴表達式 void TranslateExpress(char str[], char exp[]) {stack s;char ch, e;int i=0, j=0;InitStack(&s);ch=str[i++];while(ch!='\0') {switch(ch) {case '(':Push(&s,ch);break;case ')':while (GetTop(s,&e)&&e!='(') {Pop(&s,&e);exp[j++]=e;}Pop(&s,&e);break;case '+':case '-':while (!StackEmpty(s)&&GetTop(s,&e)&&e!='(') {Pop(&s,&e);exp[j++]=e;}Push(&s,ch);break;case '*':case '/':while(!StackEmpty(s)&&GetTop(s,&e)&&e=='/'||e=='*'||e=='^') {Pop(&s,&e);exp[j++]=e;}Push(&s,ch);break;case '^':while(!StackEmpty(s)&&GetTop(s,&e)&&e=='^') {Pop(&s,&e);exp[j++]=e;}Push(&s,ch);break;case ' ':break;default:while(ch>='0'&&ch<='9') {exp[j++]=ch;ch=str[i++];}i--;exp[j++]=' ';}ch=str[i++];}while(!StackEmpty(s)) {Pop(&s,&e);exp[j++]=e;}exp[j]='\0'; }//判斷中綴表達式是否合理 bool judge_infix(char str[]){int temp=0;if(str[0]=='/'||str[0]=='*')return false;if(str[strlen(str)-1]<'0'&&str[strlen(str)-1]>'9')return false;for(int i=0; i<strlen(str); i++) {if(str[i]=='(') {if(i==0&&(str[i+1]=='*'||str[i+1]=='/'))return false;else if(str[i-1]>='0'&&str[i-1]<='9')return false;temp++;} else if(str[i]==')') {if(i==0)return false;else if(str[i-1]=='+'||str[i-1]=='*'||str[i-1]=='-'||str[i-1]=='/')return false;else if(str[i+1]>='0'&&str[i+1]<='9')return false;temp--;}}if(temp==0)return true;return false; }int main() {char a[MAXSIZE],b[MAXSIZE];double f;int flag=0;printf("請輸入一個算術表達式:");gets(a);printf("中綴表達式為: %s\n",a);bool judge = judge_infix(a);if(judge == false){printf("表達式有誤!\n");system("pause");exit(-1);}elseTranslateExpress(a,b);printf("后綴表達式為: %s\n",b);f=ComputeExpress(b);printf("計算結果為: %f\n",f);system("pause");return 0; }

    總結

    以上是生活随笔為你收集整理的数据结构课程设计之简单计算器的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。