简单的用c语言写一个计算器程式,加减乘除能用就好

简单的用c语言写一个计算器程式,加减乘除能用就好,第1张

简单的用c语言写一个计算器程式,加减乘除能用就好 #include"stdio.h"

void main()

{

float a,b,c

char e

printf("input a,e,b\n")/*输入两个数和符号,例如3+8*/

scanf("%f%c%f",&a,&e,&b)

switch(e)

{

case '+':c=a+bbreak

case '-':c=a-bbreak

case '*':c=a*bbreak

case '/':

if(b==0.0) printf("error\n")

else c=a/bbreak

}

printf("%f%c%f=%f",a,e,b,c)

}

如何用vc++编写一个简单的(只有加减乘除)计算器程式?

先设定介面如下

加法按钮程式码

void CMy03Dlg::OnBnClickedButton1()

{

TODO:在此新增控制元件通知处理程式程式码

UpdateData(TRUE)

m_Nub3=m_Nub1+m_Nub2

UpdateData(FALSE)

}

减法按钮程式码

void CMy03Dlg::OnBnClickedButton2()

{

TODO:在此新增控制元件通知处理程式程式码

UpdateData(TRUE)

m_Nub3=m_Nub1-m_Nub2

UpdateData(FALSE)

}

乘法按钮程式码

void CMy03Dlg::OnBnClickedButton3()

{

TODO:在此新增控制元件通知处理程式程式码

UpdateData(TRUE)

m_Nub3=m_Nub1*m_Nub2

UpdateData(FALSE)

}

除法按钮程式码

void CMy03Dlg::OnBnClickedButton4()

{

TODO:在此新增控制元件通知处理程式程式码

UpdateData(TRUE)

if(m_Nub2!=0)

m_Nub3=m_Nub1 / m_Nub2

else

AfxMessageBox("被除数不能为0")

UpdateData(FALSE)

}

清除按钮程式码

void CMy03Dlg::OnBnClickedButton5()

{

TODO:在此新增控制元件通知处理程式程式码

UpdateData(TRUE)

m_Nub3=0

m_Nub1=0

m_Nub2=0

UpdateData(FALSE)

}

结束按钮程式码

void CMy03Dlg::OnBnClickedButton6()

{

TODO:在此新增控制元件通知处理程式程式码

CDialog::OnOK()

}

如果只允许在输入框中输入资料应该怎样处理?

制作托盘程式

目的:在工作列中建立一个图示,使该程式永远驻留在记忆体中。例如邮件检查程式可以作为驻留程式,一旦有邮件来了,就可以接收邮件。

Shell_NotifyIcon函式传送讯息来增加、删除、修改工作列的图示

BOOL TrayMessage(HWND hWnd, DWORD dwMessage, HICON hIcon, PSTR pszTip)

{

BOOL res

NOTIFYICONDATA tnd

tnd.cbSize = sizeof(NOTIFYICONDATA)

tnd.hWnd = hWnd

tnd.uID = IDI_ICON1

tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP

tnd.uCallbackMessage = WM_MY_TRAY_NOTIFICATION

tnd.hIcon = hIcon

lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip))

res = Shell_NotifyIcon(dwMessage, &tnd)dwMessage为NIM_ADD从工作列中新增图示、NIM_DELETE从工作列中删除图示、NIM_MODIFY改变工作列中图示

if (hIcon)

DestroyIcon(hIcon)

return res

}

定义一个回拨讯息:WM_MY_TRAY_NOTIFICATION

在DLG的CPP档案中,

#define WM_MY_TRAY_NOTIFICATION WM_USER+100

为对话方块新增讯息对映ON_MESSAGE(WM_MY_TRAY_NOTIFICATION,OnTrayNotification)

在DLG的标头档案中应该有

public:

long m_Nub1

float m_Nub3

CBitmapButton Button

afx_msg void OnBnClickedButton1()

long m_Nub2

afx_msg void OnBnClickedButton2()

afx_msg void OnBnClickedButton4()

afx_msg void OnBnClickedButton5()

afx_msg void OnBnClickedButton3()

afx_msg void OnBnClickedButton6()

afx_msg void OnBnClickedButton7()

afx_msg LRESULT OnTrayNotification(WPARAM wparam, LPARAM lparam)

在DLG的CPP档案中应该有

BEGIN_MESSAGE_MAP(CMailCheckDlg, CDialog)

