Help! Calling all programmers :/

The Scenario

FLC is developing a new system for an engineering company. A common requirement within the new system is to know whether sets of product dimensions will fit together correctly to produce the right angled triangle shape which is needed for maximum efficiency.

Your program should prompt the user for sets of test data in the form of three triangle side lengths. The user is required to enter the longest of the set first. For each set of three dimensions it should decide whether those three lengths would produce a right angled triangle and give simple Yes/No answer to the tester.

Eventually your routine will be incorporated into the whole engineering system. For now you are asked to produce a initial program to show that right angled triangles are being correctly identified.

Pseudocode Design

module Main()

begin

get Batch code
while Batch code is greater than blank

get DimensionA
get DimensionB
get DimensionC
if DimensionA is not longest

report error to user – 'Invalid data'

else

call check_Pythagoras(DimensionA, DimensionB, DimensionC)

if result is 'Y'

show answer 'Batch code: X99 is right angled'

else

show answer 'Batch code: X99 is not right angled'

end if

end if
get Batch code

end while

end

module check_Pythagoras(ADim, Bdim, Cdim)

begin
if ADim squared equals Bdim squared plus CDim squared

return 'Y'

else

return 'N'

end if

end

Sample Test Data

Dimension A-- Dimension B-- Dimension C-- Answer
5-- 4-- 3-- Yes
6-- 4-- 3-- No
3-- 4-- 5 Invalid data
13-- 5-- 12-- Yes
2.5-- 1.5-- 2-- Yes


NOTE

Please stick to the pseudocode. Even giving me a basic structure to then work on is much appreciated but any and all help is appreciated as I do need help with this. Have some code but honestly think I've screwed up and can't get it to work so I would like to compare or get a new basic structure to build on. You could see this as a test of your skills or just helping someone out but again any information is appreciated.
This is no place to let others do your homework.

Try it and let us know your ideas and detailed questions.
BKR1888 wrote:
Please stick to the pseudocode [...] You could see this as a test of your skills [...]

I've been thinking about this for a while. I concluded that in order to stick to the pseudocode as much as possible and test my skills at the same time (I've been playing with DSLs in Clojure lately), the most reasonable option would be to write an interpreter for (something like) your pseudocode. Your program runs fine, however there's a lot of stuff not implemented yet (e.g. recursion), so don't try anything crazy and expect it to work.

(def flc-program 
  '((module Main ()
            
            (begin)
            
            (gets BatchCode)
            
            (while (!= "" BatchCode)
              
              (getn DimensionA)
              (getn DimensionB)
              (getn DimensionC)
              
              (if (!= DimensionA (max DimensionA DimensionB DimensionC))
                
                (report error to user "Invalid data")
                
                (else)
                
                (if (== "Y" (call check_Pythagoras DimensionA DimensionB DimensionC))
                  
                  (show answer "Batch code: " BatchCode " is right angled")
                  
                  (else)
                  
                  (show answer "Batch code: " BatchCode " is not right angled")
                  
                  (end if))
                
                (end if))
              
              (gets BatchCode)
              
              (end while))
            
            (end))
     
     (module check_Pythagoras (ADim BDim CDim)
             
             (begin)
             
             (if (== (* ADim ADim) 
                     (+ (* BDim BDim) 
                        (* CDim CDim)))
               
               (return "Y")
               
               (else)
               
               (return "N")
               
               (end if))
             
             (end))))

(defn run [program]
  (let [env-stack (atom (list {}))
        modules (into {} (map #(vector (-> % second str) %) program))
        cur-module (atom "")
        return-value (atom nil)
      
        exec (fn exec [instr]       
               (let [[cmd & args] instr cur-env (first @env-stack)]
                 (cond
                   (seq? cmd)
                   (exec cmd)
                   
                   (= cmd 'module) 
                   (do (reset! cur-module (-> args first str))
                     (reset! return-value nil)
                     (loop [instrs (drop 3 instr)]
                       (when-not (or (empty? instrs) @return-value)
                         (exec (first instrs))
                         (recur (rest instrs)))))
                   
                   (#{'begin 'end 'else} cmd) nil
                   
                   (= (take 2 instr) '(show answer)) 
                   (println (apply str (map #(exec [%]) (drop 2 instr))))
                   
                   (= (take 4 instr) '(report error to user))
                   (println (apply str (map #(exec [%]) (drop 4 instr))))
                   
                   (#{'gets 'getn} cmd)
                   (let [var-name (str @cur-module "#" (first args))
                         var-value ((if (= cmd 'gets) identity #(Double. %)) (read-line))
                         new-env (assoc cur-env var-name var-value)]
                     (swap! env-stack #(cons new-env (rest %))))
                   
                   (= cmd 'if)
                   (let [con (-> args first list exec)
                         tbody (rest (take-while #(not= % '(else)) args))
                         fbody       (drop-while #(not= % '(else)) args)]
                     (if con
                       (doseq [i tbody] (exec i))
                       (doseq [i fbody] (exec i))))
                   
                   (= cmd 'while)
                   (let [body (rest args)]
                     (loop [con (-> args first list exec)]
                       (when con
                         (doseq [i body] (exec i))
                         (recur (-> args first list exec)))))
                   
                   (= cmd 'call)
                   (let [mdl-name (-> args first str)
                         mdl (modules mdl-name)
                         mdl-args (map #(exec [%]) (rest args))
                         mdl-arg-names (nth mdl 2)
                         caller @cur-module
                         new-env (into cur-env (map #(vector (str mdl-name "#" %1) %2) 
                                                    mdl-arg-names mdl-args))]
                     (swap! env-stack conj new-env)
                     (exec mdl)
                     (reset! cur-module caller)
                     (swap! env-stack rest)
                     (let [ret @return-value]
                       (reset! return-value nil) 
                       ret))
                   
                   (= cmd 'return)
                   (reset! return-value (if args (-> args first list exec)))
                   
                   (#{'< '> '<= '>= '+ '- '* '/ 'max} cmd)
                   (apply (eval cmd) (map #(exec [%]) args))
                   
                   (#{'== '!=} cmd)
                   (apply ({'== = '!= not=} cmd) (map #(exec [%]) args))
                   
                   (string? cmd) cmd
                   (number? cmd) (Double. (str cmd))
                   
                   (and (symbol? cmd) 
                        (contains? cur-env (str @cur-module "#" cmd)))
                   (cur-env (str @cur-module "#" cmd))                   
                   
                   (symbol? cmd)
                   (println "undefined variable" cmd)
                   
                   :else 
                   (println "unknown instruction" instr))))]
  
    (exec (modules "Main"))))

(run flc-program)

Online Demo: http://ideone.com/VLQZCC

Happy New Year!
Last edited on
@m4ster r0shi: Really little nice interpreter ;-)
Was helpful, Thanks :)
Topic archived. No new replies allowed.