[CPPHTP7 NOTES] CH8. POINTERS(2)
2014-06-19 21:26
309 查看
(Exercise 8.12) In this exercise, I am asked to write a program to simulate the classic race of the tortoise and the hare. Random numbers are used to stimulate the probability of their actions in the race.
In my implementation, I use a int array to represent the race course, where each element contain an integer to indicate a position in the race course. Then I use twoint pointers to point at the positions where the
animals are at. To pass the locations to the functions, I use twoint ** pointers to store the address of the position pointers, so that I can change the animals' location by changing*(posPtr).
I use this complicated approach to demonstrate the use of pointers between functions. Actually, one can use ints to represent the position of the animals and pass them to function by reference. The program would be simpler.
Note that const is used very often to ensure the principle of least privilege. Chance of geting potential error due to pointer manipulation is minimized.
Here comes the code:
In my implementation, I use a int array to represent the race course, where each element contain an integer to indicate a position in the race course. Then I use twoint pointers to point at the positions where the
animals are at. To pass the locations to the functions, I use twoint ** pointers to store the address of the position pointers, so that I can change the animals' location by changing*(posPtr).
I use this complicated approach to demonstrate the use of pointers between functions. Actually, one can use ints to represent the position of the animals and pass them to function by reference. The program would be simpler.
Note that const is used very often to ensure the principle of least privilege. Chance of geting potential error due to pointer manipulation is minimized.
Here comes the code:
#include <iostream> #include <iomanip> #include <cstdlib> // for rand() and srand() #include <ctime> // for time() using namespace std; void race( int course[], int ** const harePosPtr, int ** const tortoisePosPtr, const int distance ); bool showStatus( const int t, int ** const harePosPtr, int ** const tortoisePosPtr, const int distance ); void moveTortoise( int course[], int ** const posPtr, const int distance ); void moveHare( int course[], int ** const posPtr, const int distance ); int main() { const int numOfPosition = 70; // the number of possible position along the race course int raceCourse[numOfPosition]; int * harePos = &raceCourse[0]; // hare is at the starting point int * tortoisePos = &raceCourse[0]; // tortoise is at the starting point srand(time(0)); // provide seed for the random generator // initialize raceCourse for( int i=0; i<numOfPosition; i++ ) { raceCourse[i] = i+1; //position 1...70 } race( raceCourse, &harePos, &tortoisePos, numOfPosition ); return 0; } void race( int course[], int ** const harePosPtr, int ** const tortoisePosPtr, const int distance ) { int t = 0; // tick of the clock bool hasEnded = false; cout << "BANG !!!!!" << endl; cout << "AND THEY'RE OFF !!!!!" << endl; cout << setfill('-') << setw(70) << '-' << endl; showStatus( t, harePosPtr, tortoisePosPtr, distance ); while( !hasEnded ) { moveTortoise( course, tortoisePosPtr, distance ); moveHare( course, harePosPtr, distance ); hasEnded = showStatus( ++t, harePosPtr, tortoisePosPtr, distance ); } } void moveTortoise( int course[], int ** const posPtr, const int distance ) { int prob = rand()%10 + 1; // 1...10 int tempPos = **posPtr; // value of the element pointed by *posPtr, position x if( prob <= 5 ) // 50% { tempPos += 3; if( tempPos <= distance ) *posPtr += 3; // to the right by 3 } else if( prob <= 7 ) // 20% { tempPos -= 6; if( tempPos > 0 ) *posPtr -= 6; // to the left by 6 else *posPtr = &course[0]; // back to the starting point } else // 30% { *posPtr += 1; // to the right by 1 } } void moveHare( int course[], int ** const posPtr, const int distance ) { int prob = rand()%10 + 1; // 1...10 int tempPos = **posPtr; // value of the element pointed by *posPtr, position x if( prob <= 2 ) // 20% sleep { // sleep } else if( prob <= 4 ) // 20% big hop { tempPos += 9; if( tempPos <= distance ) *posPtr += 9; // to the right by 9 } else if( prob == 5 ) // 10% big slip { tempPos -= 12; if( tempPos > 0 ) *posPtr -= 12; // to the left by 12 else *posPtr = &course[0]; } else if( prob <= 8 ) // 30% small hop { *posPtr += 1; // to the right by 1 } else // 20% small slip { tempPos -= 2; if( tempPos > 0 ) *posPtr -= 2; // to the left by 2 else *posPtr = &course[0]; } } bool showStatus( const int t, int ** const harePosPtr, int ** const tortoisePosPtr, const int distance ) { bool isSamePos = ( *harePosPtr == *tortoisePosPtr ); // point to same position bool hareFinish = ( **harePosPtr == distance ); bool tortoiseFinish = ( **tortoisePosPtr == distance ); for( int i=1; i<=distance; i++ ) // print location of hare { if( **harePosPtr == i ) { if( isSamePos && t!=0 && !hareFinish && !tortoiseFinish ) { cout << "OUCH!!!"; // the tortoise bite the hare i+=6; } else { cout << 'H'; } } else cout << ' '; } cout << endl; for( int i=1; i<=distance; i++ ) // print location of tortoise { if( **tortoisePosPtr == i ) { if( isSamePos && t!=0 && !hareFinish && !tortoiseFinish ) { cout << "OUCH!!!"; // the tortoise bite the hare i+=6; } else { cout << 'T'; } } else cout << ' '; } cout << endl; if( hareFinish || tortoiseFinish ) { cout << setfill('-') << setw(70) << '-' << endl; if( hareFinish && tortoiseFinish ) { cout << "It's tie." << endl; } if( hareFinish ) { cout << "Hare wins. Yuch." << endl; } else { cout << "TORTOISE WINS!!! YAY!!!" << endl; } return true; // true when complete } return false; // continue to race }
相关文章推荐
- [CPPHTP7 NOTES] CH8. POINTERS(6) Simpletron Computer Simulator
- [CPPHTP7 NOTES] CH8. POINTERS(1)
- [CPPHTP7 NOTES] CH8. POINTERS(3)
- [CPPHTP7 NOTES] CH8. POINTERS(4)
- [CPPHTP7 NOTES] CH8. POINTERS(5) - Maze Traversal
- lotus中操作excel--引入excel数据至notes中
- Undokumentierte @Formeln/LotusScript im Lotus Notes R5 Client/Server
- ORACLE_NOTES
- 11i Notes-Profile Options
- 从NOTES视图中导出EXCEL表及word文档
- Notes 视图,表单,IE页面与EXCEL相互转换- -
- Working classes -- Code complete reading notes(6)
- This is actually a good place to put your tech notes.
- The History of Notes and Domino
- Notes for Java(Part c)
- Linux编程相关 - Notes
- Notes 8 beta 开放下载
- Lotus Notes/Domino 的 C API 编程
- LOTUS NOTES .INI文件参数设置秘籍
- Jess - Notes