……

ON_MESSAGE(WM_MY_TRAY_NOTIFICATION,OnTrayNotification)

……

END_MESSAGE_MAP()

并定义一个回拨讯息函式

LRESULT CMailCheckDlg::OnTrayNotification(WPARAM wparam, LPARAM lparam)

{

switch (lparam )

{

case WM_RBUTTONUP:

case WM_LBUTTONDBLCLK:修改不同的按钮处理事件,以观察图示退出效果。

ShowWindow(SW_SHOW)

TrayMessage(m_hWnd, NIM_DELETE, NULL, "")从工作列中删除图示

}

return 0

}

在对话方块视窗上新增“驻留”按钮,双击按钮新增程式码

void CMailCheckDlg::OnBnClickedButton1()

{

TODO:在此新增控制元件通知处理程式程式码

下面程式向工作列新增图示

TrayMessage(m_hWnd, NIM_ADD, NULL, "计算器程式")

TrayMessage(m_hWnd, NIM_MODIFY, m_hIcon, "计算器程式")

ShowWindow(SW_HIDE)

用MFC编写一个简单的加减乘除计算器

我有程式,加31782771群

c语言计算器程式设计包含加减乘除简单的函式运算

实用计算器之程式设计

[摘 要]多用计算器的构思及设计程式码

[关键词]多用计算器;设计

数值计算可以说是日常最频繁的工作了,WIN98提供了“计算器”软体供使用者使用,该软体可以处理一般的一步四则运算,例如:3+2、5/3等等,但在日常中使用者经常遇到多步四则运算问题,例如:3+4*5-4/2,45*34/2+18*7等等,那么该个计算器就无法胜任了,作者制作了一个实用的计算器,该计算器新增不少功能:(程式介面如图)

1.可以实现连续的四则运算

2.可以实现输入式子的显示

3.可以方便计算个人所得税

4.滑鼠、键盘均可输入资料

5. *** 作介面友好

6.击键可发声

构建该个计算器所需研究及解决的核心问题有如下几个:1、连乘求值?2、字元显示 3、键盘输入?4、击键发声?5、个人所得税法规,为了使大家对程式有更一步认识,现将程式码提供给读者参考:

*定义阵列及窗体变数

Dim number2(0 To 50) As Double

Dim number(0 To 50) As Double

Dim z As Integer

Dim k As Integer, r As Integer

Dim j As Integer

Dim str As String

*呼叫名为“playsound”的API函式

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Private Const SND_FILENAME = &H20000?

Private Const SND_ASYNC = &H1?

Private Const SND_SYNC = &H0

*判断通用过程

Sub pianduan(p As String)

r = 0

Dim i As Integer, l As Integer, h As Integer

h = 0

i = 1

If InStr(Trim$(p), "*") <>0 Then

k = k + 1

End If

If InStr(Trim$(p), "/") <>0 Then

r = r + 1

End If

End Sub

*连乘通用过程(略)

*各按钮事件过程

Private sub Command1_Click(Index As Integer)

PlaySound App.Path &"\start.wav", 0, SND_SYNC

Text1.Text = Text1.Text + Command1(Index).Caption

Text2.Text = Text2.Text + Command1(Index).Caption

Text1.SetFocus

End Sub

rivate sub Command10_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

str = Text3.Text

End Sub

Private sub Command11_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

Text3.Text = str

End Sub

rivate sub Command2_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

Dim totle As Double

Dim n As Integer

Call pianduan(Text1.Text)

If k >= 1 Or r >= 1 Then

Call liancheng(totle)

number2(z) = totle

If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then

number2(z) = -totle

End If

k = 0: r = 0

Else

number2(z) = Val(Text1.Text)

End If

Text1.Text = ""

Text2.Text = Text2 + "+"

z = z + 1

Text1.SetFocus

End Sub

rivate sub Command3_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

Dim totle As Double

Dim n As Integer

Call pianduan(Text1.Text)

If k >= 1 Or r >= 1 Then

Call liancheng(totle)

number2(z) = totle

If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then

number2(z) = -totle

End If

k = 0: r = 0

Else

number2(z) = Val(Text1.Text)

End If

Text1.Text = ""

Text2.Text = Text2 + "-"

Text1.Text = Text1.Text &"-"

z = z + 1

Text1.SetFocus

End Sub

Private sub Command4_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

Text2.Text = Text2.Text + "*"

Text1.Text = Text1.Text + "*"

Text1.SetFocus

End Sub

rivate sub Command5_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

Text2.Text = Text2 + "/"

Text1.Text = Text1 + "/"

Text1.SetFocus

End Sub

Private sub Command6_Click()

PlaySound App.Path &"\sound.wav", 0, SND_SYNC

Dim totle As Double

Dim n As Integer

Call pianduan(Text1.Text)

If k >= 1 Or r >= 1 Then

Call liancheng(totle)

number2(z) = totle

If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then

number2(z) = -totle

End If

k = 0: r = 0

Else

number2(z) = Val(Text1.Text)

End If

Text1.Text = ""

z = z + 1

Dim dengyu As Double

Dim v As Integer

For v = 0 To 50

dengyu = dengyu + number2(v)

Next v

If dengyu <0 Then

Text3.ForeColor = &HFF&

Else

Text3.ForeColor = &HFF0000

End If

Text3.Text = dengyu

Text1.SetFocus

If Len(Text3.Text) >= 14 Then

calcresult.Show

End If

End Sub

rivate sub Command7_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

z = 0: k = 0: r = 0: j = 0

Dim i As Integer

For i = 0 To 50

number(i) = 0

number2(i) = 0

Next i

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text1.SetFocus

End Sub

rivate sub Command8_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

If Val(Text3.Text) = 0 Then

MsgBox "除数不能为0!"

Exit Sub

End If

Text3.Text = 1 / Val(Text3.Text)

End Sub

Private sub Command9_Click()

PlaySound App.Path &"\start.wav", 0, SND_SYNC

Text3.ForeColor = &HFF0000

Text3.Text = Val(Text3.Text) * Val(Text3.Text)

End Sub

rivate sub muninter_Click()

Dim i

i = Shell("C:\Program Files\InterExplorer\iexplore.exe", vbMaximizedFocus)

End Sub

rivate sub munmp3_Click()

Dim i

i = Shell("C:\Program Files\Windows Media Player\mplayer2", vbNormalNoFocus)

End Sub

Private sub mun *** _Click()

Dialog.Show

End Sub

rivate sub muntax_Click()

tax.Show

End Sub

rivate sub munver_Click()

ver.Show

End Sub

rivate sub notepad_Click()

Dim i

i = Shell("c:\windows\notepad", vbNormalFocus)

End Sub

Private sub Text1_KeyPress(KeyAscii As Integer)

PlaySound App.Path &"\start.wav", 0, SND_SYNC

Dim num As Integer

num = Val(KeyAscii)

If num >47 And num <58 Then

Text1.Text = Text1.Text + CStr(num - 48)

Text2.Text = Text2.Text + CStr(num - 48)

End If

If num = 46 Then

Text1.Text = Text1.Text + "."

Text2.Text = Text2.Text + "."

End If

If KeyAscii = 43 Then

Dim totle As Double

Dim n As Integer

Call pianduan(Text1.Text)

If k >= 1 Or r >= 1 Then

Call liancheng(totle)

number2(z) = totle

If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then

number2(z) = -totle

End If

k = 0: r = 0

Else

number2(z) = Val(Text1.Text)

End If

Text1.Text = ""

Text2.Text = Text2 + "+"

z = z + 1

End If

If KeyAscii = 45 Then

Call pianduan(Text1.Text)

If k >= 1 Or r >= 1 Then

Call liancheng(totle)

number2(z) = totle

If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then

number2(z) = -totle

End If

k = 0: r = 0

Else

number2(z) = Val(Text1.Text)

End If

Text1.Text = ""

Text2.Text = Text2 + "-"

Text1.Text = Text1.Text &"-"

z = z + 1

End If

If KeyAscii = 42 Then

Text2.Text = Text2.Text + "*"

Text1.Text = Text1.Text + "*"

End If

If KeyAscii = 47 Then

Text2.Text = Text2.Text + "/"

Text1.Text = Text1.Text + "/"

End If

If KeyAscii = vbKeyReturn Then

PlaySound App.Path &"\sound.wav", 0, SND_SYNC

Call pianduan(Text1.Text)

If k >= 1 Or r >= 1 Then

Call liancheng(totle)

number2(z) = totle

If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then

number2(z) = -totle

End If

k = 0: r = 0

Else

number2(z) = Val(Text1.Text)

End If

Text1.Text = ""

z = z + 1

Dim dengyu As Double

Dim v As Integer

For v = 0 To 50

dengyu = dengyu + number2(v)

Next v

If dengyu <0 Then

Text3.ForeColor = &HFF&

Else

Text3.ForeColor = &HFF0000

End If

Text3.Text = dengyu

End If

If KeyAscii = vbKeyEscape Then

z = 0: k = 0: r = 0: j = 0

Dim i As Integer

For i = 0 To 50

number(i) = 0

number2(i) = 0

Next i

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text1.SetFocus

End If

If Len(Text3.Text) >= 14 Then

calcresult.Show

End If

End Sub

rivate sub Text3_Change()

tax2.Text1 = Text3.Text

End Sub

用c语言编写能运算加减乘除的计算器程式,用到栈

#include "stdio.h"

#include "string.h"

#include "ctype.h"

#include "math.h"

expression evaluate

#define iMUL 0

#define iDIV 1

#define iADD 2

#define iSUB 3

#define iCap 4

#define LtKH 5

#define RtKH 6

#define MaxSize 100

void iPush(float)

float iPop()

float StaOperand[MaxSize]

int iTop=-1

char Srcexp[MaxSize]

char Capaexp[MaxSize]

char RevPolishexp[MaxSize]

float NumCapaTab[26]

char validexp[]="*/+-()"

char NumSets[]="0123456789"

char StackSymb[MaxSize]

int operands

void NumsToCapas(char [], int , char [], float [])

int CheckExpress(char)

int PriorChar(char,char)

int GetOperator(char [], char)

void counterPolishexp(char INexp[], int slen, char Outexp[])

float CalcRevPolishexp(char [], float [], char [], int)

void main()

{

int ilen

float iResult=0.0

printf("enter a valid number string:\n")

memset(StackSymb,0,MaxSize)

memset(NumCapaTab,0,26)A--NO.1, B--NO.2, etc.

gets(Srcexp)

ilen=strlen(Srcexp)

printf("source expression:%s\n",Srcexp)

NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab)

printf("Numbers listed as follows:\n")

int i

for (i=0i<operands++i)

printf("%.2f ",NumCapaTab[i])

printf("\nCapaexp listed in the following:\n")

printf("%s\n",Capaexp)

ilen=strlen(Capaexp)

counterPolishexp(Capaexp,ilen,RevPolishexp)

printf("RevPolishexp:\n%s\n",RevPolishexp)

ilen=strlen(RevPolishexp)

iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen)

