Solved

Reversing - Urgent!

Posted on 2001-06-16
13
229 Views
Last Modified: 2010-04-16
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!
0
Comment
Question by:bizuna
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 48

Expert Comment

by:dbrunton
ID: 6199404
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6199555
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6199559
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:bizuna
ID: 6205208
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6205219
OK, I have access to a compiler now.  Give me a few moments and I will try it.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6205275
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
 
LVL 6

Expert Comment

by:My name is Mud
ID: 6205474
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
 
LVL 48

Expert Comment

by:dbrunton
ID: 6205504
<<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
 
LVL 6

Expert Comment

by:My name is Mud
ID: 6205540
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6205632
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6205644
Beyond 10 digits, you will have to use whatboy's method of directly placing the number (from the user) in a string.
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 6209292
If Tmp[I] = Tmp[J] Then

Replace by

If Tmp[I] = Tmp[L+1-I] Then
0
 

Accepted Solution

by:
k61824 earned 70 total points
ID: 6238860
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WSAAdressToString example Delphi code needed 3 1,771
Extract text from webbrowser 5 1,392
SHBrowseForFolder - Preselecting a folder 5 271
Mydac connection data base issue 3 157
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question