Hy everyone!
First I vould like to say that your great! I find lots of solutions here fom my programing problems.
I would like to write a calculator in Pascal/Delphi with scietific functions. For example: if I write [1,2,3]+[3,2,1], program return [4,4,4].
Does anybody have an idea how solve this problem?
Anna.B

## All 14 Replies

You can use arrays... :D
I created a solution for your question

``````Program SimpleCalculator;

Uses Crt;

Type NewArray = Array[1..3]Of Real;

Var a,b,c:NewArray;
e:Char;
i:Byte;

Procedure OutWrite(x:NewArray);
Var y:Byte;
Begin
For y:=1 To 3 Do Begin
If (y=3) Then Write(x[y]:1:0,' ')
Else Write(x[y]:1:0,',');
End;
End;

Begin
ClrScr;
{put the three numbers into array}
WriteLn('Write me the first three numbers: ');
For i:=1 To 3 Do ReadLn(a[i]);

WriteLn('Now give me the second three numbers: ');
For i:=1 To 3 Do ReadLn(b[i]);

WriteLn('Ok,let''s keep going,give me the operation +,-,*,/ : ');
{check the operation}
Case (e) Of
'+':For i:=1 To 3 Do c[i]:=a[i]+b[i];
'-':For i:=1 To 3 Do  c[i]:=a[i]-b[i];
'*':For i:=1 To 3 Do c[i]:=a[i]*b[i];
'/':For i:=1 To 3 Do c[i]:=a[i]/b[i];
Else Begin
Write('Wrong operation entered!');
Halt;
End;{of case else}
End;{of case}

WriteLn('The results are: ');
{call our procedure as many times as we need}
OutWrite(a);

Write(e,' ');

OutWrite(b);

Write('= ');

OutWrite(c);

End.

{

-=Created By FlamingClaw 2009.07.13=-

}``````

tnx for your solution FlamingClaw :) , but this is not what I want. My problem is a bit harder
Program should recognize characters [ ] and ,
So, I have to write to the console: [1,2,3]+[3,3,3] (exactly like this, and not number by number as you did) and then the program 'knows' that he had to sum numbers betven two comas or betwen coma and bracket. And then returns solution which is in this case [4,5,6] (solution should be also written with brackets and comas). Do you have any idea how to do it?

:'( I thought this solved your problem....
Ok,then you can use a record as a pointer and use a linked list to store these numbers

And what if you convert string to integer?

``````Program Solution;
Uses Crt;
Var MyNumbers:String;
I_tempA,I_tempB,Code:Integer;
Results:Array[1..3]Of Integer;
Begin
ClrScr;
WriteLn('Give me the line:');
{as [1,2,3]+[4,4,6]}
{
so
MyNumbers[ 0]:=15 the length of the string
MyNumbers[ 1]:='[';
MyNumbers[ 2]:='1'; * number
MyNumbers[ 3]:=',';
MyNumbers[ 4]:='2'; * number
MyNumbers[ 5]:=',';
MyNumbers[ 6]:='3'; * number
MyNumbers[ 7]:=']';
MyNumbers[ 8]:='+';  <-- operation
MyNumbers[ 9]:='[';
MyNumbers:='4'; * number
MyNumbers:=',';
MyNumbers:='4'; * number
MyNumbers:=',';
MyNumbers:='6'; * number
MyNumbers:=']';

so we have to work with these
first try to convert string to integer...
-==-
Val command converts a string to an integer.
Converts a string value to its numeric representation.
procedure Val(S; var V; var Code: Integer);
where:
S          string-type variable; must be a sequence of characters that
form a signed whole number
V          integer-type or real-type variable
Code       variable of type Integer.
Converts the string value (S) to its numeric representation,
-==-
And if we need the operation only have to look the
MyNumbers,cause this contains the operation.
And the result will be stored in *Results*
-==-
The first element of Results :
Val(MyNumbers[ 2],I_tempA,code);
If (code<>0)Then WriteLn('Error at position: ',code);
Val(MyNumbers,I_tempB,code);
If (code<>0)Then WriteLn('Error at position: ',code)
Else Results:=I_tempA+I_tempB;
}

End.``````

:D

I tried to do write code, it came out this:

``````uses
SysUtils,
Math;