printf("\ncounterPolish expression:\n%s%.6f\n",Srcexp,iResult)

}

void iPush(float value)

{

if(iTop<MaxSize) StaOperand[++iTop]=value

}

float iPop()

{

if(iTop>-1)

return StaOperand[iTop--]

return -1.0

}

void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[])

{

char ch

int i, j, k, flg=0

int sign

float val=0.0,power=10.0

i=0j=0k=0

while (i<slen)

{

ch=Srcexp[i]

if (i==0)

{

sign=(ch=='-')?-1:1

if(ch=='+'||ch=='-')

{

ch=Srcexp[++i]

flg=1

}

}

if (isdigit(ch))

{

val=ch-'0'

while (isdigit(ch=Srcexp[++i]))

{

val=val*10.0+ch-'0'

}

if (ch=='.')

{

while(isdigit(ch=Srcexp[++i]))

{

val=val+(ch-'0')/power

power*=10

}

} end if

if(flg)

{

val*=sign

flg=0

}

} end if

write Capaexp array

write NO.j to array

if(val)

{

Capaexp[k++]='A'+j

Capaexp[k++]=ch

NumCapaTab[j++]=valA--0, B--1,and C, etc.

}

else

{

Capaexp[k++]=ch

}

val=0.0

power=10.0

i++

}

Capaexp[k]='\0'

operands=j

}

