实用计算器之程序设计
[摘 要]多用计算器的构思及设计代码
[关键词]多用计算器;设计
数值计算可以说是日常最频繁的工作了,WIN98提供了“计算器”软件供用户使用,该软件可以处理一般的一步四则运算,例如:3+2、5/3等等,但在日常中用户经常遇到多步四则运算问题,例如:3+45-4/2,4534/2+187等等,那么该个计算器就无法胜任了,作者制作了一个实用的计算器,该计算器新增不少功能:(程序界面如图)
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 "winmmdll" 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 AppPath & "\startwav", 0, SND_SYNC
Text1Text = Text1Text + Command1(Index)Caption
Text2Text = Text2Text + Command1(Index)Caption
Text1SetFocus
End Sub
rivate sub Command10_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
str = Text3Text
End Sub
Private sub Command11_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text3Text = str
End Sub
rivate sub Command2_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
Text2Text = Text2 + "+"
z = z + 1
Text1SetFocus
End Sub
rivate sub Command3_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
Text2Text = Text2 + "-"
Text1Text = Text1Text & "-"
z = z + 1
Text1SetFocus
End Sub
Private sub Command4_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text2Text = Text2Text + ""
Text1Text = Text1Text + ""
Text1SetFocus
End Sub
rivate sub Command5_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text2Text = Text2 + "/"
Text1Text = Text1 + "/"
Text1SetFocus
End Sub
Private sub Command6_Click()
PlaySound AppPath & "\soundwav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
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
Text3ForeColor = &HFF&
Else
Text3ForeColor = &HFF0000
End If
Text3Text = dengyu
Text1SetFocus
If Len(Text3Text) >= 14 Then
calcresultShow
End If
End Sub
rivate sub Command7_Click()
PlaySound AppPath & "\startwav", 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
Text1Text = ""
Text2Text = ""
Text3Text = ""
Text1SetFocus
End Sub
rivate sub Command8_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
If Val(Text3Text) = 0 Then
MsgBox "除数不能为0!"
Exit Sub
End If
Text3Text = 1 / Val(Text3Text)
End Sub
Private sub Command9_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text3ForeColor = &HFF0000
Text3Text = Val(Text3Text) Val(Text3Text)
End Sub
rivate sub muninternet_Click()
Dim i
i = Shell("C:\Program Files\InternetExplorer\iexploreexe", 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()
DialogShow
End Sub
rivate sub muntax_Click()
taxShow
End Sub
rivate sub munver_Click()
verShow
End Sub
rivate sub notepad_Click()
Dim i
i = Shell("c:\windows\notepad", vbNormalFocus)
End Sub
Private sub Text1_KeyPress(KeyAscii As Integer)
PlaySound AppPath & "\startwav", 0, SND_SYNC
Dim num As Integer
num = Val(KeyAscii)
If num > 47 And num < 58 Then
Text1Text = Text1Text + CStr(num - 48)
Text2Text = Text2Text + CStr(num - 48)
End If
If num = 46 Then
Text1Text = Text1Text + ""
Text2Text = Text2Text + ""
End If
If KeyAscii = 43 Then
Dim totle As Double
Dim n As Integer
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
Text2Text = Text2 + "+"
z = z + 1
End If
If KeyAscii = 45 Then
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
Text2Text = Text2 + "-"
Text1Text = Text1Text & "-"
z = z + 1
End If
If KeyAscii = 42 Then
Text2Text = Text2Text + ""
Text1Text = Text1Text + ""
End If
If KeyAscii = 47 Then
Text2Text = Text2Text + "/"
Text1Text = Text1Text + "/"
End If
If KeyAscii = vbKeyReturn Then
PlaySound AppPath & "\soundwav", 0, SND_SYNC
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
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
Text3ForeColor = &HFF&
Else
Text3ForeColor = &HFF0000
End If
Text3Text = 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
Text1Text = ""
Text2Text = ""
Text3Text = ""
Text1SetFocus
End If
If Len(Text3Text) >= 14 Then
calcresultShow
End If
End Sub
rivate sub Text3_Change()
tax2Text1 = Text3Text
End Sub
/程序功能:一开始程序自动进入第一轮运算测试,通过按“p”“m”“t”“d”而选择加、减、乘、除运算,再每做完一道题
时按回车继续该运算,按“p”“m”“t”“d”进行相应运算切换,其间可按“s”退出该轮测试;
若按了“s”则选择是否进行下一轮,按分别按“y”“n”表示是或不是。/
# include <stdioh>
# include <timeh>
# include <stdlibh>
void main()
{int p[2]={0},m[2]={0},t[2]={0},d[2]={0},i=0,j=0,k=0;//数组分别存放每轮做相应运算的总数和正答个数;k、j分别记所做轮数和每轮所做的题数
char c,exit='y';
void pluss(int p);
void minus(int m);
void times(int t);
void division(intt);
void test(intp,intm,intt,intd);
while(exit!='n'){ //输出提示语
printf("\n 按 \"p\"--->\"+\"运算; 按 \"m\"--->\"-\"运算;\n 按 \"t\"--->\"\"运算; 按 \"d\"--->\"/\"运算;");
printf(" 按 \"s\"--->\"退出该轮测试\"\n 做完每道题后您可按回车键继续该运算或切换到别的运算");
printf("\n\n欢迎进入第%d轮测试 请选择运算 ",++k);
c=getchar();
if(k!=1)c=getchar();//选择运算
while(c!='s'){
while(c=='p'||(c!='s'&&c!='m'&&c!='t'&&c!='d')){printf("第%d道 ",++j);pluss(p);c=getchar();c=getchar();}
while(c=='m'||(c!='s'&&c!='p'&&c!='t'&&c!='d')){printf("第%d道 ",++j);minus(m);c=getchar();c=getchar();}
while(c=='t'||(c!='s'&&c!='m'&&c!='p'&&c!='d')){printf("第%d道 ",++j);times(t);c=getchar();c=getchar();}
while(c=='d'||(c!='s'&&c!='m'&&c!='t'&&c!='p')){printf("第%d道 ",++j);division(d);c=getchar();c=getchar();}
}
test(p,m,t,d); //按s后退出一轮测试,调用函数给出测试结果
printf("\n 按 \"y\"---> \"进入第%d轮\" 按 \"n\"---> \"退出程序\" ",k+1);
exit=getchar();exit=getchar();
if(exit!='n'){ //按s后选则是否进入下一轮
j=0;
for(i=0;i<2;i++)p[i]=m[i]=t[i]=d[i]=0;
}
}
printf("\n\n Bye-Bye\n\n");
}
void pluss(int p){
int y,x,sum;
srand (time(NULL));
x=(int)rand()%10+1;
y=(int)rand()%10+1;
printf("%d+%d=",x,y);
scanf("%d",&sum);
p[0]++;
if(sum==x+y){printf("Congratuations! ");p[1]++;}
else printf("Sorry! Right answer is %d ",x+y);}
void minus(int m){
int y,x,minus;
srand (time(NULL));
x=(int)rand()%10+1;
y=(int)rand()%10+1;
m[0]++;
printf("%d-%d=",x+y,y);
scanf("%d",&minus);
if(minus==x){printf("Congratulations! ");m[1]++;}
else printf("Sorry! Right answer is %d ",x);
}
void times(int t){
int y,x,tim;
srand (time(NULL));
x=(int)rand()%10+1;
y=(int)rand()%10+1;
printf("%d%d=",x,y);
scanf("%d",&tim);
t[0]++;
if(tim==xy){printf("Congratulations! ");t[1]++;}
else printf("Sorry! Right answer is %d ",xy);
}
void division(int d){
int y,x,div;
srand (time(NULL));
x=(int)rand()%10+1;
y=(int)rand()%10+1;
printf("%d/%d=",xy,y);
scanf("%d",&div);
d[0]++;
if(div==x){printf("Congratulations! ");d[1]++;}
else printf("Sorry! Right answer is %d ",x);
}
void test(int p,intm,intt,intd){
int s[2];
float rate;
s[0]=p[0]+m[0]+t[0]+d[0];
s[1]=p[1]+m[1]+t[1]+d[1];
rate=s[1]/(s[0]10);
if(rate>08)printf("\nYou have done a good job !\n\n");
else if(rate<06)printf("\nYou have done a bad job !\n\n");
else printf("\nCome on! You can do it better !\n\n");
if(s[0]!=0)
printf("total done:%d right:%d rate:%52f%c\n",s[0],s[1],rate100,'%');
else printf("total done:0 right:0\n");
if(p[0]!=0)
printf("\"+\" done:%d right:%d rate:%52f%c ",p[0],p[1],(p[1]/(p[0]10))100,'%');
else printf("\"+\" done:%d right:%d ",p[0],p[1]);
if(m[0]!=0)
printf("\"-\" done:%d right:%d rate:%52f%c\n",m[0],m[1],(m[1]/(m[0]10))100,'%');
else printf("\"-\" done:%d right:%d\n",m[0],m[1]);
if(t[0]!=0)
printf("\"\" done:%d right:%d rate:%52f%c ",t[0],t[1],(t[1]/(t[0]10))100,'%');
else printf("\"\" done:%d right:%d ",t[0],t[1]);
if(d[0]!=0)
printf("\"/\" done:%d right:%d rate:%52f%c\n",d[0],d[1],(d[1]/(d[0]10))100,'%');
else printf("\"/\" done:%d right:%d\n",d[0],d[1]);
}
找过来的,运行下看看是否可行。
以上就是关于c语言计算器程序设计包含加减乘除简单的函数运算全部的内容,包括:c语言计算器程序设计包含加减乘除简单的函数运算、用C语言设计一个儿童四则运算程序,根据用户的设定自动出题并对全部回答进行判断,全部答完后统计正确率、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)