當前位置:首頁 » 編程軟體 » 編譯原理計算器實現

編譯原理計算器實現

發布時間: 2022-05-14 00:58:13

㈠ 簡單計算器的編寫

這是個支持表達式的計算器,代碼很短。
可以輸入類似(3+3*2)*5+6這樣的表達式,自己看吧。
不懂其中的原理來找我,或者自助一下,學點編譯原理的知識,一定要懂EBNF,EBNF不知道是什麼東西?那就google一下吧。
/*
simple integer arithmetic calculator according to the EBNF
<exp> -> <term>{<addop><term>}
<addop>->+|-
<term>-><factor>{<mulop><factor>}
<mulop> -> *
<factor> -> ( <exp> )| Number
Input a line of text from stdin
Outputs "Error" or the result.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char token;/*global token variable*/
/*function prototypes for recursive calls*/
int exp(void);
int term(void);
int factor(void);

void error(void)
{
fprintf(stderr,"Error\n");
exit(1);
}

void match(char expectedToken)
{
if(token==expectedToken)token=getchar();
else error();
}

main()
{
int result;
token = getchar();/*load token with first character for lookahead*/
result = exp();
if(token=='\n')/*check for end of line */
printf("Result = %d\n",result);
else error();/*extraneous cahrs on line*/
return 0;
}

int exp(void)
{
int temp = term();
while((token=='+')||(token=='-'))
switch(token)
{
case '+':
match('+');
temp+=term();
break;
case '-':
match('-');
temp-=term();
break;
}
return temp;
}

int term(void)
{
int temp = factor();
while (token=='*')
{
match('*');
temp*=factor();
}
return temp;
}

int factor(void)
{
int temp;
if(token=='('){
match('(');
temp = exp();
match(')');
}
else if(isdigit(token)){
ungetc(token,stdin);
scanf("%d",&temp);
token = getchar();
}
else error();
return temp;
}

㈡ 這個用C語言寫的計算器的思路是什麼

對初學編程者來說,這個程序的原理確實難了點,因為它用到了編譯原理的知識.
即如果設一個四則運算表達式的形式為S,那麼它一定是一個以等號結尾的運算式,即S->exp=,->是推導符號.
運算式exp又可以繼續推導成
exp->exp+term|exp-term|term
exp表示加減運算,term表示乘除運算.這個推導式反映了乘除的優先順序比加減高.
即要先計算乘除式的結果,再來加減.
term可以推導如下:
term->term*factor|term/factor|factor
factor->num|(E)
factor是數字或者一個被括弧括住的運算式,表示最高優先順序.
數字本身是不帶運算的,是原子性的,肯定是最高優先順序.
括弧是被規定了優先計算.
這個程序的代碼就是按照上面的推導式,用遞歸方式來分析運算式的.

java怎樣實現計算器的連續運算;如1+2*3/3-4怎樣用代碼實現

如果只是實現的話,java有現成的函數
如果想自己寫的話,我覺得用堆棧吧。

㈣ 表達式計算器的設計與實現 VB

Option Explicit

Public Function str18(Num As String) As Double
Dim i&, l&, lStart&
Dim sTemp$, dTemp#, dTemp2#
If Num = vbNullString Then Exit Function
lStart = InStr(1, Num, "(")
Do While lStart > 0
If lStart > 1 Then
If InStr(1, "(*+-/", Mid$(Num, lStart - 1, 1)) <= 0 Then GoTo 10
End If
sTemp = GetInners(Num, lStart + 1)
l = Len(sTemp)
sTemp = CStr(str18(sTemp))
Num = Mid$(Num, 1, lStart - 1) & sTemp & Mid$(Num, lStart + l + 2)
10: lStart = InStr(lStart + 1, Num, "(")
Loop
lStart = InStrRev(Num, "int(")
Do While lStart > 0
sTemp = GetInners(Num, lStart + 4)
l = Len(sTemp)
sTemp = CStr(Int(str18(sTemp)))
Num = Mid$(Num, 1, lStart - 1) & sTemp & Mid$(Num, lStart + l + 5)
lStart = InStrRev(Num, "int(", lStart)
Loop
lStart = InStr(2, Num, "+")
If lStart > 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp + dTemp2: Exit Function
End If
lStart = InStr(2, Num, "-")
If lStart > 0 Then
If InStr(1, "(*+-/", Mid$(Num, lStart - 1, 1)) <= 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp2 - dTemp: Exit Function
End If
End If
lStart = InStr(1, Num, "*")
If lStart > 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp * dTemp2: Exit Function
End If
lStart = InStr(1, Num, "/")
If lStart > 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
'If dTemp = 0 Then Exit Function
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp2 / dTemp: Exit Function
End If
lStart = InStr(1, Num, "^")
If lStart > 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp2 ^ dTemp: Exit Function
End If
If Left$(Num, 1) = "-" Then
str18 = -1 * Val(Mid$(Num, 2))
Else
str18 = Val(Num)
End If
End Function
Private Function GetInners(Str$, lStart&) As String
'獲取()內容,str(lstart-1)="("
Dim i&, sTemp As String * 1, k%, l&
l = Len(Str)
For i = lStart To l
sTemp = Mid$(Str, i, 1)
Select Case sTemp
Case "(": k = k + 1
Case ")": k = k - 1
End Select
If k = -1 Then Exit For
Next
If i <= l Then GetInners = Mid$(Str, lStart, i - lStart)
End Function
最好寫在模塊中 哦~~~~~[注,來自vbgood 很久前的了 誰的我也忘了]