float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen)

{

float sval=0.0, op1,op2

int i, rt

char ch

recursive stack

i=0

while((ch=RevPolishexp[i]) &&i<slen)

{

switch(rt=GetOperator(validexp, ch))

{

case iMUL: op2=iPop()op1=iPop()

sval=op1*op2

iPush(sval)

break

case iDIV: op2=iPop()op1=iPop()

if(!fabs(op2))

{

printf("overflow\n")

iPush(0)

break

}

sval=op1/op2

iPush(sval)

break

case iADD: op2=iPop()op1=iPop()

sval=op1+op2

iPush(sval)

break

case iSUB: op2=iPop()op1=iPop()

sval=op1-op2

iPush(sval)

break

case iCap: iPush(NumCapaTab[ch-'A'])

break

default:

}

++i

}

while(iTop>-1)

{

sval=iPop()

}

return sval

}

int GetOperator(char validexp[],char oper)

{

int oplen,i=0

oplen=strlen(validexp)

if (!oplen) return -1

if(isalpha(oper)) return 4

while(i<oplen &&validexp[i]!=oper) ++i

if(i==oplen || i>=4) return -1

return i

}

int CheckExpress(char ch)

{

int i=0

char

while((=validexp[i]) &&ch!=) ++i

if (!)

return 0

return 1

}