var
vnos:string;

function vectors(s:string): string;
var v1,v2: string;
i,j,p: integer;
k: integer;
r,v,vv: array[1..255] of char;
o: integer;
op: char;
t:String;
l:Byte;
velikost:Byte;
Ar:Array[1..20]of Char;

begin

if (vnos[i]='+')  then
begin
op:='+';
o:=pos('+',vnos);
end;

for i:=1 to o do
v1:=copy(vnos,2,o-2);                 // v1= 2,3,4]
delete(v1, i, 1);

begin                                     //deleting comas from v1
i:=0;
while i<=Length(v1) do

if v1[i]=',' then

begin

velikost:=Length(v1);                     //str to int

For l:=1 To velikost Do
Begin
Ar[l]:=v1[l];
If (l = velikost) Then Break;
End;
For l:=1 To velikost Do
end
else Inc(i);
// Result:=strtoint(v1);
end;

for i := (o+1) to length(vnos) do
v2:=copy(vnos,(o+2),length(vnos));           // v2= 4,5,6]
delete(v2,length(v2),1);                 //delete ]

begin                                        //delete comas in v2
j:=0;
while j<=Length(v2) do
if v2[j]=',' then
begin
delete(v2, j, 1);
velikost:=Length(v2)-1;                     //str to int
For l:=1 To velikost Do
Begin
Ar[l]:=v1[l];
If (l = velikost) Then Break;
End;
For l:=1 To velikost Do
end
else Inc(j);
//Result:=strtoint(v2);
end;

r[p]:=(v1[i])+(v2[j]);            //sum v1 and v2
writeln('rezultat: ',r[p]);

// else
{ if vnos[i]='*' then
begin
op:='*';
o:=pos('*',vnos);
end
else
if vnos[i]='-' then
begin
op:='-';
o:=pos('-',vnos);
end;

}
end;

begin

vektor1(vnos);

end.``````

Now I don't know how to do operations(+,-,*) with these...

I tried to do write code, it came out this:

...
...
Now I don't know how to do operations(+,-,*) with these..

Hi Anna.B

When I look at your code.....there are too much error(like your function
wait string send string,but the structure of your function send integer???)
The conversion of string to integer also did not work....
Next error you want to add integer with char...?
Why do you delete the chars like ',' and '+' ?
etc......
If you look at my second code I talking about a string that can be treat as array where there is number char that can be convert to integer,and where there is a operation like MyNumbers:='+'; that can be check by Case...Else..end; statement as

``````Case (MyNumbers) Of
'+':Begin
Val(MyNumbers[ 2],I_tempA,code);
{so I_tempA := 1 as integer}
If (code<>0)Then WriteLn('Error at position: ',code);
Val(MyNumbers,I_tempB,code);
{and I_tempB := 4 as integer}
If (code<>0)Then WriteLn('Error at position: ',code)
Else Results:=I_tempA + I_tempB;
{after that Results:=1+4}
End;
'-': Begin
Val(MyNumbers[ 2],I_tempA,code);
If (code<>0)Then WriteLn('Error at position: ',code);
Val(MyNumbers,I_tempB,code);
If (code<>0)Then WriteLn('Error at position: ',code)
Else Results:=I_tempA - I_tempB;
End;
``````

etc....

I know there's lots of mistakes:\$ I'll ty to do it once again by your directions

But if I have line like this: [2,3]+[3,4], than the operation will not be on MyNumbers...

I thought that fix length of string....then use record pointers
....

I created the program for you :D

``````Program Solution;

Uses Crt;

{new type is points to a record in the memory}
Type NType = ^RType;

{here is the record}
RType = Record
Data:Char;{part of the string}
Converted:Real;{the converted char}
Position:Integer;{the position in the string}
End;

