用EXCEL做一个计算程序?

用EXCEL做一个计算程序?,第1张

你要的是下面的代码吗:

Option Explicit

Private Sub CommandButton1_Click()

Dim A1 As Single, A2 As Single

A1 = CSng(TextBox1.Value)

A2 = CSng(TextBox2.Value)

Label3.Caption = (A1 - A2) / A1 * 100

End Sub

Private Sub TextBox1_Change()

On Error Resume Next'防止一开始为空时出错

If (TextBox1.Value) = Empty Or CSng(TextBox1.Value) = "0" Then 'A1 不能为空或 0

CommandButton1.Locked = True'锁住“计算”键

Else

If Not (TextBox2.Value = Empty) Then' A1 是不为 0 的数字且 A2 也不空

CommandButton1.Locked = False '解锁 “计算”键

End If

End If

Label3.Caption = Empty

End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If Not ((KeyAscii >47 And KeyAscii <58) Or KeyAscii = 46) Or Not (IsNumeric(TextBox1.Value &Chr(KeyAscii))) Then

KeyAscii = 8'防止输出的不是数字

End If

End Sub

Private Sub TextBox2_Change()

On Error Resume Next'防止一开始为空时出错

If (TextBox1.Value) = Empty Or CSng(TextBox1.Value) = "0" Then 'A1 不能为空或 0

CommandButton1.Locked = True'锁住“计算”键

Else

If Not (TextBox2.Value = Empty) Then' A1 是不为 0 的数字且 A2 也不空

CommandButton1.Locked = False '解锁 “计算”键

End If

End If

Label3.Caption = Empty

End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If Not ((KeyAscii >47 And KeyAscii <58) Or KeyAscii = 46) Or Not (IsNumeric(TextBox2.Value &Chr(KeyAscii))) Then

KeyAscii = 8'防止输出的不是数字

End If

End Sub

Private Sub UserForm_Initialize()

TextBox1.Value = Empty

TextBox2.Value = Empty

Label3.Caption = Empty

CommandButton1.Locked = True

End Sub

/*简易计算器.cpp 你自己找个c++编译器就可以运行*/

#include<iostream>#include<string>using

namespace

stdint

main(){ float

a,bchar

strcout<<"a="cin>>a

//输入两个数 cout<<"

b="cin>>b

cout<<"请输入+,-,*,/

其中一个运算符"<<endl

cout<<"你所选的运算符是:"fflush(stdin)

//清空输入缓冲区,通常是为了确保不影响后面的数据读取

str=getchar()

cout<<endl

switch(str)

{

case

'+':cout<<"a+b="<<a+bbreak

case

'-':cout<<"a-b="<<a-bbreak

case

'*':cout<<"a*b="<<a*bbreak case

'/':cout<<"a/b="<<a/bbreak defaut:cout<<"error"

} return

0}

#include<stdio.h>//计算器

voidmenu()//自定义的菜单界面

printf("--------------------\n");

printf("请输入你的选择\n");

printf("1.+\n");

printf("2.-\n");

printf("3.*\n");

printf("4./\n");

printf("--------------------\n");

intmain()

inti=0;

intj=0;

intnum=0;//计算结果存放在nun

intselect=0;//选择的选项存放在select

do//do-while先执行再判断循环条件,即可实现重复计算功能

menu();//打印出菜单界面

scanf("%d",&select);//输入你的选项

printf("请输入计算值:");

scanf("%d%d",&i,&j);//输入要计算的数值

switch(select)

case1:

printf("%d+%d=%d\n",i,j,num=i+j);//实现加法功能

break;

case2:

printf("%d-%d=%d\n",i,j,num=i-j);//实现减法功能

break;

case3:

printf("%d*%d=%d\n",i,j,num=i*j);//实现乘法功能

break;

case4:

printf("%d-%d=%d\n",i,j,num=i/j);//实现除法功能

break;

default:

printf("输入有误重新选择");

break;

}while(select);

return0;

运行结果:

扩展资料:

return表示把程序流程从被调函数转向主调函数并把表达式的值带回主调函数,实现函数值的返回,返回时可附带一个返回值,由return后面的参数指定。

return通常是必要的,因为函数调用的时候计算结果通常是通过返回值带出的。如果函数执行不需要返回计算结果,也经常需要返回一个状态码来表示函数执行的顺利与否(-1和0就是最常用的状态码),主调函数可以通过返回值判断被调函数的执行情况。


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

原文地址: https://outofmemory.cn/yw/11383456.html

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

发表评论

登录后才能评论

评论列表(0条)

保存