is this style good ?

B

blackswift

code is from Warsaw university's CEPC code .
They are world champion in the ICPC finals.

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,x) __typeof(x) v=x
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)

template<class T>
inline int size(const T&a) { return a.size(); }

////////

const int NDIGS=40;
const int BASE = 100000000;
const int BASEDIGS = 8;

class Num {
int a[NDIGS];
void popraw() {
int p=0;
FORD(i,NDIGS-1,0) {
a+=p;
p=a/BASE;
a%=BASE;
}
}
public:
void set0() { REP(i,NDIGS) a=0; }
void set1() { REP(i,NDIGS) a=0; a[0]=1; }
void dodaj(const Num&x,const Num&y) {
REP(i,NDIGS) a=x.a+y.a;
popraw();
}
void div2() {
int p=0;
REP(i,NDIGS) {
a+=p;
p=(a&1)==0?0:BASE;
a>>=1;
}
}
/* void dodajIloczyn(const Num&x,const Num&y) {
REP(i,NDIGS) REP(j,NDIGS-i)
a[i+j] += x.a*y.a[j];
popraw();
} */
void wypisz() {
int res[BASEDIGS*(NDIGS-1)+1];
int p=BASEDIGS*(NDIGS-1)+1;
FORD(i,NDIGS-1,1) {
int x=a;
REP(j,BASEDIGS) { res[--p]=x%10; x/=10; }
}
res[0]=a[0];
int aa=0,bb=(NDIGS-1)*BASEDIGS;
while(aa<2 && res[aa]==0) ++aa;
while(bb>2 && res[bb]==0) --bb;
FOR(i,aa,bb) {
putchar('0'+res);
if(i==2 && i<bb) putchar('.');
}
printf("%%\n");
}
};


const int MAXN = 301;

int n;
int par[MAXN][2];
bool done[MAXN][MAXN];
Num pr[MAXN][MAXN];
int dnr[MAXN];
int ddd;

void dfs(int x) {
if(dnr[x]!=-1) return;
if(par[x][0]!=-1) REP(i,2) dfs(par[x]);
dnr[x]=ddd++;
}

void read() {
scanf("%d",&n);
REP(i,n) par[0]=par[1]=-1;
int k; scanf("%d",&k);
REP(i,k) {
int a,b,c; scanf("%d%d%d",&a,&b,&c); --a; --b; --c;
par[a][0]=b;
par[a][1]=c;
}
}

void calc(int a,int b) {
if(done[a]) return;
if(a==b) { pr[a].set1(); done[a]=true; return; }
if(dnr[a]<dnr) swap(a,b);
if(par[a][0]==-1) swap(a,b);
if(par[a][0]==-1) {
pr[a].set0();
}
else {
calc(par[a][0],b);
calc(par[a][1],b);
pr[a].dodaj(pr[par[a][0]],pr[par[a][1]]);
pr[a].div2();
}
pr[a]=pr[a];
done[a]=done[a]=true;
}

int main() {
read();
REP(i,n) dnr=-1;
ddd=0;
REP(i,n) dfs(i);
REP(a,n) REP(b,n) done[a]=false;
int m; scanf("%d",&m);
REP(i,m) {
int a,b;
scanf("%d%d",&a,&b);
--a; --b;
calc(a,b);
pr[a].wypisz();
}
}
 
A

Alf P. Steinbach

* blackswift:
code is from Warsaw university's CEPC code .
They are world champion in the ICPC finals.

This style is very ungood for code that is to be maintained.

For maintenable code macros are evil, language-extensions are evil,
cryptic names are evil, global variables are evil, and so on.

But judging the style in a context different than the original is
probably not meaningful.

If this is write-only code where the goal is to write the code fastest
possible, any measure that reduces the number of letters typed can be
helpful.

Of course the programmer then needs to be able to keep it all in the
head, but presumably the programmer was able to do just that: it is,
after all, a small program.

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,x) __typeof(x) v=x
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)

template<class T>
inline int size(const T&a) { return a.size(); }

////////

const int NDIGS=40;
const int BASE = 100000000;
const int BASEDIGS = 8;

