Tuesday, February 21, 2006

C++ Programs

#include
#include
#include
using namespace std;
int main()
{
cout << "\n\nWould you like to go to the zoo with me today?\n\n";
inline char ask_yes_no();
char yes_or_no = ask_yes_no();
{
if (yes_or_no == 'y')
{
cout << "\nFantastic! I love the zoo," << " ";
cout << "I am so excited to go with you.\n\n";
}
if (yes_or_no == 'n')
{
cout << "\nThat is dissapointing, Im so sad.\n\n";
}
}
cout << "How about a game of chess?\n\n";
cout << "Just kidding, I do not know how to play chess yet.\n\n";
cout << "How about a nice game of tic-tac-toe?\n\n";
yes_or_no = ask_yes_no();
{
if (yes_or_no == 'y')
{
char tic_tac();
char victor = tic_tac();
{
if (victor == 'X')
{
cout << "I guess X won, unless you are lying\n\n";
}
else
{
cout << "O must have won, unless you lied to me\n\n.";
}
}
}
}
cout << "\nWould you like to calculate test grades?\n\n";
yes_or_no = ask_yes_no();
{
if (yes_or_no == 'y')
{
float test_grader();
float average = test_grader();
{
if (average >= 90)
{
cout << "The class average is an A\n\n";
}
if (average < 90 && average >= 80)
{
cout << "The class average is a B\n\n";
}
if (average < 80 && average >= 70)
{
cout << "The class average is a C\n\n";
}
if (average < 70 && average >= 60)
{
cout << "The class average is a D\n\n";
}
if (average <= 59)
{
cout << "The class average is an F\n\n";
}
}
}
}
cout << "\nThis program can also spell words backwards,";
cout << "would you like that?\n\n";
yes_or_no = ask_yes_no();
{
if (yes_or_no == 'y')
{
string reverser();
string any_thing = reverser();
{
cout << any_thing << " was the word initially Right?";
cout << "\n\n";
}
}
}
cout << "You have reached the end of the program, I hope you had fun.\n";
system("pause");
return 0;
}
char ask_yes_no()
{
char yes_or_no;
do
{
cout << "(y/n)" << " ";
cin >> yes_or_no;
}
while (yes_or_no != 'y' && yes_or_no != 'n');
return yes_or_no;
}
char tic_tac()
{
int rw = 0;
int clm = 0;
const int ROWS = 4;
const int COLUMNS = 4;
char quit;
char victor;
string whose_move;
string O_s_move;
string X_s_move;
do
{
char board[ROWS][COLUMNS] = {{' ', '1', '2', '3'},
{'1', 'E', 'E', 'E'},
{'2', 'E', 'E', 'E'},
{'3', 'E', 'E', 'E'}};
cout << "\n\n";
for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLUMNS; ++j)
cout << "\t" << board[i][j];
cout << endl;
}
for (int c = 0; c < 5; c++)
{
if (whose_move != "O_s_move")
{
cout << "\nX's move.\n";
cout << "\nenter ROW (1-3), and COLUMN (1-3)\n";
cin >> rw >> clm;
if (board[rw][clm] == 'X' || board[rw][clm] == 'O')
{
cout << "\n\n'X' made an illeagle move!\n\n";
continue;
}
board[rw][clm] = 'X';
cout << "\nNow the tic-tac-toe board is:\n\n";
for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLUMNS; ++j)
cout << "\t" << board [i][j];
cout << endl;
}
}
if (board[1][1] == 'X' && board[1][2] == 'X' && board[1][3] == 'X'
|| board[2][1] == 'X' && board[2][2] == 'X' && board[2][3] == 'X' ||
board[3][1] == 'X' && board[3][2] == 'X' && board[3][3] == 'X' ||
board[1][1] == 'X' && board[2][1] == 'X' && board[3][1] == 'X' ||
board[1][2] == 'X' && board[2][2] == 'X' && board[3][2] == 'X' ||
board[1][3] == 'X' && board[2][3] == 'X' && board[3][3] == 'X' ||
board[1][1] == 'X' && board[2][2] == 'X' && board[3][3] == 'X' ||
board[1][3] == 'X' && board[2][2] == 'X' && board[3][1] == 'X')
{
cout << "\n\n'X' won the game!\n\n";
break;
}
if (c == 4)
{
cout << "\n\nThe game was a tie.\n\n";
break;
}
cout << "\nO's move.\n";
cout << "\nenter ROW (1-3), and COLUMN (1-3)\n";
cin >> rw >> clm;
if (board[rw][clm] == 'X' || board[rw][clm] == 'O')
{
cout << "\n\n'O' made an illeagle move!\n\n";
whose_move = "O_s_move";
continue;
}
board[rw][clm] = 'O';
whose_move = "X_s_move";
cout << "\nNow the tic-tac-toe board is:\n\n";
for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLUMNS; ++j)
cout << "\t" << board [i][j];
cout << endl;
}
if (board[1][1] == 'O' && board[1][2] == 'O' && board[1][3] == 'O'
|| board[2][1] == 'O' && board[2][2] == 'O' && board[2][3] == 'O' ||
board[3][1] == 'O' && board[3][2] == 'O' && board[3][3] == 'O' ||
board[1][1] == 'O' && board[2][1] == 'O' && board[3][1] == 'O' ||
board[1][2] == 'O' && board[2][2] == 'O' && board[3][2] == 'O' ||
board[1][3] == 'O' && board[2][3] == 'O' && board[3][3] == 'O' ||
board[1][1] == 'O' && board[2][2] == 'O' && board[3][3] == 'O' ||
board[1][3] == 'O' && board[2][2] == 'O' && board[3][1] == 'O')
{
cout << "\n\n'O' won the game!\n\n";
break;
}
}
cout << "\n\nDo you wish to quit? (y/n)\n\n";
cin >> quit;
do
{
cout << "\n\nWho won, (X/O)?";
cin >> victor;
}
while (victor != 'X' && victor != 'O');
}
while (quit == 'n');
return victor;
}
float test_grader()
{
string student_name;
int row0 = 0;
int row1 = 0;
float average = 0;
float buffer = 0;
float total = 0;
const int ROW0 = 30;
const int ROW1 = 30;
string name_matrix[ROW0];
float score_matrix[ROW1];
float score = 0;
float student_number = 0;
cout << "\nTo beging entering test scores press any key\n";
cin.get();
cout << "\n0 as a student test score = 'quit'.\n";
cout << "\nUse an underscore to seperate first and last names.\n";
do
{
student_number++;
cout << "\nEntry number: " << student_number;
cout << "\n\nEnter test score in %.\n";
cin >> score;
score_matrix[row1] = score;

if (score > 100 || score < 0)
{
cout << "\n\nInvalid data type\n\n";
system ("pause");
student_number = 0;
continue;
}
row1++;
if (score == 0)
break;
cout << "\nEnter student name\n";
cin >> student_name;
name_matrix[row0] = student_name;
++row0;
}
while (true);
row0 = 0;
row1 = 0;
for (int j = 0; j <= student_number - 2; j++)
{
cout << "\n\n" << name_matrix[row0] << " scored a ";
cout << score_matrix[row1] << "%";
cout << "\n" << name_matrix[row0];
if (score_matrix[j] >= 90)
{
cout << " got an A\n\n";
}
if (score_matrix[j] < 90 && score_matrix[j] >= 80)
{
cout << " got a B\n\n";
}
if (score_matrix[j] < 80 && score_matrix[j] >= 70)
{
cout << " got a C\n\n";
}
if (score_matrix[j] < 70 && score_matrix[j] >= 60)
{
cout << " got a D\n\n";
}
if (score_matrix[j] <= 59)
{
cout << " got a F\n\n";
}
row0++;
row1++;
}
for (row1 = 0; row1 <= student_number - 2; row1++)
{
total = buffer + score_matrix[row1];
buffer = total;
}
average = total / (student_number - 1);
cout << "\n\nAverage score: " << average << "\n\n";
return average;
}
string reverser()
{
string any_thing;
cout << "Enter a string\n\n";
cin >> any_thing;
cout << "\n";
for (int i = any_thing.length() - 1; i >= 0; i--)
{
cout << any_thing[i];
}
cout << " is " << any_thing << " spelled backwards.\n\n";
return any_thing;
}


