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

学习 时间:2026-03-30 17:12:02 阅读:6759
c语言计算器程序设计包含加减乘除简单的函数运算

最佳回答

爱笑的诺言

羞涩的摩托

2026-03-30 17:12:02

实用计算器之程序设计 [摘 要]多用计算器的构思及设计代码 [关键词]多用计算器;设计 数值计算可以说是日常最频繁的工作了,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 DoubleDim number(0 To 50) As DoubleDim z As IntegerDim k As Integer, r As IntegerDim j As IntegerDim 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 LongPrivate Const SND_FILENAME = &H20000?Private Const SND_ASYNC = &H1?Private Const SND_SYNC = &H0 *判断通用过程Sub pianduan(p As String) r = 0Dim i As Integer, l As Integer, h As Integerh = 0i = 1If InStr(Trim$(p), "*") 0 Thenk = k + 1End IfIf InStr(Trim$(p), "/") 0 Thenr = r + 1End IfEnd Sub *连乘通用过程(略) *各按钮事件过程Private sub Command1_Click(Index As Integer)PlaySound App。Path & "\start。wav", 0, SND_SYNCText1。Text = Text1。Text + Command1(Index)。CaptionText2。Text = Text2。Text + Command1(Index)。CaptionText1。SetFocusEnd Subrivate sub Command10_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCstr = Text3。TextEnd SubPrivate sub Command11_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCText3。Text = strEnd Subrivate sub Command2_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCDim totle As DoubleDim n As IntegerCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""Text2。Text = Text2 + "+"z = z + 1Text1。SetFocusEnd Subrivate sub Command3_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCDim totle As DoubleDim n As IntegerCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""Text2。Text = Text2 + "-"Text1。Text = Text1。Text & "-"z = z + 1Text1。SetFocusEnd SubPrivate sub Command4_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCText2。Text = Text2。Text + "*"Text1。Text = Text1。Text + "*"Text1。SetFocusEnd Subrivate sub Command5_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCText2。Text = Text2 + "/"Text1。Text = Text1 + "/"Text1。SetFocusEnd SubPrivate sub Command6_Click()PlaySound App。Path & "\sound。wav", 0, SND_SYNCDim totle As DoubleDim n As IntegerCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""z = z + 1Dim dengyu As DoubleDim v As IntegerFor v = 0 To 50dengyu = dengyu + number2(v)Next vIf dengyu < 0 ThenText3。ForeColor = &HFF&ElseText3。ForeColor = &HFF0000End IfText3。Text = dengyuText1。SetFocusIf Len(Text3。Text) >= 14 Thencalcresult。ShowEnd IfEnd Subrivate sub Command7_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCz = 0: k = 0: r = 0: j = 0Dim i As IntegerFor i = 0 To 50number(i) = 0number2(i) = 0Next iText1。Text = ""Text2。Text = ""Text3。Text = ""Text1。SetFocusEnd Subrivate sub Command8_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCIf Val(Text3。Text) = 0 ThenMsgBox "除数不能为0!"Exit SubEnd IfText3。Text = 1 / Val(Text3。Text)End SubPrivate sub Command9_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCText3。ForeColor = &HFF0000Text3。Text = Val(Text3。Text) * Val(Text3。Text)End Subrivate sub muninternet_Click()Dim ii = Shell("C:\Program Files\InternetExplorer\iexplore。exe", vbMaximizedFocus)End Subrivate sub munmp3_Click()Dim ii = Shell("C:\Program Files\Windows Media Player\mplayer2", vbNormalNoFocus)End SubPrivate sub munsm_Click()Dialog。ShowEnd Subrivate sub muntax_Click()tax。ShowEnd Subrivate sub munver_Click()ver。ShowEnd Subrivate sub notepad_Click()Dim i i = Shell("c:\windows\notepad", vbNormalFocus)End SubPrivate sub Text1_KeyPress(KeyAscii As Integer)PlaySound App。Path & "\start。wav", 0, SND_SYNCDim num As Integernum = Val(KeyAscii)If num > 47 And num < 58 Then Text1。Text = Text1。Text + CStr(num - 48)Text2。Text = Text2。Text + CStr(num - 48)End IfIf num = 46 ThenText1。Text = Text1。Text + "。"Text2。Text = Text2。Text + "。"End IfIf KeyAscii = 43 ThenDim totle As DoubleDim n As IntegerCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""Text2。Text = Text2 + "+"z = z + 1End IfIf KeyAscii = 45 ThenCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""Text2。Text = Text2 + "-"Text1。Text = Text1。Text & "-"z = z + 1End IfIf KeyAscii = 42 ThenText2。Text = Text2。Text + "*"Text1。Text = Text1。Text + "*"End IfIf KeyAscii = 47 ThenText2。Text = Text2。Text + "/"Text1。Text = Text1。Text + "/"End IfIf KeyAscii = vbKeyReturn ThenPlaySound App。Path & "\sound。wav", 0, SND_SYNCCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""z = z + 1Dim dengyu As DoubleDim v As IntegerFor v = 0 To 50dengyu = dengyu + number2(v)Next vIf dengyu < 0 ThenText3。ForeColor = &HFF&ElseText3。ForeColor = &HFF0000End IfText3。Text = dengyuEnd IfIf KeyAscii = vbKeyEscape Thenz = 0: k = 0: r = 0: j = 0Dim i As IntegerFor i = 0 To 50number(i) = 0number2(i) = 0Next iText1。Text = ""Text2。Text = ""Text3。Text = ""Text1。SetFocusEnd IfIf Len(Text3。Text) >= 14 Thencalcresult。ShowEnd IfEnd Subrivate sub Text3_Change()tax2。Text1 = Text3。TextEnd Su

