Home > Software engineering >  Consult a great god: VB calculator even add even subtract problems
Consult a great god: VB calculator even add even subtract problems

Time:09-27

[code=vb
] Private Sub Command7_Click (Index As an Integer)
M1=Val (Text1. Text) 'computing symbols before take the number 1
Suanfa=Command7 (Index). Caption
Text1. Text=""
End Sub
Private Sub Command6_Click ()
M2=Val (Text1. Text) 'equal access 2
The Select Case suanfa
The Case "+"
Text1. Text=m1 + m2
Case ", "
Text1. Text=m1, m2
Case '*'
Text1. Text=* m1 m2
Case "/"
If (m2=0) Then
Text1. Text="divisor cannot be zero."
The Else
Text1. Text=m1/m2
End the If
This is my homework, and can only be implemented once add and subtract, at present, how to connect the plus even minus?
End the Select
End Sub
[/code]

CodePudding user response:

"Compiler principle" reference of lexical analysis and the finite state automata,
Refer to the following, although C:
//a string 
//1 _22_333, 4444 __55555 _666666
//need to parse for
//1
//22
//333
//_
//4444
//55555
//666666
# include & lt; stdio.h>
Char [] s="_22_333, 1, 4444 __55555, _666666";
Char c, * p, * p;
Int st.
Void main () {
St=0;
P=s;
While (1) {
C=* p;
If (0==c) {
The switch (st) {
Case 1: printf (" _ \ n "); break;
Case 2: printf (" % s \ n ", (p1); break;
}
break;//
}
The switch (st) {
Case 0:
If (' _ '==c) {st=0; }
Else if (', ')==c {st=1; }
The else {p1=p; St=2; }
break;
Case 1:
If (' _ '==c) {st=1; }
Else if (', ')==c {printf (" _ \ n "); St=1; }
The else {p1=p; St=2; }
break;
Case 2:
If (' _ '==c) {* p=0; Printf (" % s \ n ", (p1); * p=c; St=0; }
Else if (', ')==c {* p=0; Printf (" % s \ n ", (p1); * p=c; St=1; }
The else {st=2; }
break;
}
p++;
}
}
//1
//22
//333
//_
//4444
//55555
//666666

CodePudding user response:

Thank the teacher answer,,, but still I didn't understand,,

CodePudding user response:

Only one character scan expression... Scanning to the character, record the position, and remove the character before the text with the mid, and then converted to digital, as the Numbers 1 to participate in the operation, and then remove the sign of operation, and then continue to scan the next operator or end expression, use mids, as number two, with just the operation of symbolic operation, the result as the number 1, and then continue to scan to the end of the expression...

The whole process is like this... However, as a subject for beginners, is a bit difficult to... To be honest...

CodePudding user response:

The
reference 3 floor TalentLi response:
only one character scan expression... Scanning to the character, record the position, and remove the character before the text with the mid, and then converted to digital, as the Numbers 1 to participate in the operation, and then remove the sign of operation, and then continue to scan the next operator or end expression, use mids, as number two, with just the operation of symbolic operation, the result as the number 1, and then continue to scan to the end of the expression...

The whole process is like this... However, as a subject for beginners, is a bit difficult to... To be honest...


Code debugging through...

Private Sub Form_Load ()
Dim exp As String

Exp="3 * 4/6" 'expression to compute the, only support from doing right + - */does not support decimal arithmetic

Dim strNum As String, strOpt As String 'temporary number
Dim STR As String
Dim the Result As Long, Num As Long
Dim As Long I

For I=1 To Len (exp)

STR=Mid $(exp, I, 1) 'take one character at a time

If (IsNumeric (STR)=False) Then 'operator,

Num=Int (strNum) 'before the number strings to Numbers
StrNum=""

If Len (strOpt)=0 Then 'found no punctuation, before the first number as the results
Result=Num
The Else
Result=CalIt (Result, Num, strOpt) 'found before punctuation, carry out calculations
End the If
StrOpt=STR 'record new punctuation

The Else 'before didn't meet operator, merged into the digital
StrNum=strNum & amp; STR
End the If
Next

If Len (strNum) & gt; 0 Then
Num=Int (strNum)
Result=CalIt (Result, Num, strOpt)
End the If

MsgBox Result
End Sub

The Function CalIt (ByVal Num1 As Long, ByVal Num2 As Long, ByVal Opt As String) As Long

If Len (Opt)=0 Then Opt=default operator is a plus sign "+" '

The Select Case Opt
The Case "+"
CalIt=Num1 + Num2

Case ", "
CalIt=Num1 - Num2

Case '*'
CalIt Num2=Num1 *

Case "/"
CalIt=Num1/Num2

End the Select
End Function
  • Related