㈤ 編譯原理語法分析編程

#include <iostream>
#include <string>
#include <fstream>
#include <queue>
#include <string.h>
#include <stdio.h>

using namespace std;

enum Datatype { RESERVE_WORD=1,IDENTIFIER=2,DIGIT=3,OPERATOR=4,SEPRATOR=5 };

struct OutputStruct
{
public:
Datatype type;
string value;
};

string operate[]={"sin","cos","pow"};
string KeyWord[]={"main","int","if","char","cout"};
const int MAX_SIZE=255;
char BUFF[MAX_SIZE]; //buffer to contain a char line.
ifstream inFile;
ofstream outFileStream;
queue<OutputStruct> tt;

bool IsKeyWord(string& cs)
{
for(int i=0;i<5;++i)
if(cs==KeyWord[i])
return true; //Exist
return false;
}

void ReadLineAndAnalyze()
{
int strSize=0;
int i;
int errFlag=0;
char ch;
string outStructStr,str;
struct OutputStruct outStruct;
{
i=0;
inFile.getline(BUFF,MAX_SIZE,'\n');
strSize=inFile.gcount();
cout<<BUFF;
do{
str="";
do{
ch=BUFF[i];
i++;
}while(ch==' '||ch==' '||ch=='\n');
switch(ch)
{

case '+':
case '-':
case '*':
case '/':
outStruct.type=OPERATOR;
outStruct.value=ch;
break;
case '=':
case '>':
case '<':
outStructStr=ch;
if(BUFF[i]=='=')
{
outStruct.type=OPERATOR;
outStructStr+=BUFF[i];
outStruct.value=outStructStr;
i++;
}
else
{
outStruct.type=OPERATOR;
outStruct.value=ch;
};
break;

case ',':
case ';':
case '{':
case '}':
case '(':
case ')':
case '[':
case ']':
case '\"':
outStruct.type=SEPRATOR;
outStruct.value=ch;
break;

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
outStructStr+=ch;
while(BUFF[i]>='0'&&BUFF[i]<='9'||BUFF[i]=='.')
{
outStructStr+=BUFF[i];
i++;
}//while
outStruct.type=DIGIT;
outStruct.value=outStructStr;
break;

default:
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
outStructStr+=ch;
while(BUFF[i]>='a'&&BUFF[i]<='z'||BUFF[i]>='A'&&BUFF[i]<='Z')
{
outStructStr+=BUFF[i];
i++;
}//while
if(IsKeyWord(outStructStr))
{
outStruct.type=RESERVE_WORD;
outStruct.value=outStructStr;
}
else
{
outStruct.type=IDENTIFIER;
outStruct.value=outStructStr;
}
break;
}
else
errFlag=1;
}//switch;
if(!errFlag)
tt.push(outStruct);
errFlag=0;
outStructStr="";
}while(i<strSize-1);

}//while(i<MAX_SIZE&&!inFile.eof());//do_while
return;
}

float F();
float T();
float E();
float S();

float F()
{
float ret;
if((tt.front().type==IDENTIFIER)||(tt.front().type==DIGIT))
{
ret=atof(tt.front().value.c_str());
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
return ret;
}
if(tt.front().value=="(")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
ret=E();
if(tt.front().value==")")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
return ret;
}
else
{
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少右括弧"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit(0);
}
}
else
{
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少因子"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit(0);
}
}
float T()
{
float i,j;
i=F();
if(tt.front().value=="*")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
j=T();
return i*j;
}
else if(tt.front().value=="/")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
j=T();
if(abs(j)<0.0000001)
{
cout<<"\b ----ERROR! 除數為零!"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit(0);
}
return i/j;
}
return i;
}

