導航:首頁 > 源碼編譯 > 編譯原理實現一個增強型計算器

編譯原理實現一個增強型計算器

發布時間:2022-12-28 13:16:38

A. 怎麼用C++編譯一個簡單的計算器

我借鑒了別人的某計算器,因為你沒有說多簡易...我就找了個差不多的...
/*
程序名稱:表達式計算器
編譯環境:Microsoft Visual C++ 6.0
作者:吉林大學 計算機科學與技術學院 2006 羅泗勇
時間:200801
*/

/*
說明:
採用樹形結構處理表達式,按優先順序運算結果,一個加,減,乘,除或數值為一個節點
優先順序如下:
函數:4
括弧:3
乘除:2
加減:1
*/

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

const char NUM[]={'0','1','2','3','4','5','6','7','8','9','.'};
const char OPERATION[]={'+','-','*','/'};
const double PI=3.14159265358979;
const double EE=2.71828182818281;

class Fun //處理系統數學函數的類
{
public:
Fun(string o,int t,double l=0.0,double r=0.0):op(o),type(t),lvalue(l),rvalue(r){}
static string FUN[];
double calc();
private:
int type; //666 0 1 sin90 2 3! 3 3C2
string op; //函數類型
double lvalue; //函數左邊的值
double rvalue; //函數右邊的值
static int FunNum;
};

int Fun::FunNum=8;
string Fun::FUN[]={"!","sin","cos","tan","log","ln","C","A","^"};
/*
函數說明:
1:log是以10為底的工程對數
2:ln 是以e為底的自然對數
3:C 計算組合數 輸入規則 如計算 3取2的組合 輸入表達式 3C2
4:A 計算排列數 輸入規則 如計算 3取2的排列 輸入表達式 3A2
5:! 計算階乘
6:^ x的y次方 輸入 x^y
*/

int factorial(int n) //階乘函數
{
int i,s=1;
for(i=1;i<=n;i++)
s*=i;
return s;
}

int C(int a,int b)
{
return factorial(a)/(factorial(b)*factorial(a-b));
}

int A(int a,int b)
{
return factorial(a)/factorial(b);
}

double Fun::calc() //計算系統函數的值
{
if(type==0)
return lvalue;
else
{
if(op=="!")
return factorial(lvalue);
if(op=="sin")
return sin(rvalue/180*PI);
if(op=="cos")
return cos(rvalue/180*PI);
if(op=="tan")
return tan(rvalue/180*PI);
if(op=="log")
return log10(rvalue);
if(op=="ln")
return log10(rvalue)/log10(EE);
if(op=="C")
return C(lvalue,rvalue);
if(op=="A")
return A(lvalue,rvalue);
if(op=="^")
return pow(lvalue,rvalue);
else
{
string err="暫時沒有函數"+op;
MessageBox(NULL,err.c_str(),"錯誤",MB_OK);
return 0;
}
}
}

struct Unit //雙向鏈表保存運算單元
{
Unit(int p,char o,string c,double v,int t,Unit * pr=NULL,Unit * n=NULL)
:PRI(p),Operation(o),Code(c),value(v),Type(t),Pre(pr),Next(n){}
int PRI; //優先順序

char Operation; //操作符
string Code; //原始代碼
double value; //數據

int Type; //類型 操作符0 數據1 函數2
Unit * Pre; //構成雙向鏈表
Unit * Next;
};

class Node //表達式樹狀結構的節點
{
public:
Node(char o,int p,int e=1,double v=0,Node * ph=NULL,Node * pl=NULL,Node * pr=NULL)
:Operation(o),PRI(p),Expression(e),value(v),Head(ph),Left(pl),Right(pr){}
Node * Head; //節點的根,左樹枝,右樹枝
Node * Left;
Node * Right;
double GetValue();
char GetOperation() const {return Operation;}
int GetPri() const {return PRI;}
int IsExp() const {return Expression;}
private:
char Operation; //操作符
int PRI; //優先順序
int Expression; //記錄該節點是否是表達式0 1
double value; //該節點的值
};

double Node::GetValue() //運算該節點的值
{
if(IsExp()) //該節點的值還未算出來
{
double lvalue,rvalue;
lvalue=Left->GetValue();
rvalue=Right->GetValue();
Expression=0;
char op=GetOperation();
switch(op)
{
case '+':
return lvalue+rvalue;
case '-':
return lvalue-rvalue;
case '*':
return lvalue*rvalue;
case '/':
return lvalue/rvalue;
default:
return 0;
}
}
else
return value;
}

bool Isnum(char c)
{
for(int i=0;i<sizeof(NUM);i++)
{
if(c==NUM[i])
return true;
}
return false;
}

bool Isoperation(char c)
{
for(int i=0;i<sizeof(OPERATION);i++)
{
if(c==OPERATION[i])
return true;
}
return false;
}

Unit * Analyse(string exp) //分析表達式並生成鏈表
{
int pri=0; //當前優先順序
int stat=-1; //當前的讀入狀態 括弧 0 數據 1 運算符 2
Unit * head=NULL,* p=NULL;
int i=0,explen;
explen=exp.size();
for(i=0;i<explen;i++)
{
char c=exp.at(i);
if(c=='(')
{
pri+=3;
stat=0;
}
else if(c==')')
{
pri-=3;
stat=0;
}
else if(Isoperation(c)) //操作符不會出現在表達式開頭
{
Unit * temp=p;
int add_pri; //自身增加的優先順序
if(c=='+' || c=='-')
add_pri=1;
else
add_pri=2;
p->Next=new Unit(pri+add_pri,c," ",0,0);
p=p->Next;
p->Pre=temp;
}
else //其他的當做函數處理
{
string function="";
while(i<explen && (c=exp.at(i),! Isoperation(c)) && c!=')')
{
function+=c;
i++;
}
i--;

if(head==NULL)
{
p=new Unit(pri,' ',function,0,2);
head=p;
}
else
{
Unit * temp=p;
p->Next=new Unit(pri,' ',function,0,2);
p=p->Next;
p->Pre=temp;
}
}
}
return head;
}

Unit * Calc(Unit * head) //計算雙向鏈表基本單元的值
{
Unit * p=head;
while(p!=NULL)
{
if(p->Type!=0) //非操作符
{
string temp=p->Code;
string op;
double lvalue=0,rvalue=0;
int l_point=0,r_point=0;
int i=0,type=0;
char ch;
while(i<temp.size() && (ch=temp.at(i),Isnum(ch)))
{
if(ch=='.')
{
l_point++;
i++;
continue;
}
if(! l_point)
lvalue*=10;
lvalue+=(ch-'0')*pow(10,-l_point);
i++;
if(l_point)
l_point++;
}
while(i<temp.size() && (ch=temp.at(i),! Isnum(ch)))
{
op+=ch;
type=1;
i++;
}
while(i<temp.size() && (ch=temp.at(i),Isnum(ch)))
{
if(ch=='.')
{
r_point++;
i++;
continue;
}
if(! r_point)
rvalue*=10;
rvalue+=(ch-'0')*pow(10,-r_point);
i++;
if(r_point)
r_point++;
}
Fun * f=new Fun(op,type,lvalue,rvalue);
p->value=f->calc();
}
p=p->Next;
}
return head;
}