int PriorChar(char curch, char stach)

{

栈外优先顺序高于(>)栈顶优先顺序时,才入栈

否则(<=),一律出栈

if (curch==stach) return 0等于时应该出栈

else if (curch=='*' || curch=='/')

{

if(stach!='*' &&stach!='/')

return 1

}

else if (curch=='+' || curch=='-')

{

if (stach=='(' || stach==')')

return 1

}

else if (curch=='(')

{

if (stach==')')

return 1

}

return 0

}

void counterPolishexp(char INexp[], int slen, char Outexp[])

{

int i, j, k,pr

char t

i=0

j=k=0

while (INexp[i]!='=' &&i<slen)

{

if (INexp[i]=='(')

StackSymb[k++]=INexp[i]

iPush(*(INexp+i))

else if(INexp[i]==')')

{

if((t=iPop())!=-1)

while((t=StackSymb[k-1])!='(')

{

Outexp[j++]=t

k--

}

k--

}

else if (CheckExpress(INexp[i])) is oparator

{

printf("operator %c k=%d\n",INexp[i],k)

while (k)

{

iPush(*(INexp+i))

if(pr=PriorChar(INexp[i],StackSymb[k-1]))

break

else

{

if ((t=iPop())!=-1)

t=StackSymb[k-1]k--

Outexp[j++]=t

}

} end while

StackSymb[k++]=INexp[i]mon process

}

else if() 变数名

{

printf("operand %c k=%d\n",INexp[i],k)

Outexp[j++]=INexp[i]

}

i++

}

while (k)

{

t=StackSymb[k-1]k--

Outexp[j++]=t

}

Outexp[j]='\0'

}

注:程式源于“百度知道”

用verilog编写一个最简单的加减乘除的计算器的程式

verilog是有加法器乘法器的。也直接识别 + - * / 符号。

module kjasdja(a,option,b,result)

input option,a,b

output result

always @(a,b,option)

begin

result_r=0结果暂存器清零

case(option)

+:result_r=a+b

-:result_r=a-b

*:result_r=a*b

/:result_r=a/b

assign result =result_r

endmodule

大概演算法就这样。写的仓促,语法可能有误。另外除法reg型别只能储存整数部分,小数通过移位 *** 作实现,比较麻烦。比如3/5=0.6

做的时候先3=30,然后30/5=6,然后对6在数码管的显示进行调整就好。把6显示在小数点后面1位就好

用vb编写一个计算器程式,实现加减乘除,

Dim v As Boolean

Dim s As Integer

Dim X As Double

Dim Y As Double

Private Sub Command1_Click(Index As Integer)

If Form1.Tag = "T" Then

If Index = 10 Then

Text1.Text = "0"

Else

Text1.Text = Command1(Index).Caption

End If

Form1.Tag = ""

Else

Text1.Text = Text1.Text &Command1(Index).Caption

End If

End Sub

Private Sub Command2_Click(Index As Integer)

Form1.Tag = "T"

If v Then

X = Val(Text1.Text)

v = Not v

Else

Y = Val(Text1.Text)

Select Case s

Case 0

Text1.Text = X + Y

Case 1

Text1.Text = X - Y

Case 2

Text1.Text = X * Y

Case 3

If Y <>0 Then

Text1.Text = X / Y

Else

MsgBox ("不能以0为除数")

Text1.Text = X

v = False

End If

Case 4

Y = 0

v = False

End Select

X = Val(Text1.Text)

End If

s = Index

End Sub

Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)

End Sub

控制元件自己新增吧,空间名要和程式码名一致

求一简单的加减乘除计算器c++程式

