PDA

View Full Version : I'm proud of my recent C++ accomplishement


Pianomahnn
03-22-2002, 04:51 PM
Im a novice...so just say "you are on your way to greatness, pmahnn." :)

This program took this data (student ID, major, under/grad, course data)
<i>
U 111234 2907
515 160 3 A
515 168 4 A
540 155.01 3 B
420 101.01 3 C
515 254 3 A
348 120 2 B
540 100 1 A
U 332345 105
550 130 4 D
550 135 3 C
360 155.02 3 W
515 168 4 B
447 101 2 B
G 553456 2900
515 275 4 B
515 375.05 3 A
515 378.05 3 C
515 432 3 B
515 460 3 C
515 353.05 3 A
515 467 3 B
515 485 3 B
515 485 3 A
E
</i>

And turns it into this:
<i>
STUDENT MAJOR U/G GPA GRADE HOURS GPA STATUS
ID CODE HOURS POINTS EARNED
EARNED
--------------------------------------------------------------------------------
111234 2907 U 3 12 3 4.00 Good
332345 105 U 4 4 4 1.00 Probabtion
553456 2900 G 4 12 4 3.00 Good


Number of students in good standing: 2
Number of students on probation: 1
Number of undergraduates: 2
Number of graduates: 1
</i>


/***********************************************************************************
* PROGRAMMER: Chris Tinnon
*
* DATE: March 26, 2002
*
* FILE NAME: program2.cpp
*
* This program processes student course data from a file and produces a report
* showing current student grade point averages. The report will be printed to the
* screen and in an output file.
*
* INPUT FILES: a:students.txt
*
* OUTPUT FILES: a:gpa.txt
*
* MODULES USED IN PROGRAM:
* header: Outputs the header to the screen and output file
* read: Reads student data from the input file. Sends data to calculate
* Outputs student gpa data to the screen and output file.
* calculate: Calculates the student gpa data and sends it to module read.
*
* COURSE: ACS 168
* INSTRUCTOR: Sally Scott
* SECTION: 13
***********************************************************************************/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <cstdlib>
#include <cstring>
using namespace std;


void header(istream& in, ostream& out);

void read(istream& in, ostream& out);

void calculate(istream& in, ostream& out, int& hours, char& grade, int& gpa_hours, int& gp_earned,
int& hours_earned, double& gpa);


void main()
{
ifstream in;
ofstream out;

in.open("a:students.txt"); //opens input file
out.open("a:gpa.txt"); //opens output file

header(in, out); //calls header function

read(in, out); //calls read function

in.close(); //closes input file
out.close(); //closes output file
}


/********************************************************************************
* This writes the headings to the screen and output file
*
* Preconditions: The files are open, but nothing has been read or written
*
* Postconditions: The files have been written to, and the screen also
********************************************************************************/

void header(istream& in, ostream& out)
{
out.setf(ios::left); //Set justification for output file

out << setw(10) << "STUDENT" << setw(10) << "MAJOR" << setw(10) << "U/G" << setw(10) << "GPA" << setw(10) <<
"GRADE" << setw(10) << "HOURS" << setw(10) << "GPA" << setw(10) << "STATUS" << endl;
out << setw(10) << "ID" << setw(10) << "CODE" << setw(10) << " " << setw(10) << "HOURS" << setw(10) <<
"POINTS" << setw(10) << "EARNED" << setw(10) << " " << setw(10) << " " << endl;
out << setw(10) << " " << setw(10) << " " << setw(10) << " " << setw(10) << " " << setw(10) << "EARNED"
<< endl;
out << "--------------------------------------------------------------------------------" << endl;

cout.setf(ios::left); //set justification for screen output

cout << setw(10) << "STUDENT" << setw(10) << "MAJOR" << setw(10) << "U/G" << setw(10) << "GPA" << setw(10) <<
"GRADE" << setw(10) << "HOURS" << setw(10) << "GPA" << setw(10) << "STATUS" << endl;
cout << setw(10) << "ID" << setw(10) << "CODE" << setw(10) << " " << setw(10) << "HOURS" << setw(10) <<
"POINTS" << setw(10) << "EARNED" << setw(10) << " " << setw(10) << " " << endl;
cout << setw(10) << " " << setw(10) << " " << setw(10) << " " << setw(10) << " " << setw(10) << "EARNED"
<< endl;
cout << "--------------------------------------------------------------------------------" << endl;

}


/************************************************************************************
* This functions reads from the input file and passes data to the function calculate
* to be processed. The data is then passed back from calculate to be printed to
* the screen and to the output file.
*
* Preconditions: There are headings on the screen and in the output file.
*
* Postconditions: The gpa values have been calculated and all have been printed to
* the screen and output file.
************************************************************************************/

