|
-
February 3rd, 2011, 07:59 PM
#1
How slick are you?
I have this code I wrote and I was told there was two ways to make it more compact. I can't see it can you?
#include <iostream>
#include <string>
using namespace std;
void daysOfChristmas (int days, char switcher, string b[12]){
(days > 0 && switcher == 'y') ? daysOfChristmas(days - 1, 'y', b): false;
if (days > 0) {
daysOfChristmas(days - 1, 'n', b);
cout << "on the " << days << " day of Christmas my true love gave me " << b[(days-1)] << endl;
}}
int main() {
string gifts [12] = {"a partridge in a Pear Tree", "two turtle doves", "three french hens", "four colly birds", "five golden rings", "six gees-a-laying", "seven swans-a-swimming", "eight maids a miliking", "nine ladies dancing", "ten lords a leaping", "eleven pipiers piping", "drummers drumming"};
daysOfChristmas(12, 'y', gifts);
}
-
February 3rd, 2011, 11:19 PM
#2
Re: How slick are you?
Define "compact". Less whitespace usually isn't an improvement in code quality. Using printf() would require fewer characters than using cout, but wouldn't substantially change the logic.
-
February 4th, 2011, 12:23 AM
#3
Re: How slick are you?
 Originally Posted by wondernate
I have this code I wrote and I was told there was two ways to make it more compact. I can't see it can you?
I think you must have misread or misheard "correct" as "compact". For example, this is the error the Comeau online compiler reports when I attempt to compile your code:
Code:
"ComeauTest.c", line 5: error: operand types are incompatible ("void" and "bool")
(days > 0 && switcher == 'y') ? daysOfChristmas(days - 1, 'y', b): false;
^
1 error detected in the compilation of "ComeauTest.c".
-
February 4th, 2011, 12:49 AM
#4
Re: How slick are you?
Hi Nate.
I certainly haven't made it more compact (indeed I slightly expanded it) but I think I may have made a modest improvement.
I downloaded your code and executed it on my machine. I found that it seems to print only one string from b per line. It wasn't particularly attractive. In order to see all twelve days of Christmas, I had to look through Goodness knows how many lines. Many.
I expanded the code by a line or two which helped the printout immensely. It still isn't a faithful reproduction of the original song (for instance, the occasional 'and' (" AND a partridge in a pear tree") is missing).
But if you download and execute the following, you may see an improvement in the text the app renders. That is, on the first day, it prints only the first string. On succeeding days it prints succeeding elements of b. I think thats how the song goes as I recall (but I wouldn't swear to it)
I'm very cautious in saying "you may see" & "wasn't particularly attractive" because I'm well aware that I might have screwed up the download or otherwise screwed up the code during editing and compiling, in which case the fault is entirely mine. But it's well past this old man's time to retire for the day so I won't look for problems I might have introduced into your code t'nite,
Code:
void daysOfChristmas (int days, string b[12]) {
string composite;
// (days > 0 && switcher == 'y') ? daysOfChristmas(days - 1, 'y', b): false;
if (days > 0) {
daysOfChristmas(days - 1, b);
for (int i = days - 1; i >= 0; i--)
composite += (b[i] + ((days > 1) ? ", " : ""));
cout << "on the " << days << " day of Christmas my true love gave to me " << composite << endl << endl ;
}
}
int main() {
string gifts [12] = {"a partridge in a Pear Tree", "two turtle doves", "three french hens",
"four colly birds", "five golden rings", "six gees-a-laying",
"seven swans-a-swimming", "eight maids a milking", "nine ladies dancing",
"ten lords a leaping", "eleven pipiers piping", "twelve drummers drumming"};
daysOfChristmas(12, gifts);
}
-
February 4th, 2011, 12:57 AM
#5
Re: How slick are you?
Frankly, I would not bother with recursion:
Code:
void daysOfChristmas(int days, string gifts[]) {
for (int day = 1; day <= days; ++day) {
for (int i = 0; i < day; ++i) {
cout << "on the " << day
<< " day of Christmas my true love gave me " << gifts[i]
<< endl;
}
}
}
Then you just call it as:
Code:
daysOfChristmas(12, gifts);
-
February 4th, 2011, 01:32 AM
#6
Re: How slick are you?
 Originally Posted by wondernate
Code:
(days > 0 && switcher == 'y') ? daysOfChristmas(days - 1, 'y', b): false;
Just what exactly do you think you are doing there? Whatever happened to using "if". Tell me, what do you expect the return type of that ternary operator to be?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
February 4th, 2011, 09:32 AM
#7
Re: How slick are you?
 Originally Posted by wondernate
I have this code I wrote and I was told there was two ways to make it more compact. I can't see it can you?
my guess would be something like this, but not tested:
Code:
#include <iostream>
#include <string>
using namespace std;
void daysOfChristmas (int days, char switcher, string b[12])
{
if (!days)
return;
cout << "on the " << 12-days+1 << " day of Christmas my true love gave me " << b[(12-days)] << endl;
daysOfChristmas(days - 1, 'y', b);
}
int main() {
string gifts [12] = {"a partridge in a Pear Tree", "two turtle doves", "three french hens", "four colly birds", "five golden rings", "six gees-a-laying", "seven swans-a-swimming", "eight maids a miliking", "nine ladies dancing", "ten lords a leaping", "eleven pipiers piping", "drummers drumming"};
daysOfChristmas(12, 'y', gifts);
}
...
-
February 7th, 2011, 12:57 AM
#8
Re: How slick are you?
Or you can do this:
Code:
#include <stdio.h>
main(int t,char _,char *a)
{
return!0<t ? t<3 ? main(-79,-13,a+main(-87,1-_,main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a )&&t== 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\ n'wk nw'iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/") :t<-50?_==*a?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}
It's more compact that way 
http://www.funenclave.com/reality-bi...code-7690.html
Regards,
Zachm
-
February 7th, 2011, 06:17 AM
#9
Re: How slick are you?
But that one prints "sevean swans", it should be "seven swans". Anyone know how to fix it?
-
February 7th, 2011, 08:54 AM
#10
Re: How slick are you?
I'll answer my own question, since I think I've worked it out:
Code:
#include <stdio.h>
main(int t,char _,char *a)
{
return!0<t ? t<3 ? main(-79,-13,a+main(-87,1-_,main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a )&&t== 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw'\ iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/") :t<-50?_==*a?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}
-
February 7th, 2011, 09:01 AM
#11
Re: How slick are you?
 Originally Posted by Zachm
Or you can do this:
Not in standard C++, though it is easy to workaround at the cost of err... compactness.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|