sekelsenmat
06-06-2006, 03:05 PM
Hello. I am writing a calculator with MidletPascal. You type into one input field and it shows the result after you click "Calcular".
Currently it only accepts very simple input, on the kind A Operator B
Where A and B are real or integer numbers and Operator is {*, /, -, +, ^}
I am showing the output using this:
uiText := FormAddString('' + Ret);
But then the text is added linearly on the form. I would like to jump one line each time a calculation is done. I tryed this:
uiText := FormAddString('' + Ret + '\n');
But it doesn¬Ąt work =/
Bellow is my code in case anyone wants to see:
{
Pascalc - a simple calculator with memory
}
program pascalc;
const
cMultiply = '*';
cSum = '+';
cSubtract = '-';
cDivide = '/';
cPower = '^';
var
cmdQuit, cmdCalc, clicked: command;
uiInput, uiText: integer;
expression: string;
Ret: real;
procedure initialize;
begin
end;
procedure drawScreen;
begin
// switch to form mode from default canvas mode
ShowForm;
SetFormTitle('Calculadora');
uiInput := FormAddTextField('Entre com a express?Ło:', '', 20, TF_ANY);
cmdQuit := createCommand('Sair', CM_EXIT, 1);
cmdCalc := createCommand('Calcular', CM_OK, 1);
addCommand(cmdQuit);
addCommand(cmdCalc);
repaint;
end;
procedure CalculateExpression;
var
first, second, str: string;
i, pos: Integer;
ifirst, isecond: real;
Found: Boolean;
begin
pos := 0; // pos = 0 indica erro na express?Ło
Found := False;
// Locates where the operator is
for i := 0 to Length(expression) - 1 do
begin
Str := Copy(expression, i, i + 1);
if (Str = cMultiply) or (Str = cSum) or (Str = cSubtract) or (Str = cDivide) or (Str = cPower) then
begin
// If the operator is found twice, the expression is wrong
if Found = True then pos := 0
else
begin
pos := i;
Found := True;
end;
end;
end;
if pos <> 0 then
begin
// Separates the string
first := Copy(expression, 0, pos);
ifirst := StringToReal(first, 10);
second := Copy(expression, pos + 1, Length(expression));
isecond := StringToReal(second, 10);
// Does the calculation
Str := Copy(expression, pos, pos + 1);
if Str = cMultiply then Ret := ifirst * isecond
else if Str = cSum then Ret := ifirst + isecond
else if Str = cSubtract then Ret := ifirst - isecond
else if Str = cDivide then Ret := ifirst / isecond
else if Str = cPower then Ret := Pow(ifirst, isecond);
// Mostra o resultado na tela
uiText := FormAddString('' + Ret);
end
else uiText := FormAddString('Erro');
end;
begin
initialize;
drawScreen;
// wait until the user clickes on a button
repeat
clicked := getClickedCommand;
if (clicked = cmdCalc) then
begin
// retrieve the entered name
expression := FormGetText(uiInput);
CalculateExpression;
end;
until clicked = cmdQuit;
end.[/code]
Currently it only accepts very simple input, on the kind A Operator B
Where A and B are real or integer numbers and Operator is {*, /, -, +, ^}
I am showing the output using this:
uiText := FormAddString('' + Ret);
But then the text is added linearly on the form. I would like to jump one line each time a calculation is done. I tryed this:
uiText := FormAddString('' + Ret + '\n');
But it doesn¬Ąt work =/
Bellow is my code in case anyone wants to see:
{
Pascalc - a simple calculator with memory
}
program pascalc;
const
cMultiply = '*';
cSum = '+';
cSubtract = '-';
cDivide = '/';
cPower = '^';
var
cmdQuit, cmdCalc, clicked: command;
uiInput, uiText: integer;
expression: string;
Ret: real;
procedure initialize;
begin
end;
procedure drawScreen;
begin
// switch to form mode from default canvas mode
ShowForm;
SetFormTitle('Calculadora');
uiInput := FormAddTextField('Entre com a express?Ło:', '', 20, TF_ANY);
cmdQuit := createCommand('Sair', CM_EXIT, 1);
cmdCalc := createCommand('Calcular', CM_OK, 1);
addCommand(cmdQuit);
addCommand(cmdCalc);
repaint;
end;
procedure CalculateExpression;
var
first, second, str: string;
i, pos: Integer;
ifirst, isecond: real;
Found: Boolean;
begin
pos := 0; // pos = 0 indica erro na express?Ło
Found := False;
// Locates where the operator is
for i := 0 to Length(expression) - 1 do
begin
Str := Copy(expression, i, i + 1);
if (Str = cMultiply) or (Str = cSum) or (Str = cSubtract) or (Str = cDivide) or (Str = cPower) then
begin
// If the operator is found twice, the expression is wrong
if Found = True then pos := 0
else
begin
pos := i;
Found := True;
end;
end;
end;
if pos <> 0 then
begin
// Separates the string
first := Copy(expression, 0, pos);
ifirst := StringToReal(first, 10);
second := Copy(expression, pos + 1, Length(expression));
isecond := StringToReal(second, 10);
// Does the calculation
Str := Copy(expression, pos, pos + 1);
if Str = cMultiply then Ret := ifirst * isecond
else if Str = cSum then Ret := ifirst + isecond
else if Str = cSubtract then Ret := ifirst - isecond
else if Str = cDivide then Ret := ifirst / isecond
else if Str = cPower then Ret := Pow(ifirst, isecond);
// Mostra o resultado na tela
uiText := FormAddString('' + Ret);
end
else uiText := FormAddString('Erro');
end;
begin
initialize;
drawScreen;
// wait until the user clickes on a button
repeat
clicked := getClickedCommand;
if (clicked = cmdCalc) then
begin
// retrieve the entered name
expression := FormGetText(uiInput);
CalculateExpression;
end;
until clicked = cmdQuit;
end.[/code]