求四项混合运算的源代码

flybird66 2002-10-25 03:02:27
要求,手工输入一个算术式如: 3+2*(4/2+6*7)-3,之后输出结果.
...全文
49 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinggou 2002-10-25
  • 打赏
  • 举报
回复
好象就是数据结构上的知识嘛!
boyfling 2002-10-25
  • 打赏
  • 举报
回复
给分吧!
boyfling 2002-10-25
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <string.h>


#define TYPE_NUMBER 0
#define TYPE_ADD 1
#define TYPE_SUB 2
#define TYPE_MUL 3
#define TYPE_DIV 4
#define TYPE_LEFT 5
#define TYPE_RIGHT 6
#define TYPE_SEMICOLON 7

#define FAIL(x) ((x)==0 ? false:true)


struct WORD
{
int iType;
long lValue;
};

struct WORDS
{
WORD iWords[255];
int iAmount;
};



int GetExpression(char *pstrExpression);
int TranslateToWords(char *pstrExpression, WORDS *stcWords);
int GetNextWord(WORD *stcWord, int *iIndex);

int S(long *iValue, WORDS *stcWords);
int AE(long *iValue, WORDS *stcWords, int *iIndex);
int T(long *iValue, WORDS *stcWords, int *iIndex);
int D(long *iValueT, long *iValueD, WORDS *stcWords, int *iIndex);
int F(long *iValue, WORDS *stcWords, int *iIndex);
int E(long *iValueT, long *iValueD, WORDS *stcWords, int *iIndex);



int main(void)
{
char strExpression[255];
WORDS stcWords;

GetExpression(strExpression);
TranslateToWords(strExpression, &stcWords);

long lValue=0;

if(FAIL(S(&lValue, &stcWords))) printf("FAIL");
else printf("%d\n", lValue);

return 0;
}


int GetExpression(char *pstrExpression)
{
printf("Enter the expression:\n");
gets(pstrExpression);

if(strlen(pstrExpression)>253) return 1;

if(*(pstrExpression+strlen(pstrExpression)-1)!=';')
*(pstrExpression+strlen(pstrExpression))=';';


return 0;
}


int TranslateToWords(char *pstrExpression, WORDS *stcWords)
{
stcWords->iAmount=0;

for(unsigned int i=0; i<strlen(pstrExpression); i++)
{
switch(pstrExpression[i])
{
case '+':
stcWords->iWords[stcWords->iAmount].iType=TYPE_ADD;
stcWords->iWords[stcWords->iAmount++].lValue='+';
break;
case '-':
stcWords->iWords[stcWords->iAmount].iType=TYPE_SUB;
stcWords->iWords[stcWords->iAmount++].lValue='-';
break;
case '*':
stcWords->iWords[stcWords->iAmount].iType=TYPE_MUL;
stcWords->iWords[stcWords->iAmount++].lValue='*';
break;
case '/':
stcWords->iWords[stcWords->iAmount].iType=TYPE_DIV;
stcWords->iWords[stcWords->iAmount++].lValue='/';
break;
case '(':
stcWords->iWords[stcWords->iAmount].iType=TYPE_LEFT;
stcWords->iWords[stcWords->iAmount++].lValue='(';
break;
case ')':
stcWords->iWords[stcWords->iAmount].iType=TYPE_RIGHT;
stcWords->iWords[stcWords->iAmount++].lValue=')';
break;
case ';':
stcWords->iWords[stcWords->iAmount].iType=TYPE_SEMICOLON;
stcWords->iWords[stcWords->iAmount++].lValue=';';
break;
default:
if(pstrExpression[i]>='0' && pstrExpression[i]<='9')
{
stcWords->iWords[stcWords->iAmount].iType=TYPE_NUMBER;

long iValue=pstrExpression[i++]-'0';
while(pstrExpression[i]>='0' && pstrExpression[i]<='9')
iValue=iValue*10+(pstrExpression[i++]-'0');
i--;

stcWords->iWords[stcWords->iAmount++].lValue=iValue;
}
else
if(pstrExpression[i]==' ')
while(pstrExpression[i]==' ') i++;
else
return 1;

break;
}
}

return 0;
}


int GetNextWord(WORD *stcWord, const WORDS *stcWords, int *iIndex)
{
if((*iIndex)>=stcWords->iAmount) return 1;

stcWord->iType=stcWords->iWords[*iIndex].iType;
stcWord->lValue=stcWords->iWords[*iIndex].lValue;

++*iIndex;

return 0;
}



int S(long *iValue, WORDS *stcWords)
{
int iIndex=0;
int iResult;

if(FAIL(iResult=AE(iValue, stcWords, &iIndex))) return iResult;

WORD stcWord;
if(FAIL(GetNextWord(&stcWord, stcWords, &iIndex))) return 1;
if(stcWord.iType!=TYPE_SEMICOLON) return 2;

return 0;
}



int AE(long *iValue, WORDS *stcWords, int *iIndex)
{
long iValueT, iValueD;
int iResult;

if(FAIL(iResult=T(&iValueT, stcWords, iIndex))) return iResult;
if(FAIL(iResult=D(&iValueT, &iValueD, stcWords, iIndex))) return iResult;

*iValue=iValueD;

return 0;
}


int T(long *iValue, WORDS *stcWords, int *iIndex)
{
long iValueF, iValueE;
int iResult;

if(FAIL(iResult=F(&iValueF, stcWords, iIndex))) return iResult;
if(FAIL(iResult=E(&iValueF, &iValueE, stcWords, iIndex))) return iResult;

*iValue=iValueE;

return 0;
}



int F(long *iValue, WORDS *stcWords, int *iIndex)
{
int iResult;

WORD stcWord;
if(FAIL(GetNextWord(&stcWord, stcWords, iIndex))) return 1;

if(stcWord.iType==TYPE_NUMBER)
{
*iValue=stcWord.lValue;
}
else
if(stcWord.iType==TYPE_LEFT)
{
if(FAIL(iResult=AE(iValue, stcWords, iIndex))) return iResult;

if(FAIL(GetNextWord(&stcWord, stcWords, iIndex))) return 1;
if(stcWord.iType!=TYPE_RIGHT) return 2;
}
else
return 2;

return 0;
}



int E(long *iValueF, long *iValueE, WORDS *stcWords, int *iIndex)
{
int iResult;

WORD stcWord;
if(FAIL(GetNextWord(&stcWord, stcWords, iIndex))) return 1;

if(stcWord.iType==TYPE_MUL)
{
long iValueTemp;

if(FAIL(iResult=T(&iValueTemp, stcWords, iIndex))) return iResult;

*iValueE=(*iValueF)*iValueTemp;
}
else
if(stcWord.iType==TYPE_DIV)
{
long iValueTemp;

if(FAIL(iResult=T(&iValueTemp, stcWords, iIndex))) return iResult;

*iValueE=(*iValueF)/iValueTemp;
}
else
{
*iValueE=*iValueF;
--*iIndex;

if(stcWord.iType!=TYPE_ADD && stcWord.iType!=TYPE_SUB &&
stcWord.iType!=TYPE_RIGHT && stcWord.iType!=TYPE_SEMICOLON)
return 2;
}

return 0;
}



int D(long *iValueT, long *iValueD, WORDS *stcWords, int *iIndex)
{
int iResult;

WORD stcWord;
if(FAIL(GetNextWord(&stcWord, stcWords, iIndex))) return 1;

if(stcWord.iType==TYPE_ADD)
{
long iValueTemp;

if(FAIL(iResult=AE(&iValueTemp, stcWords, iIndex))) return iResult;

*iValueD=(*iValueT)+iValueTemp;
}
else
if(stcWord.iType==TYPE_SUB)
{
long iValueTemp;

if(FAIL(iResult=AE(&iValueTemp, stcWords, iIndex))) return iResult;

*iValueD=(*iValueT)-iValueTemp;
}
else
{
*iValueD=*iValueT;
--*iIndex;

if(stcWord.iType!=TYPE_RIGHT && stcWord.iType!=TYPE_SEMICOLON)
return 2;
}

return 0;
}
boyfling 2002-10-25
  • 打赏
  • 举报
回复
给我两百分!!!
flybird66 2002-10-25
  • 打赏
  • 举报
回复
在VC++6下有错误
--------------------Configuration: test1 - Win32 Debug--------------Compiling...
test1.cpp
test1.cpp(9) : error C2543: expected ']' for operator '[]'
test1.cpp(9) : error C2143: syntax error : missing ';' before 'constant'
test1.cpp(9) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
kingofvc 2002-10-25
  • 打赏
  • 举报
回复
#include <stdio.h>

#define GREAT 1
#define LESS -1
#define EQUAL 0
#define ERROR -2
#define OK 2
#define NUM 7
char operator[NUM] = {'+', '-', '*', '/', '(', ')', ';' };

int priority[NUM][NUM] = {
{ 1, 1, -1, -1, -1, 1, 1 },
{ 1, 1, -1, -1, -1, 1, 1 },
{ 1, 1, 1, 1, -1, 1, 1 },
{ 1, 1, 1, 1, -1, 1, 1 },
{ -1, -1, -1, -1, -1, 0, -2},
{ -2, -2, -2, -2, -2, -2, -2},
{ -1, -1, -1, -1, -1, -2, 2} };

void main() {
char input[200];
int i;
printf("please input expression:");
scanf("%s", input);

for(i = 0; input[i] != '\0'; i++);
input[i++] = ';';
input[i] = '\0';

if(calculate(input, &i))
printf("result is:%d\n", i);
}

typedef struct t_stack {
int data;
struct t_stack *next; } stack;

void push(stack **st, int data) {
stack *p = (stack *)malloc(sizeof(stack));
p->data = data;
p->next = (*st);
*st = p;
}

void pop(stack **st) {
stack *p = *st;
*st = p -> next;
free(p);
}

int top(stack *st) {
return st->data;
}

int empty(stack *st) {
return st==NULL;
}

int calculate(char *input, int *result) {
stack *op = NULL;
stack *oprand = NULL;
push(&op, NUM-1); /**** push operator ';' into stack *****/
while(1) {
int current = *input;
int topOp = top(op);
if('0' <= current && current <= '9') {
push(&oprand, current - '0');
input++;
}
else
switch( priority[topOp][getIndex(current)] ) {
case LESS:
push(&op, getIndex(current));
input++;
break;
case EQUAL:
pop(&op);
input++;
break;
case GREAT: {
int oprand2 = top(oprand); int oprand1;
pop(&oprand);
oprand1 = top(oprand);
pop(&oprand);
pop(&op);
push(&oprand, getValue(topOp, oprand1, oprand2));
break; }
case ERROR:
printf("error found!\n");
return 0;
case OK:
*result = top(oprand);
pop(&oprand);
return 1;
}
}
}

int getIndex(int op) {
int i;
for(i = 0; i < NUM; i++)
if(operator[i] == op)
return i;
}
int getValue(int op, int oprand1, int oprand2) {
switch(op) {
case 0: /***** + ***********/
return oprand1 + oprand2;
case 1: /***** - ************/
return oprand1 - oprand2;
case 2: /***** * ************/
return oprand1 * oprand2;
case 3: /***** / ************/
return oprand1 / oprand2;
}
}


boyfling 2002-10-25
  • 打赏
  • 举报
回复
太小气了 多加一百分 帮你写一个
EthanJiang0827 2002-10-25
  • 打赏
  • 举报
回复
好像每一本数据结构树上都有啊!

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