VB做高级计算器

lml285290661 2008-05-03 01:35:36
哪个朋友可以用VB做出只有1个文本框1个按钮的计算器,文本框内输入+,-,*,/,和()的运算,点按钮结果显示在文本框上,会的朋友教下--感激不尽,上次问过了,大家有的说到其他语言上了,大家有没更好的方法,再次谢了
...全文
306 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
txzsp 2011-11-11
  • 打赏
  • 举报
回复
这个都差不多的,只要将里面的操作符和操作数以字符串方式分离后就都一样了。

推荐一个示例程序,可以直接用,还有源代码,LZ一定试下。
http://download.csdn.net/detail/txzsp/3781512
尘缘udbwcso 2011-11-11
  • 打赏
  • 举报
回复

//表达式求值
#include <stdio.h>
#include <malloc.h>
#include <string.h>
/*
*功能:根据运算符计算
*参数:a, b参与运算的数, ch运算符
*返回值:计算结果,操作符错误则返回0
*/
int cal(int a, char ch, int b)
{
switch(ch)
{
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
return a/b;
break;
}
return 0;
}
/*
*功能:计算表达式的值(用数组模拟栈)
*参数:表达式字符串
*返回值:计算结果
*/
int evaluateExpression(char *str)
{
int i = 0, result, numSub = 0, operSub = 0;
int tmp, len = strlen(str);
int *operand = (int*)malloc(sizeof(int)*len);
char *operat = (char*)malloc(sizeof(char)*len);
while(str[i] != '\0')
{
switch(str[i])
{
case '+':
while(operSub > 0 && operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
operat[operSub++] = '+';
break;
case '-':
while(operSub > 0 && operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
operat[operSub++] = '-';
break;
case '*':
if(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = 0;
while(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = tmp * 10 + str[i+1] - '0';
++i;
}
--i;
printf("%d * %d = ", operand[numSub-1], tmp);
operand[numSub-1] = cal(operand[numSub-1], '*', tmp);
printf("%d\n", operand[numSub-1]);
++i;
}
else
operat[operSub++] = '*';
break;
case '/':
if(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = 0;
while(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = tmp * 10 + str[i+1] - '0';
++i;
}
--i;
printf("%d / %d = ", operand[numSub-1], tmp);
operand[numSub-1] = cal(operand[numSub-1], '/', tmp);
printf("%d\n", operand[numSub-1]);
++i;
}
else
operat[operSub++] = '/';
break;
case '(':
operat[operSub++] = '(';
break;
case ')':
while(operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
--operSub;
break;
default:
tmp = 0;
while(str[i] >= '0' && str[i] <= '9')
{
tmp = tmp * 10 + str[i] - '0';
++i;
}
--i;
operand[numSub++] = tmp;
break;
}
++i;
}
while(numSub > 1 && operSub >= 1)
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
result = operand[numSub-1];
free(operand);
free(operat);
return result;
}
int main()
{
char *str = "225/15-20+(4-3)*2";
int result;
printf("计算过程:\n");
result = evaluateExpression(str);
printf("计算结果:result = %d\n", result);
return 0;
}

计算过程:
225 / 15 = 15
15 - 20 = -5
4 - 3 = 1
1 * 2 = 2
-5 + 2 = -3
计算结果:result = -3


用C写的,提供思路而已
咸清 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 lml285290661 的帖子:]
只有1个文本框1个按钮的计算器,文本框内输入+,-,*,/,和()的运算,点按钮结果显示在文本框上[/Quote]
据说是个高手不屑的问题~~~
我记得上学那会就有很多这类的题目,有一年据说还成了考研题目,命题的也够懒了。
分两步:
1 把字符串判定有效后转换为波兰式、逆波兰式随便一种
2 调用波兰式或逆波兰式求值的函数计算出结果
两个函数可以自己写啊,课本上、网上多的是,写不出就拿来看看。
zhufenghappy 2008-05-05
  • 打赏
  • 举报
回复
这个问题能解决,但是你要说明,就只有 +,-,*,/,和()的运算;没有其他的,可以写个函数来判断一下阿。通过函数来判断先乘除后加减有括号先处理括号,只要把规则定好了,实现起来是没问题的。
1、可以把整个式子作为一个字符串
2、重第一个字符开始判断,是运算符,在判断前后两侧的运算符级别高低,是数字则继续判断下一个字符。
更简单的办法是约定数字和运算符、运算符与运算符之间用空格分开,则直接把该整个式子用split(整个式子," ")处理成数组,然后再循环计算,直到数组中就剩下一个元素。则是最终结果。
咸清 2008-05-05
  • 打赏
  • 举报
回复
顺便问一句:
我审对题了吗?
可西哥 2008-05-04
  • 打赏
  • 举报
回复
看过好几次了,就是表达式解析嘛!

watt 2008-05-04
  • 打赏
  • 举报
回复
这个问题应该是自己处理,如果用现成的就没什么意义了。无非就是字符串分析。功力高的做的好,功力差的做得差,与语言无关,任何语言都可以做。
kisstome88 2008-05-03
  • 打赏
  • 举报
回复
你这个问题我帮你想想,改天给你答复
用户 昵称 2008-05-03
  • 打赏
  • 举报
回复
俺脚着楼主没找到关键,你需要的是写一个程序先解决表达式计算,而不应该把界面也掺杂到里面。
dbcontrols 2008-05-03
  • 打赏
  • 举报
回复
这么简单的问题这么大个论坛没人告诉你
我先告诉你思路吧:
做一组控件数组Command1,复制后粘贴的时候选是,都叫Command,它们的Index属性不一样,分别是0、1、2、3、4、5
把它们调整成正方型的,标题是+,-,*,/,(,和)
然后用一个变量保存Text里面的数据
用Select case判断他按了什么

[Quote=引用楼主 lml285290661 的帖子:]
哪个朋友可以用VB做出只有1个文本框1个按钮的计算器,文本框内输入+,-,*,/,和()的运算,点按钮结果显示在文本框上,会的朋友教下--感激不尽,上次问过了,大家有的说到其他语言上了,大家有没更好的方法,再次谢了
[/Quote]
lsftest 2008-05-03
  • 打赏
  • 举报
回复
EbExecuteLine只能在设计时有效,生成exe文件就不行了。。
还是用ScriptControl的Eval吧。。。
舉杯邀明月 2008-05-03
  • 打赏
  • 举报
回复
学习.........

想问一下CBM666大师:只能通过剪贴板取得计算结果吗?

cbm6666 2008-05-03
  • 打赏
  • 举报
回复
VbScript 你不喜欢, 下面这个你再不能接受的话, 我想你只有去找 彼尔 啦.........

Option Explicit
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long

Private Sub Command1_Click()
ExecuteLine "RtnVal = " & Text1.Text
ExecuteLine "clipboard.settext RtnVal"
Text1.Text = Clipboard.GetText
End Sub

Public Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, fCheckOnly) = 0
End Function


舉杯邀明月 2008-05-03
  • 打赏
  • 举报
回复
....................

-_-!

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