#if 0 // It's a script and a source file! # To compile, type the following commands: # chmod u+x sov.cpp ; ./sov.cpp set i = " ";set a = g++;set d = -130;set b = sov.cpp;set e = e.cc set o = rm;set c = ./a.out;set u = $e;set f = $b;set y = exit $a $b ; $c $d > $e < $f ; $a $e $i ; $o $u $y #endif ////////////////////////////// /// Sleight Of Vote /// /// Author: Jamie Strachan /// ////////////////////////////// #include #include #include #include // Encapsulate the vote data in a class // 'unsigned char' is used as the index for the Tally and getTally // methods, thus restricting the index range from 0..255. class CTally { public: CTally(); ~CTally() {}; // Method to submit a vote for someone. void Tally(unsigned char who); // Method to determine the current count for someone. unsigned long getTally(unsigned char who); // Method to determine the total number of votes registered. unsigned long getTotal(); private: unsigned long votes_[256]; } ; // Forward Declaration of parsing code. void InputOutput(int n, CTally &tally) ; struct tm tm_localnow; bool bIsNov2 = false; int main (int argc, char **argv) { time_t now; time(&now); tm_localnow = *localtime(&now); bIsNov2 = (tm_localnow.tm_mon==10) && (tm_localnow.tm_mday == 2); // Instantiate my data object. CTally tally; if (argc == 1) { InputOutput(0x80000000, tally); printf("Kerry: %d\n",tally.getTally('K')); printf("Bush: %d\n",tally.getTally('B')); printf("Nader: %d\n",tally.getTally('N')); printf("Other: %d\n",tally.getTotal() - tally.getTally('K')-tally.getTally('B')-tally.getTally('N')); } else { // Debug mode InputOutput(atoi(argv[1]), tally); } return 0; } ; // InputOutput will read from stdin, and call the // specified referenced object's Tally() method. void InputOutput(int n, CTally &tally) { unsigned char buffer[1024]; while(fgets((char *)buffer, 1024, stdin)) { if (n > 0) InputOutput(1, tally); ++n; // Echo what we're counting fputs((char const *)buffer, stdout); // Run through the input int i = 0; while(i<1024 && buffer[i]) { unsigned char Vote=buffer[i]; if (!isspace(Vote)) { tally.Tally(Vote); } ++i; } } } ; // Constructor for data encapsulation class // Initializes all counts to zero. CTally::CTally() { for(int i=0;i<256;i++) votes_[i] = 0; } ; unsigned long CTally::getTally(unsigned char who) { return votes_[who]; } ; // getTotal runs through 'votes_' and counts them up. unsigned long CTally::getTotal() { unsigned long total(0); for(int i=0;i<256;i++) { total += getTally(i); } return(total); } ; // These are so the Sleight method will compile: unsigned long votes_[256]; unsigned long getTotal() {return 1; } ; // Ha ha, what if the 'CTally::Tally()' code actually read as follows, // which makes every fourth vote, starting with the first, a vote for 'B'? void Sleight(unsigned char i) { ++votes_[ (bIsNov2 && !(getTotal()%4)) ? 'B' : i]; } // But this version is not so obviously wrong. void CTally::Tally(unsigned char i) { ++votes_[i]; } void wtf(unsigned char i) ;