#include<stdio.h>

#include<math.h>

void main()

{

float a,b

char C

while(1)

{

scanf("%f%c%f",&a,&C,&b)

if((C!='+')&&(C!='-')&&(C!='*')&&(C!='/'))

break

switch(C)

{

case '+': printf("%f+%f=%f",a,b,a+b)

break

case '-': printf("%f-%f=%f",a,b,a-b)

break

case '*': printf("%f*%f=%f",a,b,a*b)

break

case '/': printf("%f/%f=%f",a,b,a/b)

break

}

}

}

想改成按1 2 3 4分别为加减乘除,只需要将程式中的+ - * / 改成1 2 3 4即可。按除了+ - * / 以外的键就会退出。

用c++语言编写一个简单的计算器程式,会加减乘除就行,本人初学不太会,特训求帮助

这个是最简单,简陋的计算器。很多情况没考虑进去,例如除数不能为0之类的,真要写完整的话程式码还要更多。

程式码如下: #include <iostream>using namespace stdint main(){ float a, b, result char operation cout <<"请输入算式,如1+2并回车:" <<endl cin >>a >>operation >>b switch(operation) { case '+': result = a + bbreak case '-': result = a - bbreak case '*': result = a * bbreak case '/': result = a / bbreak default: cout <<"输入非法,程式退出!" <<endlreturn -1 } cout <<endl <<"结果为:" <<endl <<a <<operation <<b <<"=" <<result <<endl return 0}

知道switch函式 吗 用这个就行

建俩个int型变数 一个字元型变数

1 实验目的

了解熟悉某个算法,ke自己发挥。

2 实验步骤

实验环境的配置,例如添加什么函数,库,头文件等,以及你的思路都可以写。

3 代码

4运行结果(截图)

5总结

6参考资料(可有可无)

#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

} /*如果移到尾,再下移,则移动到最上边字符位置*/

else

{

y=y+height+height/2

n++

} /*否则,移到下边一个字符位置*/

putimage(x,y,rar,XOR_PUT) /*在新的位置显示光标箭头*/

}

c=str1[n*5+m] /*将字符保存到变量c中*/

if(isdigit(c)||c=='.') /*判断是否是数字或小数点*/

{

if(flag==-1) /*如果标志为-1,表明为负数*/

{

strcpy(str2,"-")/*将负号连接到字符串中*/

flag=1

} /*将标志值恢复为1*/

sprintf(temp,"%c",c)/*将字符保存到字符串变量temp中*/

strcat(str2,temp)/*将temp中的字符串连接到str2中*/

setfillstyle(SOLID_FILL,color+3)

bar(2*width+width/2,height/2,15*width/2,3*height/2)

outtextxy(5*width,height,str2) /*显示字符串*/

}

if(c=='+')

{

num1=atof(str2) /*将第一个 *** 作数转换为浮点数*/

strcpy(str2,"")/*将str2清空*/

act=1 /*做计算加法标志值*/

setfillstyle(SOLID_FILL,color+3)

bar(2*width+width/2,height/2,15*width/2,3*height/2)

outtextxy(5*width,height,"0.")/*显示字符串*/

}

if(c=='-')

{

if(strcmp(str2,"")==0) /*如果str2为空,说明是负号,而不是减号*/

flag=-1 /*设置负数标志*/

else

{

num1=atof(str2) /*将第二个 *** 作数转换为浮点数*/

strcpy(str2,"")/*将str2清空*/

act=2/*做计算减法标志值*/

setfillstyle(SOLID_FILL,color+3)

bar(2*width+width/2,height/2,15*width/2,3*height/2)/*画矩形*/

outtextxy(5*width,height,"0.")/*显示字符串*/

}

}

if(c=='*')

{

num1=atof(str2)/*将第二个 *** 作数转换为浮点数*/

strcpy(str2,"")/*将str2清空*/

act=3/*做计算乘法标志值*/

setfillstyle(SOLID_FILL,color+3) bar(2*width+width/2,height/2,15*width/2,3*height/2)

outtextxy(5*width,height,"0.")/*显示字符串*/

}

if(c=='/')

{

num1=atof(str2)/*将第二个 *** 作数转换为浮点数*/

strcpy(str2,"")/*将str2清空*/

act=4/*做计算除法标志值*/

setfillstyle(SOLID_FILL,color+3)

bar(2*width+width/2,height/2,15*width/2,3*height/2)

outtextxy(5*width,height,"0.")/*显示字符串*/

}