void read(istream& in, ostream& out)
{
//Variable Declarations
char input;
char grad, grade;
int id, major, dept, hours, grad_count = 0, undergrad_count = 0, gpa_hours = 0, gp_earned = 0,
hours_earned = 0;
int good = 0, prob = 0;
double course, gpa;

//Sets output file precision
out.precision(2);
out.setf(ios::fixed);
out.setf(ios::showpoint);

//Sets screen outpuf precision
cout.precision(2);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);

do //Begin loop for reading and processing data to and from files and screen
{
in.get(input);

if((input == 'U') || (input == 'G'))
{
if(hours_earned != 0)
{
out << setw(10) << gpa_hours << setw(10) << gp_earned << setw(10) << hours_earned <<
setw(10) << gpa;

cout << setw(10) << gpa_hours << setw(10) << gp_earned << setw(10) << hours_earned <<
setw(10) << gpa;

if(((grad == 'G') && (gpa >= 3.0)) || ((grad == 'U') && (gpa >= 2.0)))
{
out << setw(10) << "Good" << endl;
cout << setw(10) << "Good" << endl;
good++;
}
else
{
out << setw(10) << "Probabtion" << endl;
cout << setw(10) << "Probabtion" << endl;
prob++;
}

gpa_hours = 0;
gp_earned = 0;
hours_earned = 0;
} //end if statement

in.putback(input);
in >> grad >> id >> major;
out << setw(10) << id << setw(10) << major << setw(10) << grad;
cout << setw(10) << id << setw(10) << major << setw(10) << grad;

if(grad == 'U')
{
undergrad_count++;
}

if(grad == 'G')
{
grad_count++;
}
} //end if statement


if((input != 'U') && (input != 'G') && (input != 'E'))
{
in.putback(input);

in >> dept >> course >> hours >> grade;

//Calls Caldulate function
calculate(in, out, hours, grade, gpa_hours, gp_earned, hours_earned, gpa);

do
{
in.get(input);
} while(input != '\n'); //Gets to next line
} //end if statement



if(input == 'E')
{
break;
}


}while(! in.eof()); //end do while statement

if(hours_earned != 0) //Needed for last line processing
{
out << setw(10) << gpa_hours << setw(10) << gp_earned << setw(10) << hours_earned <<
setw(10) << gpa;
cout << setw(10) << gpa_hours << setw(10) << gp_earned << setw(10) << hours_earned <<
setw(10) << gpa;
if(((grad == 'G') && (gpa >= 3.0)) || ((grad == 'U') && (gpa >= 2.0)))
{
out << setw(10) << "Good" << endl;
cout << setw(10) << "Good" << endl;
good++;
}
else
{
out << setw(10) << "Probabtion" << endl;
cout << setw(10) << "Probabtion" << endl;
prob++;
}

//resets data
gpa_hours = 0;
gp_earned = 0;
hours_earned = 0;
} //end if statement


out << endl << endl;
out << setw(40) << "Number of students in good standing:" << setw(2) << good << endl;
out << setw(40) << "Number of students on probation:" << setw(2) << prob << endl;
out << setw(40) << "Number of undergraduates:" << setw(2) << undergrad_count << endl;
out << setw(40) << "Number of graduates:" << setw(2) << grad_count << endl << endl << endl;

cout << endl << endl;
cout << setw(40) << "Number of students in good standing:" << setw(2) << good << endl;
cout << setw(40) << "Number of students on probation:" << setw(2) << prob << endl;
cout << setw(40) << "Number of undergraduates:" << setw(2) << undergrad_count << endl;
cout << setw(40) << "Number of graduates:" << setw(2) << grad_count << endl << endl << endl;
}


/*************************************************************************************
* This function receieves data from read and calculates the gpa values.
*
* Preconditions: There is data to be processed from read.
*
* Postconditions: GPA data has been calculated.
*************************************************************************************/

void calculate(istream& in, ostream& out, int& hours, char& grade, int& gpa_hours, int& gp_earned,
int& hours_earned, double& gpa)
{
if((grade == 'A') || (grade == 'B') || (grade == 'C') || (grade == 'D'))
{
hours_earned += hours;
}

gpa_hours += hours;

if(grade == 'A')
{
gp_earned += (4 * hours);
}
if(grade == 'B')
{
gp_earned += (3 * hours);
}
if(grade == 'C')
{
gp_earned += (2 * hours);
}
if(grade == 'D')
{
gp_earned += (1 * hours);
}

gpa = double(gp_earned) / double(gpa_hours);
}

