2
1
Your code has to provide the following functions:
read(x)
Reads a complex number from the standard input. It has to accept and evaluate something in the form of for example
1.42 + 3i
.
print(x)
Prints the complex number in the form of, for example
1.23 - 4.32i
add(x,y)
sub(x,y)
mult(x,y)
div(x,y)
Adds, subtracts, multiplies and respectively divides the two complex numbers.
It is enough if your functions are accurate to two digits after the decimal point, of course, it can be more if you so wish. The input will never have more than two digits after the decimal point.
Any language without native support for complex numbers may be used. Similarly, libraries or other language features which deal with complex numbers are not permitted. The point is, you have to do all the handling of complex numbers by yourself.
Scoring
This is a code golf. The score is the sum of the lengths of the function bodies. For example, int add(int a, int b) {return a+b;}
will have a length of 11 because of return a+b
. Your solution must have a main program which demonstrates the use of the functions (for easier evaluation of your solution), but it will not be part of the scoring.
You can store the complex numbers in any way you wish, but the functions must have exactly the same amount of parameters as in the above description.
EDIT: For languages that support it, you can use operator overloading instead of the add
, sub
, etc. functions.
EDIT 2: To prohibit the cheap #define all_your_code_here A
, I'll try to clarify the "only function bodies count" better: every character in the code counts, except for the overhead required to declare and define the functions and the libraries required to do basic I/O. By basic I/O I still mean no language features which directly deal with complex numbers. The main
or equivalent where you call these functions don't count but you are not allowed to do anything besides using those functions for demonstrative purpose.
1any bonus for operator overload (in languages that support it)? – ratchet freak – 2012-10-14T11:29:14.627
You're obviously talking about languages that use the word "function" in the way C does, like Python, Lisp, JavaScript etc.. But
read
andprint
aren't actually functions (they have side-effects), so in a more rigorous language you can't define them in that precise way. Are those languages "out", or may we use the corresponding signatures (e.g.read :: IO ComplexNum
andprint :: ComplexNum -> IO()
in Haskell)? Or why shall we not rather define functions to parse / build strings, rather than perform actual IO? – ceased to turn counterclockwis – 2012-10-14T15:05:56.670If only the function bodies count, are any lines of e.g. standard library includes for free, or permitted at all? – ceased to turn counterclockwis – 2012-10-14T15:10:11.113
@leftaroundabout: includes are free, unless they deal with complex numbers, in which case they are not permitted. The point is, you have to do all the handling of complex numbers by yourself. – vsz – 2012-10-14T15:19:33.217
Allright. But I suppose any other code outside the function bodies is forbidden? In most languages it would be natural to first properly declare the type (class, or whatever) of the complex numbers, but it's easy to do this in such a way that the actual functions become trivial. — Are you going to answer my first comment, yet? – ceased to turn counterclockwis – 2012-10-14T16:03:21.207
-1 Boring and not very well specified. The mathematical definitions of the operations are really quite simple, so the most interesting part is the IO (not that it's really interesting), and in those parts the level of detail for what is acceptable is insufficient. – Peter Taylor – 2012-10-14T21:43:58.753