// Theta and
// The Fibonocci Sequence;

//up to 9.28565e+4931; or the 23601st element.
#include
using std::cin;
using std::cout;
int main()
{
unsigned long order = 0;
long double a_1 = 1;
long double a_2 = 1;
long double a_next;
long double theta;
long double dump = 0;
long double dump2;
char decision;
cout << "Press return to get the Fibonocci Sequence going";
cin.get();
order++;
cout << "\t\t" << order << ") " << a_1 << "\n";
order++;
cout << "\t\t" << order << ") " << a_2 << "\n";
theta = a_2 / a_1;
dump2 = theta + dump;
dump = dump2;
do
{
for (int i = 0; i < 255; i++)
{
a_next = a_1 + a_2;
theta = a_2 / a_1;
dump2 = theta + dump;
dump = dump2;
order++;
cout << "\t\t" << order << ") " << a_next << "\n";
a_1 = a_2;
a_2 = a_next;
}
cout << "current average of Theta: " << dump / (order - 1);
cout << "\nlast computed Theta: " << a_next / a_1;
cout << "\n\tQuit? (y/n)";
cin >> decision;
}
while (decision != 'y');
cout << "\noverall average of Theta: " << dump / (order - 1);
cout << "\nlast computed value of Theta: " << a_next / a_1;
cout << "\nI'm sorry that is as high as I can calculate the sequence\n";
cout << "press return to quit";
cin.get();
return 0;
}



