← Back to topics
Topic

factorials

p
parkins
How I can read from standard input 3!!! or 2/3 in Pascal?
u
uros94
Read it as a string, then parse it :)
p
parkins
How I can parse string? I don't know how pascal can make integer or longint from a part of string?
a
adminModerator
Well, that is one of the clues in the problem.

When you read a string "s" with: readln(s), then you can access all the elements of "s" as s[0], s[1], ...

from these values you can reconstruct the number.
A
Al3kSaNdaR
Try to google string functions in pascal, like Str, Val, Copy, Delete, Pos...
p
parkins
s[1],s[2] is an element of string, a part of number. But this element is also string type,so I can write this value to integer variable,so I can make a number in integer variable.
p
parkins
Example:
var s:string
a:integer;
begin
readln(s);
a:=s[2]; {s[2]is for example 3}

An error type mismatch occures. This is my problem whith this.
A
Al3kSaNdaR
Here is part of my code, that gives you reading input.



ReadLn(Strr);
Poz:=Pos('!', Strr);
Val(Copy(Strr, 1, Poz - 1), n);
Delete(Strr, 1, Poz - 1);
k:=LengTh(Strr);



Strr is a String type. N & K are LongInt.
A
Al3kSaNdaR
For your case to parse S[2] to Int type you should write this.



Val ( Copy ( S, 2, 1 ) , A );

A
Al3kSaNdaR
Look at this site.

http://www.macdonald.egate.net/CompSci/Pascal/hstrings.html
p
parkins
thanks a lot. Does Pascal have more functions and procedures like val,copy etc?
A
Al3kSaNdaR
Don't know. Google it. :) I use Chr and Ord function for characters.