Node * Tree(Unit * head) //生成表達式樹
{
Node * root=NULL,* proot=NULL,* pbranch=NULL;
Unit * p=head;
int now_pri; //當前優先順序
bool hadop=false;
while(p!=NULL)
{
if(p->Type==0) //如果是一個操作符
{
hadop=true;
if(root==NULL)
{
proot=new Node(p->Operation,p->PRI,1);
root=proot;
pbranch=root;
now_pri=p->PRI;
proot->Left=new Node(' ',0,0,p->Pre->value);
proot->Right=new Node(' ',0,0,p->Next->value);
}
else
{
if(p->PRI<=now_pri) //優先順序低於當前優先順序,樹根方向 //最初寫的 if(p->PRI<now_pri),9/3/3=9,錯的
{
proot=new Node(p->Operation,p->PRI,1); //新的樹根
proot->Left=root; //根的變換
proot->Right=new Node(' ',0,0,p->Next->value);
root=proot;

pbranch=proot; //右樹枝的變換
//pbranch->Right=new Node(' ',0,0,p->Pre->value); //樹枝右邊取值
}
else
{
Node * temp;
temp=new Node(p->Operation,p->PRI,1);

pbranch->Right=temp;
temp->Head=pbranch;

pbranch=pbranch->Right;
pbranch->Left=new Node(' ',0,0,p->Pre->value);
pbranch->Right=new Node(' ',0,0,p->Next->value);
}
now_pri=p->PRI;
}
}
p=p->Next;
}
if(! hadop)
root=new Node(' ',0,0,head->value);
return root;
}

int main()
{
string exp;

//ifstream infile("test.txt",ios::in);
while(! getline(cin,exp).eof())
{
if(exp=="")
continue;
Unit * h=Analyse(exp);
h=Calc(h);
Node * root=Tree(h);
cout<<exp<<"="<<root->GetValue()<<endl;
}
return 0;
}

B. c語言編寫 編寫一個簡單的計算器,實現兩個整型數的四則運算。

1、打開CodeBlocks,新建一個空白文件,先定義頭文件和主函數,接著寫程序多大的主體:

C. 用匯編語言編寫計算器