float E()
{
float i,j;
i=T();
if(tt.front().value=="+")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
j=E();
i=i+j;
}
else if(tt.front().value=="-")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
j=E();
i=i-j;
}
if(tt.front().value==";"||tt.front().type==OPERATOR||tt.front().value==")")
return i;
else
{
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少運算符"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit (0);
}
}

float S()
{
float i;
i=E();
if(tt.front().value==";")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
return i;
}
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少左括弧"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit(0);
}

void GrammaAnalize()
{
float i;
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
i=S();
cout<<"\b="<<i<<endl;
}

int main()
{
inFile.open("data.txt");
if(!inFile)
{
cout<<"打開源文件失敗!";
return 1;
}
while(!inFile.eof())
{
ReadLineAndAnalyze();
GrammaAnalize();
}
inFile.close();
return 0;
}

㈥ (高分)求一個正確lex和yacc編的計算器(編譯原理)

網上多的是,大多可以運行。關鍵是你要在linux下分別將 lex和yacc文件用 flex 和 bison 編譯成對應的c文件,然後在windows下用VC或者Codeblock的IDE下編寫界面,連接剛才對應的c文件,生成exe交給老師。

㈦ 用C#做WinFrom的計算器,怎樣實現計算器中的括弧問題高手的進!高分懸賞!

電腦實際上就是一個計算器,你在編程的時候寫一個表達式他都是可以幫你計算的,只不過你用了計算器去平均的表達式就成為了字元串,其實這個字元串程序也是可以幫你計算的就要用到動態編譯了!以前做過一個動態添加公式來算物流運費的工具,代碼找不到了

㈧ 用C語言編寫計算器,怎麼才能計算一串表達式啊,我該怎麼編。在線等,我只學了些比本的結構,如分支,循環

計算一串的表達式需要用到語義分析那部分的知識,構建一顆表達式二叉樹

比如要想計算2*(5+3)-6這個表達式的話,構建結果如圖,這個需要數據結構里二叉樹和編譯原理裡面的語義分析知識,如果你僅僅是學了分支循環的話,應該不會讓你們編這么復雜的東西的....

恩..我覺得是這樣的..

㈨ 用c++編寫一個簡單的計算器

你的這個要求絕對不會有人滿足的。因為你的這個要求不只是一個編寫一個最簡單的計算器問題。
主要是在編寫這個程序的過程中,還涉及到了計算機軟體專業中的「編譯原理」這門課程的其中重要知識。即:表達式的分析與求值(即:何時將相應的數字、以及運算符壓入堆棧,何時又需要將相應的數字、以及運算符彈出堆棧)、以及對運算符優先順序的處理(例如:括弧的最優先最高、乘除法的優先順序高於加減法)。
所以說你的這個要求可以說是:至少是一個大作業了。而且了,另外還有一個別的任何人無法滿足你的原因就是:對於編寫任何程序來說,都是必須要通過自己上機編寫程序源代碼、編譯、鏈接、通過花費很多的時間和精力去調試,最終才能夠得出程序的正確運行結果。

㈩ 編譯原理 tiny.l怎麼生成tiny.exe

最近對解釋型程序(類似python或者是linux里的bc計算器)非常感興趣,就開始學習一下編譯原理。今天自己實現了TINY語言的詞法掃描程序。大部分參考《編譯原理及實踐》一書。但是我做了一些小小的改進。 先說一下TINY語言:

熱點內容
app什麼情況下找不到伺服器 發布:2025-05-12 15:46:25 瀏覽:713
php跳過if 發布:2025-05-12 15:34:29 瀏覽:466
不定時演算法 發布:2025-05-12 15:30:16 瀏覽:129
c語言延時1ms程序 發布:2025-05-12 15:01:30 瀏覽:163
動物園靈長類動物配置什麼植物 發布:2025-05-12 14:49:59 瀏覽:732
wifi密碼設置什麼好 發布:2025-05-12 14:49:17 瀏覽:147
三位數乘兩位數速演算法 發布:2025-05-12 13:05:48 瀏覽:396
暴風影音緩存在哪裡 發布:2025-05-12 12:42:03 瀏覽:539
access資料庫exe 發布:2025-05-12 12:39:04 瀏覽:627
五開的配置是什麼 發布:2025-05-12 12:36:37 瀏覽:363