最新回答共有2条回答

  • 纯真的摩托
    回复
    2026-03-30 17:12:02

    实用计算器之程序设计 [摘 要]多用计算器的构思及设计代码 [关键词]多用计算器;设计 数值计算可以说是日常最频繁的工作了,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 DoubleDim number(0 To 50) As DoubleDim z As IntegerDim k As Integer, r As IntegerDim j As IntegerDim 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 LongPrivate Const SND_FILENAME = &H20000?Private Const SND_ASYNC = &H1?Private Const SND_SYNC = &H0 *判断通用过程Sub pianduan(p As String) r = 0Dim i As Integer, l As Integer, h As Integerh = 0i = 1If InStr(Trim$(p), "*") 0 Thenk = k + 1End IfIf InStr(Trim$(p), "/") 0 Thenr = r + 1End IfEnd Sub *连乘通用过程(略) *各按钮事件过程Private sub Command1_Click(Index As Integer)PlaySound App。Path & "\start。wav", 0, SND_SYNCText1。Text = Text1。Text + Command1(Index)。CaptionText2。Text = Text2。Text + Command1(Index)。CaptionText1。SetFocusEnd Subrivate sub Command10_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCstr = Text3。TextEnd SubPrivate sub Command11_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCText3。Text = strEnd Subrivate sub Command2_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCDim totle As DoubleDim n As IntegerCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""Text2。Text = Text2 + "+"z = z + 1Text1。SetFocusEnd Subrivate sub Command3_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCDim totle As DoubleDim n As IntegerCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""Text2。Text = Text2 + "-"Text1。Text = Text1。Text & "-"z = z + 1Text1。SetFocusEnd SubPrivate sub Command4_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCText2。Text = Text2。Text + "*"Text1。Text = Text1。Text + "*"Text1。SetFocusEnd Subrivate sub Command5_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCText2。Text = Text2 + "/"Text1。Text = Text1 + "/"Text1。SetFocusEnd SubPrivate sub Command6_Click()PlaySound App。Path & "\sound。wav", 0, SND_SYNCDim totle As DoubleDim n As IntegerCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""z = z + 1Dim dengyu As DoubleDim v As IntegerFor v = 0 To 50dengyu = dengyu + number2(v)Next vIf dengyu = 14 Thencalcresult。ShowEnd IfEnd Subrivate sub Command7_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCz = 0: k = 0: r = 0: j = 0Dim i As IntegerFor i = 0 To 50number(i) = 0number2(i) = 0Next iText1。Text = ""Text2。Text = ""Text3。Text = ""Text1。SetFocusEnd Subrivate sub Command8_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCIf Val(Text3。Text) = 0 ThenMsgBox "除数不能为0!"Exit SubEnd IfText3。Text = 1 / Val(Text3。Text)End SubPrivate sub Command9_Click()PlaySound App。Path & "\start。wav", 0, SND_SYNCText3。ForeColor = &HFF0000Text3。Text = Val(Text3。Text) * Val(Text3。Text)End Subrivate sub muninternet_Click()Dim ii = Shell("C:\Program Files\InternetExplorer\iexplore。exe", vbMaximizedFocus)End Subrivate sub munmp3_Click()Dim ii = Shell("C:\Program Files\Windows Media Player\mplayer2", vbNormalNoFocus)End SubPrivate sub munsm_Click()Dialog。ShowEnd Subrivate sub muntax_Click()tax。ShowEnd Subrivate sub munver_Click()ver。ShowEnd Subrivate sub notepad_Click()Dim i i = Shell("c:\windows\notepad", vbNormalFocus)End SubPrivate sub Text1_KeyPress(KeyAscii As Integer)PlaySound App。Path & "\start。wav", 0, SND_SYNCDim num As Integernum = Val(KeyAscii)If num > 47 And num = 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""Text2。Text = Text2 + "+"z = z + 1End IfIf KeyAscii = 45 ThenCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""Text2。Text = Text2 + "-"Text1。Text = Text1。Text & "-"z = z + 1End IfIf KeyAscii = 42 ThenText2。Text = Text2。Text + "*"Text1。Text = Text1。Text + "*"End IfIf KeyAscii = 47 ThenText2。Text = Text2。Text + "/"Text1。Text = Text1。Text + "/"End IfIf KeyAscii = vbKeyReturn ThenPlaySound App。Path & "\sound。wav", 0, SND_SYNCCall pianduan(Text1。Text)If k >= 1 Or r >= 1 ThenCall liancheng(totle)number2(z) = totleIf Mid$(Trim$(Text1。Text), 1, 1) = "-" Thennumber2(z) = -totleEnd Ifk = 0: r = 0Elsenumber2(z) = Val(Text1。Text)End IfText1。Text = ""z = z + 1Dim dengyu As DoubleDim v As IntegerFor v = 0 To 50dengyu = dengyu + number2(v)Next vIf dengyu = 14 Thencalcresult。ShowEnd IfEnd Subrivate sub Text3_Change()tax2。Text1 = Text3。TextEnd Su

上一篇 在平面直角坐标系中,A(0,3)B(5,2)

下一篇 lets clean the room now.改为否定句