jules
03-22-2002, 06:49 PM
You are on your way to greatness, Pianomahnn.

Ack. That looks incredibly complex and confusing. If I ever get around to trying to learn C++, I know I'll go crazy.

Billyman
03-24-2002, 03:42 PM
Looks Greek to me.:)

Congrats on your accomplishments. You'll be a freakin hacker in no time.

estero
04-02-2002, 02:07 PM
Ok Piano... C++ FACE OFF! I WILL BE BACK!

estero
04-04-2002, 01:31 PM
ITS ON!!! Its gonna be a little fucked up because im copying and pasting it.


/*********************************************************
* *
* Course Number : CIS 111 (C++) *
* Assignment : Project # 3 *
* Due Date : 04/02/2002 *
* Author : Estero *
* Instructor : Phil Payne *
* *
* INPUT : Disk File *
* OUTPUT : SCREEN REPORT *
* FUNCTIONS CALLED : main() open_input_file() *
* print_headers() *
* *
* PROGRAM OBJECTIVE -- Use of a while loop in *
* processing input data from a disk. *
* *
* *
*********************************************************/


// preprocessor directives
// -- removed for compatibility -- #include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <stdlib.h>

// input file stream
ifstream indata("/mnt/dfloppy/compdata.txt"); // location of floppy disk under unix

// begin main()
void main ()
{

// Function Prototypes
void open_input_file();
void print_headers();

// varible list
char computer_name[13];
double retail_price;
double amt_cust_paid; //amount customer paid
double dealer_cost;
double dealer_profit;
double commission;
double total_commission = 0;
long total_computers_sold = 0;

// I/O manipulators
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);

// Function call that allows you to open input file
open_input_file();

// Function call that prints your headers
print_headers();

// read in the first line of the file
indata >> computer_name >> retail_price >> amt_cust_paid;

// keep reading in data until the file ends
while (! indata.eof())
{
// calculate the dealer cost and profit
dealer_cost = retail_price * .68;
dealer_profit = amt_cust_paid - dealer_cost;

// calculate the commission
if(dealer_profit >= 350)
{
commission = dealer_profit * .40;
}
else if(dealer_profit >= 200)
{
commission = dealer_profit * .30;
}
else
{
commission = dealer_profit * .20;
}

// increment the number of computers sold
total_computers_sold++;

// add commission to the total commision
total_commission += commission;

// display the fields
cout << "\n";
cout << setiosflags(ios::left) << setw(14) << computer_name;
cout << setiosflags(ios::right) << setw(8) << retail_price;
cout << setiosflags(ios::right) << setw(14) << amt_cust_paid;
cout << setiosflags(ios::right) << setw(16) << dealer_profit;
cout << setiosflags(ios::right) << setw(15) << commission;

// read in the next line of data
indata >> computer_name >> retail_price >> amt_cust_paid;

} // end of loop

// show report
cout << "\n\n REPORT SUMMARY\n";
cout << "\nTOTAL NUMBER OF COMPUTERS SOLD FOR APRIL 2002:";
cout << setiosflags(ios::right) << setw(10) << total_computers_sold << "\n";
cout << "\nTOTAL COMMISSION FOR Estero: ";
cout << setiosflags(ios::right) << setw(10) << total_commission << "\n";


} // end of main()


/*****************************************/
/* Functions Called from main() */
/* */
/* 1. open_input_file */
/* 2. print_headers */
/* */
/*****************************************/

void open_input_file()
{
if(indata.fail())
{
// -- removed for compatibility -- clrscr();
cout << "\nThere is a problem locating the input file";
cout << "\nCheck that input file is on the correct drive";
cout << "\n and file-name is spelled correctly\n";
exit(1);
}
}

void print_headers()
{
// display the headers
cout << "\n PGCC COMPUTER CENTER\n";
cout << "\n COMMISSIONS EARNED FOR APRIL 2002\n";
cout << "\n SALESPERSON: Estero \n";
cout << "\nMODEL MSRP CUSTOMER COST DEALER PROFIT COMMISSION\n";

}



I would show you the output but its so fucked up because of the copying and pasting that it doesnt matter.

What the program does is calculate dealer profit, retail price and commission from a list of computers and costs from a file.

