如何用VB做一个简单计算器 要求实现加减乘除乘方开方功能
用记事本把这段代码复制过去,器线ios用户界面源码然后另存为.frm就可以了VERSION 5.
Begin VB.Form frmMain
Appearance = 0 'Flat
BackColor = &H&
BorderStyle = 3 'Fixed Dialog
Caption = "简单计算器"
ClientHeight =
ClientLeft =
ClientTop =
ClientWidth =
KeyPreview = -1 'True
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight =
ScaleWidth =
ShowInTaskbar = 0 'False
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command
Caption = "^"
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.PictureBox picDisplay
Appearance = 0 'Flat
BackColor = &H&
ForeColor = &H&
Height =
Left =
ScaleHeight =
ScaleWidth =
TabIndex =
Top =
Width =
Begin VB.TextBox Text1
Alignment = 1 'Right Justify
BorderStyle = 0 'None
Height =
Left =
Locked = -1 'True
TabIndex =
Top = 0
Width =
End
Begin VB.Label lblOperation
Appearance = 0 'Flat
BackColor = &H&
ForeColor = &H&
Height =
Left =
TabIndex =
Top = 0
Width =
End
End
Begin VB.CommandButton Command9
Caption = "退格"
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton Command8
Caption = "清零"
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton cmdDot
Caption = "."
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton Command7
Caption = "Sqrt"
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton Command6
Caption = "="
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton Command5
Caption = "/"
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton Command4
Caption = "*"
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton Command3
Caption = "-"
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton Command2
Caption = "+"
Height =
Left =
TabIndex =
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "9"
Height =
Index = 9
Left =
TabIndex = 9
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "8"
Height =
Index = 8
Left =
TabIndex = 8
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "7"
Height =
Index = 7
Left =
TabIndex = 7
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "6"
Height =
Index = 6
Left =
TabIndex = 6
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "5"
Height =
Index = 5
Left =
TabIndex = 5
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "4"
Height =
Index = 4
Left =
TabIndex = 4
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "3"
Height =
Index = 3
Left =
TabIndex = 3
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "2"
Height =
Index = 2
Left =
TabIndex = 2
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "1"
Height =
Index = 1
Left =
TabIndex = 1
Top =
Width =
End
Begin VB.CommandButton Command1
Caption = "0"
Height =
Index = 0
Left =
TabIndex = 0
Top =
Width =
End
End
Attribute VB_Name = "frmmain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Data As Double
Private lOperation As Long
Private fClear As Boolean
Private Sub cmdDot_Click()
If InStr(1,乘方乘方java简单软件源码 Text1.Text, ".") <= 0 Then Text1.Text = Text1.Text & "."
End Sub
Private Sub Command1_Click(Index As Integer)
If fClear = True Then Text1.Text = "": fClear = False
Text1.Text = Text1.Text & Index
End Sub
Private Sub Command_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1.Text = Data + Val(Text1.Text)
Case 2: Text1.Text = Data - Val(Text1.Text)
Case 3: Text1.Text = Data * Val(Text1.Text)
Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)
Case 5: Text1.Text = Data ^ Val(Text1.Text)
End Select
End If
Data = Val(Text1.Text)
fClear = True
lOperation = 5
lblOperation.Caption = "^"
End Sub
Private Sub Command2_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1.Text = Data + Val(Text1.Text)
Case 2: Text1.Text = Data - Val(Text1.Text)
Case 3: Text1.Text = Data * Val(Text1.Text)
Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)
Case 5: Text1.Text = Data ^ Val(Text1.Text)
End Select
End If
Data = Val(Text1.Text)
fClear = True
lOperation = 1
lblOperation.Caption = "+"
End Sub
Private Sub Command3_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1.Text = Data + Val(Text1.Text)
Case 2: Text1.Text = Data - Val(Text1.Text)
Case 3: Text1.Text = Data * Val(Text1.Text)
Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)
Case 5: Text1.Text = Data ^ Val(Text1.Text)
End Select
End If
Data = Val(Text1.Text)
fClear = True
lOperation = 2
lblOperation.Caption = "-"
End Sub
Private Sub Command4_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1.Text = Data + Val(Text1.Text)
Case 2: Text1.Text = Data - Val(Text1.Text)
Case 3: Text1.Text = Data * Val(Text1.Text)
Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)
Case 5: Text1.Text = Data ^ Val(Text1.Text)
End Select
End If
Data = Val(Text1.Text)
fClear = True
lOperation = 3
lblOperation.Caption = "*"
End Sub
Private Sub Command5_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1.Text = Data + Val(Text1.Text)
Case 2: Text1.Text = Data - Val(Text1.Text)
Case 3: Text1.Text = Data * Val(Text1.Text)
Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)
Case 5: Text1.Text = Data ^ Val(Text1.Text)
End Select
End If
Data = Val(Text1.Text)
fClear = True
lOperation = 4
lblOperation.Caption = "/"
End Sub
Private Sub Command6_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1.Text = Data + Val(Text1.Text)
Case 2: Text1.Text = Data - Val(Text1.Text)
Case 3: Text1.Text = Data * Val(Text1.Text)
Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)
Case 5: Text1.Text = Data ^ Val(Text1.Text)
End Select
End If
Data = Val(Text1.Text)
fClear = True
lOperation = 0
lblOperation.Caption = ""
End Sub
Private Sub Command7_Click()
If Val(Text1.Text) <= 0 Then MsgBox "开方数大于等于0"
Text1.Text = Sqr(Val(Text1.Text))
Data = Val(Text1.Text)
fClear = True
lOperation = 0
lblOperation.Caption = ""
End Sub
Private Sub Command8_Click()
Text1.Text = ""
lOperation = 0
lblOperation.Caption = ""
End Sub
Private Sub Command9_Click()
If Len(Text1.Text) > 0 Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0"): Command1_Click 0
Case Asc("1"): Command1_Click 1
Case Asc("2"): Command1_Click 2
Case Asc("3"): Command1_Click 3
Case Asc("4"): Command1_Click 5
Case Asc("5"): Command1_Click 4
Case Asc("6"): Command1_Click 6
Case Asc("7"): Command1_Click 7
Case Asc("8"): Command1_Click 8
Case Asc("9"): Command1_Click 9
Case Asc("."): cmdDot_Click
Case Asc("+"): Command2_Click
Case Asc("-"): Command3_Click
Case Asc("*"): Command4_Click
Case Asc("/"): Command5_Click
Case Asc("^"): Command_Click
Case Asc("s"): Command7_Click
Case Asc("S"): Command7_Click
Case vbKeyBack: Command9_Click
Case Asc("`"): Command8_Click
Case Asc("="): Command6_Click
Case : Command6_Click
End Select
KeyAscii = 0
End Sub
有一个进行数学乘方的C源代码,但运行结果不对,开方开方求教各位大侠,源码急用。计算
直接使用pow函数不就可以了吗?
或:
将
for(i=1;i<=exponent;i++)
{
initial=base;
base*=base;
value=base*initial;
}
改为:
value=base;
for(i=1;i<exponent;i++)
{
value=value*base;
}
å¦ä½ç¨Cè¯è¨ç¼åä¸ä¸ªç§å¦è®¡ç®å¨
ç¨æ å°±å¯ä»¥åå°äºãããè¿ä¸ªå¾è¯¦ç»çï¼ lz é便è¾å ¥ä¸ä¸ªè¡¨è¾¾å¼ï¼ä¸é´ç计ç®è¿ç¨å ¨é¨è¾åºäºï¼lzè¯ä¸¤ä¸ª å°±ç¥éæä¹åäºäºã #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE ;
typedef struct
{
char data[];
int top;//头å°å
int base;//åºå°å
int length;//é¿åº¦
}Stack;
void init(Stack *st)//åå§åæ
{
st->base=0;
st->top=0;
st->length=0;
}
int isEmpty(Stack *st)
{
int n=0,器线top,base;
top =st->top;
base =st->base;
if(top==base)
{
return 1;
}
return n;
}
int isFull(Stack *st)
{
int n=0,top,base;
top =st->top;
if(top>=)
{
return 1;
}
return n;
}
char getTop(Stack *st)// è¿åtopå¼ï¼ä¸æ¹åæ çç»æ
{
char n;
if(isEmpty(st))
{
printf("æ 为空\n");
return 0;
}
int positon= st->top-1;
n= st->data[positon];//ååºæ°æ®;
return n;
}
char pop(Stack *st)// åºæ ï¼è¿å
{
char n;
if(isEmpty(st))
{
printf("æ 为空\n");
return 0;
}
int positon= st->top-1;
n= st->data[positon];//ååºæ°æ®;
st->top--;
st->length--;
st->data[positon]='\0';//æ¶é¤æ°æ®
return n;
}
void push(char n,Stack *st)//å ¥æ
{
int positon ;
if(isFull(st))
{
printf("æ 满\n");
}
else
{
positon= st->top;//è·åä½ç½®
st->data[positon]=n;//åå ¥æ°æ®
st->top++;//æ¹åä½ç½®
}
}
void show(Stack *m1)//è¾åºæ ä¸çæ°æ®
{
int top,base;
top=m1->top;
base=m1->base;
while(top>base)
{
printf("%c,",m1->data[--top]);
}
printf("\n");
}
int isOperate(char temp)//æ¯å¦æ¯æä½ç¬¦
{
if(temp=='+'||temp=='-'||temp=='*'||temp=='/'||temp=='('||temp==')'||temp=='#')
{
return 1;
}
return 0;
}
int isValue(char temp)//æ¯å¦æ¯æ°å¼
{
if(temp>='0'&&temp<='9')//
{
return 1;
}
else
{
return 0;
}
}
int isAvail(char temp)//æ¯å¦ææå符
{
if(isOperate(temp)||isValue(temp))//å¦ætempæ¢ä¸æ¯æä½ç¬¦åæ°å¼çè¯ï¼åå®æ¯éæ³ç
{
return 1;
}
return 0;
}
int detect(char temp)//æç´¢ç©éµä½ç½®
{
int i=0;
char oper[7]={ '+','-','*','/','(',')','#'};
for(i=0;i<7;i++)
{
if(temp==oper[i])
{
return i;
}
}
}
char Priority(char temp,char optr)//å¤æä¼å 级
{
/**//
*+ - * / ( ) #
1 2 3 4 5 6 7
+ 1 < < < < > > >
- 2 < < < < > > >
* 3 > > < < > > >
/ 4 > > < < > > >
( 5 > > > > > = 0
) 6 < < < < = 0 >
# 7 < < < < > 0 =
*/
int row ,col;
char priority[7][7]={ /**//* + - * / ( ) # */
{ '<','<','<','<','>','>','>'},
{ '<','<','<','<','>','>','>'},
{ '>','>','<','<','>','>','>'},
{ '>','>','<','<','>','>','>'},
{ '>','>','>','>','>','=','>'},
{ '<','<','<','<','=','0','>'},
{ '<','<','<','<','>','<','='},
};
row = detect(temp);//æ¾åºå¯¹åºçç©éµä¸æ ;
col = detect(optr);
// printf("%d,%d",row,col);
//ä¼å 级åå¨å¨ä¸ä¸ª7x7çç©éµä¸ï¼å¯¹åºå ³ç³»ä¸å¾ï¼
return priority[row][col];
}
char evaluate(int a,int b,char oper)
{
switch(oper)
{
case '+': return a+b+'0';
case '-': return a-b+'0';
case '*': return a*b+'0';
case '/': return a/b+'0';
default : return 0+'0';
}
}
int calculateExpress(char *express)//计ç®è¡¨è¾¾å¼
{
int result=0;
int a,b;
// char oper,result;
Stack OPTR,OPND;//OPTRåå¨æä½ç¬¦ï¼OPNDæä½æ°å¼
init(&OPTR);
init(&OPND);
push('#',&OPTR);//é»è®¤ç¬¬ä¸ä¸ªä½'#'
////////////////////-ç®æ³-////////////////////////////
while(*express!='\0')
{
char temp;
temp= *(express);
printf("---------------------------------\n");
printf("å½åç符å·ä¸º%c\n",temp);
if(isAvail(temp))//æ¯å¦æ¯ææå符
{
if(isOperate(temp) )//è¾å ¥çæ¯æä½ç¬¦
{
char oper,result;
char optr = getTop(&OPTR);//æ ä¸topä½çæä½ç¬¦
printf("æ 顶æä½ç¬¦ä½ï¼%c\n",optr);
char prior = Priority(temp,optr);//å¤æä¼å 级
switch(prior)
{
case '>':
push(temp,&OPTR);
printf("å°ç¬¦å·ä½%cå ¥æ \n",temp);
express++;
break;
case '<':
//int a,b;
//char oper,result;
a=pop(&OPND)-'0';//åå¨æ ä¸çé½æ¯charå符
b=pop(&OPND)-'0';
oper=pop(&OPTR);
result=evaluate(b,a,oper);//åºæ ä¸ä¸ªæä½ç¬¦ï¼è®¡ç®ç»æ
//printf("%d",result-'0');
push(result,&OPND);//ç»æå ¥OPND
printf("%d%c%dç»æ为:%d\n",b,oper,a,result-'0');
break;
case '=':
//æ¶é¤æ¬å·
pop(&OPTR);
printf("æ¶é¤æ¬å·\n");
express++;
break;
}
}
if(isValue(temp))//è¾å ¥çæ¯æ°å¼
{
push(temp,&OPND);//å°æ°å¼ä½å ¥æ ;
express++;
printf("å°æ°å¼%cåå ¥æ \n",temp);
//show(&OPND);
}
}
else //表达å¼ä¸æéæ³å符
{
printf("表达å¼ä¸æéæ³å符\n");
exit(-1);//éåºç¨åº
}
}
// show(&OPND);
// show(&OPTR);
return getTop(&OPND)-'0';
}
void inputExpress(char *express)//è¾å ¥è¡¨è¾¾å¼
{
int length=0;
printf("请è¾å ¥ä¸ä¸ªè¡¨è¾¾å¼:");
scanf("%s",express);
int len =strlen(express);
express[len]='#';//表达å¼æåä¸ä½é»è®¤ä¸º'#';
express[len+1]='\0';
}
void output(char *express,int result)//è¾åºè¡¨è¾¾å¼
{
int i=0;
printf("----------------------------------------\n表达å¼:");
while(express[i]!='#')
{
printf("%c",express[i]);
i++;
}
printf("=%d\n",result);
}
int main()
{
char express[];//表达å¼
int result =0;
inputExpress(express);//è¾å ¥è¡¨è¾¾å¼
result = calculateExpress(express);//计ç®è¡¨è¾¾å¼ï¼
output(express,result); //è¾åºè¡¨è¾¾å¼
//ãããããããããããããæµè¯ä¼å 级ã
/**//
*char m='7' ;
m=Priority('+','*');
printf("ä¼å 级为%c",m);
int m=evaluate(5,6,'m');
printf("%d",m);
*/
return 0;
}
2024-12-26 12:11
2024-12-26 11:50
2024-12-26 11:38
2024-12-26 10:39
2024-12-26 10:22