B_P EQU BYTE PTR
W_P EQU WORD PTR
D_P EQU DWORD PTR
CODE SEGMENT
ASSUME CS:CODE,DS:CODE
ORG 100H
;主程序開始
NEWSTAR:
JMP STAR
EVEN
NUM1 DW 0,0
NUM2 DW 0,0
NUM3 DW 0,0
JGV4 DW 0,0
YSF LABEL WORD
YSF1 DB 0
YSF2 DB 0
JUV DB 0
UV DB 0
;使DS:SI指向第一有效字元
OVERS LABEL NEAR
CLD
PUSH AX
OVERS1:
LODSB
CMP AL,' '
JZ OVERS1
CMP AL,9
JZ OVERS1
DEC SI
CMP AL,13
POP AX
RET
;輸出一個製表符
TAB LABEL NEAR
MOV AL,9
JMP SHORT DISP
;輸出一個回車換行符
CRLF LABEL NEAR
MOV AL,13
CALL DISP
MOV AL,10
;顯示一個字元
DISP LABEL NEAR
PUSH AX
PUSH DX
MOV AH,2
MOV DL,AL
INT 21H
POP DX
POP AX
RET
;檢查是否為運算符
ISYSF LABEL NEAR
CLD
LODSB
CALL ISJJ
JZ ISYSF1
CALL ISII
JZ ISYSF1
DEC SI
ISYSF1:
RET
;檢查是否是加減運算
ISJJ LABEL NEAR
CMP AL,'+'
JZ ISYSF1
CMP AL,'-'
RET
;檢查是否為乘除運算
ISII LABEL NEAR
CMP AL,'*'
JZ ISYSF1
CMP AL,'/'
JZ ISYSF1
CMP AL,'\'
RET
;數據輸出
;CX=10 十進制
;CX=16 十六進制
DOUT LABEL NEAR
PUSH AX
PUSH BX
PUSH DX
PUSH BP
XOR BP,BP
DOUT1:
INC BP
PUSH AX
MOV AX,DX
XOR DX,DX
DIV CX
MOV BX,AX
POP AX
DIV CX
PUSH DX
MOV DX,BX
OR BX,AX
JNZ DOUT1
DOUT2:
POP AX
ADD AL,'0'
CMP AL,'9'
JBE DOUT3
ADD AL,'A'-'9'-1
DOUT3:
CALL DISP
DEC BP
JNZ DOUT2
POP BP
POP DX
POP BX
POP AX
RET
;輸入數據在DX:AX中返回
;CX=0 數據為10進制
;CX#0 數據為16進制
DATE LABEL NEAR
PUSH BX
PUSH BP
PUSH DI
XOR AX,AX
XOR DX,DX
DATE1:
MOV DI,AX
LODSB
CMP AL,'0'
JB DATE7
CMP AL,'9'
JA DATE6
DATE2:
AND AX,0FH
SHL DI,1
RCL DX,1
MOV BX,DI
MOV BP,DX
SHL DI,1
RCL DX,1
SHL DI,1
RCL DX,1
JCXZ DATE3
SHL DI,1
RCL DX,1
Jmp short DATE4
DATE3:
ADD DI,BX
ADC DX,BP
DATE4:
ADD AX,DI
ADC DX,0
JMP DATE1
DATE5:
ADD AL,9
JMP DATE2
DATE6:
JCXZ DATE7
CMP AL,'A'
JB DATE7
CMP AL,'F'
JBE DATE5
CMP AL,'a'
JB DATE7
CMP AL,'f'
JBE DATE5
DATE7:
MOV AX,DI
DEC SI
OR DI,DX
POP DI
POP BP
POP BX
RET
;數據1與數據2根據YSF1進行加減運算
JSJJ LABEL NEAR
MOV AX,NUM2
MOV DX,NUM2+2
CMP YSF1,'+'
JZ JSJJ1
SUB NUM1,AX
SBB NUM1+2,DX
JMP SHORT JSJJ2
JSJJ1:
ADD NUM1,AX
ADC NUM1+2,DX
JSJJ2:
RET
;數據1與數據2根據YSF1進行乘除運算
JSII1 LABEL NEAR
MOV BX,OFFSET NUM1
JMP SHORT JSII2_1
;數據2與數據3根據YSF2進行乘除運算
JSII2 LABEL NEAR
MOV BX,OFFSET NUM2
JSII2_1:
DB 66H
MOV AX,[BX]
DB 66H
MOV CX,[BX+4]
CMP YSF2,'*'
JNZ JSII2_2
DB 66H
IMUL CX
JMP SHORT JSII2_3
JSII2_2:
DB 66H
CWD
DB 66H
IDIV CX
CMP YSF2,'/'
JZ JSII2_3
DB 66H
XCHG DX,AX
JSII2_3:
DB 66H
MOV [BX],AX
RET
;顯示結果
XUJG LABEL NEAR
MOV AX,JGV4
MOV DX,JGV4+2
MOV CX,10
CMP UV,10
JZ XUJG0
MOV CX,16
XUJG0:
TEST DX,8000H
JZ XUJG1
CMP UV,10
JZ XUJG2
CALL DOUT
XUJG2:
NOT AX
NOT DX
ADD AX,1
ADC DX,0
PUSH AX
CMP UV,10
JZ XUJG3
MOV AL,' '
CALL DISP
MOV AL,'('
CALL DISP
XUJG3:
MOV AL,'-'
CALL DISP
POP AX
CMP UV,10
JZ XUJG1
CALL DOUT
MOV AL,')'
CALL DISP
RET
XUJG1:
CALL DOUT
RET
;計算結果放入DX:AX中
JSJG LABEL NEAR
CALL JSJJ
MOV AX,NUM1
MOV DX,NUM1+2
RET
;從DS:SI處取一個數據
LOADATE LABEL NEAR
LODSB
CMP AL,'('
JZ LOADATE1
DEC SI
PUSH CX
XOR CX,CX
CMP UV,10
JZ LOADATE0
INC CX ;取16進制數
LOADATE0:
CALL DATE ;取數據放入DX:AX中
POP CX
RET
LOADATE1:
PUSH NUM1 ;保存數據1數據2及運算符
PUSH NUM1+2
PUSH NUM2
PUSH NUM2+2
PUSH YSF
INC JUV
MOV JGV4,0 ;結果值清零
MOV JGV4+2,0
;進行四則運算
SZYS LABEL NEAR
CALL ISYSF ;首字元是運算符?
JZ SZYS2
CALL LOADATE ;不是,取數並保存
MOV NUM1,AX
MOV NUM1+2,DX
SZYS1:
XOR AX,AX ;數據2清零
MOV NUM2,AX
MOV NUM2+2,AX
CALL ISYSF ;下個是運算符?
JZ SZYS3
JNZ SZYS6
SZYS2:
PUSH AX
MOV AX,JGV4 ;將結果作為數據1
MOV NUM1,AX
MOV AX,JGV4+2
MOV NUM1+2,AX
POP AX
SZYS3:
MOV YSF1,AL
MOV YSF2,AL
CALL ISJJ ;是加減運算轉
JZ SZYS4
CALL LOADATE ;取數據2
MOV NUM2,AX
MOV NUM2+2,DX
CALL JSII1 ;數據1與數據2根據YSF1進行乘除運算
JMP SZYS1 ; 結果保存在數據1中
SZYS4:
CALL LOADATE ;取數據2並保存
MOV NUM2,AX
MOV NUM2+2,DX
SZYS4_1:
CALL ISYSF
JNZ SZYS6
CALL ISJJ ;運算符2是加減運算?
JNZ SZYS5 ;不是轉
PUSH AX
CALL JSJJ ;數據1與數據2根據YSF1進行加減運算
POP AX ; 結果保存在數據1中
MOV YSF1,AL ;保存新的運算符
JMP SZYS4
SZYS5:
MOV YSF2,AL ;保存運算符2
CALL LOADATE ;取數據3
MOV NUM3,AX
MOV NUM3+2,DX
CALL JSII2 ;數據2與數據3根據YSF2進行乘除運算
JMP SZYS4_1 ; 結果保存在數據2中
SZYS6:
MOV CL,AL
CMP AL,13
JNZ SZYS9
SZYS7:
CALL JSJG
CMP JUV,0
JZ SZYS8
DEC JUV
POP YSF
POP NUM2+2
POP NUM2
POP NUM1+2
POP NUM1
RET
SZYS8:
CMP CL,')'
JZ SZYS10
MOV JGV4,AX
MOV JGV4+2,DX
JNZ SZYS12
SZYS9:
CMP AL,')'
JNZ SZYS11
INC SI
JMP SZYS7
SZYS10:
MOV NUM1,AX
MOV NUM1+2,DX
JMP SZYS1
SZYS11:
STC
RET
SZYS12:
CLC
RET
;數制處理
UVIL LABEL NEAR
PUSH SI
UVIL1:
LODSB
CMP AL,' '
JZ UVIL2
CMP AL,9
JZ UVIL2
CMP AL,13
JZ UVIL4
JNZ UVIL1
UVIL2:
MOV BX,SI
CALL OVERS
JZ UVIL3
LODSW
CALL OVERS
POP SI
JNZ SZYS11
CMP AX,'01'
JNZ SZYS11
PUSH SI
MOV UV,10
UVIL3:
MOV AL,13
MOV [BX-1],AL
UVIL4:
POP SI
CLC
RET
;預置結果
YVJG LABEL NEAR
MOV AH,52H
INT 21H
MOV AX,ES:[BX-2]
MOV DS,AX
MOV AX,DS:[0CH]
MOV CS:JGV4,AX
MOV AX,DS:[0EH]
MOV CS:JGV4+2,AX
PUSH CS
POP DS
PUSH CS
POP ES
RET
;保存結果
BCJG LABEL NEAR
MOV AH,52H
INT 21H
MOV AX,ES:[BX-2]
MOV DS,AX
MOV AX,CS:JGV4
MOV DS:[0CH],AX
MOV AX,CS:JGV4+2
MOV DS:[0EH],AX
PUSH CS
POP DS
PUSH CS
POP ES
RET
STAR:
MOV SI,81H
CLD
CALL OVERS
JNZ STAR1
STAR0:
MOV DX,OFFSET MESS1
JMP STAR4
STAR1:
CALL YVJG
CALL UVIL
JB STAR0
MOV SAVESP,SP
CALL SZYS
MOV SP,SAVESP
MOV DX,OFFSET MESS2
JB STAR4
CALL CRLF
CALL XUJG
CALL BCJG
MOV DX,OFFSET MESS3
STAR4:
MOV AH,9
INT 21H
INT 20H
SAVESP DW 0
MESS1 DB 13,10,' Syntax:',13,10
DB ' JS <Expression> [10]',13,10,'$'
MESS2 DB 'Error in expression !$'
MESS3 DB 13,10,'$'
CODE ENDS
END NEWSTAR

D. 怎樣用51單片機做計算器啊

1、硬體模擬圖

4、程序源代碼

#include <reg51.h>#include <intrins.h>

#include <ctype.h>

#include <stdlib.h>

#define uchar unsigned char

#define uint unsigned int

uchar operand1[9], operand2[9];

uchar operator;

void delay(uint);

uchar keyscan();

void disp(void);

void buf(uint value);

uint compute(uint va1,uint va2,uchar optor);

uchar code table[] = {0xc0,0xf9,0xa4,0xb0,0x99,

0x92,0x82,0xf8,0x80,0x90,0xff};

uchar dbuf[8] = {10,10,10,10,10,10,10,10};

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

uchar keyscan()