Pianomahnn
04-04-2002, 05:13 PM
/********************************************************************************
* Programmed by Chris Tinnon
* Date: April 3, 2002
* Section: 15
* File: skeleton.cpp
*
* Program 3 converted to using classes
*
* This program accepts input for a camper from the keyboard and prints the fee
* to a file and the screen. It also tracks the number of campers and fee totals:
* tracking in-state and out-of-state statictics. These stats are printed as a
* summary report at the end of the output both on the screen and in the outfile.
*
* File:
* output -- report.txt
* stored on the a-drive
*
* Classes:
* Camper:
* This class holds the individual camper's information: their camper id, their
* residency code, the number of days they stayed at the park, and the fee
* charged. It has all the necessary functions: accessor, input, calculate, and
* print functions
*
* FeeRecord:
* This class holds the number of campers with an Illinois residency code,
* the number with some other residency code, the number of campers who stayed
* more than one week, the total fees collected from Illinois residents, and
* those collected from other residents.
* It has all the necessary functions: constructor, calculate, and print functions.
* The class tracks and reports on the camper statistics for the period of time
* the user has choosen to input information.
*
* Functions:
* There are no functions outside the classes.
*
*
************************************************************************************/



#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;


/*********************** BEGIN Camper CLASS ***********************/

class Camper
{
public:

Camper();

int get_id();
// returns id


char get_res_code();
// returns res_code


int get_days();
// returns days


double get_fee();
// returns fee


double get_in_fees();
// returns in_fees


double get_out_fees();
// returns out_fees


void ReadInput();
// postcondition: values for id, res_code, and days have been
// read from the keyboard


void CalculateFee();
// precondition: the camper res_code and days have values
// postcondition: the fee for the camper has be determined


void PrintCamperDetail(ostream& outfile);
// precondition: an ostream parameter has been passed to the function
// postcondition: the details for the camper have been printed to
// either the screen or to a file


private:
int id;
char res_code;
int days;
double fee;
double in_fees;
double out_fees;

};

/*********************** END Camper CLASS ****************************/



/*************** BEGIN Camper CLASS FUNCTION DEFINITIONS ******************/

Camper::Camper()
{
id = 0;
days = 0;
fee = 0;
in_fees = 0;
out_fees = 0;
}



/**********************************************************************************
* Reads the id, res_code, and number of days for the camper from the user
*
* Preconditions: none
*
* Postconditions: id, res_code, and days have been read
**********************************************************************************/
void Camper::ReadInput()
{

cout << "Please enter the Camper's information (enter -999 to quit):" << endl;
cout << "ID: ";
cin >> id;

if(id != -999)
{
cout << endl << "Residency Code: ";
cin >> res_code;
cout << endl << "Number of Days: ";
cin >> days;
}

}


/************************************************************************
* The following 6 functions return variables to the calling function
*
* Preconditions: The variables will be either 0 or another value
*
* Postconditions: The variables are the same and have returned a value
*************************************************************************/

double Camper::get_fee()
{
return fee;
}


int Camper::get_id()
{
return id;
}


char Camper::get_res_code()
{
return res_code;
}


int Camper::get_days()
{
return days;
}


double Camper::get_in_fees()
{
return in_fees;
}


double Camper::get_out_fees()
{
return out_fees;
}


/*********************************************************************************
* Calculates the fee based on the residency of the camper
*
* Preconditions: rays and res_code have values
*
* Postconditions: fee has been calculated
*********************************************************************************/
void Camper::CalculateFee()
{
if((res_code == 'R') || (res_code == 'r'))
{
if(days > 7)
{
fee = ((7 * 12.95) + ((days - 7) * 10.95));
}
else
{
fee = days * 12.95;
}
}
else
{
fee = days * 15.95;
}
}


/*********************************************************************************
* Prints column headings and the detail line for one camper
*
* Preconditions: id, res_code, days, and fee have values
*
* Postconditions: line has been printed
*********************************************************************************/
void Camper::PrintCamperDetail(ostream& outfile)
{
if((res_code == 'r') || (res_code == 'R'))
{
cout << " " << id << "\t Resident \t" << days << "\t $" << fee << endl;
outfile << " " << id << "\t Resident \t" << days << "\t $" << fee << endl;
}
else
{
if((res_code == 'n') || (res_code == 'N'))
{
cout << " " << id << "\t Non-Resident \t" << days << "\t $" << fee << endl;
outfile << " " << id << "\t Non-Resident \t" << days << "\t $" << fee << endl;
}
else
{
cout << " " << id << "\t Unknown \t" << days << "\t $" << fee << endl;
outfile << " " << id << "\t Unknown \t" << days << "\t $" << fee << endl;
}
}

cout << endl;
}

/********************* END Camper CLASS FUNCTION DEFINITIONS ********************/



