PROGRAM Weighted Grade Calculator */ /* AUT

advertisement
/* ========================================================================== */
/* PROGRAM Weighted Grade Calculator */
/* AUTHOR: Corey Morabito
FSU MAIL NAME: csm13b
RECITATION SECTION NUMBER: 11
RECITATION INSTRUCTOR NAME: Avishek Mukherjee
COURSE: COP 3014 Spring 2014
PROJECT NUMBER: 1
DUE DATE: Wednesday 1/22/2014
PLATFORM: Windows OS / C++ 11 / Microsoft Visual C++ Express 2012 IDE */
//SUMMARY
/*This program takes as input the user's scores on 6 programs and 2 exams.
It then calculates the weighted total grade for the user. Note the item
weights are exactly as given in this term's course syllabus. You will
be able to use this program to calculate your own final weighted total. */
//INPUT
//All input is interactive. The user inputs the scores as integer values.
//OUTPUT
/* The program prints an output title, echoprints the user's input in
a readable fashion, and the prints out the calculated final result. */
//ASSUMPTIONS
/* We assume that all input data is valid and correctly entered by the user.
The program is therefore not guaranteed to work correctly if bad data
is entered. */
#include <iostream>
// include standard I/O libraries
#include <iomanip>
using namespace std;
int main ()
{
// the constant weights for all scoring items
const double P1_WEIGHT = 0.02;
const double P2_WEIGHT = 0.10;
const double P3_TO_P6_WEIGHT = 0.12;
const double EXAM_WEIGHT = 0.20;
int p1score;
// scores on the 6 programs
int p2score;
// Multiple Syntax Errors: Expected ";" for scores
int p3score;
int p4score;
int p5score;
int p6score;
int exam1score;
// scores on the 2 exams
int exam2score;
double wtdTotalGrd;
// the weighted total grade as calculated
// write the main output heading
cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
cout << "Welcome to the Weighted Grade Calculator Program" << endl;
cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl << endl;
// ask the user to type in the program grades first
cout << "Please enter your score on program 1 -> ";
cin >> p1score;
cout << "
program 2 -> ";
cin >> p2score;
cout << "
program 3 -> ";
cin >> p3score;
cout << "
program 4 -> ";
cin >> p4score;
cout << "
program 5 -> ";
cin >> p5score;
cout << "
program 6 -> ";
cin >> p6score;
// now ask the user to type in the exam scores
cout << endl << "Please enter your score on exam 1 -> ";
cin >> exam1score;
cout << "
exam 2 -> ";
cin >> exam2score;
// echoprint all of the user's scores to verify accuracy
cout << endl << "You have entered the following scores:" << endl
<< "\tProgram 1: " << p1score << endl
<< "\tProgram 2: " << p2score << endl
<< "\tProgram 3: " << p3score << endl
<< "\tProgram 4: " << p4score << endl
<< "\tProgram 5: " << p5score << endl
<< "\tProgram 6: " << p6score << endl
<< "\tExam 1: " << exam1score << endl
<< "\tExam 2: " << exam2score << endl << endl;
// calculate the weighted total grade as a real number
wtdTotalGrd = (p1score * P1_WEIGHT) + (p2score * P2_WEIGHT)
+ ((p3score + p4score + p5score + p6score) * P3_TO_P6_WEIGHT)
+ ((exam1score + exam2score) * EXAM_WEIGHT);
semantic error
// changed variable to exam2score to fix
// now print the weighted total with 2 digits past the decimal point
cout << fixed << showpoint << setprecision(2);
cout << "Your weighted total is: " << wtdTotalGrd << endl;
// print a closing message and signal normal termination
cout << endl << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
cout << endl << "Program Run Completed." << endl; // error: changed "end" to "endl"
cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl << endl;
return (0);
}
//end of program
/* ========================================================================== */
/* PROGRAM High-Low Guessing Game */
/* AUTHOR: Corey Morabito
FSU MAIL NAME: csm13b
RECITATION SECTION NUMBER: 11
RECITATION INSTRUCTOR NAME: Avishek Mukherjee
COURSE: COP 3014 Spring 2014
PROJECT NUMBER: 4
DUE DATE: Wednesday 3/26/2014
PLATFORM: Windows OS / C++ 11 / Microsoft Visual C++ Express 2012 IDE */
//SUMMARY
/*This program uses multiple functions to take as input the users bet, and then takes in the users
guesses,
whilst prompting the user if the guess was too high or too low. If the user wins, the program calculates
the new amount of money by dividing the bet by the number of guesses it took. If the user loses, the
new
amount would be reflected by taking the entire bet amount from the amount of money the user has.
Afterwards,
the program will tell these monetary values, calculate and tell the percentage of games won, and ask the
user
if they would like to play again.*/
//INPUT
/*All input is interactive. The user inputs a bet and multiple guesses as integer values.
A char value is taken as a response is the user would like to play again or not.*/
//OUTPUT
/* The program prints an output title and summary, as well as prompts for the users bet, guesses
and the calculated totals. It also prompts the user if there was an error with any input for the initial bet.
*/
//ASSUMPTIONS
/* We assume that input data can be invalid and incorrectly entered by the user.
If bad input data is entered, the program will check for errors and go through
proper steps to functon normally.*/
#include <iostream>
//includes standard io libraries
#include <iomanip>
#include <cstdlib>
//includes c libraries cstdlib and ctime
#include <ctime>
using namespace std;
//standard namespace being used
//function prototypes with int or void arguments, initialized before main
void PrintHeading (int money);
int PlayGame(int money);
void GetBet(int money, int& bet);
int DrawNum(int max);
int GetGuess(void);
int CalcNewMoney(int money, int bet, int guesses);
bool PlayAgain();
// int main
int main ()
{
const int START_AMOUNT = 1000;
integer called START_AMOUNT and initializes it to 1000.
//declares a constant
int money = START_AMOUNT;
START_AMOUNT to an integer variable called money.
//assigns the value of
int preMoney;
integer variable preMoney.
//declaration of
unsigned int seed;
unsigned integer varaible called seed.
//declaration of
int gamesPlayed=0;
variables gamesPlayed and gamesWon are initialized to 0.
//integer
int gamesWon=0;
double percentage;
double called percentage.
seed = (static_cast<unsigned>(time(NULL)));
value in seed
srand(seed);
using the seed value.
PrintHeading(money);
PrintHeading using the money value.
//declares a
//get time from computer and stores
//calls srand
//calls function
do
//start of do while loop
{
preMoney = money;
value of money to preMoney.
//assigns the
money = PlayGame(money);
//calls the PlayGame
function using the money value, then after exiting the function assigns that new value to money.
cout << "You now have $" << money << endl; //prompts the user the amount of money
they now have after the game.
gamesPlayed++;
//increments gamesPlayed;
if(preMoney <= money)
comparing preMoney and money
//if statement
{
gamesWon++;
//increments gamesWon
}
percentage = (static_cast<double>(gamesWon)/gamesPlayed) * 100; //calculates
percentage of games won
cout << fixed << showpoint << setprecision(2) << "You have won " << percentage << "%
of your games." << endl << endl; //prints percentage of games won
}
while(PlayAgain() && money > 0);
//end of do while loop
cout << "Thank you for playing." << endl << endl; //prompts user thanks for playing.
return(0);
//return value to main
}
//end of int main
/*function definition for PrintHeading*/
void PrintHeading (int money)
{
/*prints out a heading for the game*/
cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
cout << "Welcome to the high-low betting game." << endl;
cout << "You have $" << money << " to begin the game." << endl;
cout << "Valid guesses are numbers between 1 and 100." << endl;
cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
return;
}
//end of PrintHeading function
/*function definition for PlayGame*/
int PlayGame(int money)
{
const int MAX = 100;
integer MAX initilized to 100
int bet;
type bet
int guesses = 0;
initialized to 0
GetBet(money,bet);
function
int random = DrawNum(MAX);
function and stores the result in int random
//declares constant
//declares int
//declares int guesses
//calls GetBet
//calls DrawNum
int guess;
//declares int called guess
bool playerWins = false;
initialized to false value
while(guesses < 6 && !playerWins)
//declares bool playerWins
//start of while loop
{
guess = GetGuess();
GetGuess and sets the result to guess
//calls
if(guess == random)
equal to random, executes code
//if guess is
{
playerWins = true;
//playwerWins
assigned to true value
cout << "You win!!!" << endl << endl;
}
else if(guess < random)
random, executes code
//if guess is less than
{
cout << "Too low..." << endl;
}
else
executes this code
//else
{
cout << "Too high..." << endl;
}
guesses++;
//increments guesses
}
//end of while loop
if (!playerWins)
executes code
{
//if not playerWins,
guesses = -1;
cout << "The correct answer is " << random << "." << endl << endl;
}
money = CalcNewMoney(money, bet, guesses);
result to money
//calls CalcNewMoney function, sets
return money;
}
//end of PlayGame function
/*function definition for GetBet*/
void GetBet(int money, int& bet)
{
bool isValid = true;
of a bool called isValid to true
//initializes declaration
do
//start of do-while loop
{
isValid = true;
//isValid
initialized to true value
cout << "Please enter valid bet:";
cin >> bet;
//gets
user input
cin.ignore(200,'\n');
if (!cin)
//if not cin,
execute code
{
cin.clear();
cin.ignore(200, '\n');
isValid=false;
//isValid is
assigned to false value
cout << "Non integer input." << endl;
}
else if(bet > money)
//if bet greater than
money, executes code
{
isValid=false;
cout << "Not enough money..." << endl;
}
else if(bet < 1)
//if bet is less than 1,
executes code
{
isValid=false;
cout << "Bet cannot be 0 or a negative integer." << endl;
}
}
while(isValid==false);
while isValid equals false value
//executes do code
return;
}
//end of GetBet function
/*function definition for DrawNum*/
int DrawNum(int MAX)
{
double x = RAND_MAX + 1.0;
auxiliary
// x and y are both
int y;
variables used to do the calculation
//
y = static_cast<int> (1 + rand() * (MAX / x));
return (y);
contains the result
// y
}
// end of DrawNum function
/*function definition for GetGuess*/
int GetGuess(void)
{
int guess;
//declaration of int guess
bool isValid = true;
initialized to true value
//declares bool isValid
do
//start of do-while loop
{
isValid = true;
cout << "Please enter a valid guess:";
cin >> guess;
//gets user
if (!cin)
//if not cin,
input
executes code
{
cin.clear();
cin.ignore(200, '\n');
guess = 0;
}
else if (guess < 1 || guess > 100)
100, executes code
//if guess is less than 1 or greater than
{
guess = 0;
}
}
while(isValid == false);
isValid is assigned to false
//executes do code while
return guess;
}
//end of GetGuess function
/*function definition for CalcNewMoney*/
int CalcNewMoney(int money, int bet, int guesses)
{
if (guesses == -1)
equals -1, executes code
//if guesses
{
money = money - bet;
}
else
executes this code
{
//else
money = money + static_cast<int>(bet/guesses);
}
return money;
value for money
//returns new
}
//end of CalcNewMoney function
/*function definition for PlayAgain*/
bool PlayAgain()
{
char response;
variable called response
//declares char
cout << "Do you want to play again? Enter Y or N:";
cin >> response;
//gets user
input
cin.ignore(200,'\n');
while((response != 'Y') && (response != 'y') && (response != 'N') && (response != 'n')) //start of
while loop, given these conditions, executes code
{
cout << "Please enter Y or N:";
cin >> response;
cin.ignore(200,'\n');
}
if(response == 'Y' || response == 'y')
code
{
return true;
}
//if response equals 'Y' or 'y', executes
else
executes this code
{
return false;
}
}
//end of PlayAgain function
//end of program
/* ========================================================================== */
/* PROGRAM THE DECRYPTER!!!!!!!!!
/* AUTHOR: Corey Morabito
FSU MAIL NAME: csm13b
RECITATION SECTION NUMBER: 11
RECITATION INSTRUCTOR NAME: Avishek Mukherjee
COURSE: COP 3014 Spring 2014
PROJECT NUMBER: 5
DUE DATE: Wednesday 4/9/2014
PLATFORM: Windows OS / C++ 11 / Microsoft Visual C++ Express 2012 IDE */
//SUMMARY
//else,
/*This program uses multiple functions to decrypt an encrypted file. The user inputs in
a selection for what type of decryption needs to be done, then asks for a valid file to be decrypted.
Using two arrays, one for a standard english alphabet and one for the key to decrypt, the program goes
line by line decrypting the file until the end of file. The program will then prompt the selection screen
again
where the user can choose to decrypt another file or quit the program.
//INPUT
/*All input is interactive. The user inputs a selection when prompted as well as a file to be opened and
read
by the program.
//OUTPUT
/* The program prints an output title and summary, as well as prompts for the different decryptions and
the end of program.
It also prompts the user if there was an error with any input. */
//ASSUMPTIONS
/* We assume that input data can be invalid and incorrectly entered by the user.
If bad input data is entered, the program will check for errors and go through
proper steps to functon normally.*/
#include <iostream>
// including libraries to be used
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
// declaring namespace
const int ALPHABET_SIZE = 26;
declaring constants, typedefs, and function prototypes before main
//
const string BORDER =
"********************************************************************************";
typedef char AlphaArray[ALPHABET_SIZE];
void PrintHeading();
char GetSelection();
void SubCipher(ifstream&, AlphaArray);
void CeasarCipher(ifstream&, AlphaArray);
void FileDecode(char);
int main()
// start of int main
{
PrintHeading();
// calls PrintHeading function
char selection;
// declares a char called selection
do
{
cout << BORDER << endl;
// prints out the constant BORDER
selection = GetSelection();
// calls GetSelection function and assigns it to the local variable selection
FileDecode(selection);
// calls FileDecode function using selection as a parameter
}
while(selection != 'q' && selection != 'Q');
return(0);
}
// end of int main
// end of program
void PrintHeading()
// start of PrintHeading function
{
// Prints out the Title and a summary of the program
cout << BORDER;
cout << "
Welcome to the DECRYPTER!!!!!!!" << endl << endl;
cout << " You have the option of performing either a ceasar decryption" << endl << "
or a substitution decryption." << endl << endl;
cout << "
You will be asked for a file to be encoded." << endl << endl;
cout << "
The DECRYPTER will then echoprint one line of your encoded" << endl << "
from your specified file followed by the same text decoded." << endl;
return;
}
// end of PrintHeading function
char GetSelection()
// start of GetSelection function
{
char selection;
// set up of char selection and bool isValid initialized to false
text
bool isValid = false;
cout << "What would you like to do?" << endl;
a selection
//prompts the user for
cout << "To decode a text file using the substitution cypher, press s." << endl;
cout << "To decode a text file using the ceasar cypher, press c." << endl;
cout << "To quit decoding, press q." << endl;
do
{
cout << "What is your selection? ";
inputed selection
//asks for a user
cin >> selection;
cout << endl;
cin.ignore(200,'\n');
switch(selection)
{
case 's':
case 'S': isValid = true;
the program returns the selection to main
// if selection is s or S,
break;
case 'q':
selection is q or Q, prompts that the program has ended then exits the program
// if
case 'Q': cout << endl;
cout << BORDER;
cout << "
Thank you for using" << endl;
cout << "
The DECRYPTER!!!!!!" << endl;
cout << BORDER;
exit(EXIT_SUCCESS);
break;
case 'c':
selection is c or C, the program returns the selection to main
// if
case 'C': isValid = true;
break;
default: cout << "Sorry, that was not a valid input. Please try again." << endl <<
endl;
}
}
while(!isValid);
return selection;
returns selectiong
//
}
// end of GetSelection
void FileDecode(char selection)
program, taking in selection as a parameter
// start of FileDecode
{
AlphaArray alphabet =
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; //declares an array
called alphabet of typedef AlphaArray, and stores the alphabet in it
string fileName;
string variable called fileName
ifstream fileRead;
declares an ifstream variable called fileRead
cout << "File names must not contain blanks." << endl;
opens the file
// declares a
//
// prompts for a file input, then
cout << "Please enter the file name to decode: ";
getline(cin, fileName);
fileRead.open(fileName.c_str());
while(!fileRead)
{
cout << "Reenter a valid file name:";
file if open failed, then reopens
// reprompts for a valid input
getline(cin, fileName);
fileRead.clear();
fileRead.open (fileName.c_str());
}
while(fileRead)
//
depending on the selection from GetSelection function, runs SubCipher or CaesarCipher function
{
if(selection == 's' || selection == 'S')
// if selection is s or S, run code
{
SubCipher(fileRead, alphabet);
}
else if(selection == 'c' || selection == 'C')
{
CeasarCipher(fileRead, alphabet);
}
}
return;
// return to main
// else if selection is c or C , run code
}
// end of FileDecode
void SubCipher(ifstream& fileRead, AlphaArray alphabet)
// start of SubCipher function, taking in
pass by reference ifstream fileRead and AlphaArray alphabet as arguments
{
string linetext;
string called linetext
// declares a
AlphaArray key;
declares key as an AlphaArray typedef
//
int count = 0;
initializes int count to 0
//
int loopcontrol;
loopcontrol and textlength as well as bool decoded
// declares ints
int textlength;
bool decoded;
cout << BORDER;
// prompts the title and decrytption type to the output screen
cout << "
cout << "
cout << "
cout << "
The DECRYPTER" << endl;
of" << endl;
Substitution Scheme Encryption" << endl;
Using Substitution set" << endl;
cout << BORDER << endl << endl << endl;
for(count; count< ALPHABET_SIZE; count++)
{
key[count] = fileRead.get();
alphabet in order to set up the key to decipher the file
}
// goes through the
cout << "Original Alphabet: ";
displays the characters stored in the alphabet array
// prompts the user and then
for(count = 0; count < ALPHABET_SIZE; count++)
{
cout << alphabet[count];
}
cout << endl;
cout << "Encrypted Alphabet: ";
then displayes the characters in the encrypted alphabet
// prompts the user
for(count = 0; count < ALPHABET_SIZE; count++)
{
cout << key[count];
}
cout << endl;
getline(fileRead, linetext);
be deciphered, uses it as a key
// gets the first line to
do
{
cout << linetext << endl;
// prints
linetext
textlength = linetext.length();
linetext and assignes that value to textlength
// gets the length of
for(loopcontrol = 0; loopcontrol < textlength; loopcontrol++) // increments loopcontrol
as long as loopcontrol is less than textlength
{
decoded = false;
for(count = 0; count < ALPHABET_SIZE; count++)
count as long as count is less than ALPHABET_SIZE constant int
// increments
{
if(linetext.at(loopcontrol) == key[count])
// if
the linetext at the loopcontrol position is equal to the count for the AlphaArray key, then it prints the
AlphaArray alphabet at that position to the screen
{
cout << alphabet[count];
decoded = true;
}
}
if((linetext.at(loopcontrol) == ',') || (linetext.at(loopcontrol) == '.')) // if
the character is one of those listed not part of the array it prints them
{
cout << linetext.at(loopcontrol);
}
else if(!decoded)
was one that was not deciphered then it prints a space
// if it
{
cout << " ";
}
}
cout << endl << endl;
getline(fileRead, linetext);
be deciphered
}
while(fileRead);
// gets the next line to
return;
// return to FileDecode
}
// end of SubCipher
void CeasarCipher(ifstream& fileRead, AlphaArray alphabet) // start of CeasarCipher function
{
string linetext;
string called linetext
// declares a
AlphaArray key;
declares a array key of typdef AlphaArray
//
int count = 0;
initializes int count to 0
//
int loopcontrol;
loopcontrol, textlength and offset as well as bool decoded
// declares ints
int textlength;
int offset;
bool decoded;
cout << BORDER;
// prompts the title and decrytption type to the output screen
cout << "
The DECRYPTER" << endl;
cout << "
of" << endl;
cout << "
Substitution Scheme Encryption" << endl;
cout << "
Using Caesar Substitution set" << endl;
cout << BORDER << endl;
fileRead >> offset;
fileRead takes in offset
fileRead.ignore(200,'\n');
//
for(count = 0; count < ALPHABET_SIZE; count++)
that const ALPHABET_SIZE
// increments count while count is less
{
key[count] = alphabet[((offset + count) % ALPHABET_SIZE)]; // calculates the position in
memory for AlphaArray alphabet and stores the value at the equivalent count in key
}
cout << "Original Alphabet: ";
// prompts the alphabet to the
screen
for(count = 0; count < ALPHABET_SIZE; count++)
{
cout << alphabet[count];
}
cout << endl;
cout << "Offset Value: " << offset << endl;
to the screen
cout << "Encrypted Alphabet: ";
encrypted alphabet to the screen
// prompts the value of offset
// prompts the
for(count = 0; count < ALPHABET_SIZE; count++)
{
cout << key[count];
}
cout << endl << endl;
getline(fileRead, linetext);
text from the file as a key to deciper it
do
{
cout << linetext << endl;
textlength = linetext.length();
// gets the first line of
for(loopcontrol = 0; loopcontrol < textlength; loopcontrol++) // increments loopcontrol
as long as loopcontrol is less than textlength
{
decoded = false;
for(count = 0; count < ALPHABET_SIZE; count++) // increments count as
long as count is less than ALPHABET_SIZE constant int
{
if(linetext.at(loopcontrol) == key[count]) // if the linetext at the
loopcontrol position is equal to the count for the AlphaArray key, then it prints the AlphaArray alphabet
at that position to the screen
{
cout << alphabet[count];
decoded = true;
}
}
if((linetext.at(loopcontrol) == ',') || (linetext.at(loopcontrol) == '.')) // if
the character is one of those listed not part of the array it prints them
{
cout << linetext.at(loopcontrol);
}
else if(!decoded)
// if it was one that was not deciphered then it prints a space
{
cout << " ";
}
}
cout << endl << endl;
getline(fileRead, linetext);
the next line of the file
}
while(fileRead);
return;
// returns to FileDecode
}
// end of CaesarCipher
// gets
Download