C++: Lab 1
17 Jan 2005
//Name: Andrew Ferguson
//Date: 1/17/2005
//Purpose: To see if I can get this work!
#include <iostream.h>
void main()
{
cout << "Hello\n";
cout << "Goodbye\n";
}
Categories : Programming Share:
//Name: Andrew Ferguson
//Date: 1/17/2005
//Purpose: To see if I can get this work!
#include <iostream.h>
void main()
{
cout << "Hello\n";
cout << "Goodbye\n";
}
Here’s this week C++ project we had to do. I’m just going to post original work that I made, not files they had us fix.
//Name: Andrew Ferguson
//Date: 1/27/2005
//Purpose: Compute gas costs
#include <conio.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int prevMonth, thisMonth, gasDiff;
double cost = 0;
char n;
int main(){
cout << "Enter last months gas reading:";
cin >> prevMonth;
cout << "Enter this months gas reading:";
cin >> thisMonth;
if(thisMonth < prevMonth){
thisMonth += 10000;
}
gasDiff = thisMonth - prevMonth;
if(gasDiff <= 70){
cost = 14.95;
}
else if(gasDiff <= 170){
cost = gasDiff * 0.095;
}
else if(gasDiff <= 400){
cost = gasDiff * 0.0525;
}
else{
cost = gasDiff * 0.02125;
}
cout << "Your total gas bill for this month is: " << cost << endl;
printf("Press any key to continue...");
n = getch();
return 0;
}
Here’s last weeks project:
//Name: Andrew Ferguson
//Date: 1/19/2005
//Purpose: To split up bubble gum
#include <conio.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int sticks, people, sticks_person, sticks_remain;
char n;
int main()
{
cout << "How many sticks of gum do you have?n";
cin >> sticks;
cout << "How many people are in MACS 261C?n";
cin >> people;
sticks_person = sticks/people;
sticks_remain = sticks%people;
cout << "Every person can receive " << sticks_person << ".n";
cout << "There are " << sticks_remain << " left over.n";
printf("Press any key to continue...");
n = getch();
return 0;
}
This weeks programming activity was actually pretty cool. We had to create a game that would generate a random integer between 1 and 10. The user then guesses a number and the computer tells you if you are high, low, or right on. Give it a whirl!
//Name: Andrew Ferguson
//Date: 2/6/2005
//Purpose: Make a program that makes you guess a number between 1 and 10
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int guessNum;
int userGuess;
char n;
int main(){
srand(time(0));
guessNum = 1+rand()%10;
cout << "Try to guess the number. It's between 1 and 10\n";
do{
cout << "What's your guess?\n";
cin >> userGuess;
if(userGuess < guessNum)
cout << "You guessed too low.\n\n";&
else if(userGuess > guessNum)
cout << "You guessed to high.\n\n";
} while(userGuess != guessNum);
cout << "That's right! My number was " << guessNum << ".\n";
printf("Press any key to continue...");
n = getch();
return 0;
}
//Name: Andrew Ferguson
//Date: 2/20/2005
//Purpose: To create a calculator to calculate different things. w00t!
#include <iostream.h>
#include <math.h>
void getOption();
double bmi(double& pounds, double& inches);
void doBMI();
double poundsToKilograms(double& input);
void doPoundsToKilograms();
double feetToMeters(double& input);
void doFeetToMeters();
void quadratic(int& a, int& b, int& c, double& soln_1, double& soln_2);
void doQuadratic();
int gcf(int& a, int& b, int& c);
void doGCF();
double stardate(int& mont, int& day, int& year, int& hour, int& minute, int& second);
void doStardate();
int main()
{
getOption();
return 0;
}
void getOption(){
int choice;
cout << endl << "AFdN Calc v0.1" << endl << endl;
do{
cout << "Your options are:" << endl
<< "1 - Calculate your BMI" << endl
<< "2 - Convert Pounds to Kilograms" << endl
<< "3 - Convert Feet to Meters" << endl
<< "4 - Solve a quadratic equation (no complex numbers)" << endl
<< "5 - Find the Greatest Common Factor for 3 numbers" << endl
<< "6 - Convert a date and time into Stardate format" << endl
<< "0 - Exit" << endl << endl
<< "Enter your choice: ";
cin >> choice;
}while((choice > 7) || (choice < 0));
switch(choice)
{
case 1:
doBMI();
getOption();
break;
case 2:
doPoundsToKilograms();
getOption();
break;
case 3:
doFeetToMeters();
getOption();
break;
case 4:
doQuadratic();
getOption();
break;
case 5:
doGCF();
getOption();
break;
case 6:
doStardate();
getOption();
break;
case 0:
break;
}
}
double bmi(double& pounds, double& inches){
return (poundsToKilograms(pounds)/(feetToMeters(inches)*feetToMeters(inches)));
}
void doBMI(){
double pounds, inches;
cout << "Enter your weight in pounds:";
cin >> pounds;
cout << "Enter your height in inches: ";
cin >> inches;
cout << "You have a BMI of " << bmi(pounds, inches) << endl;
}
double poundsToKilograms(double& input){
return (input*0.454);
}
void doPoundsToKilograms(){
double pounds;
cout << "Enter pounds: ";
cin >> pounds;
cout << pounds << " lbs is " << poundsToKilograms(pounds) << " kg" << endl;
}
double feetToMeters(double& input){
return (input*0.3048/12);
}
void doFeetToMeters(){
double inches;
cout << "Enter inches: ";
cin >> inches;
cout << inches << " in is " << feetToMeters(inches) << " m" << endl;
}
void quadratic(int& a, int& b, int& c, double& soln_1, double& soln_2){
soln_1 = ((-b)+sqrt((b*b)-(4*a*c)))/(2*a);
soln_2 = ((-b)-sqrt((b*b)-(4*a*c)))/(2*a);
}
void doQuadratic(){
int a,b,c;
double soln_1, soln_2;
cout << "Enter term 'a':";
cin >> a;
cout << "Enter term 'b':";
cin >> b;
cout << "Enter term 'c':";
cin >> c;
quadratic(a,b,c,soln_1,soln_2);
cout << "The solution to " << a << "x^2 + " << b << "x + " << c << " = 0 is x=" << soln_1 << " and x=" << soln_2 << endl;
}
int gcf(int& a, int& b, int& c){
{
int remainder = b % a;
if (remainder != 0)
return gcf(remainder,a,c);
remainder = c % a;
if (remainder != 0)
return gcf(remainder,a,c);
return a;
}
}
void doGCF(){
int a,b,c;
cout << "Enter first term: ";
cin >> a;
cout << "Enter second term: ";
cin >> b;
cout << "Enter third term: ";
cin >> c;
cout << "The Greatest Common Factor is " << gcf(a,b,c) << endl;
}
double stardate(int& month, int& day, int& year, int& hour, int& minute, int& second){
double nowTime = ((2322-1970)*365.2422*24*60*60)+(90*24*60*60);
double itTime;
int monthDays[] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
itTime = ((year-1970)*365.2422*24*60*60)+(monthDays[month]*24*60*60)+(day*24*60*60)+((hour-7)*60*60)+(minute*60)+second;
double newTime = itTime - nowTime;
double starTime = (newTime / (60*60*24*365.2422))*1000;
return starTime;
}
void doStardate(){
int month, day, year, hour, minute, second;
cout << "Enter the month: ";
cin >> month;
cout << "Enter the day: ";
cin >> day;
cout << "Enter the year: ";
cin >> year;
cout << "Enter the hour: ";
cin >> hour;
cout << "Enter the minute: ";
cin >> minute;
cout << "Enter the second: ";
cin >> second;
cout << endl << stardate(month, day, year, hour, minute, second);
}
C++, while tedious, is probably one of my favorite classes. I’ve never actually taken a e first line of each file
//Write the higher valuprogramming class before now and I imagine I will be taking more. Anyways, another has gone by and thus another lab is due and you get to see what I’ve done! This week we worked on interacting with outside files. Just simple tasks, like opening, closing, reading, and writing. The program I had to create delt with taking two seperate files and merging them into one while activly sorting the numbers from lowest to highest. I could not merge the data and then sort it. Enjoy!
//Name: Andrew Ferguson
//Date: 2/27/2005
//Purpose: Merge two files while sorting the data
//Open two read files and one write file
//Read and compare the first line of each file
//Write the higher value to the screen and to the write file
//Read the next highest number from the file that containted the previous higher number and compare again
//Repeat until both read files are completly read
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main(){
ifstream file_one, file_two;
ofstream fout;
double next_one, next_two;
file_one.open("location1.dat");
file_two.open("location2.dat");
fout.open("output.dat");
if(file_one.fail()){
cout << "File failed to open." << endl;
exit(1);
}
if(file_two.fail()){
cout << "File failed to open." << endl;
exit(1);
}
file_one >> next_one; //Prime the pump...so to speak
file_two >> next_two; //Prime the pump...so to speak
cout << "Merging Files..." << endl;
while((!file_one.eof()) && (!file_two.eof())){
if(next_one <= next_two){
fout << next_one << endl;
cout << next_one << endl;
file_one >> next_one;
}
else{
fout << next_two << endl;
cout << next_two << endl;
file_two >> next_two;
}
while((file_one.eof()) && (!file_two.eof())){
fout << next_two << endl;
cout << next_two << endl;
file_two >> next_two;
}
while((file_two.eof()) && (!file_one.eof())){
fout << next_one << endl;
cout << next_one << endl;
file_one >> next_one;
}
}
file_one.close();
file_two.close();
fout.close();
cout << "...Complete!" << endl;
}
Download the Windows Executable
File “Location1.dat”
File “Location2.dat”
The last two files are the files that are merged. You can download and use them or you can create your own files called location1.dat and location2.dat. If you create your own files, each file can contain as many floating points as you want with one per line. The files must be in the same directory as the program.
I spend the better part of two hours working on a regex (regular expression) Perl program for my web programming class. Granted, regexs are very power; but boy are they hard. Download the following code and compile/run with ActivePerl 5.8 or better:
#!C:perlbinperl.exe
#Program: Contact.pl
#Author: Andrew Ferguson
#Copyright Andrew Ferguson 2005. All Rights Reserved.
sub FormatName{
my ($n) = @_;
$n =~ tr/A-Z/a-z/; #Convert everything to lowercase to make it easier to work with
$n =~ s/^(w)/uc($1)/ge; #Capitilze the first letter;
return $n;
}
do{
print "Enter your first name: ";
$first_name = <>;
}while($first_name !~ / ^([A-Z]||[a-z])*$ /x); #Make sure only alphas are getting through
$first_name = &FormatName($first_name);
print "$first_name";
do{
print "Enter your last name: ";
$last_name = <>;
}while($last_name !~ / ^([A-Z]||[a-z])*$ /x);#Make sure only only alphas are getting through
$last_name = &FormatName($last_name);
print "$last_name";
do{
print "Enter your phone number [(123)555-1212]: ";
$phone = <>;
}while($phone !~ / ^(?(d{3})?)?(-)?(d{3})(-?)(d{4})$ /x ); #Check syntax of entry and make sure it fits what we want
$phone = "$1-$3-$5";
print "$phonen";
do{
print "Enter today's date: ";
$date = <>;
}while($date !~ /^([0-1])?([0-9])/([0-3])?([0-9])/([0-2])?([0-9])?([0-9]{2})$/); #Check syntax of entry and make sure it fits what we want
$_1 = $1-0;
$_3 = $3-0;
$date = "$_1$2/$_3$4/$5$6$7$8";
print "$daten";
do{
print "Enter the time: ";
$time = <>;
}while($time !~ /^([0-2])?([0-9]):([0-5])?([0-9]):([0-5])?([0-9])$/); #Check syntax of entry and make sure it fits what we want
$_1 = $1-0;
$_3 = $3-0;
$_5 = $5-0;
$time = "$_1$2:$_3$4:$_5$6";
print "$timen";
do{
print "Enter your email address: ";
$email = <>;
}
while($email =~ /(@.*@)|(..)|(@.)|(.@)|(^.)/ || $email !~ /^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$/); #Checks to make sure format is correct. Acceptable formats are things like: a.ferg@fergcorp.com, andrew@fergcorp.com, aferg@70.56.95.233, aferg@fergcorp.co.uk, etc
print "$emailn";
print "First name:$first_name
Last name: $last_name
Phone: $phonen
Date: $daten
Time: $timen
Email: $emailn";
open(DAT,">data.txt") || die("Cannot Open File");
print DAT "First name:$first_name
Last name: $last_name
Phone: $phonen
Date: $daten
Time: $timen
Email: $emailn";
close(DAT);
Go check it out. This is the first update in exactly two months. Sorry for the delay. I hope to release version 1.0 in a few more weeks.
I’ve been working on some cool stuff for the next release of all three of my plugins. I’m also working on a fourth plugin, so watch for that too. I’d just thought that I’d share that. I have a Calc II test tonight and then after that, I have to do my CAPA and Chem II homework, so don’t expect anything tonight, but maybe tomorrow or Thursday.
I’ve made Nightly builds of my plugins available. Builds are only listed if anything has changed (using an MD5 checksum). Builds usually work because they are exact copies of what I use on my site, but I make no guarantee whatsoever. Have at it, enjoy, and let me know if you don’t see something that you want, or if you see a better way to do something!