class Num {
int a[NDIGS];
void popraw() {
int p=0;
FORD(i,NDIGS-1,0) {
a+=p;
p=a/BASE;
a%=BASE;
}
}
public:
void set0() { REP(i,NDIGS) a=0; }
void set1() { REP(i,NDIGS) a=0; a[0]=1; }
void dodaj(const Num&x,const Num&y) {
REP(i,NDIGS) a=x.a+y.a;
popraw();
}
void div2() {
int p=0;
REP(i,NDIGS) {
a+=p;
p=(a&1)==0?0:BASE;
a>>=1;
}
}
/* void dodajIloczyn(const Num&x,const Num&y) {
REP(i,NDIGS) REP(j,NDIGS-i)
a[i+j] += x.a*y.a[j];
popraw();
} */
void wypisz() {
int res[BASEDIGS*(NDIGS-1)+1];
int p=BASEDIGS*(NDIGS-1)+1;
FORD(i,NDIGS-1,1) {
int x=a;
REP(j,BASEDIGS) { res[--p]=x%10; x/=10; }
}
res[0]=a[0];
int aa=0,bb=(NDIGS-1)*BASEDIGS;
while(aa<2 && res[aa]==0) ++aa;
while(bb>2 && res[bb]==0) --bb;
FOR(i,aa,bb) {
putchar('0'+res);
if(i==2 && i<bb) putchar('.');
}
printf("%%\n");
}
};


const int MAXN = 301;

int n;
int par[MAXN][2];
bool done[MAXN][MAXN];
Num pr[MAXN][MAXN];
int dnr[MAXN];
int ddd;

void dfs(int x) {
if(dnr[x]!=-1) return;
if(par[x][0]!=-1) REP(i,2) dfs(par[x]);
dnr[x]=ddd++;
}

void read() {
scanf("%d",&n);
REP(i,n) par[0]=par[1]=-1;
int k; scanf("%d",&k);
REP(i,k) {
int a,b,c; scanf("%d%d%d",&a,&b,&c); --a; --b; --c;
par[a][0]=b;
par[a][1]=c;
}
}

void calc(int a,int b) {
if(done[a]) return;
if(a==b) { pr[a].set1(); done[a]=true; return; }
if(dnr[a]<dnr) swap(a,b);
if(par[a][0]==-1) swap(a,b);
if(par[a][0]==-1) {
pr[a].set0();
}
else {
calc(par[a][0],b);
calc(par[a][1],b);
pr[a].dodaj(pr[par[a][0]],pr[par[a][1]]);
pr[a].div2();
}
pr[a]=pr[a];
done[a]=done[a]=true;
}

int main() {
read();
REP(i,n) dnr=-1;
ddd=0;
REP(i,n) dfs(i);
REP(a,n) REP(b,n) done[a]=false;
int m; scanf("%d",&m);
REP(i,m) {
int a,b;
scanf("%d%d",&a,&b);
--a; --b;
calc(a,b);
pr[a].wypisz();
}
}
 
B

BobR

Alf P. Steinbach wrote in message
* blackswift:

Was Hitler still in power when that code was written?

<huh?>
CEPC - Childish Error Producing Code.
ICPC - ( I ? Irritating : Irrational ) Coding Practices Contest.
For maintenable code macros are evil, language-extensions are evil,
cryptic names are evil, global variables are evil, and so on.

Hi Alf,

Flat-out makes my eyes bleed!

If I get time, I may compile it with the g++ -save_temps flag, just to see
what it really looks like. :-}
[ I am anti-macro ever since I got a 'macro Assembler'(1980).]

Where the heck does the 'i' variable, used throughout the code, come from?
(? declaration/definition ?)
 
M

Mike Wahler

BobR said:
Alf P. Steinbach wrote in message


Was Hitler still in power when that code was written?

<huh?>
CEPC - Childish Error Producing Code.
ICPC - ( I ? Irritating : Irrational ) Coding Practices Contest.


Hi Alf,

Flat-out makes my eyes bleed!

Makes mine itch. :)
If I get time, I may compile it with the g++ -save_temps flag, just to see
what it really looks like. :-}
[ I am anti-macro ever since I got a 'macro Assembler'(1980).]