/*********************** BEGIN FeeRecord CLASS ***********************/

class FeeRecord
{
public:

FeeRecord();
// pre: an instance of this class has been declared calling the default constructor
// post: all data members have been initialized to zero


void CalcTotals(Camper Camper);
// Preconditions: fee, res_code, and days have values
// Postconditions: in_campers, out_campers, in_fees, out_fees have been updated.


void PrintSummary(ostream& outfile);
// Preconditions: in_campers, out_campers, in_fees, out_fees, and over_week have
// values
// Postconditions: summary has been printed

private:
double total_fees;
double total_out_fees;
double total_in_fees;
int in_campers;
int out_campers;
int over_week;
int total_campers;

};

/********************* END FeeRecord CLASS *************************/



/************ BEGIN FeeRecord CLASS FUNCTIONS DEFINITIONS **************/


FeeRecord::FeeRecord()
{
total_fees = 0;
total_out_fees = 0;
total_in_fees = 0;
in_campers = 0;
out_campers = 0;
over_week = 0;
total_campers = 0;
}



/**********************************************************************************
* Counts the number of in-state, out-of-state, and accumulates the in-state and
* out-of-state fees.
*
* Preconditions: fee, res_code, and days have values
*
* Postconditions: in_campers, out_campers, in_fees, out_fees have been updated.
**********************************************************************************/
void FeeRecord::CalcTotals(Camper Camper)
{

if((Camper.get_res_code() == 'r') || (Camper.get_res_code() == 'R'))
{
in_campers++;
total_in_fees = ((Camper.get_fee()) + total_in_fees);
}
else
{
out_campers++;
total_out_fees = ((Camper.get_fee()) + total_out_fees);
}

if((Camper.get_days()) > 7)
{
over_week++;
}

total_campers++;

total_fees += (Camper.get_fee());
}


/***********************************************************************************
* Prints the in-state, out-of-state, and total number of campers; in-state,
* out-of-state, and total fees; and the number of campers staying more than 7 days
*
* Preconditions: in_campers, out_campers, in_fees, out_fees, and over_week have
* values
*
* Postconditions: summary has been printed
************************************************************************************/
void FeeRecord::PrintSummary(ostream& outfile)
{
outfile << "----------------------------------------------------------------------------" << endl;
outfile << endl << "\tSummary Report" << endl << endl;
outfile << "----------------------------------------------------------------------------" << endl;
outfile << endl;
outfile << " Numbers of Campers \t\tFees Collected" << endl;
outfile << " In-State:\t" << in_campers << "\t\t$" << total_in_fees << endl;
outfile << " Out-of-state:\t" << out_campers << "\t\t$" << total_out_fees << endl << endl;
outfile << "----------------------------------------------------------------------------" << endl;
outfile << "\tTotal\t" << total_campers << "\t\t$" << total_fees << endl << endl;
outfile << " Number of campers staying more than 7 days: " << over_week << endl;
}


/**************** END FeeRecord CLASS FUNCTION DEFINITIONS **************************/



/******************************** BEGIN MAIN ************************************/
int main()
{

Camper Camper;
FeeRecord FeeRecord;

/*** OPEN OUTPUT FILE COMMANDS ***/

ofstream outfile;
outfile.open("a:report.txt");

if(outfile.fail())
{
exit(1);
}




/*** SCREEN AND FILE OUTPUT MANIPULATION COMMANDS ***/

cout.setf(ios::right);
cout.setf(ios::fixed);
cout.precision(2);

outfile.setf(ios::right);
outfile.setf(ios::fixed);
outfile.precision(2);




/*** PRINT HEADER ***/

outfile << " ID\t Residency\tDays\tFee" << endl;
outfile << "----------------------------------------------------------------------------" << endl;




/*** BEGIN CALLS FOR CLASS FUNCTIONS FOR CAMPER INPUT AND FILE OUTPUT ***/

while(Camper.get_id() != -999)
{
Camper.ReadInput();

if(Camper.get_id() != -999)
{
Camper.CalculateFee();
Camper.PrintCamperDetail(outfile);
FeeRecord.CalcTotals(Camper);
}
}

FeeRecord.PrintSummary(outfile);



return 0;


/*** CLOSE OUTPUT FILE ***/

outfile.close();

}

skalie
04-04-2002, 08:43 PM
That's all very nice, but how about something that's actually usefull?

estero
04-11-2002, 03:35 PM
Piano, I'min the process of writing something right now... I WILL GET YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Rabble Rouser
04-11-2002, 10:32 PM
Oh dear lord, I am so glad I dropped my programming course. :eek: