相当强悍
源代码在线查看: opration prior.cpp
软件大小: |
133 K |
上传用户: |
ok34090512 |
|
|
关键词: |
|
下载地址: |
免注册下载 普通下载
|
|
// Opration Prior.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
char OPTR[10];
int f[6]={6,8,2,8,1};
int g[6]={4,7,9,2,1};
float OPND[10];
int OP=0,OR=1;
int m=0;
float Operat(char ch,float x,float y)//算符运算
{
switch (ch)
{
case '+': return x+y;
// case '-': return x-y;
case '*': return x*y;
// case '/': return x/y;
}
printf("ERROR!\n");
return 44;
}
int gg(char c)
{
switch (c)
{
case '+': return(0);
case '*': return(1);
case '(': return(2);
case ')': return(3);
case '#': return(4);
}
printf("ERROR!\n");
return 44;
}
int Preced(int a,int b)
{
if(f[a] return 1;
else if(f[a]==g[b])
return 2;
else if(f[a]>g[b])
return 3;
else return 0;
}
int main(int argc, char* argv[])
{
float x,y;
int t=0;
char ch,c;
OPTR[0]='#';
printf("Please enter the expression and end with #:");//如 2*(3+4)#
scanf("%c",&ch);
while(OPTR[OR-1]!=OPTR[0]||ch!='#')
{
if(ch='0')
{ m=(ch-'0')+m*10; scanf("%c",&ch);t=2;}//数字字符转换为数字,置t为2
else if(!t&&ch!='(') {printf("Too many opnd!\n");return 44;}//t为0或t不为(
else
{
switch (Preced(gg(OPTR[OR-1]),gg(ch)))
{
case 1: if(t==2){OPND[OP++]=(float)m; m=0;}OPTR[OR++]=ch; scanf("%c",&ch);
if(ch=='(')t=1;
else t=0;
break;
case 2: OR--; scanf("%c",&ch); break;
case 3: if(t==2) {OPND[OP++]=(float)m;m=0;}
x=OPND[--OP];
y=OPND[--OP];c=OPTR[--OR];
OPND[OP++]=Operat(c,x,y);
if(ch!='#'&&ch!=')'){ OPTR[OR++]=ch;scanf("%c",&ch);t=0;}
else t=1; break;
}
}
}
printf("%f",OPND[0]);
return 0;
}