Where the heck does the 'i' variable, used throughout the code, come from?
(? declaration/definition ?)

It's created in the expansions of macros FOR, FORD, and REP

-Mike
 
D

davidrubin

BobR said:
Alf P. Steinbach wrote in message


Was Hitler still in power when that code was written?

I think you just ended this thread pre-maturely... /david
 
B

BobR

Mike Wahler wrote in message
"BobR" wrote in message
[ I am anti-macro ever since I got a 'macro Assembler'(1980).]

Where the heck does the 'i' variable, used throughout the code, come from?
(? declaration/definition ?)

It's created in the expansions of macros FOR, FORD, and REP
-Mike

Hi Mike,

Ah, I see now, it's only used in the bodies after the macro(s). Thanks Mike.

Also noticed that 'FOR' is only used once. That seems like bad programming
practice to me (aside from using macros (like those) to begin with <G>). No
gain, adds clutter!
 
B

BobR

(e-mail address removed) wrote in message
I think you just ended this thread pre-maturely... /david

It was NOT a statement, it was a question. And the connection was 'ugly
code', 'ugly war'.
[ The code IMHO looked like something the 'SS' would force upon the world. ].

Sorry you took offense.
 
B

BobR

(e-mail address removed) wrote in message
No, I didn't take offence. I was referring to Godwin's Law:
http://en.wikipedia.org/wiki/Godwin's_law

:) /david

Interesting.

"Morgan's corollary to Godwin's Law
As soon as such a comparison occurs, someone will start a Nazi-discussion
thread on alt.censorship."

I don't subscribe to alt.censorship, so, would you be so kind as to keep me
informed? <G>

If I had used Nixon in reference to the time period, would I have still
invoked the wrath of Godwin? Does asking that question invoke Godwin's law
again? What questions are we allowed to ask?


Thanks for the link, glad your not mad.

OOOoohh NNOOooooo!
"Guy's corollary
If a Usenet discussion mentions Godwin's law as a conterrebuttal to a mention
of Hitler/Nazis, then the probability of Godwin's law being disputed is equal
to 1.
"
So, if we code:
while( Is_Godwins_law ){
}
....we're instantly in an endless loop?


BTW - "offence" is spelled "offense", I had looked it up. :-}
 
D

davidrubin

BobR wrote:

[snip]
BTW - "offence" is spelled "offense", I had looked it up. :-}

Well, the difference between 'c' and 's' is one bit, so it's about the
same. /david
 
M

Mike Wahler

BobR said:
BTW - "offence" is spelled "offense", I had looked it up. :-}

Both are 'correct', which spelling is used usually depends upon
which side of the pond you're on.

-Mike
 
H

Howard

BobR said:
Mike Wahler wrote in message
"BobR" wrote in message
[ I am anti-macro ever since I got a 'macro Assembler'(1980).]

Where the heck does the 'i' variable, used throughout the code, come
from?
(? declaration/definition ?)

It's created in the expansions of macros FOR, FORD, and REP
-Mike

Hi Mike,

Ah, I see now, it's only used in the bodies after the macro(s). Thanks
Mike.

Also noticed that 'FOR' is only used once. That seems like bad programming
practice to me (aside from using macros (like those) to begin with <G>).
No
gain, adds clutter!

--

Not to mention the problem of attempting to nest those loops. What is "i"
referring to then???

for (int i = ...
for (int i = ...
a = ...

If it's too much trouble to type in your own loop control statements, then
hire someone else to do it for you! :)

-Howard
 
B

BobR

Mike Wahler wrote in message ...
Both are 'correct', which spelling is used usually depends upon
which side of the pond you're on.

-Mike

Oh great! So now we have to write:

"offen( cp==437 ? s : c )e" // <G> [1]

Thanks for the heads-up, Mike.

My apology to david. Next time don't hide yer Englich accent, eh. :-}

[1] - and that only covers the old dos/window$ codepage (?). Is that covered
in the i18n specs?
 
E

Elcaro Nosille

#include said:
#include <cstdio>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,x) __typeof(x) v=x
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)

I really appreciate this style!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,789
Messages
2,569,634
Members
45,342
Latest member
Sicuro

Latest Threads

Top