當前位置:首頁 » 編程語言 » c語言能實現eval函數嗎
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言能實現eval函數嗎

發布時間: 2022-12-21 06:55:07

c語言表達式求值

//表達式求值
//By:jimly
//10/10/2009
//例如:輸入2+2(4-6*3)=
//以"="結束,然後回車即出結果
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <assert.h>
typedef float ElemType;
typedef struct Stack
{
ElemType *base; // 棧基址
ElemType *top; // 棧頂
int stacksize; // 棧存儲空間的尺寸
} SqStack;
/*------------------------------------------------------------
// 棧的基本操作
------------------------------------------------------------*/
bool InitStack(SqStack *S);
bool InitStack(SqStack *S);
void DestroyStack(SqStack *S);
bool StackEmpty(SqStack S);
int StackLength(SqStack S);
ElemType GetTop(SqStack S, ElemType *e);
void StackTraverse(SqStack S, void (*fp)(ElemType));
bool Push(SqStack *S, ElemType e);
bool Pop(SqStack *S, ElemType *e);
/*------------------------------------------------------------
// 表達式求值的操作函數定義
------------------------------------------------------------*/
char Precede(char A1,char A2);
ElemType Operate(ElemType a,ElemType theta,ElemType b);
bool In(char c,char op[]);
ElemType EvaluateExpression();
void Menu();//////////////////////////////////////////////
// Eval_exdivssion.cpp 表達式求值實現函數 //
//////////////////////////////////////////////
/*------------------------------------------------------------
操作目的: 判定運算符棧的棧頂運算符A1和讀入的運算符A2之間優先關系的函數
初始條件: 無
操作結果: 判斷出優先關系
函數參數:
char A1 運算符
char A2 運算符
返回值:
char 大小關系
------------------------------------------------------------*/
char Precede(char A1,char A2)
{
if (A1 == '+' || A1 == '-')
{
if (A2 == '+' || A2 == '-' || A2 == ')' || A2 == '=')
{
return '>';
}
else
return '<';
}
if (A1 == '*' || A1 == '/')
{
if (A2 == '(')
{
return '<';
}
else
return '>';
}
if (A1 == '(')
{
if (A2 == ')')
{
return '=';
}
if (A2 == '=')
{
return 'E';
}
else
return '<';
}
if (A1 == ')')
{
if (A2 == '(')
{
return 'E';
}
if (A2 == '=')
{
return 'E';
}
else
return '>';
}
if (A1 == '=')
{
if (A2 == '=')
{
return '=';
}
else
return '<';
}
else
return '=';
}
/*------------------------------------------------------------
操作目的: 二元運算a與b的函數
初始條件: 無
操作結果: 返回運算結果
函數參數:
ElemType a 操作數
ElemType theta 操作符
ElemType b 操作數
返回值:
ElemType 運算結果
------------------------------------------------------------*/
ElemType Operate(ElemType a,ElemType theta,ElemType b)
{
switch(char(theta))
{
case '+':
return a += b;
break;
case '-':
return a -= b;
break;
case '*':
return a *= b;
break;
case '/':
if(b==0)
{
printf("除數不能為0!!\n");
exit(0);
}
return a /= b;
break;
} return 0;
}
/*------------------------------------------------------------
操作目的: 判斷字元c是否屬於運算符集合op
初始條件: 無
操作結果: 返回判斷結果
函數參數:
char c 要判斷的字元
char op[] 運算符集合
返回值:
bool 屬於返回true 否則返回false
------------------------------------------------------------*/
bool In(char c,char op[])
{
for (int i = 0;i<7;i++)
{
if (op[i] == c)
return true;
}
return false;
}
/*------------------------------------------------------------
操作目的: 算術表達式求值的運算符優先演算法
初始條件: 無
操作結果: 返回表達式的值
函數參數:

返回值:
ElemType 運算結果
------------------------------------------------------------*/
ElemType EvaluateExpression()
{
SqStack OPTR; //運算符棧
SqStack OPND; //運算數棧
char Ct = '='; //判斷是否結束的標識
int i = 0,j = 1;
ElemType e = 0,t = 0,c;
char op[7] = {'+','-','*','/','(',')','='}; InitStack(&OPTR); //初始化
Push(&OPTR,Ct);
InitStack(&OPND); //初始化 c = (float)getchar();
while (c!='=' || GetTop(OPTR,&e)!='=')
{
if (!In((char)c,op)) //不是運算e符進棧
{
while(!In((char)c,op)) //可以是幾位數
{
t = t*10+(c-48);
c = (float)getchar();
}
Push(&OPND,t);
t = 0;
} else
{
switch (Precede((char)GetTop(OPTR,&e),(char)c))
{
case '<'://棧頂元素優先權低
Push(&OPTR,c);
c = (float)getchar();
break;
case '='://脫括弧並接受下個字元
ElemType x;
Pop(&OPTR,&x);
c = (float)getchar();
break;
case '>'://退棧並將運算結果入棧
ElemType b,theta,a;
Pop(&OPTR,&theta);
Pop(&OPND,&b);
Pop(&OPND,&a);
Push(&OPND,Operate(a,theta,b));
break;
case 'E':
printf("括弧不匹配!!\n");
exit(0);
break;

}
}
}
ElemType tem = GetTop(OPND,&e);
DestroyStack(&OPND);
DestroyStack(&OPTR);
return tem;
}/***
*DynaSeqStack.cpp - 動態順序棧,即棧的動態順序存儲實現
****/
const int STACK_INIT_SIZE = 100; // 初始分配的長度
const int STACKINCREMENT = 10; // 分配內存的增量
/*------------------------------------------------------------
操作目的: 初始化棧
初始條件: 無
操作結果: 構造一個空的棧
函數參數:
SqStack *S 待初始化的棧
返回值:
bool 操作是否成功
------------------------------------------------------------*/
bool InitStack(SqStack *S)
{
assert(S != NULL);
S->base = (ElemType *)malloc(sizeof(ElemType) * STACK_INIT_SIZE);

if(S->base == NULL) return false; S->top = S->base;
S->stacksize = STACK_INIT_SIZE; return true;
}/*------------------------------------------------------------
操作目的: 銷毀棧
初始條件: 棧S已存在
操作結果: 銷毀棧S
函數參數:
SqStack *S 待銷毀的棧
返回值:

------------------------------------------------------------*/
void DestroyStack(SqStack *S)
{
assert(S != NULL); free(S->base);
S->top = S->base = NULL;
}/*------------------------------------------------------------
操作目的: 判斷棧是否為空
初始條件: 棧S已存在
操作結果: 若S為空棧,則返回true,否則返回false
函數參數:
SqStack S 待判斷的棧
返回值:
bool 是否為空
------------------------------------------------------------*/
bool StackEmpty(SqStack S)
{
assert((S.base != NULL) && (S.top != NULL));
return(S.base == S.top);
}/*------------------------------------------------------------
操作目的: 得到棧的長度
初始條件: 棧S已存在
操作結果: 返回S中數據元素的個數
函數參數:
SqStack S 棧S
返回值:
int 數據元素的個數
------------------------------------------------------------*/
int StackLength(SqStack S)
{
assert((S.base != NULL) && (S.top != NULL));
return(S.top-S.base);
}/*------------------------------------------------------------
操作目的: 得到棧頂元素
初始條件: 棧S已存在
操作結果: 用e返回棧頂元素
函數參數:
SqStack S 棧S
ElemType *e 棧頂元素的值
返回值:
bool 操作是否成功
------------------------------------------------------------*/
ElemType GetTop(SqStack S, ElemType *e)
{
assert((S.base != NULL) && (S.top != NULL));
if(StackEmpty(S)) return false; *e = *(S.top-1);
return *e;
}/*------------------------------------------------------------
操作目的: 遍歷棧
初始條件: 棧S已存在
操作結果: 依次對S的每個元素調用函數fp
函數參數:
SqStack S 棧S
void (*fp)() 訪問每個數據元素的函數指針
返回值:

------------------------------------------------------------*/
void StackTraverse(SqStack S, void (*fp)(ElemType))
{
assert((S.base != NULL) && (S.top != NULL));
for(; S.base<S.top; S.base++) (*fp)(*S.base);
}/*------------------------------------------------------------
操作目的: 壓棧——插入元素e為新的棧頂元素
初始條件: 棧S已存在
操作結果: 插入數據元素e作為新的棧頂
函數參數:
SqStack *S 棧S
ElemType e 待插入的數據元素
返回值:
bool 操作是否成功
------------------------------------------------------------*/
bool Push(SqStack *S, ElemType e)
{
if (S->top - S->base>=S->stacksize)
{
S->base = (ElemType *)realloc(S->base,(S->stacksize + STACKINCREMENT) * sizeof(ElemType));
if (!S->base)
exit(0);
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
}
*S->top++ = e; return true;
}/*------------------------------------------------------------
操作目的: 彈棧——刪除棧頂元素
初始條件: 棧S已存在且非空
操作結果: 刪除S的棧頂元素,並用e返回其值
函數參數:
SqStack *S 棧S
ElemType *e 被刪除的數據元素值
返回值:
bool 操作是否成功
------------------------------------------------------------*/
bool Pop(SqStack *S, ElemType *e)
{
if(S == NULL) return false;
assert((S->base != NULL) && (S->top != NULL));
if(StackEmpty(*S)) return false; *e = *(--S->top);
return true;
}//////菜單///////
void Menu()
{
printf("表達式求值模擬程序\n\n");

printf("功能菜單:\n");
printf("==============\n");
printf("[1] 輸入表達式並求值\n");
printf("[0] 退出 \n");
printf("==============\n");
printf("請輸入你的選擇(0~1)(以回車結束):");
}///////// 主函數 ///////////
//////////////////////////////
int main()
{
char ch = ' ',tp; do
{
system("cls");
Menu();
ch = getchar();
if (ch == '0')
break;
tp = getchar();
printf("請輸入一個表達式(最後輸入」=「,然後回車出結果):");
printf("這個表達式結果為:%g\n",EvaluateExpression());
tp = getchar();
printf("任意鍵繼續...");
getch();
} while (true); return 0;
}//end

② 如何用C語言實現MATLAB中eval函數的功能

eval_r()函數的功能就是將括弧內的字元串視為語句並運行
例如: eval_r('y1=sin(2)')就是相當於在matlab命令窗口輸入了y1=sin(2)這條命令。

③ C語言怎麼動態創建函數

C語言可以通過宏,在你需要的位置宏展開出一個新的函數。
例如:
#define XXX(funcname) \
int funcname (int arg1, int arg2) \
{ return arg1 + arg2; }
但這也是編譯期就決定好了的,也不能實現運行期動態創建。

④ 數據結構c語言版表達式求值標准程序

思路:中綴表達式-後綴表達式-求值

參考代碼:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <algorithm>

// 堆棧的數組實現,數組的大小固定。
template<class T>
class stack
{
private:
T *s; // 數組的首地址(棧底)
size_t N; // 指向棧頂第一個空閑塊
const size_t size; // 堆棧的大小,固定不變

public:
stack(size_t n) : size(n)
{
s = new T[n]; // 可能拋出異常
N = 0; // 設置棧頂指針
}

~stack()
{
delete [] s;
}

bool empty() const
{
return N == 0;
}

bool full() const
{
return N == size;
}

void push(const T &object)
{
if (full())
{
throw "error: stack is full !";
}

s[N++] = object;
}

T pop()
{
if (empty())
{
throw "error: stack is empty !";
}

return s[--N];
}

T peek() const
{
if (empty())
{
throw "error: stack is empty !";
}

return s[N-1];
}

friend std::ostream& operator<<(std::ostream& os, const stack<T> &stk)
{
for (size_t i = 0; i < stk.N; i++)
{
std::cout << stk.s[i] << " ";
}

return os;
}
};

// 堆棧的鏈表實現
template<class T>
class STACK
{
private:
typedef struct node
{
T item;
node *next;
node(T x, node *t = NULL) : item(x), next(t) {}
}*link;

link head; // 指向棧頂第一個有效對象

public:
STACK(size_t n)
{
head = NULL;
}

~STACK() // 也可以用pop的方法刪除,但效率低
{
link t = head;

while (t != NULL)
{
link d = t;
t = t->next;
delete d;
}
}

bool empty() const
{
return head == NULL;
}

bool full() const
{
return false;
}

void push(const T &object)
{
head = new node(object, head);
}

T pop()
{
if (empty())
{
throw "error: stack is empty !";
}

T v = head->item;
link t = head->next;
delete head;
head = t;
return v;
}

T peek() const
{
if (empty())
{
throw "error: stack is empty !";
}

return head->item;
}

friend std::ostream& operator<<(std::ostream& os, const STACK<T> &stk)
{
for (link t = stk.head; t != NULL; t = t->next)
{
std::cout << t->item << " ";
}

return os;
}
};

// 中綴表達式轉化為後綴表達式,僅支持加減乘除運算、操作數為1位十進制非負整數的表達式。
char* infix2postfix(const char *infix, char *postfix)
{
const size_t N = strlen(infix);

if (N == 0 || postfix == NULL)
{
return postfix;
}

stack<char> opcode(N); // 堆棧存放的是操作符

for (size_t i = 0; i < N; i++)
{
switch (infix[i])
{
case '(': // 直接忽略左括弧
break;

case ')': // 彈出操作符
*postfix++ = opcode.pop();
*postfix++ = ' ';
break;

case '+':
case '-':
case '*':
case '/':
opcode.push(infix[i]); // 壓入操作符
break;

default:
if (isdigit(infix[i])) // 如果是數字,直接輸出
{
*postfix++ = infix[i];
*postfix++ = ' ';
}
}
}

return postfix;
}

// 後綴表達式轉化為中綴表達式,僅支持加減乘除運算、操作數為1位十進制非負整數的表達式。
char* postfix2infix(const char *postfix, char *infix)
{
const size_t N = strlen(postfix);

if (N == 0 || infix == NULL)
{
return infix;
}

*infix = '\0'; // 初始化輸出字元串為空串
std::vector<std::string> v;

// 初始化,將所有有效字元放入容器
for (size_t i = 0; i < N; i++)
{
if (isdigit(postfix[i]) || postfix[i] == '+'
|| postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/')
{
v.push_back(std::string(1, postfix[i]));
}
}

// 處理每一個操作符
for (std::vector<std::string>::iterator b = v.begin(); b < v.end(); b++)
{
if (*b == "+" || *b == "-" || *b == "*" || *b == "/")
{
(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
std::cout << "------------------------------------------------" << std::endl;

std::string opcode = *(b);
std::string oprand1 = *(b - 2);
std::string oprand2 = *(b - 1);
b = v.erase(b - 2, b + 1); // 刪除原來的三個表達式,用一個新的表達式替換
b = v.insert(b, std::string("(") + oprand1 + opcode + oprand2 + std::string(")"));
}
}

for (std::vector<std::string>::iterator b = v.begin(); b < v.end(); b++)
{
strcat(infix, (*b).c_str());
}

return infix;
}

// 計算後綴表達式的值,僅支持加減乘除運算、操作數為非負整數的表達式。
int postfix_eval(const char * postfix)
{
const size_t N = strlen(postfix);

if (N == 0)
{
return 0;
}

STACK<int> operand(N); // 堆棧存放的是操作數

for (size_t i = 0 ; i < N; i++)
{
switch (postfix[i])
{
int op1, op2;

case '+':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 + op2);
break;

case '-':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 - op2);
break;

case '*':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 * op2);
break;

case '/':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 / op2);
break;

default:

if (isdigit(postfix[i])) // 執行類似atoi()的功能
{
operand.push(0);

while (isdigit(postfix[i]))
{
operand.push(10 * operand.pop() + postfix[i++] - '0');
}

i--;
}
}

std::cout << operand << std::endl; // 輸出堆棧的內容
}

return operand.pop();
}

// 本程序演示了如何後綴表達式和中綴表達式的相互轉換,並利用堆棧計算後綴表達式。
// 轉換方向:org_infix --> postfix --> infix
int main(int argc, const char *argv[])
{
// const char *org_infix = "(5*(((9+8)*(4*6))+7))"; // section 4.3
const char *org_infix = "(5*((9*8)+(7*(4+6))))"; // exercise 4.12
std::cout << "原始中綴表達式:" << org_infix << std::endl;

char *const postfix = new char[strlen(org_infix) + 1];
infix2postfix(org_infix, postfix);
std::cout << "後綴表達式:" << postfix << std::endl;

char *const infix = new char[strlen(postfix) + 1];
postfix2infix(postfix, infix);
std::cout << "中綴表達式:" << infix << std::endl;

std::cout << "計算結果是:" << postfix_eval(postfix) << std::endl;
std::cout << "計算結果是:" << postfix_eval("5 9*8 7 4 6+*2 1 3 * + * + *") << std::endl; // exercise 4.13

delete []infix;
delete []postfix;
return 0;
}