c语言计算器程序调用函数 c语言运用调用函数的方法实现简易计算器

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

实用计算器之程序设计

创新互联主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、成都响应式网站建设公司、程序开发、网站优化、微网站、微信平台小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都做网站、成都网站设计、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。

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

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

数值计算可以说是日常最频繁的工作了,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 muninternet_Click()

Dim i

i = Shell("C:\Program Files\InternetExplorer\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 munsm_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语言 如何在main中调用函数

是这样的,C语言在编译时候是顺序编译的 ,也就是说,如果被调用的函数是在调用函数之前被声明过,就可以通过编译,反之就会报错。

你这个有两种解决方案,1,在程序最开始声明所有函数 2,把float jian(float a;float b;)这函数所有内容放到main()前面

使用c语言编程,用函数实现一个计算器,在主函数中调用函数,包括加减乘除,乘方,绝对值和sin函数。

#includestdio.h

#includestdlib.h

double jia(double a,double b)

{

return a+b;

}

double jian(double a,double b)

{

return a-b;

}

double cheng(double a,double b)

{

return a*b;

}

double chu(double a,double b)

{

return a/b;

}

double juedui(double a)

{

return a0 ? a : -a;

}

double chengfang(double a,double b)

{

return pow(a,b);

}

double sinx(double a)

{

return sin(a);

}

int main()

{

int m;

double a,b;

while(1)

{

printf("请输入第一个操作数:");

scanf("%lf",a);

printf("0、退出\n1、加\n2、减\n3、乘\n4、除\n5、绝对值\n6、乘方\n7sin、\n请选择一个:");

scanf("%d",m);

if(1==m || 2==m || 3==m || 4==m || 6==m)

{

printf("请输入第二个操作数:");

scanf("%lf",b);

}

switch(m)

{

case 0:

exit(0);

break;

case 1:

printf("%lf+%lf=%lf\n",a,b,jia(a,b));

break;

case 2:

printf("%lf-%lf=%lf\n",a,b,jian(a,b));

break;

case 3:

printf("%lf*%lf=%lf\n",a,b,cheng(a,b));

break;

case 4:

if(0.0==b)

{

printf("除数不能为0。\n");

}

else

{

printf("%lf/%lf=%lf\n",a,b,chu(a,b));

}

break;

case 5:

printf("|%lf|=%lf\n",a,juedui(a));

break;

case 6:

printf("%lf的%lf方=%lf\n",a,b,chengfang(a,b));

break;

case 7:

printf("sin(%lf)=%lf\n",a,sinx(a));

break;

default:

printf("无法处理的命令。\n");

break;

}

}

system("PAUSE");

return EXIT_SUCCESS;

}

计算机c语言中函数之间如何调用

C语言都是先定义后使用的

通过函数名调用如:

double

sum(double

x,double

y)

{

double

z;

z=x+y;

retun(z);

}

main()

{

double

h;

double

a,b;

h=sum(a,b);

}

名称栏目:c语言计算器程序调用函数 c语言运用调用函数的方法实现简易计算器
转载源于:https://www.cdcxhl.com/article4/docjgie.html

成都网站建设公司_创新互联,为您提供App开发电子商务虚拟主机域名注册外贸网站建设静态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发