{

uchar skey;

P1 = 0xfe;

while((P1 & 0xf0) != 0xf0)

{

delay(3);

while((P1 & 0xf0) != 0xf0)

{

switch(P1)

{

case 0xee: skey = '7'; break;

case 0xde: skey = '8'; break;

case 0xbe: skey = '9'; break;

case 0x7e: skey = '/'; break;

default: skey = '#';

}

while((P1 & 0xf0) != 0xf0)

;

}

}

P1 = 0xfd;

while((P1 & 0xf0) != 0xf0)

{

delay(3);

while((P1 & 0xf0) != 0xf0)

{

switch(P1)

{

case 0xed: skey = '4'; break;

case 0xdd: skey = '5'; break;

case 0xbd: skey = '6'; break;

case 0x7d: skey = '*'; break;

default: skey = '#';

}

while((P1 & 0xf0) != 0xf0)

;

}

}

P1 = 0xfb;

while((P1 & 0xf0) != 0xf0)

{

delay(3);

while((P1 & 0xf0) != 0xf0)

{

switch(P1)

{

case 0xeb: skey = '1'; break;

case 0xdb: skey = '2'; break;

case 0xbb: skey = '3'; break;

case 0x7b: skey = '-'; break;

default: skey = '#';

}

while((P1 & 0xf0) != 0xf0)

;

}

}

P1 = 0xf7;

while((P1 & 0xf0) != 0xf0)

{

delay(3);

while((P1 & 0xf0) != 0xf0)

{

switch(P1)

{

case 0xe7: skey = '$'; break;

case 0xd7: skey = '0'; break;

case 0xb7: skey = '='; break;

case 0x77: skey = '+'; break;

default: skey = '#';

}

while((P1 & 0xf0) != 0xf0)

;

}

}

return skey;

}

void main()

{

uint value1, value2, value;

uchar ckey, cut1 = 0, cut2 = 0;

uchar operator;

uchar i, bool = 0;

init:

buf(0);

disp();

value = 0;

cut1 = cut2 = 0;

bool = 0;

for(i = 0;i < 9;i++)

{

operand1[i] = '';

operand2[i] = '';

}

while(1)

{

ckey = keyscan();

if(ckey != '#')

{

if(isdigit(ckey))

{

switch(bool)

{

case 0:

operand1[cut1] = ckey;

operand1[cut1+1] = '';

value1 = atoi(operand1);

cut1++;

buf(value1);

disp();

break;

case 1:

operand2[cut2] = ckey;

operand2[cut2+1] = '';

value2 = atoi(operand2);

cut2++;

buf(value2);

disp();

break;

default: break;

}

}

else if(ckey=='+'||ckey=='-'||ckey=='*'||ckey=='/')

{

bool = 1;

operator = ckey;

buf(0);

dbuf[7] = 10;

disp();

}

else if(ckey == '=')

{

value = compute(value1,value2,operator);

buf(value);

disp();

while(1)

{

ckey = keyscan();

if(ckey == '$')

goto init;

else

{

buf(value);

disp();

}

}

}

else if(ckey == '$')

{ goto init;}

}

disp();

}

}

uint compute(uint va1,uint va2,uchar optor)

{

uint value;

switch(optor)

{

case '+' : value = va1+va2; break;

case '-' : value = va1-va2; break;

case '*' : value = va1*va2; break;

case '/' : value = va1/va2; break;

default : break;

}

return value;

}

void buf(uint val)

{

uchar i;

if(val == 0)

{

dbuf[7] = 0;

i = 6;

}

else

for(i = 7; val > 0; i--)

{

dbuf[i] = val % 10;

val /= 10;

}

for( ; i > 0; i--)

dbuf[i] = 10;

}

void disp(void)

{

uchar bsel, n;

bsel=0x01;

for(n=0;n<8;n++)

{

P2=bsel;

P0=table[dbuf[n]];

bsel=_crol_(bsel,1);

delay(3);

P0=0xff;

}

}

(4)編譯原理實現一個增強型計算器擴展閱讀:

PROTEUS 是單片機課堂教學的先進助手

PROTEUS不僅可將許多單片機實例功能形象化,也可將許多單片機實例運行過程形象化。前者可在相當程度上得到實物演示實驗的效果,後者則是實物演示實驗難以達到的效果。

它的元器件、連接線路等卻和傳統的單片機實驗硬體高度對應。這在相當程度上替代了傳統的單片機實驗教學的功能,例:元器件選擇、電路連接、電路檢測、電路修改、軟體調試、運行結果等。

課程設計、畢業設計是學生走向就業的重要實踐環節。由於PROTEUS提供了實驗室無法相比的大量的元器件庫,提供了修改電路設計的靈活性、提供了實驗室在數量、質量上難以相比的虛擬儀器、儀表,因而也提供了培養學生實踐精神、創造精神的平台

隨著科技的發展,「計算機模擬技術」已成為許多設計部門重要的前期設計手段。它具有設計靈活,結果、過程的統一的特點。可使設計時間大為縮短、耗資大為減少,也可降低工程製造的風險。相信在單片機開發應用中PROTEUS也能茯得愈來愈廣泛的應用。

使用Proteus 軟體進行單片機系統模擬設計,是虛擬模擬技術和計算機多媒體技術相結合的綜合運用,有利於培養學生的電路設計能力及模擬軟體的操作能力;

在單片機課程設計和全國大學生電子設計競賽中,我們使用 Proteus開發環境對學生進行培訓,在不需要硬體投入的條件下,學生普遍反映,對單片機的學習比單純學習書本知識更容易接受,更容易提高。

實踐證明,在使用 Proteus 進行系統模擬開發成功之後再進行實際製作,能極大提高單片機系統設計效率。因此,Proteus 有較高的推廣利用價值。

E. 用C語言設計並實現一個簡單計算器

額,搞定了。

你交給老師的時候,你要告訴他for循環的功能,for()循環體里也就是for下方{}大括弧里的代碼要被循環執行。然後你就一行一行的解釋 switch()里的語句就行了。

break表示跳出switch()。

至於int a,b,i; 這些你肯定懂了的吧。

最後那裡表示在主函數 main()里調用自定義的函數

#include <stdio.h>

int calculator() //定義一個函數。完成計算功能

{

int a,b, i;

char c;

for(i=0;;i++)

{

printf("請輸入所要計算的兩個數,以及所要執行的計算符號 ");

scanf("%d %d %c", &a,&b,&c);

switch (c)

{

case '+':

printf("所要計算的式子:%d+%d ",a,b);

a = a + b;printf("計算結果為:%d ",a);

break;

case '-':

printf("所要計算的式子:%d-%d ",a,b);

a = a - b;printf("計算結果為:%d ",a);

break;

case '*':

printf("所要計算的式子:%d*%d ",a,b);

a = a * b;printf("所要計算的式子:%d*%d ",a,b);printf("計算結果為:%d ",a);

break;

case '/':

printf("所要計算的式子:%d/%d ",a,b);

a = a / b;printf("所要計算的式子:%d/%d ",a,b);printf("計算結果為:%d ",a);

break;

}

}

}

int main()

{

calculator();//在main()函數里調用自定義的函數calculator

}

F. 編寫一個C語言程序,模擬一個計算器。要求:

支持
加減乘除括弧負數開根乘方
#include<stdio.h>
#include<math.h>
#include<malloc.h>
double
jisuan(char
a[])
{
int
i=1,j,k,m,cnt=0,t1=0,t2=0,t3=0;
char
nibo[50],zhan2[50];
double
x,n,l,z=0,zhan3[50];
typedef
struct
{
double
d1;
int
d2;
}dd;
typedef
struct
{
dd
data[50];
int
top;
}zhan1;
zhan1
*shu;
shu=(zhan1
*)malloc(sizeof(zhan1));
shu->top=0;
while(a[i]!='\0')
{
if(a[i]>='0'&&a[i]<='9')
{
z=0;
j=i+1;
while(a[j]>='0'&&a[j]<='9')
{j++;}
j--;
for(k=i;k<=j;k++)
{
z=z*10+a[k]-'0';
}
j=j+1;
x=z;
if(a[j]=='.')
{
l=1;
i=j+1;
j=i+1;
while(a[j]>='0'&&a[j]<='9')
{j++;}
j--;
for(k=i;k<=j;k++)
{
n=pow(0.1,l);
l=l+1;
x=x+n*(a[k]-'0');
}
i=j+1;
}
else
i=j;
shu->data[++shu->top].d1=x;
shu->data[shu->top].d2=++cnt;
nibo[++t1]='0'+shu->data[shu->top].d2;
nibo[t1+1]='\0';
}
else
if(a[i]=='(')
{
zhan2[++t2]=a[i];
i++;
}
else
if(a[i]==')')
{
j=t2;
while(zhan2[j]!='(')
{
nibo[++t1]=zhan2[j];
nibo[t1+1]='\0';
j--;
}
t2=j-1;
i++;
}
else
if(a[i]=='+')
{
while(t2>0&&zhan2[t2]!='(')
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
zhan2[++t2]=a[i];
i++;
}
else
if(a[i]=='-')
{
if(a[i-1]=='$')
{
a[0]='0';
i=0;
}
else
if(a[i-1]=='(')
{
a[i-1]='0';
a[i-2]='(';
i=i-2;
t2--;
}
else
{
while(t2>0&&zhan2[t2]!='(')
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
zhan2[++t2]=a[i];
i++;
}
}
else
if(a[i]=='*'||a[i]=='/')
{
while(zhan2[t2]=='*'||zhan2[t2]=='/'||zhan2[t2]=='^'||zhan2[t2]=='#')
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
zhan2[++t2]=a[i];
i++;
}
else
if(a[i]=='^'||a[i]=='#')
{
while(zhan2[t2]=='^'||zhan2[t2]=='#')
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
zhan2[++t2]=a[i];
i++;
}
}
while(t2>0)
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
j=1;t3=0;
while(j<=t1)
{
if(nibo[j]>='0'&&nibo[j]!='^'&&nibo[j]!='#')//
{
for(i=1;i<=shu->top;i++)
{
if((int)(nibo[j]-'0')==shu->data[i].d2)
{
m=i;
break;
}
}
zhan3[++t3]=shu->data[m].d1;
}
else
if(nibo[j]=='+')
{
zhan3[t3-1]=zhan3[t3-1]+zhan3[t3];
t3--;
}
else
if(nibo[j]=='-')
{
zhan3[t3-1]=zhan3[t3-1]-zhan3[t3];
t3--;
}
else
if(nibo[j]=='*')
{
zhan3[t3-1]=zhan3[t3-1]*zhan3[t3];
t3--;
}
else
if(nibo[j]=='/')
{
zhan3[t3-1]=zhan3[t3-1]/zhan3[t3];
t3--;
}
else
if(nibo[j]=='^')
{
zhan3[t3-1]=pow(zhan3[t3-1],zhan3[t3]);
t3--;
}
else
if(nibo[j]=='#')
{
zhan3[t3]=sqrt(zhan3[t3]);
}
j++;
}
return
zhan3[t3];
}
void
main()
{
for(;;)
{
char
x,a[50];
double
jieguo;
int
i=0;
a[0]='$';
printf("#表示開方,^表示乘方(支持負數)\n");
printf("請輸入表達式,退出請輸入q:\n\n");
scanf("%c",&x);
if(x=='q')
break;
while(x!='\n')
{
a[++i]=x;
scanf("%c",&x);
}
a[i+1]='\0';
jieguo=jisuan(a);
printf("\n");
printf("結果為:%lf",jieguo);
printf("\n\n\n\n\n");
}
}

G. 求按要求用C語言作的一個計算器

計算器的c語言程序設計
一.功能概述:本程序能計算帶二重括弧的計算式的結果,如輸入((3+2)*(3+2)+5)*((3+2)*(3+2)+5)*(5+5)= 將輸出結果為9000
二.源程序:
#include<stdio.h>
int w;
double B[4][40];

shuru(char *pt)
{
int i=0,m;
do
{
*(pt+i)=getchar();
m=i;
i++;
}
while(*(pt+m)!='=');
*(pt+m)='+';
*(pt+m+1)='=';
}

tlchli(char *p)
{
int a,b;
if(*p=='-'||*p=='+')
{
for(b=0;*(p+b)!='=';b++)
;
for(;b>=0;b--)
*(p+b+1)=*(p+b);
*p='0';
}
else
{
for(a=1;*(p+a)!='=';a++)
{
if(*(p+a)=='-'||*(p+a)=='+')
{
if(*(p+a-1)=='('&&*(p+a+1)>='0'&&*(p+a+1)<='9')
{
for(b=0;*(p+a+b)!='=';b++)
;
for(;b>=0;b--)
*(p+a+b+1)=*(p+a+b);
*(p+a)='0';
a=(a+1);
}
}
}
}
}

jgh(double B[4][40],char *p)
{
int a,b,c=0,d,e,f,h,i,k,n,m;
double g=0,j=0;
for(a=0;a<4;a++)
for(b=0;b<40;b++)
B[a]=0;
for(d=0;*(p+d)!='=';d++)
{
if(*(p+d)=='+'||*(p+d)=='-'||*(p+d)=='*'||*(p+d)=='/')
{
if(*(p+d)=='+') B[3][c]='+';
if(*(p+d)=='-') B[3][c]='-';
if(*(p+d)=='*') B[3][c]='*';
if(*(p+d)=='/') B[3][c]='/';
if(*(p+d-1)==')'&&*(p+d-2)==')')
{
B[0][c]=')';
B[1][c]=')';
}
if(*(p+d-1)==')'&&*(p+d-2)!=')')
B[1][c]=')';
for(e=d-1;*(p+e)<'0'||*(p+e)>'9';e--)
;
for(f=e;*(p+f)!='.'&&(f>0)&&*(p+f)>='0'&&*(p+f)<='9';f--)
;
if(*(p+f)=='.')
{
for(h=f-1;(h>=0)&&*(p+h)>='0'&&*(p+h)<='9';h--)
;
h++;
if(*(p+h-1)=='('&&*(p+h-2)=='(')
{
B[0][c]='(';
B[1][c]='(';
}
if(*(p+h-1)=='('&&*(p+h-2)!='(')
B[1][c]='(';
for(i=h;*(p+i)>='0'&&*(p+i)<='9';i++)
g=(*(p+i)-48)+g*10.0;
for(k=f+1;*(p+k)>='0'&&*(p+k)<='9';k++)
;
k--;
for(n=k;*(p+n)>='0'&&*(p+n)<='9';n--)
j=(j/10.0+(*(p+n)-48)/10.0);
B[2][c]=(g+j);
g=j=0;
}
else
{
for(m=e;*(p+m)>='0'&&*(p+m)<='9';m--);
m++;
if(*(p+m-1)=='('&&*(p+m-2)=='(')
{
B[0][c]='(';
B[1][c]='(';
}
if(*(p+m-1)=='('&&*(p+m-2)!='(')
B[1][c]='(';
for(i=m;*(p+i)>='0'&&*(p+i)<='9';i++)
g=(*(p+i)-48)+g*10.0;
B[2][c]=g;
g=0;
}
c++;
}
}
w=(c-1);
}

