Sie sind auf Seite 1von 7

1.

CPP

11/5/2011

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

/* Program extracts from Chapter 1 of "Data Structures and Program Design in C++" by Robert L. Kruse and Alexander J. Ryba Copyright (C) 1999 by Prentice-Hall, Inc. All rights reserved. Extracts from this file may be used in the construction of other programs, but this code will not compile or execute as given here. */ // Section 1.2: #include "utility.h" #include "life.h" int main() // Program to play Conway's game of Life. /* Pre: The user supplies an initial configuration of living cells. Post: The program prints a sequence of pictures showing the changes in the configuration of living cells according to the rules for the game of Life. Uses: The class Life and its methods initialize(), print(), and update(). The functions instructions(), user_says_yes(). */ { Life configuration; instructions(); configuration.initialize(); configuration.print(); cout << "Continue viewing new generations? " << endl; while (user_says_yes()) { configuration.update(); configuration.print(); cout << "Continue viewing new generations? " << endl; } } #include <iostream> using namespace std; #include "utility.h" // Section 1.3: do { study(); } while (TV.in_hock()); if (!sleepy) play(); else nap(); class a { // a deck of cards
Page 1

1.CPP

11/5/2011

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

int X; thing Y1[52]; /* X is the location of the top card in the deck. Y1 lists the cards. */ public: a(); public void Shuffle(); // Shuffle randomly arranges the cards. thing d(); // deals the top card off the deck } ; int a[n][n], i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[i][j] = ((i + 1) / (j + 1)) * ((j + 1) / (i + 1)); void does_something(int &first, int &second) int { first = second - first; second = second - first; first = second + first; } int calculate(int apple, int orange) int { int peach,lemon; peach = 0; lemon = 0; if (apple < orange) peach = orange; else if (orange <= apple) peach = apple; else { peach = 17; lemon = 19;} return(peach); return } float figure (vector vector1) { int loop1,loop4; float loop2,loop3; loop1 = 0; loop2 = vector1[loop1]; loop3 = 0.0; loop4 = loop1; for (loop4 = 0; loop4<max;loop4++) { loop1 = loop1 + 1; loop2 = vector1[loop1 - 1]; loop3 = loop2 + loop3;} loop1 = loop1 - 1; loop2 = loop1 + 1; return(loop2 = loop3/loop2); } return int question(int &a17, int &stuff) int { int another, yetanother, stillonemore; another = yetanother; stillonemore = a17; yetanother = stuff; another = stillonemore; a17 = yetanother; stillonemore = yetanother; stuff = another; another = yetanother; yetanother = stuff;} int mystery(int apple, int orange, int peach) int
Page 2

1.CPP

11/5/2011

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

{ if (apple > orange) if (apple > peach) if (peach > orange) return return(peach); else if (apple < orange) return(apple); else return(orange); else return(apple); else return return return return(orange); else if (peach > apple) if (peach > orange) return return(peach); else return return(apple);} return if (x < if (y if (x if (z z) if (x < y) if (y < z) c = 1; < z) c = 3; else c = 4; else if < z) c = 5; else c = 6; else if < x) if (z < y) c = 8; else c = else c = 2; else (x < y) (y < z) c = 7; else 9; else c = 10;

float function fcn(float stuff) float { float april, tim, tiny, shadow, tom, tam, square; int flag; tim = stuff; tam = stuff; tiny = 0.00001; if (stuff != 0) do {shadow = tim + tim; square = tim * tim; tom = (shadow + stuff/square); april = tom / 3.0; if (april*april * april - tam > -tiny) if (april*april*april - tam < tiny) flag = 1; else flag = 0; else flag = 0; if (flag == 0) tim = april; else tim = tam;} while (flag != 1); return(stuff); else return return(april); } if (stuff == 0) return

// Section 1.4: void instructions() { } return(true true);} bool user_says_yes() { return true bool; typedef int bool const bool false = 0; const bool true = 1; class Life { public: public void initialize(); void print(); void update(); }; void Life::initialize() {} void Life::print() {} void Life::update() {} const int maxrow = 20, maxcol = 60; class Life { public: public void initialize(); void print();
Page 3

