国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

簡(jiǎn)易的四則運(yùn)算

2019-11-17 05:47:05
字體:
供稿:網(wǎng)友
這個(gè)程序自己寫的有些局限,可惜只能對(duì)0~9的四則運(yùn)算起作用,我正在對(duì)它進(jìn)行改進(jìn),希望各位能提出好的意見,謝謝!!
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#define maxism 15
typedef strUCt stack
{
char stack[maxism];
int top;
}stack;
stack *L;
char after[maxism];
void initialstack(stack *T)

void push(stack *T,int x)

void change()
{
int i=0,j,m=0;
char str[maxism],ch;
gets(str);
i++;
ch=str[i];
initialstack(L);
while(ch!='#')
{
    switch(ch)
    {
        case'(':
           L->top++;
           L->stack[L->top]=ch;
           break;
        case')':
           while(L->stack[L->top]!='(')
           {
              after[m]=L->stack[L->top];
              L->top--;
              m++;
           }
           L->top--;
           break;
        case'+':
        case'-':
           while(L->top!=0&&L->stack[L->top]!='(')
           {
              after[m]=L->stack[L->top];
              L->top--;
              m++;
           }
           L->top++;
           L->stack[L->top]=ch;
           break;
        case'*':
        case'/':
           while(L->stack[L->top]=='*'L->stack[L->top]=='/')
           {
              after[m]=L->stack[L->top];
              L->top--;
              m++;
           }
           L->top++;
           L->stack[L->top]=ch;
           break;
        case' ':
           break;
        default:
           if(ch>='0'&&ch<='9')
           {
              after[m]=ch;
              m++;
           }
           else
           {
              printf("error! ");
              exit(1);
           }
     }
     i++;
     ch=str[i];
  }
while(L->stack[L->top]!=0)

printf("the changed expression is: ");
for(j=0;j<m;j++)
printf("%c",after[j]);
after[m]='#';
printf("%c",after[m]);
}
void  compute(stack *L)
{
int i=0,result,d;
char ch;
initialstack(L);
ch=after[i];
while(ch!='#')
{
    while(ch>='0'&&ch<='9')
    {
       d=ch-'0';
       push(L,d);
       i++;
       ch=after[i];
    }
    switch(ch)
    {
       case'+':
          L->stack[L->top-1]=L->stack[L->top]+L->stack[L->top-1];
          L->top--;
          break;
          case'-':
          L->stack[L->top-1]=L->stack[L->top-1]-L->stack[L->top];
          L->top--;
          break;
      case'*':
          L->stack[L->top-1]=L->stack[L->top]*L->stack[L->top-1];
          L->top--;
          break;
      case'/':
          if(L->stack[L->top-1]==0)
          {
              printf("error due to the wrong input format! ");
              exit(0);
          }
          else
             L->stack[L->top-1]=L->stack[L->top-1]/L->stack[L->top];
          L->top--;
          break;
                                    default:
                                                      break;
                                     }
            i++;
            ch=after[i];
     }
printf(" the final result is:%d",L->stack[L->top]);
}
main()
{int result;
clrscr();
change();
compute(L);
}
 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沈阳市| 梨树县| 横峰县| 镇雄县| 奇台县| 泰宁县| 织金县| 东宁县| 临洮县| 丁青县| 湛江市| 昌宁县| 平舆县| 龙川县| 凤台县| 绥中县| 许昌县| 台东市| 渑池县| 曲周县| 嘉定区| 宁明县| 富顺县| 乌恰县| 都江堰市| 垣曲县| 阳原县| 平谷区| 瑞金市| 仁化县| 巴林右旗| 昭平县| 湄潭县| 太仆寺旗| 宿松县| 虎林市| 安龙县| 府谷县| 桦川县| 朝阳区| 阜宁县|