Reversing - Urgent!

Hello

I need to write a program that receives a
real num. if the frac part of the num is a
mirror image of the whole part, (for example:
123.321 is, 52.9 isn't, 43.30 isn't, etc.) then
the program will print "yes" else it will print
"No". the point is that you don't know how many
digits the num has. Please tell me how to it
(without using external units or anything complex)

I need it urgently and ASAP!!!!!!!!!!!!!!!

Thanks

P.S. It's not homework, but a question I had last year.
     I need it urgently because I have been trying for
     months and I didn't manage to do it. So if you can
     do it - please post your answer! Thanks!
bizunaAsked:
Who is Participating?
 
k61824Connect With a Mentor Commented:
try something like this for really long numbers (I think it is at the limit of the best real types you can get) also this thing assumes ASCII set.  there is nothing typed real, so precision isn't a problem.
it checks for numbers too.
it also ignores the exponent (that e+06 in 123.45e+06), biased decimal points (1234.321), and signs that appears in the front, like -252

program try;
var i: integer;{loop control variable}
s: string;{user input}
e: integer;{required by val}
r: boolean;{reverse}

function checknum(s: string): boolean;
var f: boolean;{number flag}
    p: boolean;{decimal point}
    e: boolean;{exponent sign}
    i: integer;{loop control variable}
begin
  e := false;{initialization}
  p := false;{initialization}
  f := true;{initialization}
  for i := 1 to length(s)do
    begin
      if f then
        begin
          if upcase(s[i]) = 'E' then
            begin
              if e then
                f := false
              else
                e := true
            end
          else if (s[i]) = '.' then
            begin
             if (i > pos(s,'e')) and (i > pos(s,'E')) then
                f := false
             else
               begin
                 if e then
                   f := false
                 else
                   e := true
               end
            end
          else if (s[i] = '+') or(s[i] = '-') then
            begin
              if (i <> 1) and (i <> pos(s,'e')+1) and
                 (i <> pos(s,'E')+1) then
                f := false
            end
          else if not (s[i] in ['0'..'9']) then
             f := false
        end
    end;
  checknum := f
end;

begin
  writeln('I want a number here. ');
  readln(s);{user input, set it to something else for defined inputs}
  r := false;{initialization}
  if checknum(s) then
    begin
      for i = 1 to length(s) do {drop exponents}
         if not (s[i] in ['0'..'9','.','+','-'] then
           del(s,i,length(s));
      for i = length(s) downto 1 do
        if (s[i] in ['0'..'9'] then
          delete(s,i,1);
      while s[1] = '0' do {delete leading zeros}
        delete(s,1,1);
      r = true;{check the thing now}
      for i = 1 to (length(s) div 2) do
        if s[i] <> s[length(s)-i] then
          r := false;
    end;
  if r then
    writeln('Yes')
  else
    writeln('No')
end.
0
 
dbruntonCommented:
Convert it to text.  (That's the hard part).

Here you'll have to take each number one by one and convert.  Have fun.  Also check to see if it is a full stop.


boolean result is set to true
The with a for loop
  compare the first char to the last char
    Are they equal - then keep going?  If false set result to false
   Increment the pointer to the first char and decrement the pointer to the last char.  And go to the compare again.

0
 
scrapdogCommented:
You can try the following algorithm.  I don't have access to a pascal compiler at the moment, so I haven't tested it.  Try it anyway.




var
   i :integer;
   number, f :real;
   match, done  :boolean;

.
.
.

i := trunc(number);
f := 10 * frac(number);
match := true;
done := false;

while (match and not done) do begin
  if (i mod 10 <> trunc(f)) then
     match := false
  else begin
      i := i div 10;
      f := 10 * frac(f);
      if (i = 0) then done := true
  end;
end;



"number" should contain the number you want to test, and "match" will contain the result of the comparison.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
scrapdogCommented:
If you DO decide to convert it to a string as dbrunton suggests, make sure you compare starting from the inside of the string going outward (from the digits directly before and after the decimal point).
0
 
bizunaAuthor Commented:
Hi Scrapdog


Your solution is good but doesn't work
if the number has 1 in it for example 123.321
doesn't work. Any other number (for example:
349.943) does work. Why? Please try and fix.


Thanks
0
 
scrapdogCommented:
OK, I have access to a compiler now.  Give me a few moments and I will try it.
0
 
scrapdogCommented:
The problem has to do with precision.  I will need to know the maximum number of digits you will need to process (e.g. 1234.4321 would be 8 digits).  If it goes beyond 8, then things will get much more complicated and non-trivial.
0
 
My name is MudSystems EngineerCommented:
Since this is Home work and the program suppose to accept a real number (or integer whatever) treat the user input as a string that way it would be easy...

Const
  Match: Boolean = False;
var
  L,I,J: byte;
  Tmp: String;

Begin
  Write('Gimmy a number: ');
  ReadLn(Tmp);
  L:=Length(Tmp);
  If Not((L Mod 2) = 0) And (L <> 1)  Then
    Begin
      J:=L;
      For I:=1 to ((L-1) Div 2) Do
        If Tmp[I] = Tmp[J] Then
          Match:=True
        Else
          Match:=False;
      If Tmp[I+1] <> '.' Then
        Match:=False
    End;
  If Match Then
    WriteLn('Yes')
  Else
    WriteLn('No')
End.
0
 
dbruntonCommented:
<<Since this is Home work and the program suppose to accept a real number (or integer whatever) treat
the user input as a string that way it would be easy...>>

Very true.  I must be getting old and decrepit.

Would pay for user to verify that valid input has been entered.  Leave that as an exercise. :-)
0
 
My name is MudSystems EngineerCommented:
Hmmm... Again... there's no need in validating a String... "Alpha-Numeric"... nor a home work...


The User can input something like A.A and the program will say yes... but this is homework...
0
 
scrapdogCommented:
This puppy should work for up to 10 digits (and some numbers of 12 digits).


var
  i1,i2 :integer;
  number :real;
  match :boolean;
  s :string[13];


begin
     
     number := 13456.65431;

     match := true;
     str(number:13:6, s);
     i1 := 6;
     i2 := 8;
     while (match and (i1 > 0)) do begin
       if (s[i1] = ' ') then s[i1] := '0';
       if s[i1] <> s[i2] then match := false;
       dec(i1);  inc(i2);
     end;

     writeln(match);
     readln;

end.
0
 
scrapdogCommented:
Beyond 10 digits, you will have to use whatboy's method of directly placing the number (from the user) in a string.
0
 
My name is MudSystems EngineerCommented:
If Tmp[I] = Tmp[J] Then

Replace by

If Tmp[I] = Tmp[L+1-I] Then
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.