// Lock Combination. Nested if.
#include
using namespace std;
int main()
{
const int first_num = 25;
const int second_num = 30;
const int third_num = 88;
int first_pick;
int second_pick;
int third_pick;
cout << "\n\nEnter first number.\n\n";
cin >> first_pick;
if (first_pick == first_num)
{
cout << "\n\nNow enter second pick.\n\n";
cin >> second_pick;
if (second_pick == second_num)
{
cout << "\n\nNow enter third pick.\n\n";
cin >> third_pick;
if (third_pick == third_num)
{
cout << "\n\nYou opened the combination lock!\n\n";
}
}
}
else
{
cout << "\n\nThis lock does not belong to you.\n\n";
}
return 0;
}



// Lock Combination
#include
using namespace std;
int main()
{
const int first_num = 25;
const int second_num = 30;
const int third_num = 88;
int first_pick;
int second_pick;
int third_pick;
cout << "\n\nenter first number\n\n";
cin >> first_pick;
cout << "\n\nnow enter second pick\n\n";
cin >> second_pick;
cout << "\n\nnow enter third pick\n\n";
cin >> third_pick;
if (first_pick == first_num && second_pick == second_num &&
third_pick == third_num)
{
cout << "\n\nI cant believe it,";
cout << "you opened the combination lock\n\n";
}
else
{
cout << "\n\nthis lock does not belong to you\n\n";
}
return 0;
}



//The Keno Player. With help from Jim
#include
#include
#include
#include
using namespace std;
int main()
{
srand(time( 0 ));
int random_number;
int pick;
string answer;
bool done = false;
while (!done)
{
random_number = rand();
pick = (random_number % 80) + 1;
cout << "\n\npick:" << "\t" << pick << "\n\n";
cout << "\n\nDo you want another value? (yes/no)\n\n";
cin >> answer;
if ( answer != "yes" )
done = true;
}
return 0;
}



//prime number finder 2.0
#include
#include
#include
using namespace std;
int main()
{ofstream fout("primes.txt");
float stop = 0;
int place = 0;
const int size = 100000;
cout << "\nEnter ending number of primes calculation\n";
cin >> stop;
float testedNums[size];
float primeNums[size];
stop++;
primeNums[place] = 1;
testedNums[place] = place;
place++;
time_t start = time(0);
for(float allNums = 2; allNums <= stop; allNums++)
{float twoForPrime = 0;
for(float divNum = 1; divNum <= stop; divNum++)
{float tested = allNums / divNum;
int logicLow = tested - 1;
int logicHigh = tested + 1;
for(float testNum = logicLow; testNum <= logicHigh; testNum++)
{if(tested == testNum)
twoForPrime++;}}
if(twoForPrime == 2)
{primeNums[place] = allNums;
testedNums[place] = place;
place++;}}
time_t end = time(0);
for(int i = 0; i < place; i++)
{cout << testedNums[i] + 1 << " " << primeNums[i] << endl;
fout << testedNums[i] + 1 << " " << primeNums[i] << endl;}
cout << "\nElapsed seconds: " << difftime(end, start) << endl;
fout << flush;
fout.close();
return 0;}