if(c=='^')

{

num1=atof(str2)/*将第二个 *** 作数转换为浮点数*/

strcpy(str2,"")/*将str2清空*/

act=5/*做计算乘方标志值*/

setfillstyle(SOLID_FILL,color+3)/*设置用淡绿色实体填充*/

bar(2*width+width/2,height/2,15*width/2,3*height/2)/*画矩形*/

outtextxy(5*width,height,"0.")/*显示字符串*/

}

if(c=='%')

{

num1=atof(str2)/*将第二个 *** 作数转换为浮点数*/

strcpy(str2,"") /*将str2清空*/

act=6/*做计算模运算乘方标志值*/

setfillstyle(SOLID_FILL,color+3)/*设置用淡绿色实体填充*/

bar(2*width+width/2,height/2,15*width/2,3*height/2)/*画矩形*/

outtextxy(5*width,height,"0.")/*显示字符串*/

}

if(c=='=')

{

num2=atof(str2)/*将第二个 *** 作数转换为浮点数*/

switch(act) /*根据运算符号计算*/

{

case 1:result=num1+num2break/*做加法*/

case 2:result=num1-num2break/*做减法*/

case 3:result=num1*num2break/*做乘法*/

case 4:result=num1/num2break/*做除法*/

case 5:result=pow(num1,num2)break/*做x的y次方*/

case 6:result=fmod(num1,num2)break/*做模运算*/

}

setfillstyle(SOLID_FILL,color+3)/*设置用淡绿色实体填充*/

bar(2*width+width/2,height/2,15*width/2,3*height/2)/*覆盖结果区*/

sprintf(temp,"%f",result)/*将结果保存到temp中*/

outtextxy(5*width,height,temp)/*显示结果*/

}

if(c=='c')

{

num1=0/*将两个 *** 作数复位0,符号标志为1*/

num2=0

flag=1

strcpy(str2,"")/*将str2清空*/

setfillstyle(SOLID_FILL,color+3) /*设置用淡绿色实体填充*/

bar(2*width+width/2,height/2,15*width/2,3*height/2)/*覆盖结果区*/

outtextxy(5*width,height,"0.")/*显示字符串*/

}

if(c=='Q')exit(0) /*如果选择了q回车,结束计算程序*/

}

putimage(x,y,rar,XOR_PUT)/*在退出之前消去光标箭头*/

return /*返回*/

}

/*窗口函数*/

void mwindow( char *header )

{

int height

cleardevice() /* 清除图形屏幕 */

setcolor( MaxColors - 1 ) /* 设置当前颜色为白色*/

setviewport( 20, 20, MaxX/2, MaxY/2, 1 )/* 设置视口大小 */

height = textheight( "H" ) /* 读取基本文本大小 */

settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 )/*设置文本样式*/

settextjustify( CENTER_TEXT, TOP_TEXT )/*设置字符排列方式*/

outtextxy( MaxX/4, 2, header ) /*输出标题*/

setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ) /*设置视口大小*/

drawboder()/*画边框*/

}

void drawboder(void) /*画边框*/

{

struct viewporttype vp /*定义视口类型变量*/

setcolor( MaxColors - 1 ) /*设置当前颜色为白色 */

setlinestyle( SOLID_LINE, 0, NORM_WIDTH )/*设置画线方式*/

getviewsettings( &vp )/*将当前视口信息装入vp所指的结构中*/

rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top )/*画矩形边框*/

}

/*设计鼠标图形函数*/

int arrow()

{

int size

int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}/*定义多边形坐标*/

setfillstyle(SOLID_FILL,2)/*设置填充模式*/

fillpoly(8,raw) /*画出一光标箭头*/

size=imagesize(4,4,16,16) /*测试图象大小*/

rar=malloc(size) /*分配内存区域*/

getimage(4,4,16,16,rar)/*存放光标箭头图象*/

putimage(4,4,rar,XOR_PUT)/*消去光标箭头图象*/

return 0

}

/*按键函数*/

int specialkey(void)

{

int key

while(bioskey(1)==0) /*等待键盘输入*/

key=bioskey(0) /*键盘输入*/

key=key&0xff? key&0xff:key>>8 /*只取特殊键的扫描值,其余为0*/

return(key) /*返回键值*/

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/12072298.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存