{global vars}
Var First,Akt,One,Second,Akt02,One02:NType;
Ustring:String;{user string}
OpPos:Integer;{stores the operation's position in the string}
OpIs:Char;{stores the operation as +,-,*,/}
SPart:String;{stores the second part of the user string}
Res:Real;{stores the sum of the real numbers}

{this procedure converts char to real}
{cause we want to division too...}
Procedure CharToReal(Var x:Char;Var y:Real);
Var code:integer;{if it is not 0 then can't be converted }
num:Real; {stores the converted numeric value}

Begin
Val(x,num,code);{call the built in function}
If code = 0 Then y:=num{y stores the converted value}
Else y:=Random(1);{I want to add some value if can't converted}

End;

{the Op function waits string to check the operation
and send back the position of the operation in the string}
Function Op(x:String):Integer;
Var ix:Integer;{will store the position of the operation}
Begin
For ix:=1 To Length(x) Do Begin
Case (x[ix]) Of
'+':Begin
OpIs:=x[ix]; {'+'}
Op:=ix;
End;
'-':Begin
OpIs:=x[ix]; {'-'}
Op:=ix;
End;
'*':Begin
OpIs:=x[ix]; {'*'}
Op:=ix;
End;
'/':Begin
OpIs:=x[ix]; {'/'}
Op:=ix;
End;
End;{of case}
End;{of for}
End;{of Op}

{fill the list}
Procedure FillUp(x:string;Var a,b,c:NType);
{*Counts* will count the number of '[' or ']'}
Var Counts,indx:Integer;
Begin
Counts:=0;
indx  :=0;
a     :=NIL;
Repeat
Inc(indx);
New(b);
b^.Next:=Nil;
b^.Data:=x[indx];
b^.Position:=indx;
CharToReal(b^.Data,b^.Converted);
If (a=Nil) Then a:=b
Else c^.Next:=b;
c:=b;
If (x[indx]='[')Or(x[indx]=']') Then Inc(counts);
Until Counts = 2;
End;

{we create this procedure to wipe out the list}
Procedure DestroyList(x:NType);
Begin
If x^.Next <> Nil Then DestroyList(x^.Next);
Dispose(x);
End;

Begin {main}
ClrScr;
SPart:='';
WriteLn('The line: ');
FillUp(Ustring,First,One,Akt);{first part}
OpPos:=Op(Ustring);
SPart:=Copy(Ustring,OpPos+1,100);
FillUp(Spart,Second,One02,Akt02);{second part}
{ok,the list is done,let's go back
to the begining of the two list}
ClrScr;
Write(Ustring,'=');
Akt:=First;
Akt02:=Second;
While (Akt<>Nil) And (Akt02<>Nil) Do Begin
If (Akt^.Position Mod 2 = 0) Then Begin
Case (OpIs) Of
'+':Res:=Akt^.Converted + Akt02^.Converted;
'-':Res:=Akt^.Converted - Akt02^.Converted;
'*':Res:=Akt^.Converted * Akt02^.Converted;
'/':Res:=Akt^.Converted / Akt02^.Converted;
End;
Write(Res:1:0);

End
Else Write(Akt^.Data);

Akt02:=Akt02^.Next;
Akt:=Akt^.Next;
End;
{wipe out them}
Akt:=First;
Akt02:=Second;
DestroyList(Akt);
DestroyList(Akt02);

End.
{Created By FlamingClaw 2009.07.15}``````

just copy and paste in your editor
it is can +,-,*,/ so you can try [1,2,3,4,5,6]-[1,1,2,3,1] too
:D

Tnx FlamingClaw! I realy appreciate your help. I already tried the program, it's working just like I want :)
tnx again

:D

``````(*   Hello AnnaB
I fixed my program as you wanted to.

Changes:
1,
Procedure CharToReal(Var x:char;Var y:Real);  To
Procedure CharsToReal(Var x:String;Var y:Real);
so if you type [245,354]+[100,200] then the result is
[245,354]+[100,200]=[345,554]

2,
RType = Record
*Data:String;{part of the string} fixed char to string
...
...

3,
If (OpIs = '/') Then Write(Res:1:2)
Else Write(Res:1:0);
so if you type [3,5]/[2,4] then the result is [3,5]/[2,4]=[1.50,1.25]

*)
Program BigCalculator_v02; {fixed version}

Uses Crt;

{new type is points to a record in the memory}
Type NType = ^RType;

{here is the record}
RType = Record
Data:String;{part of the string}
Converted:Real;{the converted char}
Position:Integer;{the position in the string}
End;

{global vars}
Var First,Akt,One,Second,Akt02,One02:NType;
Ustring:String;{user string}
OpPos:Integer;{stores the operation's position in the string}
OpIs:Char;{stores the operation as +,-,*,/}
SPart:String;{stores the second part of the user string}
Res:Real;{stores the sum of the real numbers}

{this procedure converts char to real}
{cause we want to division too...}
Procedure CharsToReal(Var x:String;Var y:Real);
Var code:integer;{if it is not 0 then can't be converted }
num:Real; {stores the converted numeric value}

Begin
Val(x,num,code);{call the built in function}
If code = 0 Then y:=num{y stores the converted value}
Else y:=Random(1);{I want to add some value if can't converted}

End;

{the Op function waits string to check the operation
and send back the position of the operation in the string}
Function Op(x:String):Integer;
Var ix:Integer;{will store the position of the operation}
Begin
For ix:=1 To Length(x) Do Begin
Case (x[ix]) Of
'+':Begin
OpIs:=x[ix]; {'+'}
Op:=ix;
End;
'-':Begin
OpIs:=x[ix]; {'-'}
Op:=ix;
End;
'*':Begin
OpIs:=x[ix]; {'*'}
Op:=ix;
End;
'/':Begin
OpIs:=x[ix]; {'/'}
Op:=ix;
End;
End;{of case}
End;{of for}
End;{of Op}

{fill the list}
Procedure FillUp(x:string;Var a,b,c:NType);
{*Counts* will count the number of '[' or ']'}
Var Counts,indx:Integer;
Begin
Counts:=0;
indx  :=0;
a     :=NIL;
Repeat
Inc(indx);
New(b);
b^.Next:=Nil;
b^.Data:=x[indx];
b^.Position:=indx;
CharsToReal(b^.Data,b^.Converted);
If (a=Nil) Then a:=b
Else c^.Next:=b;
c:=b;
If (x[indx]='[')Or(x[indx]=']') Then Inc(counts);
Until Counts = 2;
End;

{we create this procedure to wipe out the list}
Procedure DestroyList(x:NType);
Begin
If x^.Next <> Nil Then DestroyList(x^.Next);
Dispose(x);
End;

Begin {main}
ClrScr;
SPart:='';
WriteLn('The line: ');
FillUp(Ustring,First,One,Akt);{first part}
OpPos:=Op(Ustring);
SPart:=Copy(Ustring,OpPos+1,100);
FillUp(Spart,Second,One02,Akt02);{second part}
{ok,the list is done,let's go back
to the begining of the two list}
ClrScr;
Write(Ustring,'=');
Akt:=First;
Akt02:=Second;
While (Akt<>Nil) And (Akt02<>Nil) Do Begin
If (Akt^.Position Mod 2 = 0) Then Begin
Case (OpIs) Of
'+':Res:=Akt^.Converted + Akt02^.Converted;
'-':Res:=Akt^.Converted - Akt02^.Converted;
'*':Res:=Akt^.Converted * Akt02^.Converted;
'/':Res:=Akt^.Converted / Akt02^.Converted;
End;
If (OpIs = '/') Then Write(Res:1:2)
Else Write(Res:1:0);

End
Else Write(Akt^.Data);

Akt02:=Akt02^.Next;
Akt:=Akt^.Next;
End;
{wipe out them}
Akt:=First;
Akt02:=Second;
DestroyList(Akt);
DestroyList(Akt02);

End.
{Created By FlamingClaw 2009.07.15 and fixed 2009.07.29}``````
``````{
==%%==%%==%%==%%==%%
Well, I checked detaily code which you write.
There is one thing, which is wrong...
if we do the *, result is a number:
[1,2,3]*[4,5,6]=1*4+2*5+3*6=32 and not [5,7,9].
And we don't use / here, so we use only +,- and *.
Do you have any idea how to fix it?
Thanks
==%%==%%==%%==%%==%%
-= Notes by me =-
I didn't think that we have to add these numbers together
1*4+2*5+3*6=32 ... ok now fixed
Changes:
1,division deleted.
2,new var *add_res* to store the sum of the numbers.
now if you type [1,2,3]*[4,5,6]then the result is 32 as you wished,
and [1,2]+[3,4]=10.
3,when you get the result you have to press the enter twice.
4,changed the version of the program name
}

Program BigCalculator_v03; {refixed version}

Uses Crt;

{new type is points to a record in the memory}
Type NType = ^RType;

{here is the record}
RType = Record
Data:String;{part of the string}
Converted:Real;{the converted char}
Position:Integer;{the position in the string}
End;

{global vars}
Var First,Akt,One,Second,Akt02,One02:NType;
Ustring:String;{user string}
OpPos:Integer;{stores the operation's position in the string}
OpIs:Char;{stores the operation as +,-,*}
SPart:String;{stores the second part of the user string}
Res,add_res:Real;{stores the sum of the real numbers}

{this procedure converts char to real}
{cause we want to division too...}
Procedure CharsToReal(Var x:String;Var y:Real);
Var code:integer;{if it is not 0 then can't be converted }
num:Real; {stores the converted numeric value}

Begin
Val(x,num,code);{call the built in function}
If code = 0 Then y:=num{y stores the converted value}
Else y:=Random(1);{I want to add some value if can't converted}

End;

{the Op function waits string to check the operation
and send back the position of the operation in the string}
Function Op(x:String):Integer;
Var ix:Integer;{will store the position of the operation}
Begin
For ix:=1 To Length(x) Do Begin
Case (x[ix]) Of
'+':Begin
OpIs:=x[ix]; {'+'}
Op:=ix;
End;
'-':Begin
OpIs:=x[ix]; {'-'}
Op:=ix;
End;
'*':Begin
OpIs:=x[ix]; {'*'}
Op:=ix;
End;
End;{of case}
End;{of for}
End;{of Op}

{fill the list}
Procedure FillUp(x:string;Var a,b,c:NType);
{*Counts* will count the number of '[' or ']'}
Var Counts,indx:Integer;
Begin
Counts:=0;
indx  :=0;
a     :=NIL;
Repeat
Inc(indx);
New(b);
b^.Next:=Nil;
b^.Data:=x[indx];
b^.Position:=indx;
CharsToReal(b^.Data,b^.Converted);
If (a=Nil) Then a:=b
Else c^.Next:=b;
c:=b;
If (x[indx]='[')Or(x[indx]=']') Then Inc(counts);
Until Counts = 2;
End;

{we create this procedure to wipe out the list}
Procedure DestroyList(x:NType);
Begin
If x^.Next <> Nil Then DestroyList(x^.Next);
Dispose(x);
End;

Begin {main}
ClrScr;
SPart:='';
WriteLn('The line: ');
FillUp(Ustring,First,One,Akt);{first part}
OpPos:=Op(Ustring);
SPart:=Copy(Ustring,OpPos+1,100);
FillUp(Spart,Second,One02,Akt02);{second part}
{ok,the list is done,let's go back
to the begining of the two list}
ClrScr;
Write(Ustring,'=');
Akt:=First;
Akt02:=Second;
While (Akt<>Nil) And (Akt02<>Nil) Do Begin
If (Akt^.Position Mod 2 = 0) Then Begin
Case (OpIs) Of
'+':Res:=Akt^.Converted + Akt02^.Converted;
'-':Res:=Akt^.Converted - Akt02^.Converted;
'*':Res:=Akt^.Converted * Akt02^.Converted;
End;{of case}
End;{of If}

Akt02:=Akt02^.Next;
Akt:=Akt^.Next;
End; {of while}

WriteLn(add_Res:1:0); {write the result as decimal number}

{wipe out the records from the memory}
Akt:=First;
Akt02:=Second;
DestroyList(Akt);
DestroyList(Akt02);

End.
{Created By FlamingClaw 2009.07.15 and fixed again 2009.07.29}``````
``````(*   Hello AnnaB
I rewrote my program.

Changes:
1,

2,
RType = Record
*Data:String; fixed char to string
*Converted:LongInt; fixed real to longint
...

3,
Res:Real fixed to Res:LongInt

4,
If we give the line: [1,2]+[3,4], the solution is [4,6].
But when we do the *, solution is 11. So if we do the + or - we get solution
like you did in your first program. But if we do the * we get a number as a
solution. So, can you fix our code again?
Example:
[1,2]+[2,2]=[2,4]
[3,3]-[2,2]=[1,1]
[2,3]*[5,2]=16
*)
Program BigCalculator_v03; {new version BigCalculator v.0.3}

Uses Crt;

{===========================================
new type is points to a record in the memory
===========================================}
Type NType = ^RType;
{=================
here is the record
=================}
RType = Record
Data:String;{part of the string}
Converted:LongInt;{the converted char}
Position:Integer;{the position in the string}
End;
{==========
global vars
==========}
Var First,Akt,One,Second,Akt02,One02:NType;
Ustring:String;{user string}
OpPos:Integer;{stores the operation's position in the string}
OpIs:Char;{stores the operation as +,-,*}
SPart:String;{stores the second part of the user string}
Res:LongInt;{stores the sum of the numbers}
{======================================
this procedure converts char to LongInt
=======================================}
Procedure CharsToLongInt(Var x:String;Var y:LongInt);
Var code:integer;{if it is not 0 then can't be converted }
num:LongInt; {stores the converted numeric value}
Begin
Val(x,num,code);{call the built in function}
If code = 0 Then y:=num{y stores the converted value}
Else y:=Random(1);{I want to add some value if can't converted}
End;
{========================================================
the Op function waits string to check the operation
and send back the position of the operation in the string
========================================================}
Function Op(x:String):Integer;
Var ix:Integer;{will store the position of the operation}
Begin
For ix:=1 To Length(x) Do Begin
Case (x[ix]) Of
'+':Begin
OpIs:=x[ix]; {'+'}
Op:=ix;
End;
'-':Begin
OpIs:=x[ix]; {'-'}
Op:=ix;
End;
'*':Begin
OpIs:=x[ix]; {'*'}
Op:=ix;
End;
End;{of case}
End;{of for}
End;{of Op}
{=============================================
fill the list,one char one element of the list
=============================================}
Procedure FillUp(x:string;Var a,b,c:NType);
{*Counts* will count the number of '[' or ']'}
Var Counts,indx:Integer;
Begin
Counts:=0;
indx  :=0;
a     :=NIL;
Repeat
Inc(indx);
New(b);
b^.Next:=Nil;
b^.Data:=x[indx];
b^.Position:=indx;
CharsToLongInt(b^.Data,b^.Converted);
If (a=Nil) Then a:=b
Else c^.Next:=b;
c:=b;
If (x[indx]='[')Or(x[indx]=']') Then Inc(counts);
Until Counts = 2;
End;
{============================================
we create this procedure to wipe out the list
from the memory
============================================}
Procedure DestroyList(x:NType);
Begin
If x^.Next <> Nil Then DestroyList(x^.Next);
Dispose(x);
End;
{===========================
The main program begins here
===========================}
Begin {main program}
ClrScr;
SPart:='';
WriteLn('The line: ');
{==================
first part,one list
==================}
FillUp(Ustring,First,One,Akt);
OpPos:=Op(Ustring);
SPart:=Copy(Ustring,OpPos+1,100);
{=======================
second part,another list
========================}
FillUp(Spart,Second,One02,Akt02);
{==================================
ok,the lists are done,let's go back
to the begining of the two list
==================================}
ClrScr;
Write(Ustring,'=');
Akt:=First;
Akt02:=Second;
While (Akt<>Nil) And (Akt02<>Nil) Do Begin
If (Akt^.Position Mod 2 = 1) And (OpIs <> '*') Then Begin
Write(Akt^.Data);
End;
If (Akt^.Position Mod 2 = 0) Then Begin
Case (OpIs) Of
'+':Begin
Res:=Akt^.Converted + Akt02^.Converted;
Write(Res);{!!!}
End;
'-':Begin
Res:=Akt^.Converted - Akt02^.Converted;
Write(Res);{!!!}
End;
'*':Begin
Res:=Akt^.Converted * Akt02^.Converted;
End;
End;{of Case}
End;{of IF}
Akt02:=Akt02^.Next;{points to the next record}
Akt:=Akt^.Next;
End;{of While}
{================================
Check if there was multiplication
If OpIs = '*' then write add_res
without '[' or ']' or ','
================================}
If (OpIs = '*') Then Begin
End;{of IF}

{=================
wipe out the lists
=================}
Akt:=First;
Akt02:=Second;
DestroyList(Akt);
DestroyList(Akt02);