// Tic Tac Toe Board 3.0 AI
#include
#include
using namespace std;
int main()
{srand(time(0));
int rw = 0;
int clm = 0;
const int ROWS = 4;
const int COLUMNS = 4;
char quit;
string whose_move;
string O_s_move;
string X_s_move;
do
{char board[ROWS][COLUMNS] = {{' ', '1', '2', '3'},
{'1', 'E', 'E', 'E'},
{'2', 'E', 'E', 'E'},
{'3', 'E', 'E', 'E'}};
cout << "\n\n";
for (int i = 0; i < ROWS; ++i)
{for (int j = 0; j < COLUMNS; ++j)
cout << "\t" << board[i][j];
cout << endl;}
for (int c = 0; c < 5; c++)
{if (whose_move != "O_s_move")
{cout << "\nX's move.\n";
cout << "\nenter ROW (1-3), and COLUMN (1-3)\n";
cin >> rw >> clm;
if (board[rw][clm] == 'X' || board[rw][clm] == 'O')
{cout << "\n\n'X' made an illeagle move!\n\n";
continue;}
board[rw][clm] = 'X';
cout << "\nNow the tic-tac-toe board is:\n\n";
for (int i = 0; i < ROWS; ++i)
{for (int j = 0; j < COLUMNS; ++j)
cout << "\t" << board [i][j];
cout << endl;}}
if (board[1][1] == 'X' && board[1][2] == 'X' && board[1][3] == 'X'
|| board[2][1] == 'X' && board[2][2] == 'X' && board[2][3] == 'X' ||
board[3][1] == 'X' && board[3][2] == 'X' && board[3][3] == 'X' ||
board[1][1] == 'X' && board[2][1] == 'X' && board[3][1] == 'X' ||
board[1][2] == 'X' && board[2][2] == 'X' && board[3][2] == 'X' ||
board[1][3] == 'X' && board[2][3] == 'X' && board[3][3] == 'X' ||
board[1][1] == 'X' && board[2][2] == 'X' && board[3][3] == 'X' ||
board[1][3] == 'X' && board[2][2] == 'X' && board[3][1] == 'X')
{cout << "\n\n'X' won the game!\n\n";
break;}
if (c == 4)
{cout << "\n\nThe game was a tie.\n\n";
break;}
do
{cout << "\nO's move.\n";
rw = (rand() % 3) + 1;
clm = (rand() % 3) + 1;
if (board[2][2] == 'E')
rw = 2, clm = 2;
if (board[rw][clm] == 'X' || board[rw][clm] == 'O')
{whose_move = "O_s_move";
continue;}
board[rw][clm] = 'O';
whose_move = "X_s_move";
cout << "\nNow the tic-tac-toe board is:\n\n";
for (int i = 0; i < ROWS; ++i)
{for (int j = 0; j < COLUMNS; ++j)
cout << "\t" << board [i][j];
cout << endl;}}
while (whose_move == "O_s_move");
if (board[1][1] == 'O' && board[1][2] == 'O' && board[1][3] == 'O'
|| board[2][1] == 'O' && board[2][2] == 'O' && board[2][3] == 'O' ||
board[3][1] == 'O' && board[3][2] == 'O' && board[3][3] == 'O' ||
board[1][1] == 'O' && board[2][1] == 'O' && board[3][1] == 'O' ||
board[1][2] == 'O' && board[2][2] == 'O' && board[3][2] == 'O' ||
board[1][3] == 'O' && board[2][3] == 'O' && board[3][3] == 'O' ||
board[1][1] == 'O' && board[2][2] == 'O' && board[3][3] == 'O' ||
board[1][3] == 'O' && board[2][2] == 'O' && board[3][1] == 'O')
{cout << "\n\n'O' won the game!\n\n";
break;}}
cout << "\n\nDo you wish to continue? (y/n)\n\n";
cin >> quit;}
while (quit != 'n');
return 0;}

0 Comments:

Post a Comment

<< Home