qkh1(double B[4][40])
{
int a=1,b,c,d,e,f,j,k;
for(b=0;b<w;b++)
{
if(B[a]=='(')
{
for(c=b;B[a][c]!=')';c++)
;
for(d=b+1;B[a][d]!='('&&d<c;d++)
;
if(B[a][d]=='(')
k=d;
else
k=b;
B[a][k]=0;B[a][c]=0;
for(e=k;e<c;e++)
{
if(B[3][e]=='*')
{
B[2][e+1]=B[2][e]*B[2][e+1];
if(B[3][e-1]=='-'&&(e>0))
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}

}
if(B[3][e]=='/')
{
B[2][e+1]=B[2][e]/B[2][e+1];
if(B[3][e-1]=='-'&&e>0)
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
}
for(f=k;f<c;f++)
{
if(B[3][f]=='+')
{
B[2][f+1]=B[2][f]+B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
if(B[3][f]=='-')
{
B[2][f+1]=B[2][f]-B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
}
b=c-1;
if(B[3][k-1]=='*'&&k>0)
{
for(;k<c;k++)
{
B[3][k]='*';
B[2][k]=1.0;
}
}
if(B[3][k-1]=='/'&&k>0)
{
for(;k<c;k++)
{
B[3][k]='/';
B[2][k]=1.0;
}
}
}
}
for(j=0;j<=w;j++)
{
if(B[1][j]!='('&&B[1][j]!=')')
B[1][j]=B[0][j];
}
}

qkh2(double B[4][40])
{
int a,b,c,d,e,f,j;
for(b=0;b<w;b++)
{
if(B[1]=='(')
{
for(c=b;B[1][c]!=')';c++)
;
B[1]=0;B[1][c]=0;
for(e=b;e<c;e++)
{
if(B[3][e]=='*')
{
B[2][e+1]=B[2][e]*B[2][e+1];
if(B[3][e-1]=='-'&&(e>0))
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
if(B[3][e]=='/')
{
B[2][e+1]=B[2][e]/B[2][e+1];
if(B[3][e-1]=='-'&&(e>0))
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
}
for(f=b;f<c;f++)
{
if(B[3][f]=='+')
{
B[2][f+1]=B[2][f]+B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
if(B[3][f]=='-')
{
B[2][f+1]=B[2][f]-B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
}
if(B[3][b-1]=='*'&&b>0)
{
for(;b<c;b++)
{
B[3]='*';
B[2]=1.0;
}
}
if(B[3][b-1]=='/'&&b>0)
{
for(;b<c;b++)
{
B[3]='/';
B[2]=1.0;
}
}
}
}
}

jshjg(double B[4][40])
{
int b,d,e,f;
for(e=0;e<w;e++)
{
if(B[3][e]=='*')
{
B[2][e+1]=B[2][e]*B[2][e+1];
if(B[3][e-1]=='-'&&e>0)
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
if(B[3][e]=='/')
{
B[2][e+1]=B[2][e]/B[2][e+1];
if(B[3][e-1]=='-'&&e>0)
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
}
for(f=0;f<w;f++)
{
if(B[3][f]=='+')
{
B[2][f+1]=B[2][f]+B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
if(B[3][f]=='-')
{
B[2][f+1]=(B[2][f]-B[2][f+1]);
B[2][f]=0;B[3][f]='+';
}
}
}

shuchu(double B[4][40],char *p)
{
printf("answer %f\n",B[2][w]);
}

main()
{
char *p,A[100];
shuru(A);
p=&A[0];
tlchli(p);
jgh(B,p);
qkh1(B);
qkh2(B);
jshjg(B);
shuchu(B,p);
}

#include"string.h"
#include"graphics.h"
#include"conio.h"
#include"dos.h"
#include"time.h"
#include"stdlib.h"
#include"stdio.h"
#include"math.h"
/*調用一系列的庫函數*/
int py;/*定義一個全局變數*/
void mode(int py)/*mode函數,用來設置的,來使用計算器的多種運算功能*/
{ double x1,x2,ans,ans2;
if(py<11)
{ printf("input x1,x2:");
scanf("%lf,%lf",&x1,&x2); }
else {printf("input x:");scanf("%lf",&x1);}
switch(py)
{ case 6 : ans=x1+x2;printf("=%.3lf",ans);break;
case 7 : ans=x1-x2;printf("=%.3lf",ans);break;
case 8 : ans=x1*x2;printf("=%.3lf",ans);break;
case 9 : ans=x1/x2;printf("=%.3lf",ans);break;
case 10 : ans=pow(x1,x2);printf("x^%.1lf=%.3lf",x2,ans);break;
case 11 : ans=sqrt(x1);printf("=%.3lf",ans);break;
case 12 : ans=sin(x1);ans2=cos(x1);printf("sin x=%.3lf,cos x=%.3lf",ans,ans2);break;
case 13 : ans=asin(x1);ans2=acos(x1);printf("asin x=%.3lf,acos x=%.3lf",ans,ans2);break;
}
getch();clrscr();
}
int join(char *ps) /*join函數是輸入用戶名和密碼的,可於已注冊的用戶進行匹配,注冊用戶可使用高級功能*/
{ char name[30],pass[20];
int len,i;
printf("ID:");
scanf("%s",name);
printf("\npass word:");
scanf("%s",pass);
clrscr();
for(i=0;i<strlen(ps);i++)
if(pass[i]!=*(ps+i)) return 0;
return 1;
}

int menu(int pass)/*menu函數,菜單*/
{int py=6;char key=0;
gotoxy(1,4);
printf("MENU\n\n");
printf("ADD +\n");
printf("SUB -\n");
printf("MUL *\n");
printf("DIV /");
gotoxy(10,6);printf("<-");
if(pass) {gotoxy(1,10);printf("INV ^\n");
printf("EVO ~\n");
printf("SIN COS\n");
printf("ASIN ACOS\n");
printf("modify\n");
printf("QUIT"); }
else {gotoxy(1,10);printf("QUIT");}
while(key!=13) /*以下都是控制選擇箭頭的位子的語句*/
{ key=getch();
switch(key)
{ case 80 : gotoxy(10,py);printf(" ");if(!pass) py=py%5+6; else {++py;if(py==16) py=6;}break;

case 72 : gotoxy(10,py);printf(" ");--py; if(py==5) py=10+5*pass; break;
}
gotoxy(10,py);printf("<-");
}
return py;
}

main() /*主函數,關於函數分配和開始動畫的語句*/
{ int gd=DETECT,gm,x=320,y=10,i,l=10,h=10,t,co=3,v=0;
int pass;
FILE *fp;char ps[20];
initgraph(&gd,&gm,"");/*初始化圖形模式*/
registerbgidriver(EGAVGA_driver);
for(i=1;i<31;co=(co++%10),v++,y+=2+v,i++)
{ delay(9e10);cleardevice();
setcolor(co);
setfillstyle(1,co);
sector(x,y,0,360,l,h);/*畫圓*/
}
for(i=1;i<300;i++)
{ setcolor(i%10);
circle(x,y,i);
delay(1000);/*延時*/
}
cleardevice();/*清屏*/
closegraph();
fp=fopen("file1","r");
if(fp==NULL) pass=1;
else {fgets(ps,20,fp);
pass=join(ps);}
ld: py=menu(pass);
if(py==pass*5+10) {clrscr();printf("bye bye!");getch();return 0;}
else if(py!=14){clrscr();mode(py);} else { fp=fopen("file1","w");
printf("\npass word:");
scanf("%s",ps);
fputs(ps,fp);
fclose(fp); getch();clrscr();
}
goto ld; /*函數內跳轉*/
}
#include <stdio.h> /*庫函數*/
struct s_node
{
int data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;

link push(link stack,int value)
{
link newnode;

newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{
printf("\nMemory allocation failure!!!");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack;
}

link pop(link stack,int *value)
{
link top;
if(stack !=NULL)
{
top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack;
}
else
*value=-1;
}

int empty(link stack)
{
if(stack==NULL)
return 1;
else
return 0;

}

int is_operator(char operator)
{
switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}

int priority(char operator)
{
switch(operator)
{
case '+': case '-' : return 1;
case '*': case '/' : return 2;
default: return 0;
}
}

int two_result(int operator,int operand1,int operand2)
{
switch(operator)
{
case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1);
}
}

void main()
{
char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;

printf("\nPlease input the inorder expression:");
gets(expression);

while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);

operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
}

H. C語言程序設計 簡單計算器(急急急!!!!)

#include <dos.h> /*DOS介面函數*/
#include <math.h> /*數學函數的定義*/
#include <conio.h> /*屏幕操作函數*/
#include <stdio.h> /*I/O函數*/
#include <stdlib.h> /*庫函數*/
#include <stdarg.h> /*變數長度參數表*/
#include <graphics.h> /*圖形函數*/
#include <string.h> /*字元串函數*/
#include <ctype.h> /*字元操作函數*/
#define UP 0x48 /*游標上移鍵*/
#define DOWN 0x50 /*游標下移鍵*/
#define LEFT 0x4b /*游標左移鍵*/
#define RIGHT 0x4d /*游標右移鍵*/
#define ENTER 0x0d /*回車鍵*/
void *rar; /*全局變數,保存游標圖象*/
struct palettetype palette; /*使用調色板信息*/
int GraphDriver; /* 圖形設備驅動*/
int GraphMode; /* 圖形模式值*/
int ErrorCode; /* 錯誤代碼*/
int MaxColors; /* 可用顏色的最大數值*/
int MaxX, MaxY; /* 屏幕的最大解析度*/
double AspectRatio; /* 屏幕的像素比*/
void drawboder(void); /*畫邊框函數*/
void initialize(void); /*初始化函數*/
void computer(void); /*計算器計算函數*/
void changetextstyle(int font, int direction, int charsize); /*改變文本樣式函數*/
void mwindow(char *header); /*窗口函數*/
int specialkey(void) ; /*獲取特殊鍵函數*/
int arrow(); /*設置箭頭游標函數*/
/*主函數*/
int main()
{
initialize();/* 設置系統進入圖形模式 */
computer(); /*運行計算器 */
closegraph();/*系統關閉圖形模式返迴文本模式*/
return(0); /*結束程序*/
}
/* 設置系統進入圖形模式 */
void initialize(void)
{
int xasp, yasp; /* 用於讀x和y方向縱橫比*/
GraphDriver = DETECT; /* 自動檢測顯示器*/
initgraph( &GraphDriver, &GraphMode, "" );
/*初始化圖形系統*/
ErrorCode = graphresult(); /*讀初始化結果*/
if( ErrorCode != grOk ) /*如果初始化時出現錯誤*/
{
printf("Graphics System Error: %s\n",
grapherrormsg( ErrorCode ) ); /*顯示錯誤代碼*/
exit( 1 ); /*退出*/
}
getpalette( &palette ); /* 讀面板信息*/
MaxColors = getmaxcolor() + 1; /* 讀取顏色的最大值*/
MaxX = getmaxx(); /* 讀屏幕尺寸 */
MaxY = getmaxy(); /* 讀屏幕尺寸 */
getaspectratio( &xasp, &yasp ); /* 拷貝縱橫比到變數中*/
AspectRatio = (double)xasp/(double)yasp;/* 計算縱橫比值*/
}
/*計算器函數*/
void computer(void)
{
struct viewporttype vp; /*定義視口類型變數*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作數和計算結果變數*/
char cnum[5],str2[20]={""},c,temp[20]={""};
char str1[]="1230.456+-789*/Qc=^%";/* 定義字元串在按鈕圖形上顯示的符號 */
mwindow( "Calculator" ); /* 顯示主窗口 */
color = 7; /*設置灰顏色值*/
getviewsettings( &vp ); /* 讀取當前窗口的大小*/
width=(vp.right+1)/10; /* 設置按鈕寬度 */
height=(vp.bottom-10)/10 ; /*設置按鈕高度 */
x = width /2; /*設置x的坐標值*/
y = height/2; /*設置y的坐標值*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
/*畫一個二維矩形條顯示運算數和結果*/
setcolor( color+3 ); /*設置淡綠顏色邊框線*/
rectangle( x+width*2, y, x+7*width, y+height );
/*畫一個矩形邊框線*/
setcolor(RED); /*設置顏色為紅色*/
outtextxy(x+3*width,y+height/2,"0."); /*輸出字元串"0."*/
x =2*width-width/2; /*設置x的坐標值*/
y =2*height+height/2; /*設置y的坐標值*/
for( j=0 ; j<4 ; ++j ) /*畫按鈕*/
{
for( i=0 ; i<5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); /*畫一個矩形條*/
rectangle( x, y, x+width, y+height );
sprintf(str2,"%c",str1[j*5+i]);
/*將字元保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; /*移動列坐標*/
}
y +=(height/2)*3; /* 移動行坐標*/
x =2*width-width/2; /*復位列坐標*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); /*移動游標到x,y位置*/
arrow(); /*顯示游標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*設置str2為空串*/
while((v=specialkey())!=45) /*當壓下Alt+x鍵結束程序,否則執行下面的循環*/
{
while((v=specialkey())!=ENTER) /*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT); /*顯示游標圖象*/
if(v==RIGHT) /*右移箭頭時新位置計算*/
if(x>=x0+6*width)
/*如果右移,移到尾,則移動到最左邊字元位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*否則,右移到下一個字元位置*/
if(v==LEFT) /*左移箭頭時新位置計算*/
if(x<=x0)
{
x=x0+6*width;
m=4;
} /*如果移到頭,再左移,則移動到最右邊字元位置*/
else
{
x=x-width-width/2;
m--;
} /*否則,左移到前一個字元位置*/
if(v==UP) /*上移箭頭時新位置計算*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
} /*如果移到頭,再上移,則移動到最下邊字元位置*/
else
{
y=y-height-height/2;
n--;
} /*否則,移到上邊一個字元位置*/
if(v==DOWN) /*下移箭頭時新位置計算*/
if(y>=7*height)
{
y=y0;
n=0;

I. 如何用VC編寫一個計算器程序

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define
N
26
/*根據自己的需要,可以定義要進行運算的數字的最大個數*/
int
main(void)
{
int
i,j,k;
double
num[N];
char
sym[N],str[12];
loop1:
printf("Input
a
calculation
method
like
1+2^3-4*5/10=↙\nPlease:");
/*可以進行加、減、乘、除、乘方的運算*/
for(i=0;i<N;i++)
{
scanf("%lf%c",&num[i],&sym[i]);
/*num[N]用來存儲運算數字,sym[N]用來存儲運算符號*/
if(sym[i]=='=')
break;
/*當出現等號時,表示計算式結束*/
}
for(j=0;j<=i;j++)
if(sym[j]=='^')
/*判斷優先順序中的乘方,它是首先要處理的*/
{
if(num[j]==0&&num[j+1]==0)
{printf("Input
error!\n");goto
loop2;}
/*指數為零時,底數不為零。否則,進入標記位置*/
else
num[j]=pow(num[j],num[j+1]);
/*將本次出現的乘方運算結果,放在前一項中*/
for(k=j;k<i;k++)
{
sym[k]=sym[k+1];
/*依次將後面的符號替換到前面,因為符號中有"=",所以要多替換1次*/
if(k!=i-1)
num[k+1]=num[k+2];
/*依次將後面的數字替換到前面*/
}
j--,i--;
/*"j--"表示下一次從本次的位置開始繼續判斷是否為優先順序中的乘方,因為本次位置替換後的符號仍然可能是"^",
"i--"表示下次處理的數字將減少1個*/
}
for(j=0;j<=i;j++)
{
if(sym[j]=='*')
/*當計算式中沒有"^"時,將開始判斷優先順序中的"*",它是其次要處理的*/
{
num[j]=num[j]*num[j+1];
/*將本次出現的乘法運算結果,放在前一項中*/
for(k=j;k<i;k++)
{
sym[k]=sym[k+1];
/*依次將後面的符號替換到前面,因為符號中有"=",所以要多替換1次*/
if(k!=i-1)
num[k+1]=num[k+2];
/*依次將後面的數字替換到前面*/
}
j--,i--;
/*"j--"表示下一次從本次的位置開始繼續判斷是否為優先順序中的乘法,因為本次位置替換後的符號仍然可能是"*",
"i--"表示下次處理的數字將減少1個*/
}
if(sym[j]=='/')
/*當計算式中沒有"^"時,將開始判斷優先順序中的"/",它也是其次要處理的*/
{
if(num[j+1]==0)
{printf("Input
error!\n");goto
loop2;}
/*分母不為零。否則,進入標記位置*/
else
num[j]=num[j]/num[j+1];
/*將本次出現的除法運算結果,放在前一項中*/
for(k=j;k<i;k++)
{
sym[k]=sym[k+1];
/*依次將後面的符號替換到前面,因為符號中有"=",所以要多替換1次*/
if(k!=i-1)
num[k+1]=num[k+2];
/*依次將後面的數字替換到前面*/
}
j--,i--;
/*"j--"表示下一次從本次的位置開始繼續判斷是否為優先順序中的除法,因為本次位置替換後的符號仍然可能是"/",
"i--"表示下次處理的數字將減少1個*/
}
}
for(j=0;j<=i;j++)
switch(sym[j])
/*最後只剩"+"和"-"了,將每次運算的結果放在後一項中*/
{
case
'+':num[j+1]=num[j]+num[j+1];break;
case
'-':num[j+1]=num[j]-num[j+1];break;
case
'=':break;
}
gcvt(num[i],12,str);
/*將浮點數轉換成字元串*/
for(j=0;j<12;j++)
if(str[j]=='.'&&!str[j+1])
str[j]=str[j+1];
/*當小數點後緊接字元串結束標志時,原本應該輸出如"5.",處理後為"5"*/
printf("Result=%s\n",str);
/*計算式的結果,最終存放在最後一項的字元串中*/
loop2:
printf("Continue(y/n)?");
/*是否繼續,y表示繼續,n表示退出*/
switch(getch())
{
case
'y':{system("cls");goto
loop1;}
/*點擊y,清空屏幕,返回標記位置*/
case
'n':exit(0);
/*點擊n,退出程序*/
default
:exit(0);
/*點擊其它鍵,也退出程序*/
}
getch();
return
0;
}
說明:必須將擴展名設置為c,若是cpp,將無法通過系統的編譯。

J. c語言設計一個簡單的計算器程序

#include<stdio.h>//計算器

voidmenu()//自定義的菜單界面

printf("--------------------\n");

printf("請輸入你的選擇\n");

printf("1.+\n");

printf("2.-\n");

printf("3.*\n");

printf("4./\n");

printf("--------------------\n");

intmain()

inti=0;

intj=0;

intnum=0;//計算結果存放在nun

intselect=0;//選擇的選項存放在select

do//do-while先執行再判斷循環條件,即可實現重復計算功能

menu();//列印出菜單界面

scanf("%d",&select);//輸入你的選項

printf("請輸入計算值:");

scanf("%d%d",&i,&j);//輸入要計算的數值

switch(select)

case1:

printf("%d+%d=%d\n",i,j,num=i+j);//實現加法功能

break;

case2:

printf("%d-%d=%d\n",i,j,num=i-j);//實現減法功能

break;

case3:

printf("%d*%d=%d\n",i,j,num=i*j);//實現乘法功能

break;

case4:

printf("%d-%d=%d\n",i,j,num=i/j);//實現除法功能

break;

default:

printf("輸入有誤重新選擇");

break;

}while(select);

return0;

運行結果:

(10)編譯原理實現一個增強型計算器擴展閱讀:

return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。

return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。

閱讀全文

與編譯原理實現一個增強型計算器相關的資料

熱點內容
好興動app還款怎麼登錄不上去了 瀏覽:665
鄭州雲伺服器託管 瀏覽:722
伺服器地址跟蹤 瀏覽:980
免費google雲伺服器 瀏覽:516
摘譯和編譯的英文 瀏覽:359
熱泵壓縮機選型 瀏覽:121
op手機微信加密如何解除 瀏覽:386
如何在王牌戰爭找到高爆率伺服器 瀏覽:13
江浙小學語文輔導課用什麼APP 瀏覽:99
新夢幻大陸伺服器地址 瀏覽:241
網吧伺服器怎麼更換壁紙 瀏覽:530
linux命令方法 瀏覽:332
linux下載freetype 瀏覽:123
程序員入駐平台 瀏覽:327
程序員大戰外掛 瀏覽:745
html實例教程pdf 瀏覽:157
linux命令開放所有許可權 瀏覽:575
30歲能學會編程 瀏覽:737
小火箭的伺服器是什麼 瀏覽:967
cad查信息命令 瀏覽:402