//

grid dimensions

1.CPP

11/5/2011

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

void update(); private: private int grid[maxrow + 2][maxcol + 2]; // and columns int neighbor_count(int row, int col); int };

allows for two extra rows

int Life::neighbor_count(int row, int col) int /* Pre: The Life object contains a configuration, and the coordinates row and col define a cell inside its hedge. Post: The number of living neighbors of the specified cell is returned. */ { int i, j; int count = 0; for (i = row - 1; i <= row + 1; i++) for (j = col - 1; j <= col + 1; j++) count += grid[i][j]; // Increase the count if neighbor is alive. count -= grid[row][col]; // Reduce count, since cell is not its own neighbor. return count; } void Life::update() /* Pre: The Life object contains a configuration. Post: The Life object contains the next generation of configuration. */ { int row, col; int new_grid[maxrow + 2][maxcol + 2]; for (row = 1; row <= maxrow; row++) for (col = 1; col <= maxcol; col++) switch (neighbor_count(row, col)) { case 2: new_grid[row][col] = grid[row][col]; the same. break; break case 3: new_grid[row][col] = 1; alive. break; break default: default new_grid[row][col] = 0; dead. } for (row = 1; row <= maxrow; row++) for (col = 1; col <= maxcol; col++)
Page 4

//

Status stays

//

Cell is now

//

Cell is now

1.CPP

11/5/2011

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

grid[row][col] = new_grid[row][col]; } void instructions() /* Pre: None. Post: Instructions for using the Life program have been printed. */ { cout << "Welcome to Conway's game of Life." << endl; cout << "This game uses a grid of size " << maxrow << " by " << maxcol << " in which" << endl; cout << "each cell can either be occupied by an organism or not." << endl; cout << "The occupied cells change from generation to generation" << endl; cout << "according to the number of neighboring cells which are alive." << endl; } void Life::initialize() /* Pre: None. Post: The Life object contains a configuration specified by the user. */ { int row, col; for (row = 0; row <= maxrow+1; row++) for (col = 0; col <= maxcol+1; col++) grid[row][col] = 0; cout << "List the coordinates for living cells." << endl; cout << "Terminate the list with the special pair -1 -1" << endl; cin >> row >> col; while (row != -1 || col != -1) { if (row >= 1 && row <= maxrow) if (col >= 1 && col <= maxcol) grid[row][col] = 1; else cout << "Column " << col << " is out of range." << endl; else cout << "Row " << row << " is out of range." << endl; cin >> row >> col; } } void Life::print() /* Pre: The Life object contains a configuration.
Page 5

1.CPP

11/5/2011

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

Post: The configuration is written for the user. */ { int row, col; cout << "\nThe current Life configuration is:" <<endl; for (row = 1; row <= maxrow; row++) { for (col = 1; col <= maxcol; col++) if (grid[row][col] == 1) cout << '*'; else cout << ' '; cout << endl; } cout << endl; } bool user_says_yes() { int c; true; bool initial_response = true do { // Loop until an appropriate input is received. if (initial_response) cout << " (y,n)? " << flush; else cout << "Respond with either y or n: " << flush; do { // Ignore white space. c = cin.get(); } while (c == '\n' || c ==' ' || c == '\t'); initial_response = false false; } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'); return (c == 'y' || c == 'Y'); } driver for neighbor_count() int main () // /* Pre: None. Post: Verifies that the method neighbor_count() returns the correct values. Uses: The class Life and its method initialize(). */ { Life configuration; configuration.initialize(); for (row = 1; row <= maxrow; row++){ for (col = 1; col <= maxrow; col++) cout << configuration.neighbor_count(row,col) << " "; cout << endl; } }
Page 6

1.CPP

11/5/2011

318 319 320 321 322 323 324 325 326 327 328 329

configuration.initialize(); configuration.print(); if (a < b) if (c > d) x = 1; else if (c == d) x = 2; else x = 3; else if (a == b) x = 4; else if (c == d) x = 5; else x = 6; /********************************************************************** ***/

Page 7

Das könnte Ihnen auch gefallen