Poll: Is the program good?
You do not have permission to vote in this poll.
Yes
100.00%
2 100.00%
No
0%
0 0%
Total 2 vote(s) 100%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String adder, multipiler, divider, substracter
#1
I am making a program that adds strings and yadda yadda. this was made before but i felt like making one.
Heres the add part. (this is all i have done for the time being)
Code:
DECLARE FUNCTION add$ (n1$, n2$)

FUNCTION add$ (n1$, n2$)
IF VAL(n1$) > VAL(n2$) THEN b1$ = n1$ ELSE b1$ = n2$     'Finds the biggest number

'###Following 2 lines make the lengths the same###
IF LEN(n1$) < LEN(n2$) THEN n1$ = STRING$(LEN(n2$) - LEN(n1$), "0") + n1$
IF LEN(n2$) < LEN(n1$) THEN n2$ = STRING$(LEN(n1$) - LEN(n2$), "0") + n2$

DIM num(LEN(b1$) + 1, 3) 'Dims an array

FOR i = LEN(b1$) + 1 TO 2 STEP -1    '\
num(i, 1) = VAL(MID$(n1$, i - 1, 1)) '|===Puts numbers
num(i, 2) = VAL(MID$(n2$, i - 1, 1)) '|===into array
NEXT                                 '/

'the following 4 lines add.
FOR x = LEN(b1$) + 1 TO 1 STEP -1
num(x, 3) = num(x, 3) + num(x, 2) + num(x, 1)
IF num(x, 3) > 9 THEN num(x - 1, 3) = 1: num(x, 3) = VAL(LTRIM$(RIGHT$(STR$(num(x, 3)), 1)))
NEXT x

'Num$ = the added numbers
FOR x = 1 TO LEN(b1$) + 1
num$ = num$ + LTRIM$(STR$(num(x, 3)))
NEXT x

'Removes "0"s off the front
DO
z = z + 1
IF z > LEN(num$) THEN EXIT DO
IF MID$(num$, z, 1) = "0" THEN num$ = RIGHT$(num$, LEN(num$) - 1) ELSE EXIT DO
LOOP
add$ = num$

END FUNCTION

if you have a problem just ask. I accept compliments and complaint but i tjhink i will listen to the complements more

edit: found mistake and fixed
edit: added comments
edit: fixed problem when adding 0 and 0
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#2
Heres subtraction!!!!

Code:
DECLARE FUNCTION sub$ (n1$, n2$)

FUNCTION sub$ (n1$, n2$)
IF VAL(n1$) >= VAL(n2$) THEN b1$ = n1$ ELSE PRINT "First num must be bigger then sec": END

IF LEN(n1$) < LEN(n2$) THEN n1$ = STRING$(LEN(n2$) - LEN(n1$), "0") + n1$
IF LEN(n2$) < LEN(n1$) THEN n2$ = STRING$(LEN(n1$) - LEN(n2$), "0") + n2$

DIM num(LEN(b1$), 3)

FOR i = LEN(b1$) TO 1 STEP -1
num(i, 1) = VAL(MID$(n1$, i, 1))
num(i, 2) = VAL(MID$(n2$, i, 1))
NEXT

PRINT
FOR i = LEN(b1$) TO 1 STEP -1
IF num(i, 1) < num(i, 2) THEN num(i - 1, 1) = num(i - 1, 1) - 1: num(i, 1) = VAL("1" + LTRIM$(STR$(num(i, 1))))
num(i, 3) = num(i, 1) - num(i, 2)
NEXT i

FOR i = 1 TO LEN(b1$)
num$ = num$ + LTRIM$(STR$(num(i, 3)))
NEXT i

DO
z = z + 1
IF z = LEN(num$) THEN EXIT DO
IF MID$(num$, z, 1) = "0" THEN num$ = RIGHT$(num$, LEN(num$) - 1) ELSE EXIT DO
LOOP

sub$ = num$

END FUNCTION
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#3
It's already been done by Neo. And I'm using it as the basis for StatLib (which, BTW, can handle negative numbers, decimal points and numberEXPpower format (like 1.6e9)). Sorry Wink
Reply
#4
i kow it was already done
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#5
when I was a student, back in 1983, we were asked to calculate the 1000 first decimals of Pi. The school director, considering this would consume to much CPU, issued a memo saying 100 decimals would be enough! (we were using the Univac of a research center).

In fact, I never calculated these decimals, since I did not see any interest in such a stupid and useless problem... I just joined a fellow who was a computer freak, and signed his prog (he hated physics and chemistry, and I hated maths and computers: so we only worked half time each :lol: )

Last year, I decided to take a revenge on my students years, and developped some array calcultation routines to calculate Pi with QB on a PC. I think my prog works, it's just the suite I used that was the slowest available : after 3/4 of an hour, the prog told me that Pi was approximately 3.14 :rotfl:

I have to come back on that prog when I have time
hink Global, Make Symp' All ! ®
[Image: Banner.gif]
Reply
#6
This code by Jason Stratos Papadopoulos calculates 8000 decimals of pi in a few minutes.
http://www.basicguru.com/files/abc/abc9905/picalc48.bas
Antoni
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)