init
This commit is contained in:
commit
286f21b9d6
|
@ -0,0 +1,176 @@
|
|||
Listing PG1.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program converts decimal to dozenal.
|
||||
|
||||
#include <iostream> //import
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include "PG1.h"
|
||||
using namespace std;
|
||||
//main method runs at execution time
|
||||
int main (int argc, char **argv) { //signature
|
||||
int remainder = 0;
|
||||
cout << "Enter a number in decimal: ";
|
||||
string input;
|
||||
getline (cin,input);
|
||||
cout << input << " in dozenal is " << dozenal (input) << endl;
|
||||
system ("pause");
|
||||
return 0;
|
||||
}
|
||||
//this method divides by 12 and returns the quotient, to be stored
|
||||
string div12 (string dividend, int &remainder) {
|
||||
|
||||
int save = 0;
|
||||
int quotient = 0; // change this
|
||||
|
||||
for (int i = 0 ; i < dividend.length() ; i++){
|
||||
|
||||
if(save != 0){
|
||||
|
||||
save = (save * 10) + (dividend[i]-'0');
|
||||
quotient = (quotient * 10)+ save/12;
|
||||
save = save%12;
|
||||
|
||||
}
|
||||
else {
|
||||
save = dividend[i]-'0';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
remainder = save;
|
||||
|
||||
stringstream ss;
|
||||
ss << quotient;
|
||||
string str = ss.str();
|
||||
|
||||
|
||||
return str;
|
||||
}
|
||||
//this method calls the div12 method and stores each quotient as the next digit
|
||||
string dozenal (string input) {
|
||||
|
||||
string dozenalNumber = "";
|
||||
int remainder = 0;
|
||||
|
||||
while( div12(input,remainder) != "0" ) {
|
||||
|
||||
input = div12(input,remainder);
|
||||
|
||||
char remainderChar = remainder;
|
||||
|
||||
if(remainder == 10){
|
||||
remainderChar = 'A';
|
||||
}else if (remainder == 11){
|
||||
remainderChar = 'B';
|
||||
}else remainderChar = remainder+'0';
|
||||
|
||||
dozenalNumber = remainderChar+dozenalNumber;
|
||||
|
||||
}
|
||||
|
||||
|
||||
input = div12(input,remainder);
|
||||
|
||||
char remainderChar = remainder;
|
||||
|
||||
if(remainder == 10){
|
||||
remainderChar = 'A';
|
||||
}else if (remainder == 11){
|
||||
remainderChar = 'B';
|
||||
}else remainderChar = remainder+'0';
|
||||
|
||||
dozenalNumber = remainderChar+dozenalNumber;
|
||||
|
||||
return dozenalNumber;
|
||||
}
|
||||
|
||||
|
||||
Listing PG1.h...
|
||||
|
||||
//Ben Harris
|
||||
//this is the header file...
|
||||
|
||||
#ifndef _PG1_
|
||||
#define _PG1_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv); //prototype
|
||||
string div12 (string dividend, int &remainder);
|
||||
string dozenal (string input);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG1.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG1.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG1.exe
|
||||
PG1.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG1.exe
|
||||
/OUT:PG1.exe
|
||||
PG1.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-1.in"
|
||||
Enter a number in decimal: 1728 in dozenal is 1000
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-2.in"
|
||||
Enter a number in decimal: 1729 in dozenal is 1001
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-3.in"
|
||||
Enter a number in decimal: 1727 in dozenal is BBB
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-4.in"
|
||||
Enter a number in decimal: 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 in dozenal is 4A075B14
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-5.in"
|
||||
Enter a number in decimal: 828179745220145502584084235957368498016122811853894435464201864103254919330121223037770283296858019385573376 in dozenal is 7A361800
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-6.in"
|
||||
Enter a number in decimal: 828179745220145502584084235957368498016122811853894435464201864103254919330121223037770283296858019385573377 in dozenal is 7A361801
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-7.in"
|
||||
Enter a number in decimal: 0 in dozenal is 0
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-8.in"
|
||||
Enter a number in decimal: 828179745220145502584084235957368498016122811853894435464201864103254919330121223037770283296858019385573375 in dozenal is 7A3619%B
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Answers are wrong because you have to do this in strings. You can't
|
||||
store the quotient as an int! First write a division method to divide a
|
||||
long string by 12, putting the quotient in another long string.
|
||||
|
||||
10/50.
|
||||
|
||||
Fix by Monday 15 September 2014.
|
|
@ -0,0 +1,168 @@
|
|||
Listing PG1.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program converts decimal to dozenal.
|
||||
|
||||
#include <iostream> //import
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include "PG1.h"
|
||||
using namespace std;
|
||||
|
||||
//main method runs at execution time
|
||||
int main (int argc, char **argv) { //signature
|
||||
cout << "Enter a number in decimal: ";
|
||||
string input;
|
||||
getline (cin,input);
|
||||
cout << input << " in dozenal is " << dozenal (input) << endl;
|
||||
system ("pause");
|
||||
return 0;
|
||||
}
|
||||
//this method divides by 12 and returns the quotient, to be stored
|
||||
string div12 (string dividend, int &remainder) {
|
||||
|
||||
int save = 0;
|
||||
string quotient = "";
|
||||
|
||||
for (int i = 0 ; i < dividend.length() ; i++){
|
||||
|
||||
if(save != 0){
|
||||
|
||||
save = (save * 10) + (dividend[i]-'0');
|
||||
int j = save/12; //divide by 12
|
||||
char c = j+'0'; //convert to char
|
||||
quotient = quotient+c;
|
||||
save = save%12;
|
||||
|
||||
}
|
||||
else {
|
||||
save = dividend[i]-'0';
|
||||
quotient += '0';
|
||||
}
|
||||
}
|
||||
remainder = save;
|
||||
return quotient;
|
||||
}
|
||||
//this method calls the div12 method and stores each remainders as the next digit
|
||||
string dozenal (string input) {
|
||||
|
||||
string dozenalNumber = "";
|
||||
int remainder = 0;
|
||||
|
||||
while( atoi(div12(input,remainder).c_str()) != 0 ) {
|
||||
|
||||
input = div12(input,remainder);
|
||||
|
||||
char remainderChar;
|
||||
|
||||
if(remainder == 10){
|
||||
remainderChar = 'A';
|
||||
}else if (remainder == 11){
|
||||
remainderChar = 'B';
|
||||
}else remainderChar = remainder+'0';
|
||||
|
||||
dozenalNumber = remainderChar+dozenalNumber;
|
||||
|
||||
}
|
||||
|
||||
|
||||
input = div12(input,remainder);
|
||||
|
||||
char remainderChar = remainder;
|
||||
|
||||
if(remainder == 10){
|
||||
remainderChar = 'A';
|
||||
}else if (remainder == 11){
|
||||
remainderChar = 'B';
|
||||
}else remainderChar = remainder+'0';
|
||||
|
||||
dozenalNumber = remainderChar+dozenalNumber;
|
||||
|
||||
return dozenalNumber;
|
||||
}
|
||||
|
||||
|
||||
Listing PG1.h...
|
||||
|
||||
//Ben Harris
|
||||
//this is the header file...
|
||||
|
||||
#ifndef _PG1_
|
||||
#define _PG1_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv); //prototype
|
||||
string div12 (string dividend, int &remainder);
|
||||
string dozenal (string input);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG1.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG1.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG1.exe
|
||||
PG1.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG1.exe
|
||||
/OUT:PG1.exe
|
||||
PG1.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-1.in"
|
||||
Enter a number in decimal: 1728 in dozenal is 1000
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-2.in"
|
||||
Enter a number in decimal: 1729 in dozenal is 1001
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-3.in"
|
||||
Enter a number in decimal: 1727 in dozenal is BBB
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-4.in"
|
||||
Enter a number in decimal: 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 in dozenal is 11626240A2A9A68B90086BB42431626701401797184068A243B6A038A8465B48641A615192A2141B2079125A247896551826247B30A702664818943A268377699237A78A151BBA632B56394A5A0850B7B6A8A62B8533ABB07904A7B0687741B266917257836B09625367341624B9B00281B9608134B6465130A43AB0695BA9647B7959A1B7973473B689539BAB49897B3227971023891953739A49938B577780452155B91B4074B938934159B889330686BA823470770774330B8B60469B9BAB629812B19278A95A7BBA58AAA527208119768B545408560B41A922A69045A712A7A5054
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-5.in"
|
||||
Enter a number in decimal: 828179745220145502584084235957368498016122811853894435464201864103254919330121223037770283296858019385573376 in dozenal is 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-6.in"
|
||||
Enter a number in decimal: 828179745220145502584084235957368498016122811853894435464201864103254919330121223037770283296858019385573377 in dozenal is 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-7.in"
|
||||
Enter a number in decimal: 0 in dozenal is 0
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG1.exe < "C:\Grading\Classes\CS201-01-14F\PG1\PG1-8.in"
|
||||
Enter a number in decimal: 828179745220145502584084235957368498016122811853894435464201864103254919330121223037770283296858019385573375 in dozenal is BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
I probably wouldn't use atoi at all. It may not always correctly end
|
||||
the loop, but you're getting the right answers.
|
||||
|
||||
50/50.
|
|
@ -0,0 +1,81 @@
|
|||
//Ben Harris
|
||||
//This program converts decimal to dozenal.
|
||||
|
||||
#include <iostream> //import
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include "PG1.h"
|
||||
using namespace std;
|
||||
|
||||
//main method runs at execution time
|
||||
int main (int argc, char **argv) { //signature
|
||||
cout << "Enter a number in decimal: ";
|
||||
string input;
|
||||
getline (cin,input);
|
||||
cout << input << " in dozenal is " << dozenal (input) << endl;
|
||||
system ("pause");
|
||||
return 0;
|
||||
}
|
||||
//this method divides by 12 and returns the quotient, to be stored
|
||||
string div12 (string dividend, int &remainder) {
|
||||
|
||||
int save = 0;
|
||||
string quotient = "";
|
||||
|
||||
for (int i = 0 ; i < dividend.length() ; i++){
|
||||
|
||||
if(save != 0){
|
||||
|
||||
save = (save * 10) + (dividend[i]-'0');
|
||||
int j = save/12; //divide by 12
|
||||
char c = j+'0'; //convert to char
|
||||
quotient = quotient+c;
|
||||
save = save%12;
|
||||
|
||||
}
|
||||
else {
|
||||
save = dividend[i]-'0';
|
||||
quotient += '0';
|
||||
}
|
||||
}
|
||||
remainder = save;
|
||||
return quotient;
|
||||
}
|
||||
//this method calls the div12 method and stores each remainders as the next digit
|
||||
string dozenal (string input) {
|
||||
|
||||
string dozenalNumber = "";
|
||||
int remainder = 0;
|
||||
|
||||
while( atoi(div12(input,remainder).c_str()) != 0 ) {
|
||||
|
||||
input = div12(input,remainder);
|
||||
|
||||
char remainderChar;
|
||||
|
||||
if(remainder == 10){
|
||||
remainderChar = 'A';
|
||||
}else if (remainder == 11){
|
||||
remainderChar = 'B';
|
||||
}else remainderChar = remainder+'0';
|
||||
|
||||
dozenalNumber = remainderChar+dozenalNumber;
|
||||
|
||||
}
|
||||
|
||||
|
||||
input = div12(input,remainder);
|
||||
|
||||
char remainderChar = remainder;
|
||||
|
||||
if(remainder == 10){
|
||||
remainderChar = 'A';
|
||||
}else if (remainder == 11){
|
||||
remainderChar = 'B';
|
||||
}else remainderChar = remainder+'0';
|
||||
|
||||
dozenalNumber = remainderChar+dozenalNumber;
|
||||
|
||||
return dozenalNumber;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
//Ben Harris
|
||||
//this is the header file...
|
||||
|
||||
#ifndef _PG1_
|
||||
#define _PG1_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv); //prototype
|
||||
string div12 (string dividend, int &remainder);
|
||||
string dozenal (string input);
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,388 @@
|
|||
Listing PG2.cpp...
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "time.h"
|
||||
#include "PG2.h"
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv){
|
||||
srand((unsigned)time(0));
|
||||
int *deck = new int[52];
|
||||
for(int i=0;i<52;i++){
|
||||
deck[i]=i;
|
||||
}
|
||||
shuffle(deck);
|
||||
int *hand = new int[5];
|
||||
for(int j = 0; j<10;j++){//deals ten hands
|
||||
|
||||
int foundcount =0;
|
||||
int rankfound =-1;
|
||||
int secondarycount=0;
|
||||
for(int i = j*5;i<j*5+5;i++){//process, check each hand for duplicates
|
||||
int rank = deck[i]/4;
|
||||
cout << getRankName(deck[i]) << " of " << getSuitName(deck[i]) << endl;
|
||||
|
||||
if(inArray(hand,rank,i)){
|
||||
if(rank!=rankfound && i != 0)secondarycount++;
|
||||
else {
|
||||
rankfound=rank;
|
||||
foundcount++;
|
||||
|
||||
}
|
||||
}else{
|
||||
//hand[i] = (deck[i])/4;
|
||||
}
|
||||
}
|
||||
cout<<endl;
|
||||
if(foundcount>0)cout<<"ONE PAIR";
|
||||
else if(foundcount>1)cout<<"THREE OF A KIND";
|
||||
else if(foundcount>2)cout<<"FOUR OF A KIND";
|
||||
|
||||
cout<<foundcount<<endl;
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
system("pause");
|
||||
delete[] deck;
|
||||
return 0;
|
||||
}
|
||||
string getSuitName(int card){
|
||||
string suits[4] = {"Hearts","Clubs","Diamonds","Spades"};
|
||||
return suits[card%4];
|
||||
}
|
||||
string getRankName(int card){
|
||||
string ranks[13] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
|
||||
return ranks[card/4];
|
||||
}
|
||||
void swapTwo(int a, int b,int *d){
|
||||
d[b] = d[a]+d[b];
|
||||
d[a] = d[b]-d[a];
|
||||
d[b] = d[b]-d[a];
|
||||
}
|
||||
void shuffle(int *d){
|
||||
for(int i = 0; i<52;i++){
|
||||
if(i==0)continue;
|
||||
int r = rand()%i;
|
||||
swapTwo(i,r,d);
|
||||
}
|
||||
}
|
||||
bool inArray(int *d,int toinsert, int sz){
|
||||
for(int i = 0; i<sz;i++){
|
||||
if(toinsert == d[i])return true;
|
||||
}return false;
|
||||
}
|
||||
|
||||
|
||||
Listing PG2.h...
|
||||
|
||||
#ifndef _PG2_
|
||||
#define _PG2_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv);
|
||||
string getSuitName(int card);
|
||||
string getRankName(int card);
|
||||
void swapTwo(int a, int b,int *d);
|
||||
void shuffle(int *d);
|
||||
bool inArray(int *d,int toinsert, int sz);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG2.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG2.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG2.exe
|
||||
PG2.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG2.exe
|
||||
/OUT:PG2.exe
|
||||
PG2.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Jack of Spades
|
||||
Five of Diamonds
|
||||
King of Hearts
|
||||
Eight of Hearts
|
||||
Five of Hearts
|
||||
|
||||
0
|
||||
|
||||
King of Clubs
|
||||
Six of Diamonds
|
||||
Eight of Diamonds
|
||||
Four of Spades
|
||||
Six of Spades
|
||||
|
||||
0
|
||||
|
||||
Nine of Spades
|
||||
Jack of Hearts
|
||||
Ace of Spades
|
||||
Jack of Diamonds
|
||||
Nine of Clubs
|
||||
|
||||
0
|
||||
|
||||
Ten of Hearts
|
||||
Three of Clubs
|
||||
Jack of Clubs
|
||||
Ten of Diamonds
|
||||
King of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Queen of Hearts
|
||||
Nine of Hearts
|
||||
Ace of Clubs
|
||||
Queen of Diamonds
|
||||
Six of Hearts
|
||||
|
||||
0
|
||||
|
||||
Five of Spades
|
||||
Four of Diamonds
|
||||
Ten of Clubs
|
||||
Two of Clubs
|
||||
Five of Clubs
|
||||
|
||||
0
|
||||
|
||||
Queen of Clubs
|
||||
Three of Hearts
|
||||
Seven of Spades
|
||||
Three of Diamonds
|
||||
King of Spades
|
||||
|
||||
0
|
||||
|
||||
Four of Hearts
|
||||
Ace of Hearts
|
||||
Seven of Clubs
|
||||
Seven of Diamonds
|
||||
Two of Spades
|
||||
|
||||
0
|
||||
|
||||
Eight of Clubs
|
||||
Eight of Spades
|
||||
Four of Clubs
|
||||
Seven of Hearts
|
||||
Ace of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Ten of Spades
|
||||
Three of Spades
|
||||
Two of Hearts
|
||||
Two of Diamonds
|
||||
Nine of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>tart /wait timeout 2
|
||||
'tart' is not recognized as an internal or external command,
|
||||
operable program or batch file.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Jack of Spades
|
||||
Five of Diamonds
|
||||
King of Hearts
|
||||
Eight of Hearts
|
||||
Five of Hearts
|
||||
|
||||
0
|
||||
|
||||
King of Clubs
|
||||
Six of Diamonds
|
||||
Eight of Diamonds
|
||||
Four of Spades
|
||||
Six of Spades
|
||||
|
||||
0
|
||||
|
||||
Nine of Spades
|
||||
Jack of Hearts
|
||||
Ace of Spades
|
||||
Jack of Diamonds
|
||||
Nine of Clubs
|
||||
|
||||
0
|
||||
|
||||
Ten of Hearts
|
||||
Three of Clubs
|
||||
Jack of Clubs
|
||||
Ten of Diamonds
|
||||
King of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Queen of Hearts
|
||||
Nine of Hearts
|
||||
Ace of Clubs
|
||||
Queen of Diamonds
|
||||
Six of Hearts
|
||||
|
||||
0
|
||||
|
||||
Five of Spades
|
||||
Four of Diamonds
|
||||
Ten of Clubs
|
||||
Two of Clubs
|
||||
Five of Clubs
|
||||
|
||||
0
|
||||
|
||||
Queen of Clubs
|
||||
Three of Hearts
|
||||
Seven of Spades
|
||||
Three of Diamonds
|
||||
King of Spades
|
||||
|
||||
0
|
||||
|
||||
Four of Hearts
|
||||
Ace of Hearts
|
||||
Seven of Clubs
|
||||
Seven of Diamonds
|
||||
Two of Spades
|
||||
|
||||
0
|
||||
|
||||
Eight of Clubs
|
||||
Eight of Spades
|
||||
Four of Clubs
|
||||
Seven of Hearts
|
||||
Ace of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Ten of Spades
|
||||
Three of Spades
|
||||
Two of Hearts
|
||||
Two of Diamonds
|
||||
Nine of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>tart /wait timeout 2
|
||||
'tart' is not recognized as an internal or external command,
|
||||
operable program or batch file.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Jack of Spades
|
||||
Five of Diamonds
|
||||
King of Hearts
|
||||
Eight of Hearts
|
||||
Five of Hearts
|
||||
|
||||
0
|
||||
|
||||
King of Clubs
|
||||
Six of Diamonds
|
||||
Eight of Diamonds
|
||||
Four of Spades
|
||||
Six of Spades
|
||||
|
||||
0
|
||||
|
||||
Nine of Spades
|
||||
Jack of Hearts
|
||||
Ace of Spades
|
||||
Jack of Diamonds
|
||||
Nine of Clubs
|
||||
|
||||
0
|
||||
|
||||
Ten of Hearts
|
||||
Three of Clubs
|
||||
Jack of Clubs
|
||||
Ten of Diamonds
|
||||
King of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Queen of Hearts
|
||||
Nine of Hearts
|
||||
Ace of Clubs
|
||||
Queen of Diamonds
|
||||
Six of Hearts
|
||||
|
||||
0
|
||||
|
||||
Five of Spades
|
||||
Four of Diamonds
|
||||
Ten of Clubs
|
||||
Two of Clubs
|
||||
Five of Clubs
|
||||
|
||||
0
|
||||
|
||||
Queen of Clubs
|
||||
Three of Hearts
|
||||
Seven of Spades
|
||||
Three of Diamonds
|
||||
King of Spades
|
||||
|
||||
0
|
||||
|
||||
Four of Hearts
|
||||
Ace of Hearts
|
||||
Seven of Clubs
|
||||
Seven of Diamonds
|
||||
Two of Spades
|
||||
|
||||
0
|
||||
|
||||
Eight of Clubs
|
||||
Eight of Spades
|
||||
Four of Clubs
|
||||
Seven of Hearts
|
||||
Ace of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Ten of Spades
|
||||
Three of Spades
|
||||
Two of Hearts
|
||||
Two of Diamonds
|
||||
Nine of Diamonds
|
||||
|
||||
0
|
||||
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>xit
|
||||
'xit' is not recognized as an internal or external command,
|
||||
operable program or batch file.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
|
||||
Not rating hands yet...and please get rid of system ("pause").
|
||||
|
||||
10/50.
|
||||
|
||||
Fix by Friday 26 September 2014.
|
|
@ -0,0 +1,396 @@
|
|||
Listing PG2.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program shuffles a deck of cards, deals ten hands of five,
|
||||
//then checks for poker hands excluding straight, flush, and straight flush
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "time.h"
|
||||
#include "PG2.h"
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv){//main method
|
||||
//initialize deck and randomize it
|
||||
srand((unsigned)time(0));
|
||||
int *deck = new int[52];
|
||||
for(int i=0;i<52;i++){
|
||||
deck[i]=i;
|
||||
}
|
||||
shuffle(deck);
|
||||
|
||||
//start to deal the hands
|
||||
for(int j = 0; j<10;j++){//deals ten hands
|
||||
|
||||
int rankcount[13] = {0};
|
||||
for(int i = j*5;i<j*5+5;i++){//print, then check each hand for duplicates
|
||||
rankcount[deck[i]/4]++;
|
||||
cout << getRankName(deck[i]) << " of " << getSuitName(deck[i]) << endl;
|
||||
|
||||
}
|
||||
//for(int q = 0; q<13;q++){
|
||||
// cout<<rankcount[q]<<" ";
|
||||
//}
|
||||
//
|
||||
//cout<<maximum(rankcount,13)-1<<endl;
|
||||
cout<<endl;
|
||||
if(inArray(rankcount,2,13))cout<<"ONE PAIR";
|
||||
else if(inArray(rankcount,3,13))cout<<"THREE OF A KIND";
|
||||
else if(inArray(rankcount,4,13))cout<<"FOUR OF A KIND";
|
||||
else if(inArray(rankcount,2,13)&&inArray(rankcount,2,13))cout<<"TWO PAIR";
|
||||
else if(inArray(rankcount,2,13)&&inArray(rankcount,3,13))cout<<"FULL HOUSE";
|
||||
else cout<<"NOTHING";
|
||||
cout<<endl;
|
||||
cout<<endl;
|
||||
|
||||
}
|
||||
//remove systempause before turning in
|
||||
//system("pause");
|
||||
delete deck;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string getSuitName(int card){//returns the suit of the input card
|
||||
string suits[4] = {"Hearts","Clubs","Diamonds","Spades"};
|
||||
return suits[card%4];
|
||||
}
|
||||
|
||||
string getRankName(int card){//returns the rank of the input card
|
||||
string ranks[13] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
|
||||
return ranks[card/4];
|
||||
}
|
||||
|
||||
void swapTwo(int a, int b,int *d){//swaps two ints a and b within an array d
|
||||
d[b] = d[a]+d[b];
|
||||
d[a] = d[b]-d[a];
|
||||
d[b] = d[b]-d[a];
|
||||
}
|
||||
|
||||
void shuffle(int *d){//swaps each element with a random index at or before the current index, shuffling the deck
|
||||
for(int i = 0; i<52;i++){
|
||||
if(i==0)continue;
|
||||
int r = rand()%i;
|
||||
swapTwo(i,r,d);
|
||||
}
|
||||
}
|
||||
|
||||
bool inArray(int *d,int toinsert, int sz){//returns true if toinsert is found in array d; checks only up to size
|
||||
for(int i = 0; i<sz;i++){
|
||||
if(toinsert == d[i])return true;
|
||||
}return false;
|
||||
}
|
||||
int maximum(int *a,int sz){
|
||||
int max = a[0];
|
||||
for(int i = 1;i<sz;i++){
|
||||
if(a[i]>max)max=a[i];
|
||||
}return max;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Listing PG2.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for PG2
|
||||
|
||||
#ifndef _PG2_
|
||||
#define _PG2_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv);
|
||||
string getSuitName(int card);
|
||||
string getRankName(int card);
|
||||
void swapTwo(int a, int b,int *d);
|
||||
void shuffle(int *d);
|
||||
bool inArray(int *d,int toinsert, int sz);
|
||||
int maximum(int *a,int sz);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG2.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG2.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG2.exe
|
||||
PG2.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG2.exe
|
||||
/OUT:PG2.exe
|
||||
PG2.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Jack of Hearts
|
||||
Seven of Diamonds
|
||||
Four of Clubs
|
||||
Five of Clubs
|
||||
Seven of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Two of Diamonds
|
||||
Nine of Clubs
|
||||
Two of Hearts
|
||||
Six of Spades
|
||||
Ten of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Jack of Spades
|
||||
King of Spades
|
||||
King of Hearts
|
||||
Ten of Diamonds
|
||||
Seven of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Eight of Spades
|
||||
Six of Diamonds
|
||||
Three of Spades
|
||||
Four of Diamonds
|
||||
Five of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
King of Diamonds
|
||||
Three of Clubs
|
||||
Ten of Clubs
|
||||
Ace of Spades
|
||||
Queen of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Nine of Diamonds
|
||||
Ace of Clubs
|
||||
Ten of Spades
|
||||
Queen of Hearts
|
||||
Nine of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Six of Hearts
|
||||
Jack of Diamonds
|
||||
Three of Diamonds
|
||||
Eight of Diamonds
|
||||
Eight of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Six of Clubs
|
||||
Two of Spades
|
||||
Four of Hearts
|
||||
Seven of Spades
|
||||
Four of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Spades
|
||||
Jack of Clubs
|
||||
King of Clubs
|
||||
Ace of Diamonds
|
||||
Queen of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Three of Hearts
|
||||
Two of Clubs
|
||||
Ace of Hearts
|
||||
Nine of Spades
|
||||
Five of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>start /wait timeout 2
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Ten of Clubs
|
||||
Eight of Clubs
|
||||
Six of Spades
|
||||
Jack of Spades
|
||||
Queen of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
Queen of Clubs
|
||||
Two of Spades
|
||||
Seven of Hearts
|
||||
Seven of Diamonds
|
||||
Nine of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Ten of Hearts
|
||||
Two of Clubs
|
||||
King of Clubs
|
||||
Five of Hearts
|
||||
King of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Jack of Hearts
|
||||
Three of Hearts
|
||||
Queen of Diamonds
|
||||
Four of Spades
|
||||
Three of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Three of Diamonds
|
||||
Jack of Diamonds
|
||||
Five of Spades
|
||||
Five of Diamonds
|
||||
Ten of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Six of Hearts
|
||||
Ten of Diamonds
|
||||
Seven of Spades
|
||||
King of Diamonds
|
||||
Eight of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Nine of Spades
|
||||
Ace of Spades
|
||||
Ace of Clubs
|
||||
King of Spades
|
||||
Queen of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Six of Diamonds
|
||||
Six of Clubs
|
||||
Five of Clubs
|
||||
Nine of Hearts
|
||||
Three of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Four of Diamonds
|
||||
Eight of Diamonds
|
||||
Two of Hearts
|
||||
Two of Diamonds
|
||||
Four of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Nine of Clubs
|
||||
Jack of Clubs
|
||||
Ace of Diamonds
|
||||
Eight of Spades
|
||||
Four of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>start /wait timeout 2
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Queen of Hearts
|
||||
Nine of Diamonds
|
||||
Two of Clubs
|
||||
Eight of Spades
|
||||
King of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Six of Hearts
|
||||
Seven of Diamonds
|
||||
Six of Clubs
|
||||
Four of Clubs
|
||||
Nine of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Spades
|
||||
Five of Diamonds
|
||||
Ten of Clubs
|
||||
Five of Hearts
|
||||
Eight of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Eight of Diamonds
|
||||
Two of Diamonds
|
||||
King of Spades
|
||||
Nine of Spades
|
||||
Ace of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
Four of Diamonds
|
||||
Ten of Spades
|
||||
Five of Spades
|
||||
Queen of Diamonds
|
||||
Seven of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Three of Diamonds
|
||||
Six of Spades
|
||||
Queen of Clubs
|
||||
King of Clubs
|
||||
Eight of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Jack of Hearts
|
||||
Four of Spades
|
||||
Three of Spades
|
||||
Jack of Clubs
|
||||
Five of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Three of Clubs
|
||||
King of Diamonds
|
||||
Ten of Hearts
|
||||
Ace of Clubs
|
||||
Two of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Ace of Spades
|
||||
Jack of Spades
|
||||
Jack of Diamonds
|
||||
Two of Spades
|
||||
Ten of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Seven of Hearts
|
||||
Seven of Spades
|
||||
Ace of Hearts
|
||||
Nine of Clubs
|
||||
Six of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Not properly shuffling (not allowing a card to swap with itself). Not
|
||||
properly checking for anything beyond a pair.
|
||||
|
||||
30/50.
|
||||
|
||||
Fix by Friday 3 October 2014.
|
|
@ -0,0 +1,387 @@
|
|||
Listing PG2.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program shuffles a deck of cards, deals ten hands of five,
|
||||
//then checks for poker hands excluding straight, flush, and straight flush
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "time.h"
|
||||
#include "PG2.h"
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv){//main method
|
||||
//initialize deck and randomize it
|
||||
srand((unsigned)time(0));
|
||||
int *deck = new int[52];
|
||||
for(int i=0;i<52;i++){
|
||||
deck[i]=i;
|
||||
}
|
||||
shuffle(deck);
|
||||
|
||||
//start to deal the hands
|
||||
for(int j = 0; j<10;j++){//deals ten hands
|
||||
|
||||
int rankcount[13] = {0};
|
||||
for(int i = j*5;i<j*5+5;i++){//print, then check each hand for duplicates
|
||||
rankcount[deck[i]/4]++;
|
||||
cout << getRankName(deck[i]) << " of " << getSuitName(deck[i]) << endl;
|
||||
|
||||
}
|
||||
cout<<endl;
|
||||
if (inArray(rankcount, 2, 13) && inArray(rankcount, 3, 13))cout << "FULL HOUSE";
|
||||
else if (dupeinarray(rankcount, 2, 13))cout << "TWO PAIR";
|
||||
else if (inArray(rankcount, 4, 13))cout << "FOUR OF A KIND";
|
||||
else if (inArray(rankcount, 3, 13))cout << "THREE OF A KIND";
|
||||
else if(inArray(rankcount,2,13))cout<<"ONE PAIR";
|
||||
else cout<<"NOTHING";
|
||||
cout<<endl;
|
||||
cout<<endl;
|
||||
|
||||
}
|
||||
//remove systempause before turning in
|
||||
//system("pause");
|
||||
delete deck;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string getSuitName(int card){//returns the suit of the input card
|
||||
string suits[4] = {"Hearts","Clubs","Diamonds","Spades"};
|
||||
return suits[card%4];
|
||||
}
|
||||
string getRankName(int card){//returns the rank of the input card
|
||||
string ranks[13] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
|
||||
return ranks[card/4];
|
||||
}
|
||||
void swapTwo(int a, int b,int *d){//swaps two ints a and b within an array d
|
||||
d[b] = d[a]+d[b];
|
||||
d[a] = d[b]-d[a];
|
||||
d[b] = d[b]-d[a];
|
||||
}
|
||||
void shuffle(int *d){//swaps each element with a random index at or before the current index, shuffling the deck
|
||||
for(int i = 1; i<52;i++){
|
||||
int r = rand()%i;
|
||||
swapTwo(i,r,d);
|
||||
}
|
||||
}
|
||||
bool inArray(int *d,int toinsert, int sz){//returns true if toinsert is found in array d; checks only up to size
|
||||
for(int i = 0; i<sz;i++){
|
||||
if(toinsert == d[i])return true;
|
||||
}return false;
|
||||
}
|
||||
bool dupeinarray(int *d, int dupetocheck, int sz){//checks for duplicate dupetocheck in array d
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < sz; i++){
|
||||
if (d[i] == dupetocheck)cnt++;
|
||||
}
|
||||
if (cnt > 1)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
|
||||
Listing PG2.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for PG2
|
||||
|
||||
#ifndef _PG2_
|
||||
#define _PG2_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv);
|
||||
string getSuitName(int card);
|
||||
string getRankName(int card);
|
||||
void swapTwo(int a, int b,int *d);
|
||||
void shuffle(int *d);
|
||||
bool inArray(int *d,int toinsert, int sz);
|
||||
int maximum(int *a,int sz);
|
||||
bool dupeinarray(int *d, int dupetocheck, int sz);
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG2.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG2.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG2.exe
|
||||
PG2.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG2.exe
|
||||
/OUT:PG2.exe
|
||||
PG2.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Seven of Spades
|
||||
Ace of Hearts
|
||||
Five of Spades
|
||||
Ten of Diamonds
|
||||
Ace of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Eight of Clubs
|
||||
Two of Hearts
|
||||
Four of Clubs
|
||||
Jack of Diamonds
|
||||
Three of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Ten of Clubs
|
||||
Four of Hearts
|
||||
Two of Diamonds
|
||||
Ten of Hearts
|
||||
Jack of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Ace of Diamonds
|
||||
King of Hearts
|
||||
Jack of Hearts
|
||||
Nine of Hearts
|
||||
Ace of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Clubs
|
||||
Three of Clubs
|
||||
Eight of Diamonds
|
||||
Eight of Hearts
|
||||
Two of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Six of Spades
|
||||
Queen of Spades
|
||||
Four of Diamonds
|
||||
Five of Diamonds
|
||||
Nine of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
Jack of Spades
|
||||
Seven of Hearts
|
||||
Four of Spades
|
||||
Seven of Clubs
|
||||
King of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Five of Clubs
|
||||
Seven of Diamonds
|
||||
Two of Clubs
|
||||
King of Diamonds
|
||||
Three of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
King of Spades
|
||||
Nine of Diamonds
|
||||
Six of Diamonds
|
||||
Five of Hearts
|
||||
Nine of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Diamonds
|
||||
Eight of Spades
|
||||
Six of Hearts
|
||||
Ten of Spades
|
||||
Six of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>start /wait timeout 2
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Three of Spades
|
||||
Four of Clubs
|
||||
Queen of Clubs
|
||||
Six of Hearts
|
||||
King of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Nine of Hearts
|
||||
Seven of Clubs
|
||||
Three of Clubs
|
||||
Five of Spades
|
||||
Five of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Ace of Clubs
|
||||
Five of Clubs
|
||||
Nine of Diamonds
|
||||
Eight of Hearts
|
||||
Ten of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Ten of Diamonds
|
||||
Four of Hearts
|
||||
Nine of Spades
|
||||
King of Hearts
|
||||
Jack of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Ten of Clubs
|
||||
Nine of Clubs
|
||||
Three of Hearts
|
||||
Jack of Spades
|
||||
King of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
Ten of Spades
|
||||
Five of Diamonds
|
||||
Four of Spades
|
||||
Ace of Spades
|
||||
Queen of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Three of Diamonds
|
||||
Six of Diamonds
|
||||
Jack of Hearts
|
||||
Ace of Hearts
|
||||
Queen of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
Seven of Hearts
|
||||
Jack of Diamonds
|
||||
Ace of Diamonds
|
||||
Two of Spades
|
||||
Seven of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Diamonds
|
||||
Eight of Spades
|
||||
Two of Hearts
|
||||
Seven of Spades
|
||||
Eight of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
King of Diamonds
|
||||
Six of Clubs
|
||||
Four of Diamonds
|
||||
Six of Spades
|
||||
Two of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>start /wait timeout 2
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Jack of Diamonds
|
||||
Ace of Hearts
|
||||
Eight of Hearts
|
||||
Queen of Diamonds
|
||||
Nine of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
Three of Diamonds
|
||||
Eight of Spades
|
||||
King of Diamonds
|
||||
King of Spades
|
||||
Ace of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Four of Clubs
|
||||
Seven of Hearts
|
||||
Two of Diamonds
|
||||
Seven of Diamonds
|
||||
Seven of Spades
|
||||
|
||||
THREE OF A KIND
|
||||
|
||||
Three of Clubs
|
||||
Ace of Clubs
|
||||
Four of Spades
|
||||
Two of Spades
|
||||
Ten of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
King of Clubs
|
||||
Five of Clubs
|
||||
Ten of Spades
|
||||
Eight of Diamonds
|
||||
Queen of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Three of Hearts
|
||||
Six of Diamonds
|
||||
Eight of Clubs
|
||||
Nine of Clubs
|
||||
Two of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Four of Diamonds
|
||||
Ten of Clubs
|
||||
Five of Diamonds
|
||||
Nine of Diamonds
|
||||
Two of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Queen of Hearts
|
||||
Ace of Spades
|
||||
Six of Spades
|
||||
Jack of Clubs
|
||||
Queen of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Six of Clubs
|
||||
Three of Spades
|
||||
Seven of Clubs
|
||||
Six of Hearts
|
||||
Jack of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
King of Hearts
|
||||
Five of Spades
|
||||
Jack of Spades
|
||||
Five of Hearts
|
||||
Ten of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Still not properly shuffling (cards need to be able to swap with
|
||||
themselves).
|
||||
|
||||
35/50.
|
||||
|
||||
Fix by Monday 6 October 2014.
|
|
@ -0,0 +1,396 @@
|
|||
Listing PG2.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program shuffles a deck of cards, deals ten hands of five,
|
||||
//then checks for poker hands excluding straight, flush, and straight flush
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "time.h"
|
||||
#include "PG2.h"
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv){//main method
|
||||
//initialize deck and randomize it
|
||||
srand((unsigned)time(0));
|
||||
int *deck = new int[52];
|
||||
for(int i=0;i<52;i++){
|
||||
deck[i]=i;
|
||||
}
|
||||
shuffle(deck);
|
||||
|
||||
//start to deal the hands
|
||||
for(int j = 0; j<10;j++){//deals ten hands
|
||||
|
||||
int rankcount[13] = {0};
|
||||
for(int i = j*5;i<j*5+5;i++){//print, then check each hand for duplicates
|
||||
rankcount[deck[i]/4]++;
|
||||
cout << getRankName(deck[i]) << " of " << getSuitName(deck[i]) << endl;
|
||||
|
||||
}
|
||||
cout<<endl;
|
||||
if (inArray(rankcount, 2, 13) && inArray(rankcount, 3, 13))cout << "FULL HOUSE";
|
||||
else if (dupeinarray(rankcount, 2, 13))cout << "TWO PAIR";
|
||||
else if (inArray(rankcount, 4, 13))cout << "FOUR OF A KIND";
|
||||
else if (inArray(rankcount, 3, 13))cout << "THREE OF A KIND";
|
||||
else if(inArray(rankcount,2,13))cout<<"ONE PAIR";
|
||||
else cout<<"NOTHING";
|
||||
cout<<endl;
|
||||
cout<<endl;
|
||||
|
||||
}
|
||||
//remove systempause before turning in
|
||||
system("pause");
|
||||
delete deck;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string getSuitName(int card){//returns the suit of the input card
|
||||
string suits[4] = {"Hearts","Clubs","Diamonds","Spades"};
|
||||
return suits[card%4];
|
||||
}
|
||||
string getRankName(int card){//returns the rank of the input card
|
||||
string ranks[13] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
|
||||
return ranks[card/4];
|
||||
}
|
||||
void swapTwo(int a, int b,int *d){//swaps two ints a and b within an array d
|
||||
d[b] = d[a]+d[b];
|
||||
d[a] = d[b]-d[a];
|
||||
d[b] = d[b]-d[a];
|
||||
}
|
||||
void shuffle(int *d){//swaps each element with a random index at or before the current index, shuffling the deck
|
||||
for(int i = 1; i<52;i++){
|
||||
int r = rand()%(i+1);
|
||||
swapTwo(i,r,d);
|
||||
}
|
||||
}
|
||||
bool inArray(int *d,int toinsert, int sz){//returns true if toinsert is found in array d; checks only up to size
|
||||
for(int i = 0; i<sz;i++){
|
||||
if(toinsert == d[i])return true;
|
||||
}return false;
|
||||
}
|
||||
bool dupeinarray(int *d, int dupetocheck, int sz){//checks for duplicate dupetocheck in array d
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < sz; i++){
|
||||
if (d[i] == dupetocheck)cnt++;
|
||||
}
|
||||
if (cnt > 1)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
|
||||
Listing PG2.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for PG2
|
||||
|
||||
#ifndef _PG2_
|
||||
#define _PG2_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv);
|
||||
string getSuitName(int card);
|
||||
string getRankName(int card);
|
||||
void swapTwo(int a, int b,int *d);
|
||||
void shuffle(int *d);
|
||||
bool inArray(int *d,int toinsert, int sz);
|
||||
bool dupeinarray(int *d, int dupetocheck, int sz);
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG2.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG2.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG2.exe
|
||||
PG2.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG2.exe
|
||||
/OUT:PG2.exe
|
||||
PG2.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Six of Clubs
|
||||
Ace of Spades
|
||||
Five of Hearts
|
||||
Ten of Clubs
|
||||
Six of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
King of Hearts
|
||||
Two of Hearts
|
||||
Six of Diamonds
|
||||
Eight of Diamonds
|
||||
Seven of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
King of Clubs
|
||||
Four of Diamonds
|
||||
Seven of Diamonds
|
||||
Two of Hearts
|
||||
Jack of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Five of Clubs
|
||||
Queen of Clubs
|
||||
Nine of Spades
|
||||
Two of Diamonds
|
||||
Queen of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Five of Diamonds
|
||||
Nine of Clubs
|
||||
Three of Diamonds
|
||||
Five of Spades
|
||||
Ten of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Four of Spades
|
||||
Six of Spades
|
||||
Four of Hearts
|
||||
Eight of Clubs
|
||||
King of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Two of Spades
|
||||
Jack of Diamonds
|
||||
Ace of Diamonds
|
||||
Seven of Clubs
|
||||
Ace of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Hearts
|
||||
Three of Spades
|
||||
Seven of Hearts
|
||||
Eight of Hearts
|
||||
Ace of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Ten of Hearts
|
||||
Ten of Diamonds
|
||||
Three of Clubs
|
||||
Four of Clubs
|
||||
Three of Hearts
|
||||
|
||||
TWO PAIR
|
||||
|
||||
Queen of Diamonds
|
||||
Jack of Clubs
|
||||
Nine of Diamonds
|
||||
Eight of Spades
|
||||
Jack of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>tart /wait timeout 2
|
||||
'tart' is not recognized as an internal or external command,
|
||||
operable program or batch file.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Six of Clubs
|
||||
Ace of Spades
|
||||
Five of Hearts
|
||||
Ten of Clubs
|
||||
Six of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
King of Hearts
|
||||
Two of Hearts
|
||||
Six of Diamonds
|
||||
Eight of Diamonds
|
||||
Seven of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
King of Clubs
|
||||
Four of Diamonds
|
||||
Seven of Diamonds
|
||||
Two of Hearts
|
||||
Jack of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Five of Clubs
|
||||
Queen of Clubs
|
||||
Nine of Spades
|
||||
Two of Diamonds
|
||||
Queen of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Five of Diamonds
|
||||
Nine of Clubs
|
||||
Three of Diamonds
|
||||
Five of Spades
|
||||
Ten of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Four of Spades
|
||||
Six of Spades
|
||||
Four of Hearts
|
||||
Eight of Clubs
|
||||
King of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Two of Spades
|
||||
Jack of Diamonds
|
||||
Ace of Diamonds
|
||||
Seven of Clubs
|
||||
Ace of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Hearts
|
||||
Three of Spades
|
||||
Seven of Hearts
|
||||
Eight of Hearts
|
||||
Ace of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Ten of Hearts
|
||||
Ten of Diamonds
|
||||
Three of Clubs
|
||||
Four of Clubs
|
||||
Three of Hearts
|
||||
|
||||
TWO PAIR
|
||||
|
||||
Queen of Diamonds
|
||||
Jack of Clubs
|
||||
Nine of Diamonds
|
||||
Eight of Spades
|
||||
Jack of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>tart /wait timeout 2
|
||||
'tart' is not recognized as an internal or external command,
|
||||
operable program or batch file.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Six of Clubs
|
||||
Ace of Spades
|
||||
Five of Hearts
|
||||
Ten of Clubs
|
||||
Six of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
King of Hearts
|
||||
Two of Hearts
|
||||
Six of Diamonds
|
||||
Eight of Diamonds
|
||||
Seven of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
King of Clubs
|
||||
Four of Diamonds
|
||||
Seven of Diamonds
|
||||
Two of Hearts
|
||||
Jack of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Five of Clubs
|
||||
Queen of Clubs
|
||||
Nine of Spades
|
||||
Two of Diamonds
|
||||
Queen of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Five of Diamonds
|
||||
Nine of Clubs
|
||||
Three of Diamonds
|
||||
Five of Spades
|
||||
Ten of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Four of Spades
|
||||
Six of Spades
|
||||
Four of Hearts
|
||||
Eight of Clubs
|
||||
King of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Two of Spades
|
||||
Jack of Diamonds
|
||||
Ace of Diamonds
|
||||
Seven of Clubs
|
||||
Ace of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Hearts
|
||||
Three of Spades
|
||||
Seven of Hearts
|
||||
Eight of Hearts
|
||||
Ace of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Ten of Hearts
|
||||
Ten of Diamonds
|
||||
Three of Clubs
|
||||
Four of Clubs
|
||||
Three of Hearts
|
||||
|
||||
TWO PAIR
|
||||
|
||||
Queen of Diamonds
|
||||
Jack of Clubs
|
||||
Nine of Diamonds
|
||||
Eight of Spades
|
||||
Jack of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>xit
|
||||
'xit' is not recognized as an internal or external command,
|
||||
operable program or batch file.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
|
||||
Get rid of the system ("pause"). Also, use delete[] to delete an array.
|
||||
|
||||
35/50.
|
||||
|
||||
Fix by Wednesday 8 October 2014.
|
|
@ -0,0 +1,381 @@
|
|||
Listing PG2.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program shuffles a deck of cards, deals ten hands of five,
|
||||
//then checks for poker hands excluding straight, flush, and straight flush
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "time.h"
|
||||
#include "PG2.h"
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv){//main method
|
||||
//initialize deck and randomize it
|
||||
srand((unsigned)time(0));
|
||||
int *deck = new int[52];
|
||||
for(int i=0;i<52;i++){
|
||||
deck[i]=i;
|
||||
}
|
||||
shuffle(deck);
|
||||
|
||||
//start to deal the hands
|
||||
for(int j = 0; j<10;j++){//deals ten hands
|
||||
|
||||
int rankcount[13] = {0};
|
||||
for(int i = j*5;i<j*5+5;i++){//print, then check each hand for duplicates
|
||||
rankcount[deck[i]/4]++;
|
||||
cout << getRankName(deck[i]) << " of " << getSuitName(deck[i]) << endl;
|
||||
|
||||
}
|
||||
cout<<endl;
|
||||
if (inArray(rankcount, 2, 13) && inArray(rankcount, 3, 13))cout << "FULL HOUSE";
|
||||
else if (dupeinarray(rankcount, 2, 13))cout << "TWO PAIR";
|
||||
else if (inArray(rankcount, 4, 13))cout << "FOUR OF A KIND";
|
||||
else if (inArray(rankcount, 3, 13))cout << "THREE OF A KIND";
|
||||
else if(inArray(rankcount,2,13))cout<<"ONE PAIR";
|
||||
else cout<<"NOTHING";
|
||||
cout<<endl;
|
||||
cout<<endl;
|
||||
|
||||
}
|
||||
//remove systempause before turning in
|
||||
//system("pause");
|
||||
delete[] deck;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string getSuitName(int card){//returns the suit of the input card
|
||||
string suits[4] = {"Hearts","Clubs","Diamonds","Spades"};
|
||||
return suits[card%4];
|
||||
}
|
||||
string getRankName(int card){//returns the rank of the input card
|
||||
string ranks[13] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
|
||||
return ranks[card/4];
|
||||
}
|
||||
void swapTwo(int a, int b,int *d){//swaps two ints a and b within an array d
|
||||
d[b] = d[a]+d[b];
|
||||
d[a] = d[b]-d[a];
|
||||
d[b] = d[b]-d[a];
|
||||
}
|
||||
void shuffle(int *d){//swaps each element with a random index at or before the current index, shuffling the deck
|
||||
for(int i = 1; i<52;i++){
|
||||
int r = rand()%(i+1);
|
||||
swapTwo(i,r,d);
|
||||
}
|
||||
}
|
||||
bool inArray(int *d,int toinsert, int sz){//returns true if toinsert is found in array d; checks only up to size
|
||||
for(int i = 0; i<sz;i++){
|
||||
if(toinsert == d[i])return true;
|
||||
}return false;
|
||||
}
|
||||
bool dupeinarray(int *d, int dupetocheck, int sz){//checks for duplicate dupetocheck in array d
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < sz; i++){
|
||||
if (d[i] == dupetocheck)cnt++;
|
||||
}
|
||||
if (cnt > 1)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
|
||||
Listing PG2.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for PG2
|
||||
|
||||
#ifndef _PG2_
|
||||
#define _PG2_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv);
|
||||
string getSuitName(int card);
|
||||
string getRankName(int card);
|
||||
void swapTwo(int a, int b,int *d);
|
||||
void shuffle(int *d);
|
||||
bool inArray(int *d,int toinsert, int sz);
|
||||
bool dupeinarray(int *d, int dupetocheck, int sz);
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG2.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG2.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG2.exe
|
||||
PG2.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG2.exe
|
||||
/OUT:PG2.exe
|
||||
PG2.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Jack of Diamonds
|
||||
Four of Clubs
|
||||
Six of Clubs
|
||||
Two of Clubs
|
||||
Nine of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Ten of Clubs
|
||||
Four of Hearts
|
||||
Two of Spades
|
||||
Queen of Diamonds
|
||||
Queen of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Three of Clubs
|
||||
Ace of Diamonds
|
||||
Ten of Spades
|
||||
Ace of Spades
|
||||
Seven of Diamonds
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Seven of Hearts
|
||||
Three of Diamonds
|
||||
Ace of Clubs
|
||||
Five of Diamonds
|
||||
Eight of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
Seven of Spades
|
||||
Queen of Hearts
|
||||
Ace of Hearts
|
||||
Nine of Diamonds
|
||||
King of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
Nine of Hearts
|
||||
Ten of Diamonds
|
||||
Nine of Spades
|
||||
Jack of Hearts
|
||||
Two of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Eight of Clubs
|
||||
Ten of Hearts
|
||||
Two of Hearts
|
||||
King of Diamonds
|
||||
Six of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Four of Diamonds
|
||||
Three of Hearts
|
||||
Six of Spades
|
||||
Three of Spades
|
||||
King of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
King of Clubs
|
||||
Two of Hearts
|
||||
Six of Diamonds
|
||||
Five of Clubs
|
||||
Five of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Jack of Spades
|
||||
Eight of Hearts
|
||||
Eight of Spades
|
||||
Five of Spades
|
||||
Jack of Clubs
|
||||
|
||||
TWO PAIR
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>start /wait timeout 2
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Six of Clubs
|
||||
Ten of Hearts
|
||||
Nine of Clubs
|
||||
King of Hearts
|
||||
Ace of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Queen of Spades
|
||||
Six of Diamonds
|
||||
Jack of Hearts
|
||||
Seven of Hearts
|
||||
Eight of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Four of Hearts
|
||||
Two of Diamonds
|
||||
Four of Clubs
|
||||
Ace of Spades
|
||||
Five of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Jack of Diamonds
|
||||
Five of Hearts
|
||||
Eight of Spades
|
||||
Seven of Spades
|
||||
Jack of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Queen of Clubs
|
||||
King of Diamonds
|
||||
Five of Diamonds
|
||||
Eight of Clubs
|
||||
Two of Spades
|
||||
|
||||
NOTHING
|
||||
|
||||
Jack of Clubs
|
||||
Queen of Hearts
|
||||
Four of Diamonds
|
||||
Nine of Diamonds
|
||||
Ten of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Ace of Clubs
|
||||
Four of Spades
|
||||
Three of Spades
|
||||
Ten of Diamonds
|
||||
Seven of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
Two of Hearts
|
||||
Eight of Diamonds
|
||||
Six of Hearts
|
||||
King of Clubs
|
||||
Two of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Three of Diamonds
|
||||
Ace of Diamonds
|
||||
Three of Hearts
|
||||
King of Spades
|
||||
Five of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Three of Clubs
|
||||
Two of Clubs
|
||||
Nine of Hearts
|
||||
Ten of Spades
|
||||
Queen of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>start /wait timeout 2
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG2.exe
|
||||
Six of Spades
|
||||
Queen of Spades
|
||||
Ten of Hearts
|
||||
Five of Hearts
|
||||
King of Hearts
|
||||
|
||||
NOTHING
|
||||
|
||||
Seven of Clubs
|
||||
Three of Clubs
|
||||
Four of Spades
|
||||
Jack of Hearts
|
||||
King of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
Ace of Clubs
|
||||
Eight of Diamonds
|
||||
Seven of Diamonds
|
||||
Ten of Diamonds
|
||||
Ace of Spades
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Nine of Hearts
|
||||
Seven of Spades
|
||||
Jack of Spades
|
||||
Six of Hearts
|
||||
Ten of Clubs
|
||||
|
||||
NOTHING
|
||||
|
||||
Ace of Diamonds
|
||||
Eight of Hearts
|
||||
Five of Diamonds
|
||||
Eight of Clubs
|
||||
Four of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Six of Clubs
|
||||
Two of Hearts
|
||||
Six of Diamonds
|
||||
Four of Clubs
|
||||
Three of Hearts
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Four of Diamonds
|
||||
Jack of Diamonds
|
||||
Jack of Clubs
|
||||
Two of Hearts
|
||||
Two of Spades
|
||||
|
||||
TWO PAIR
|
||||
|
||||
Ace of Hearts
|
||||
Nine of Clubs
|
||||
King of Clubs
|
||||
Two of Hearts
|
||||
Three of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
Two of Hearts
|
||||
Two of Hearts
|
||||
Eight of Spades
|
||||
Nine of Diamonds
|
||||
Five of Clubs
|
||||
|
||||
ONE PAIR
|
||||
|
||||
Ten of Spades
|
||||
King of Spades
|
||||
Seven of Hearts
|
||||
Three of Spades
|
||||
Queen of Diamonds
|
||||
|
||||
NOTHING
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
50/50.
|
|
@ -0,0 +1,78 @@
|
|||
//Ben Harris
|
||||
//This program shuffles a deck of cards, deals ten hands of five,
|
||||
//then checks for poker hands excluding straight, flush, and straight flush
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "time.h"
|
||||
#include "PG2.h"
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv){//main method
|
||||
//initialize deck and randomize it
|
||||
srand((unsigned)time(0));
|
||||
int *deck = new int[52];
|
||||
for(int i=0;i<52;i++){
|
||||
deck[i]=i;
|
||||
}
|
||||
shuffle(deck);
|
||||
|
||||
//start to deal the hands
|
||||
for(int j = 0; j<10;j++){//deals ten hands
|
||||
|
||||
int rankcount[13] = {0};
|
||||
for(int i = j*5;i<j*5+5;i++){//print, then check each hand for duplicates
|
||||
rankcount[deck[i]/4]++;
|
||||
cout << getRankName(deck[i]) << " of " << getSuitName(deck[i]) << endl;
|
||||
|
||||
}
|
||||
cout<<endl;
|
||||
if (inArray(rankcount, 2, 13) && inArray(rankcount, 3, 13))cout << "FULL HOUSE";
|
||||
else if (dupeinarray(rankcount, 2, 13))cout << "TWO PAIR";
|
||||
else if (inArray(rankcount, 4, 13))cout << "FOUR OF A KIND";
|
||||
else if (inArray(rankcount, 3, 13))cout << "THREE OF A KIND";
|
||||
else if(inArray(rankcount,2,13))cout<<"ONE PAIR";
|
||||
else cout<<"NOTHING";
|
||||
cout<<endl;
|
||||
cout<<endl;
|
||||
|
||||
}
|
||||
//remove systempause before turning in
|
||||
//system("pause");
|
||||
delete[] deck;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string getSuitName(int card){//returns the suit of the input card
|
||||
string suits[4] = {"Hearts","Clubs","Diamonds","Spades"};
|
||||
return suits[card%4];
|
||||
}
|
||||
string getRankName(int card){//returns the rank of the input card
|
||||
string ranks[13] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
|
||||
return ranks[card/4];
|
||||
}
|
||||
void swapTwo(int a, int b,int *d){//swaps two ints a and b within an array d
|
||||
d[b] = d[a]+d[b];
|
||||
d[a] = d[b]-d[a];
|
||||
d[b] = d[b]-d[a];
|
||||
}
|
||||
void shuffle(int *d){//swaps each element with a random index at or before the current index, shuffling the deck
|
||||
for(int i = 1; i<52;i++){
|
||||
int r = rand()%(i+1);
|
||||
swapTwo(i,r,d);
|
||||
}
|
||||
}
|
||||
bool inArray(int *d,int toinsert, int sz){//returns true if toinsert is found in array d; checks only up to size
|
||||
for(int i = 0; i<sz;i++){
|
||||
if(toinsert == d[i])return true;
|
||||
}return false;
|
||||
}
|
||||
bool dupeinarray(int *d, int dupetocheck, int sz){//checks for duplicate dupetocheck in array d
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < sz; i++){
|
||||
if (d[i] == dupetocheck)cnt++;
|
||||
}
|
||||
if (cnt > 1)return true;
|
||||
else return false;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
//Ben Harris
|
||||
//Header file for PG2
|
||||
|
||||
#ifndef _PG2_
|
||||
#define _PG2_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char **argv);
|
||||
string getSuitName(int card);
|
||||
string getRankName(int card);
|
||||
void swapTwo(int a, int b,int *d);
|
||||
void shuffle(int *d);
|
||||
bool inArray(int *d,int toinsert, int sz);
|
||||
bool dupeinarray(int *d, int dupetocheck, int sz);
|
||||
#endif
|
|
@ -0,0 +1,34 @@
|
|||
//Ben Harris
|
||||
//List.cpp defines the methods for a List
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
List::List(){
|
||||
head = NULL;
|
||||
}
|
||||
List::~List(){
|
||||
delete head;
|
||||
}
|
||||
void List::add(string s){
|
||||
if (head == NULL)head = new Node(s, NULL);
|
||||
else head->add(s);
|
||||
}
|
||||
void List::print(){
|
||||
int maxcnt = 0;
|
||||
Node* n = head;
|
||||
while (n != NULL){
|
||||
if (n->getcount() > maxcnt) maxcnt = n->getcount();
|
||||
n = n->getnext();
|
||||
}
|
||||
cout << endl << "<<Commonest Words>>"<<endl<<endl;
|
||||
n = head;
|
||||
while (n != NULL){
|
||||
if (n->getcount() == maxcnt) cout << n->getvalue() << endl;
|
||||
n = n->getnext();
|
||||
}cout << endl;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
//Ben Harris
|
||||
//Header file for prototypes of List methods
|
||||
|
||||
#ifndef _LIST_
|
||||
#define _LIST_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
class Node;
|
||||
class List{
|
||||
private: // Each list contains only the pointer to its head Node.
|
||||
Node *head;
|
||||
public:
|
||||
List(); // List Constructor
|
||||
~List(); // List Destructor
|
||||
void add(string s); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered.
|
||||
void print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value.
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,33 @@
|
|||
//Ben Harris
|
||||
//Node.cpp contains the code for each node
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
Node::Node(string s, Node *n){
|
||||
cnt = 1;
|
||||
value = s;
|
||||
next = n;
|
||||
}
|
||||
Node::~Node(){
|
||||
delete next;
|
||||
cnt = 0;
|
||||
//farewell
|
||||
}
|
||||
string Node::getvalue(){
|
||||
return value;
|
||||
}
|
||||
int Node::getcount(){
|
||||
return cnt;
|
||||
}
|
||||
Node * Node::getnext(){
|
||||
return next;
|
||||
}
|
||||
void Node::add(string s){
|
||||
if (value == s)cnt++;
|
||||
else if (next == NULL) next = new Node(s, NULL);
|
||||
else next->add(s);
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
//Ben Harris
|
||||
//Node.h is a header file for Node methods
|
||||
|
||||
#ifndef _NODE_
|
||||
#define _NODE_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
class Node{
|
||||
private: // Each Node contains a vale, count, and a pointer to the next Node in the list.
|
||||
string value;
|
||||
int cnt;
|
||||
Node *next;
|
||||
public:
|
||||
Node(string s, Node *n); // Node Constructor
|
||||
~Node(); // Node Destructor
|
||||
string getvalue(); // Value accessor
|
||||
int getcount(); // Count accessor
|
||||
Node * getnext(); // Next pointer accessor
|
||||
void add(string s); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once.
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,269 @@
|
|||
Listing List.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//List.cpp defines the methods for a List
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
List::List(){
|
||||
head = NULL;
|
||||
}
|
||||
List::~List(){
|
||||
delete head;
|
||||
}
|
||||
void List::add(string s){
|
||||
if (head == NULL)head = new Node(s, NULL);
|
||||
else head->add(s);
|
||||
}
|
||||
void List::print(){
|
||||
int maxcnt = 0;
|
||||
Node* n = head;
|
||||
while (n != NULL){
|
||||
if (n->getcount() > maxcnt) maxcnt = n->getcount();
|
||||
n = n->getnext();
|
||||
}
|
||||
cout << endl << "<<Commonest Words>>"<<endl<<endl;
|
||||
n = head;
|
||||
while (n != NULL){
|
||||
if (n->getcount() == maxcnt) cout << n->getvalue() << endl;
|
||||
n = n->getnext();
|
||||
}cout << endl;
|
||||
}
|
||||
|
||||
|
||||
Listing List.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for prototypes of List methods
|
||||
|
||||
#ifndef _LIST_
|
||||
#define _LIST_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
class Node;
|
||||
class List{
|
||||
private: // Each list contains only the pointer to its head Node.
|
||||
Node *head;
|
||||
public:
|
||||
List(); // List Constructor
|
||||
~List(); // List Destructor
|
||||
void add(string s); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered.
|
||||
void print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value.
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing Node.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//Node.cpp contains the code for each node
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
Node::Node(string s, Node *n){
|
||||
cnt = 1;
|
||||
value = s;
|
||||
next = n;
|
||||
}
|
||||
Node::~Node(){
|
||||
delete next;
|
||||
cnt = 0;
|
||||
//farewell
|
||||
}
|
||||
string Node::getvalue(){
|
||||
return value;
|
||||
}
|
||||
int Node::getcount(){
|
||||
return cnt;
|
||||
}
|
||||
Node * Node::getnext(){
|
||||
return next;
|
||||
}
|
||||
void Node::add(string s){
|
||||
if (value == s)cnt++;
|
||||
else if (next == NULL) next = new Node(s, NULL);
|
||||
else next->add(s);
|
||||
}
|
||||
|
||||
|
||||
Listing Node.h...
|
||||
|
||||
//Ben Harris
|
||||
//Node.h is a header file for Node methods
|
||||
|
||||
#ifndef _NODE_
|
||||
#define _NODE_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
class Node{
|
||||
private: // Each Node contains a vale, count, and a pointer to the next Node in the list.
|
||||
string value;
|
||||
int cnt;
|
||||
Node *next;
|
||||
public:
|
||||
Node(string s, Node *n); // Node Constructor
|
||||
~Node(); // Node Destructor
|
||||
string getvalue(); // Value accessor
|
||||
int getcount(); // Count accessor
|
||||
Node * getnext(); // Next pointer accessor
|
||||
void add(string s); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once.
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG3.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program builds a list based on user input and prints the
|
||||
//most commonly entered words, printing all in the case of a tie.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG3.h"
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string input =" ";
|
||||
List * list = new List();
|
||||
while (input!=""){
|
||||
cout << "Input<<";
|
||||
getline(cin, input);
|
||||
if(input!="")list->add(input);
|
||||
}
|
||||
list->print();
|
||||
delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Listing PG3.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for PG3
|
||||
|
||||
#ifndef _PG3_
|
||||
#define _PG3_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv);// Main method.
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG3.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "List.cpp" /Tp "Node.cpp" /Tp "PG3.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG3.exe
|
||||
List.cpp
|
||||
Node.cpp
|
||||
PG3.cpp
|
||||
Generating Code...
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:List.exe
|
||||
/OUT:PG3.exe
|
||||
List.obj
|
||||
Node.obj
|
||||
PG3.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG3.exe < "C:\Grading\Classes\CS201-02-14F\PG3\PG3-1.in"
|
||||
Input<<Input<<Input<<Input<<Input<<Input<<
|
||||
<<Commonest Words>>
|
||||
|
||||
apple
|
||||
banana
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG3.exe < "C:\Grading\Classes\CS201-02-14F\PG3\PG3-2.in"
|
||||
Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<
|
||||
<<Commonest Words>>
|
||||
|
||||
q
|
||||
w
|
||||
e
|
||||
r
|
||||
t
|
||||
y
|
||||
u
|
||||
i
|
||||
o
|
||||
p
|
||||
a
|
||||
s
|
||||
d
|
||||
f
|
||||
g
|
||||
h
|
||||
j
|
||||
k
|
||||
l
|
||||
z
|
||||
x
|
||||
c
|
||||
v
|
||||
b
|
||||
n
|
||||
m
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG3.exe < "C:\Grading\Classes\CS201-02-14F\PG3\PG3-3.in"
|
||||
Input<<
|
||||
<<Commonest Words>>
|
||||
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG3.exe < "C:\Grading\Classes\CS201-02-14F\PG3\PG3-4.in"
|
||||
Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<Input<<
|
||||
<<Commonest Words>>
|
||||
|
||||
SAW
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG3.exe < "C:\Grading\Classes\CS201-02-14F\PG3\PG3-5.in"
|
||||
Input<<Input<<
|
||||
<<Commonest Words>>
|
||||
|
||||
ONLY
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
50/50.
|
|
@ -0,0 +1,23 @@
|
|||
//Ben Harris
|
||||
//This program builds a list based on user input and prints the
|
||||
//most commonly entered words, printing all in the case of a tie.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG3.h"
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string input =" ";
|
||||
List * list = new List();
|
||||
while (input!=""){
|
||||
cout << "Input<<";
|
||||
getline(cin, input);
|
||||
if(input!="")list->add(input);
|
||||
}
|
||||
list->print();
|
||||
delete list;
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
//Ben Harris
|
||||
//Header file for PG3
|
||||
|
||||
#ifndef _PG3_
|
||||
#define _PG3_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv);// Main method.
|
||||
|
||||
#endif
|
|
@ -0,0 +1,39 @@
|
|||
//Ben Harris
|
||||
//List.cpp defines the methods for a List
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
#include "Node.h"
|
||||
#include "PG4.h"
|
||||
using namespace std;
|
||||
|
||||
List::List(){
|
||||
head = NULL;
|
||||
}
|
||||
List::~List(){
|
||||
delete head;
|
||||
}
|
||||
void List::add(string s, string l){
|
||||
if (head && head->getvalue() == l) {
|
||||
cout << "already in library. aborted." << endl;
|
||||
return;
|
||||
}
|
||||
if (!head || converttolower(head->getvalue()) > l) head = new Node(s, head);
|
||||
else head = head->add(s, l);
|
||||
}
|
||||
|
||||
void List::print(){
|
||||
|
||||
cout << endl << "Library List" << endl << endl;
|
||||
if (head) head->print();
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void List::remove(string s){
|
||||
if (head) head = head->remove(s, NULL, this);
|
||||
}
|
||||
|
||||
void List::sethead(Node * n){ head = n; }
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
//Ben Harris
|
||||
//Header file for prototypes of List methods
|
||||
|
||||
#ifndef _LIST_
|
||||
#define _LIST_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
class Node;
|
||||
class List{
|
||||
private: // Each list contains only the pointer to its head Node.
|
||||
Node *head;
|
||||
public:
|
||||
List(); // List Constructor
|
||||
~List(); // List Destructor
|
||||
void add(string s,string l); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered.
|
||||
void print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value.
|
||||
void remove(string s); // Calls remove on the nodes that match string s
|
||||
void sethead(Node* n); // Head mutator
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,52 @@
|
|||
//Ben Harris
|
||||
//Node.cpp contains the code for each node
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
#include "Node.h"
|
||||
#include "List.h"
|
||||
#include "PG4.h"
|
||||
using namespace std;
|
||||
|
||||
Node::Node(string s, Node *n){
|
||||
value = s;
|
||||
next = n;
|
||||
}
|
||||
Node::~Node(){delete next;}
|
||||
string Node::getvalue(){return value;}
|
||||
int Node::getcount(){return cnt;}
|
||||
Node * Node::getnext(){return next;}
|
||||
void Node::setnext(Node *n){ next = n; }
|
||||
|
||||
Node * Node::add(string s, string l){
|
||||
|
||||
string lvalue = converttolower(value);
|
||||
|
||||
if (lvalue == l) return this;
|
||||
if (l < lvalue) return new Node(s, this);
|
||||
|
||||
if (!next){ next = new Node(s, NULL); return this; }
|
||||
|
||||
next = next->add(s,l);
|
||||
return this;
|
||||
}
|
||||
|
||||
void Node::print(){
|
||||
cout << value << endl;
|
||||
if (next) next->print();
|
||||
}
|
||||
|
||||
Node* Node::remove(string s, Node *prev, List *l){
|
||||
string lvalue = converttolower(value);
|
||||
if (next) next = next->remove(s, this, l);
|
||||
if (lvalue.find(s) < value.length() && lvalue.find(s) >= 0){
|
||||
Node *t = next;
|
||||
next = NULL;
|
||||
delete this;
|
||||
return t;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
//Ben Harris
|
||||
//Node.h is a header file for Node methods
|
||||
|
||||
#ifndef _NODE_
|
||||
#define _NODE_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
class List;
|
||||
class Node{
|
||||
private: // Each Node contains a value, count, and a pointer to the next Node in the list.
|
||||
string value;
|
||||
int cnt;
|
||||
Node *next;
|
||||
public:
|
||||
Node(string s, Node *n); // Node Constructor
|
||||
~Node(); // Node Destructor
|
||||
string getvalue(); // Value accessor
|
||||
int getcount(); // Count accessor
|
||||
Node * getnext(); // Next pointer accessor
|
||||
void setnext(Node* n); // Next pointer mutator
|
||||
Node * add(string s,string l); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once.
|
||||
void print(); // Tells each node to print itself and its next
|
||||
Node* remove(string s, Node *prev, List *l); // Remove the nodes with matching string s and returns the new head
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,363 @@
|
|||
Listing List.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//List.cpp defines the methods for a List
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
List::List(){
|
||||
head = NULL;
|
||||
}
|
||||
List::~List(){
|
||||
delete head;
|
||||
}
|
||||
void List::add(string s){
|
||||
if (head && head->getvalue() == s) return;
|
||||
if (!head || head->getvalue() > s)head = new Node(s, head);
|
||||
else head = head->add(s);
|
||||
}
|
||||
|
||||
void List::print(){
|
||||
|
||||
cout << endl << "Library List" << endl << endl;
|
||||
if (head) head->print();
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void List::remove(string s){
|
||||
if (head) head = head->remove(s, nullptr, this);
|
||||
}
|
||||
|
||||
void List::sethead(Node * n){ head = n; }
|
||||
|
||||
|
||||
|
||||
Listing List.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for prototypes of List methods
|
||||
|
||||
#ifndef _LIST_
|
||||
#define _LIST_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
class Node;
|
||||
class List{
|
||||
private: // Each list contains only the pointer to its head Node.
|
||||
Node *head;
|
||||
public:
|
||||
List(); // List Constructor
|
||||
~List(); // List Destructor
|
||||
void add(string s); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered.
|
||||
void print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value.
|
||||
void remove(string s); // Calls remove on the nodes that match string s
|
||||
void sethead(Node* n); // Head mutator
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing Node.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//Node.cpp contains the code for each node
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
Node::Node(string s, Node *n){
|
||||
value = s;
|
||||
next = n;
|
||||
}
|
||||
Node::~Node(){delete next;}
|
||||
string Node::getvalue(){return value;}
|
||||
int Node::getcount(){return cnt;}
|
||||
Node * Node::getnext(){return next;}
|
||||
void Node::setnext(Node *n){ next = n; }
|
||||
|
||||
Node * Node::add(string s){
|
||||
if (value == s)return this;
|
||||
if (value > s) return new Node(s, this);
|
||||
if (!next){ next = new Node(s, nullptr); return this; }
|
||||
next = next->add(s);
|
||||
return this;
|
||||
}
|
||||
|
||||
void Node::print(){
|
||||
cout << value << endl;
|
||||
if (next) next->print();
|
||||
}
|
||||
|
||||
Node* Node::remove(string s, Node *prev, List *l){
|
||||
|
||||
if (next) next = next->remove(s, this, l);
|
||||
if (value.find(s) < value.length() && value.find(s) >= 0)
|
||||
{
|
||||
Node *t = next;
|
||||
next = nullptr;
|
||||
delete this;
|
||||
return t;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Listing Node.h...
|
||||
|
||||
//Ben Harris
|
||||
//Node.h is a header file for Node methods
|
||||
|
||||
#ifndef _NODE_
|
||||
#define _NODE_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
class List;
|
||||
class Node{
|
||||
private: // Each Node contains a value, count, and a pointer to the next Node in the list.
|
||||
string value;
|
||||
int cnt;
|
||||
Node *next;
|
||||
public:
|
||||
Node(string s, Node *n); // Node Constructor
|
||||
~Node(); // Node Destructor
|
||||
string getvalue(); // Value accessor
|
||||
int getcount(); // Count accessor
|
||||
Node * getnext(); // Next pointer accessor
|
||||
void setnext(Node* n); // Next pointer mutator
|
||||
Node * add(string s); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once.
|
||||
void print(); // Tells each node to print itself and its next
|
||||
Node* remove(string s, Node *prev, List *l); // Remove the nodes with matching string s and returns the new head
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG4.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program manages a library of books by title, stored in a linked list
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include<cctype>
|
||||
#include "PG4.h"
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string input = " ";
|
||||
List * list = new List();
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
while (converttolower(input) != "exit"){
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
if (lower.substr(0, 3) == "add") list->add(input.substr(5, input.length() - 6));
|
||||
if (lower.substr(0, 6) == "remove") list->remove(lower.substr(8, input.length() - 9));
|
||||
if (lower == "print") list->print();
|
||||
if (list && lower == "clear") {delete list; List * list = new List();}
|
||||
if (lower == "help"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "clear \t\t\t--clears the entire library" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string converttolower(string s){
|
||||
for (int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;
|
||||
}
|
||||
|
||||
|
||||
Listing PG4.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG4
|
||||
|
||||
#ifndef _PG4_
|
||||
#define _PG4_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Used to convert strings to lower, since tolower() only converts one char at a time
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG4.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "List.cpp" /Tp "Node.cpp" /Tp "PG4.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG4.exe
|
||||
List.cpp
|
||||
Node.cpp
|
||||
PG4.cpp
|
||||
Generating Code...
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:List.exe
|
||||
/OUT:PG4.exe
|
||||
List.obj
|
||||
Node.obj
|
||||
PG4.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG4.exe < "C:\Grading\Classes\CS201-01-14F\PG4\PG4-1.in"
|
||||
cLibrary Version 1.0 -- type help for a list of commands
|
||||
Enter command:
|
||||
Library List
|
||||
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
A
|
||||
D
|
||||
E
|
||||
F
|
||||
G
|
||||
H
|
||||
J
|
||||
K
|
||||
L
|
||||
Q
|
||||
R
|
||||
S
|
||||
T
|
||||
W
|
||||
X
|
||||
b
|
||||
c
|
||||
e
|
||||
i
|
||||
m
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
t
|
||||
u
|
||||
v
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
A
|
||||
D
|
||||
E
|
||||
F
|
||||
G
|
||||
H
|
||||
J
|
||||
K
|
||||
L
|
||||
Q
|
||||
R
|
||||
S
|
||||
T
|
||||
W
|
||||
X
|
||||
m
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
t
|
||||
u
|
||||
v
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
A
|
||||
D
|
||||
E
|
||||
F
|
||||
G
|
||||
H
|
||||
J
|
||||
K
|
||||
L
|
||||
Mostly Harmless
|
||||
Q
|
||||
R
|
||||
S
|
||||
T
|
||||
The Hitchhiker's Guide To The Galaxy
|
||||
The Restaurant At The End Of The Universe
|
||||
W
|
||||
X
|
||||
m
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
t
|
||||
u
|
||||
v
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
|
||||
Enter command:
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Wrong answers due to not implementing case insensitivity.
|
||||
|
||||
25/50.
|
||||
|
||||
Fix by Friday 31 October 2014.
|
|
@ -0,0 +1,351 @@
|
|||
Listing List.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//List.cpp defines the methods for a List
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
#include "Node.h"
|
||||
#include "PG4.h"
|
||||
using namespace std;
|
||||
|
||||
List::List(){
|
||||
head = NULL;
|
||||
}
|
||||
List::~List(){
|
||||
delete head;
|
||||
}
|
||||
void List::add(string s, string l){
|
||||
if (head && head->getvalue() == l) return;
|
||||
if (!head || converttolower(head->getvalue()) > l)head = new Node(s, head);
|
||||
else head = head->add(s,l);
|
||||
}
|
||||
|
||||
void List::print(){
|
||||
|
||||
cout << endl << "Library List" << endl << endl;
|
||||
if (head) head->print();
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void List::remove(string s){
|
||||
if (head) head = head->remove(s, nullptr, this);
|
||||
}
|
||||
|
||||
void List::sethead(Node * n){ head = n; }
|
||||
|
||||
|
||||
|
||||
Listing List.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for prototypes of List methods
|
||||
|
||||
#ifndef _LIST_
|
||||
#define _LIST_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
class Node;
|
||||
class List{
|
||||
private: // Each list contains only the pointer to its head Node.
|
||||
Node *head;
|
||||
public:
|
||||
List(); // List Constructor
|
||||
~List(); // List Destructor
|
||||
void add(string s,string l); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered.
|
||||
void print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value.
|
||||
void remove(string s); // Calls remove on the nodes that match string s
|
||||
void sethead(Node* n); // Head mutator
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing Node.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//Node.cpp contains the code for each node
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
#include "Node.h"
|
||||
#include "List.h"
|
||||
#include "PG4.h"
|
||||
using namespace std;
|
||||
|
||||
Node::Node(string s, Node *n){
|
||||
value = s;
|
||||
next = n;
|
||||
}
|
||||
Node::~Node(){delete next;}
|
||||
string Node::getvalue(){return value;}
|
||||
int Node::getcount(){return cnt;}
|
||||
Node * Node::getnext(){return next;}
|
||||
void Node::setnext(Node *n){ next = n; }
|
||||
|
||||
Node * Node::add(string s, string l){
|
||||
if (converttolower(value) == l)return this;
|
||||
if (l < converttolower(value)) return new Node(s, this);
|
||||
if (!next){ next = new Node(s, nullptr); return this; }
|
||||
next = next->add(s,l);
|
||||
return this;
|
||||
}
|
||||
|
||||
void Node::print(){
|
||||
cout << value << endl;
|
||||
if (next) next->print();
|
||||
}
|
||||
|
||||
Node* Node::remove(string s, Node *prev, List *l){
|
||||
|
||||
if (next) next = next->remove(s, this, l);
|
||||
if (value.find(s) < value.length() && value.find(s) >= 0)
|
||||
{
|
||||
Node *t = next;
|
||||
next = nullptr;
|
||||
delete this;
|
||||
return t;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Listing Node.h...
|
||||
|
||||
//Ben Harris
|
||||
//Node.h is a header file for Node methods
|
||||
|
||||
#ifndef _NODE_
|
||||
#define _NODE_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
class List;
|
||||
class Node{
|
||||
private: // Each Node contains a value, count, and a pointer to the next Node in the list.
|
||||
string value;
|
||||
int cnt;
|
||||
Node *next;
|
||||
public:
|
||||
Node(string s, Node *n); // Node Constructor
|
||||
~Node(); // Node Destructor
|
||||
string getvalue(); // Value accessor
|
||||
int getcount(); // Count accessor
|
||||
Node * getnext(); // Next pointer accessor
|
||||
void setnext(Node* n); // Next pointer mutator
|
||||
Node * add(string s,string l); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once.
|
||||
void print(); // Tells each node to print itself and its next
|
||||
Node* remove(string s, Node *prev, List *l); // Remove the nodes with matching string s and returns the new head
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG4.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program manages a library of books by title, stored in a linked list
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include<cctype>
|
||||
#include "PG4.h"
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string input = " ";
|
||||
List * list = new List();
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
while (converttolower(input) != "exit"){
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
if (lower.substr(0, 3) == "add") list->add(input.substr(5, input.length() - 6),lower.substr(5, input.length() -6));
|
||||
if (lower.substr(0, 6) == "remove") list->remove(lower.substr(8, input.length() - 9));
|
||||
if (lower == "print") list->print();
|
||||
if (list && lower == "clear") {delete list; List * list = new List();}
|
||||
if (lower.substr(0,4) == "help"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "clear \t\t\t--clears the entire library" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string converttolower(string s){
|
||||
for (int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;
|
||||
}
|
||||
|
||||
|
||||
Listing PG4.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG4
|
||||
|
||||
#ifndef _PG4_
|
||||
#define _PG4_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Used to convert strings to lower, since tolower() only converts one char at a time
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG4.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "List.cpp" /Tp "Node.cpp" /Tp "PG4.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG4.exe
|
||||
List.cpp
|
||||
Node.cpp
|
||||
PG4.cpp
|
||||
Generating Code...
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:List.exe
|
||||
/OUT:PG4.exe
|
||||
List.obj
|
||||
Node.obj
|
||||
PG4.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG4.exe < "C:\Grading\Classes\CS201-01-14F\PG4\PG4-1.in"
|
||||
cLibrary Version 1.0 -- type help for a list of commands
|
||||
Enter command:
|
||||
Library List
|
||||
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
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
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
A
|
||||
D
|
||||
F
|
||||
G
|
||||
H
|
||||
J
|
||||
K
|
||||
L
|
||||
m
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
S
|
||||
t
|
||||
u
|
||||
v
|
||||
w
|
||||
X
|
||||
y
|
||||
z
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
A
|
||||
D
|
||||
F
|
||||
G
|
||||
H
|
||||
J
|
||||
K
|
||||
L
|
||||
m
|
||||
Mostly Harmless
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
S
|
||||
t
|
||||
The Hitchhiker's Guide To The Galaxy
|
||||
The Restaurant At The End Of The Universe
|
||||
u
|
||||
v
|
||||
w
|
||||
X
|
||||
y
|
||||
z
|
||||
|
||||
Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
|
||||
Enter command:
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Not deleting everything that should be deleted.
|
||||
|
||||
30/50.
|
||||
|
||||
Fix by Monday 3 November 2014.
|
|
@ -0,0 +1,335 @@
|
|||
Listing List.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//List.cpp defines the methods for a List
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
#include "Node.h"
|
||||
#include "PG4.h"
|
||||
using namespace std;
|
||||
|
||||
List::List(){
|
||||
head = NULL;
|
||||
}
|
||||
List::~List(){
|
||||
delete head;
|
||||
}
|
||||
void List::add(string s, string l){
|
||||
if (head && head->getvalue() == l) return;
|
||||
if (!head || converttolower(head->getvalue()) > l)head = new Node(s, head);
|
||||
else head = head->add(s,l);
|
||||
}
|
||||
|
||||
void List::print(){
|
||||
|
||||
cout << endl << "Library List" << endl << endl;
|
||||
if (head) head->print();
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void List::remove(string s){
|
||||
if (head) head = head->remove(s, nullptr, this);
|
||||
}
|
||||
|
||||
void List::sethead(Node * n){ head = n; }
|
||||
|
||||
|
||||
|
||||
Listing List.h...
|
||||
|
||||
//Ben Harris
|
||||
//Header file for prototypes of List methods
|
||||
|
||||
#ifndef _LIST_
|
||||
#define _LIST_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "Node.h"
|
||||
using namespace std;
|
||||
|
||||
class Node;
|
||||
class List{
|
||||
private: // Each list contains only the pointer to its head Node.
|
||||
Node *head;
|
||||
public:
|
||||
List(); // List Constructor
|
||||
~List(); // List Destructor
|
||||
void add(string s,string l); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered.
|
||||
void print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value.
|
||||
void remove(string s); // Calls remove on the nodes that match string s
|
||||
void sethead(Node* n); // Head mutator
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing Node.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//Node.cpp contains the code for each node
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
#include "Node.h"
|
||||
#include "List.h"
|
||||
#include "PG4.h"
|
||||
using namespace std;
|
||||
|
||||
Node::Node(string s, Node *n){
|
||||
value = s;
|
||||
next = n;
|
||||
}
|
||||
Node::~Node(){delete next;}
|
||||
string Node::getvalue(){return value;}
|
||||
int Node::getcount(){return cnt;}
|
||||
Node * Node::getnext(){return next;}
|
||||
void Node::setnext(Node *n){ next = n; }
|
||||
|
||||
Node * Node::add(string s, string l){
|
||||
string lvalue = converttolower(value);
|
||||
if (lvalue == l)return this;
|
||||
if (l < lvalue) return new Node(s, this);
|
||||
if (!next){ next = new Node(s, nullptr); return this; }
|
||||
next = next->add(s,l);
|
||||
return this;
|
||||
}
|
||||
|
||||
void Node::print(){
|
||||
cout << value << endl;
|
||||
if (next) next->print();
|
||||
}
|
||||
|
||||
Node* Node::remove(string s, Node *prev, List *l){
|
||||
string lvalue = converttolower(value);
|
||||
if (next) next = next->remove(s, this, l);
|
||||
if (lvalue.find(s) < value.length() && lvalue.find(s) >= 0){
|
||||
Node *t = next;
|
||||
next = nullptr;
|
||||
delete this;
|
||||
return t;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Listing Node.h...
|
||||
|
||||
//Ben Harris
|
||||
//Node.h is a header file for Node methods
|
||||
|
||||
#ifndef _NODE_
|
||||
#define _NODE_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
class List;
|
||||
class Node{
|
||||
private: // Each Node contains a value, count, and a pointer to the next Node in the list.
|
||||
string value;
|
||||
int cnt;
|
||||
Node *next;
|
||||
public:
|
||||
Node(string s, Node *n); // Node Constructor
|
||||
~Node(); // Node Destructor
|
||||
string getvalue(); // Value accessor
|
||||
int getcount(); // Count accessor
|
||||
Node * getnext(); // Next pointer accessor
|
||||
void setnext(Node* n); // Next pointer mutator
|
||||
Node * add(string s,string l); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once.
|
||||
void print(); // Tells each node to print itself and its next
|
||||
Node* remove(string s, Node *prev, List *l); // Remove the nodes with matching string s and returns the new head
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG4.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//This program manages a library of books by title, stored in a linked list
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include<cctype>
|
||||
#include "PG4.h"
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string input = " ";
|
||||
List * list = new List();
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
while (converttolower(input).substr(0,4) != "exit"){
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
if (lower.substr(0, 3) == "add") list->add(input.substr(5, input.length() - 6),lower.substr(5, input.length() -6));
|
||||
if (lower.substr(0, 6) == "remove") list->remove(lower.substr(8, input.length() - 9));
|
||||
if (lower == "print") list->print();
|
||||
if (list && lower == "clear") {delete list; List * list = new List();}
|
||||
if (lower.substr(0,4) == "help" || lower.substr(0,4) == "halp"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "clear \t\t\t--clears the entire library" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string converttolower(string s){
|
||||
for (int i = 0; i < (int) s.length(); i++) s[i] = tolower(s[i]); return s;
|
||||
}
|
||||
|
||||
|
||||
Listing PG4.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG4
|
||||
|
||||
#ifndef _PG4_
|
||||
#define _PG4_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Used to convert strings to lower, since tolower() only converts one char at a time
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG4.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "List.cpp" /Tp "Node.cpp" /Tp "PG4.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG4.exe
|
||||
List.cpp
|
||||
Node.cpp
|
||||
PG4.cpp
|
||||
Generating Code...
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:List.exe
|
||||
/OUT:PG4.exe
|
||||
List.obj
|
||||
Node.obj
|
||||
PG4.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG4.exe < "C:\Grading\Classes\CS201-01-14F\PG4\PG4-1.in"
|
||||
cLibrary Version 1.0 -- type help for a list of commands
|
||||
Enter command:
|
||||
Library List
|
||||
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
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
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
G
|
||||
K
|
||||
L
|
||||
m
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
S
|
||||
t
|
||||
u
|
||||
v
|
||||
w
|
||||
X
|
||||
y
|
||||
z
|
||||
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
K
|
||||
L
|
||||
m
|
||||
Mostly Harmless
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
S
|
||||
t
|
||||
The Restaurant At The End Of The Universe
|
||||
u
|
||||
v
|
||||
w
|
||||
X
|
||||
y
|
||||
z
|
||||
|
||||
Enter command: Enter command:
|
||||
Library List
|
||||
|
||||
|
||||
Enter command:
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
50/50.
|
|
@ -0,0 +1,40 @@
|
|||
//Ben Harris
|
||||
//This program manages a library of books by title, stored in a linked list
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include<cctype>
|
||||
#include "PG4.h"
|
||||
#include "List.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string input = " ";
|
||||
List * list = new List();
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
while (converttolower(input).substr(0,4) != "exit"){
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
if (lower.substr(0, 3) == "add") list->add(input.substr(5, input.length() - 6),lower.substr(5, input.length() -6));
|
||||
if (lower.substr(0, 6) == "remove") list->remove(lower.substr(8, input.length() - 9));
|
||||
if (lower == "print") list->print();
|
||||
if (list && lower == "clear") {delete list; List * list = new List();}
|
||||
if (lower.substr(0,4) == "help" || lower.substr(0,4) == "halp"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "clear \t\t\t--clears the entire library" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string converttolower(string s){
|
||||
for (int i = 0; i < (int) s.length(); i++) s[i] = tolower(s[i]); return s;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
// Ben Harris
|
||||
// Header for PG4
|
||||
|
||||
#ifndef _PG4_
|
||||
#define _PG4_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Used to convert strings to lower, since tolower() only converts one char at a time
|
||||
|
||||
#endif
|
|
@ -0,0 +1,54 @@
|
|||
//Ben Harris
|
||||
//Template for an linked list that can be used as a array.
|
||||
|
||||
#ifndef _ARRAYLL_
|
||||
#define _ARRAYLL_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "ArrayLLN.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL{
|
||||
private:
|
||||
ArrayLLN<T> * head;
|
||||
public:
|
||||
ArrayLL(){ head = nullptr; }
|
||||
~ArrayLL(){ delete head; }
|
||||
void sethead(ArrayLLN<T>* t){ head = t; }
|
||||
|
||||
int length(){ // returns length of the list
|
||||
int cnt = 0;
|
||||
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext());
|
||||
return cnt;
|
||||
}
|
||||
T remove(int pos){ // removes the node at pos
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (int cnt = 0; cnt < pos; cnt++, Q = P, P = P->getnext());
|
||||
T tempcontents = P->getcontents();
|
||||
P->removeself(P, Q, this);
|
||||
return tempcontents;
|
||||
}
|
||||
T& operator[] (const int pos){ // overloads [], which returns a reference to the node at pos
|
||||
ArrayLLN<T>* P = head;
|
||||
for (int cnt = 0; cnt < pos; cnt++, P = P->getnext());
|
||||
return P->getcontents();
|
||||
}
|
||||
void insert(int pos, T stuff){ // inserts a node at pos with stuff contents
|
||||
if (pos == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
|
||||
else if (pos > 0 && pos < length()){ // if inserting not at the head or tail
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (int i = 0; i < pos; i++, Q = P, P = P->getnext());
|
||||
if (Q) Q->setnext(new ArrayLLN<T>(stuff, P));
|
||||
else P->setnext(new ArrayLLN<T>(stuff, P->getnext()));
|
||||
}
|
||||
else if (pos == length()) // if inserting at the tail.
|
||||
if (head == NULL) head = new ArrayLLN<T>(stuff, NULL);
|
||||
else head->addback(stuff);
|
||||
else return;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,47 @@
|
|||
//Ben Harris
|
||||
//List Node Template
|
||||
|
||||
#ifndef _ARRAYLLN_
|
||||
#define _ARRAYLLN_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL;
|
||||
|
||||
template <class T> class ArrayLLN{
|
||||
private:
|
||||
T contents;
|
||||
ArrayLLN<T> *next;
|
||||
public:
|
||||
|
||||
ArrayLLN(T t, ArrayLLN<T>* n){ // Constructor
|
||||
contents = t;
|
||||
next = n;
|
||||
}
|
||||
~ArrayLLN(){delete next;} // Destructor
|
||||
void setcontents(T t){contents = t;} // contents mutator
|
||||
T& getcontents(){return contents;} // contents accessor
|
||||
ArrayLLN<T> * getnext(){return next;} // next ptr accessor
|
||||
void setnext(ArrayLLN<T>* n){next = n;} // next ptr mutator
|
||||
|
||||
void removeself(ArrayLLN<T>* curr, ArrayLLN<T>* prev, ArrayLL<T>* l){ // deletes curr using prev, sets l's head if !prev
|
||||
ArrayLLN<T>*temp = curr->getnext();
|
||||
if (prev) prev->setnext(temp);
|
||||
else l->sethead(temp);
|
||||
curr->setnext(nullptr);
|
||||
delete curr;
|
||||
}
|
||||
|
||||
void addback(T stuff){ // adds new node at the end of the list.
|
||||
if (!next) next = new ArrayLLN<T>(stuff, nullptr);
|
||||
else next->addback(stuff);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,481 @@
|
|||
Listing ArrayLL.h...
|
||||
|
||||
//Ben Harris
|
||||
//Template for an linked list that can be used as an array.
|
||||
//Operator overload does not yet properly return the pointer for each node.
|
||||
|
||||
#ifndef _ARRAYLL_
|
||||
#define _ARRAYLL_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "ArrayLLN.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL{
|
||||
private:
|
||||
ArrayLLN<T> * head;
|
||||
public:
|
||||
ArrayLL();
|
||||
~ArrayLL();
|
||||
void add(T s,T l);
|
||||
int length();
|
||||
void remove(int i);
|
||||
void remove_0(T s);
|
||||
T& operator[] (const int i);
|
||||
void print();
|
||||
void sort();
|
||||
T at(int i);
|
||||
void push(T s);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
ArrayLL<T>::ArrayLL(){
|
||||
|
||||
head = NULL;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
ArrayLL<T>::~ArrayLL(){
|
||||
|
||||
delete head;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void ArrayLL<T>::add(T s,T l){
|
||||
|
||||
if (head && head->getcontents() == l) return;
|
||||
if (!head || converttolower(head->getcontents()) > l) head = new ArrayLLN<T>(s, head);
|
||||
else head = head->insert(s, l);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int ArrayLL<T>::length(){
|
||||
|
||||
int cnt = 0;
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (; P != nullptr; cnt++, Q = P, P = P->getnext());
|
||||
return cnt;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void ArrayLL<T>::remove(int i)
|
||||
{
|
||||
int cnt = 0;
|
||||
ArrayLLN<T> *P = NULL;
|
||||
for (; cnt < i; cnt++, P = P->getnext());
|
||||
P = P->remove(P);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void ArrayLL<T>::remove_0(T s){
|
||||
|
||||
if (head) head = head->remove_0(s, nullptr);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T ArrayLL<T>::at(int i){
|
||||
|
||||
ArrayLLN<T>* P = head;
|
||||
for (int cnt = 0; cnt < i; cnt++, P = P->getnext());
|
||||
return P->getcontents();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T& ArrayLL<T>::operator[](const int i){
|
||||
|
||||
return *at(i);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void ArrayLL<T>::print(){
|
||||
if (head) {
|
||||
sort(); head->print();
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void ArrayLL<T>::sort(){
|
||||
if (head){
|
||||
ArrayLLN<T>* current = head;
|
||||
ArrayLLN<T>* prev = nullptr;
|
||||
ArrayLLN<T>* end = head;
|
||||
for(; end; end = end->getnext());
|
||||
ArrayLLN<T>* tempnode = nullptr;
|
||||
bool flag = false;
|
||||
for (int i = 0; i < this->length(); i++){
|
||||
while (current->getnext()){
|
||||
tempnode = current->getnext();
|
||||
if (current->getcontents() > tempnode->getcontents()){
|
||||
flag = true;
|
||||
current->setnext(tempnode->getnext());
|
||||
tempnode->setnext(current);
|
||||
if (prev)
|
||||
prev->setnext(tempnode);
|
||||
prev = tempnode;
|
||||
if (head == current)
|
||||
head = tempnode;
|
||||
if (!current->getnext())
|
||||
end = current;
|
||||
|
||||
}else{
|
||||
prev = current;
|
||||
current = current->getnext();
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
break;
|
||||
else{
|
||||
prev = nullptr;
|
||||
current = head;
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <class T>
|
||||
void ArrayLL<T>::push(T s){
|
||||
ArrayLLN<T>* temp = head;
|
||||
head = new ArrayLLN<T>(s, temp);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Listing ArrayLLN.h...
|
||||
|
||||
//Ben Harris
|
||||
//List Node Template
|
||||
|
||||
#ifndef _ARRAYLLN_
|
||||
#define _ARRAYLLN_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLLN{
|
||||
private:
|
||||
T contents;
|
||||
ArrayLLN *next;
|
||||
public:
|
||||
ArrayLLN::ArrayLLN(T t, ArrayLLN* n){
|
||||
|
||||
contents = t;
|
||||
next = n;
|
||||
}
|
||||
|
||||
ArrayLLN::~ArrayLLN(){
|
||||
|
||||
delete next;
|
||||
}
|
||||
|
||||
void ArrayLLN::setcontents(T s){
|
||||
contents = s;
|
||||
}
|
||||
|
||||
T ArrayLLN::getcontents(){
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
ArrayLLN * ArrayLLN::getnext(){
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
void ArrayLLN::setnext(ArrayLLN* s){
|
||||
|
||||
next = s;
|
||||
}
|
||||
|
||||
ArrayLLN * ArrayLLN::add(T s, T l){
|
||||
|
||||
string lvalue = converttolower(contents);
|
||||
if (lvalue == l)return this;
|
||||
if (l < lvalue) return new ArrayLLN(s, this);
|
||||
if (!next){ next = new ArrayLLN(s, nullptr); return this; }
|
||||
next = next->add(s, l);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ArrayLLN * ArrayLLN::remove(ArrayLLN* s){
|
||||
|
||||
if (next) remove(s);
|
||||
ArrayLLN* temp = s;
|
||||
delete s;
|
||||
this->setnext(temp);
|
||||
return temp;
|
||||
}
|
||||
|
||||
ArrayLLN * ArrayLLN::remove_0(string s, ArrayLLN<T> *prev){
|
||||
string lvalue = converttolower(contents);
|
||||
if (next) next = next->remove_0(s, this);
|
||||
if (lvalue.find(s) < contents.length() && lvalue.find(s) >= 0){
|
||||
ArrayLLN<T> *t = next;
|
||||
next = nullptr;
|
||||
delete this;
|
||||
return t;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
void ArrayLLN::print(){
|
||||
cout << contents << endl;
|
||||
if (next) next->print();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG5.cpp...
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
string input = "";
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
ArrayLL<string>* list = new ArrayLL<string>();
|
||||
while (converttolower(input).substr(0, 4) != "exit"){
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
if (lower.substr(0, 3) == "add"){
|
||||
|
||||
list->push(input.substr(5,input.length()-7));
|
||||
}
|
||||
if (lower.substr(0, 5) == "print"){
|
||||
//list->sort();
|
||||
list->print();
|
||||
cout << endl << "There are " << list->length() << " items in the library." << endl;
|
||||
}
|
||||
if (lower.substr(0, 6) == "remove") list->remove_0(lower.substr(8, input.length() - 9));
|
||||
if (list && lower.substr(0, 5) == "clear"){ delete list; ArrayLL<string>* list = new ArrayLL<string>(); }
|
||||
|
||||
|
||||
/*if (lower.substr(0, 3) == "add") list->add(input.substr(5, input.length() - 6), lower.substr(5, input.length() - 6));
|
||||
if (lower.substr(0, 6) == "remove") list->remove_0(lower.substr(8, input.length() - 9));
|
||||
if (lower == "print") list->print();
|
||||
if (list && lower == "clear") { delete list; ArrayLL<string> * list = new ArrayLL<string>(); }*/
|
||||
|
||||
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "clear \t\t\t--clears the entire library" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string converttolower(string s){for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;}
|
||||
|
||||
|
||||
Listing PG5.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG5
|
||||
|
||||
#ifndef _PG5_
|
||||
#define _PG5_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Returns a version of the input string all lower case.
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG5.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG5.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG5.exe
|
||||
PG5.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG5.exe
|
||||
/OUT:PG5.exe
|
||||
PG5.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG5.exe < "C:\Grading\Classes\CS201-01-14F\PG5\PG5-1.in"
|
||||
cLibrary Version 1.0 -- type help for a list of commands
|
||||
Enter command:
|
||||
There are 0 items in the library.
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
There are 31 items in the library.
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
There are 31 items in the library.
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Mostly Harmles
|
||||
The Restaurant At The End Of The Univers
|
||||
|
||||
There are 33 items in the library.
|
||||
Enter command: Enter command:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
There are 31 items in the library.
|
||||
Enter command:
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Just getting started.
|
||||
|
||||
10/50.
|
||||
|
||||
Fix by Monday 17 November 2014.
|
||||
|
|
@ -0,0 +1,255 @@
|
|||
Listing ArrayLL.h...
|
||||
|
||||
//Ben Harris
|
||||
//Template for an linked list that can be used as an array.
|
||||
|
||||
#ifndef _ARRAYLL_
|
||||
#define _ARRAYLL_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "ArrayLLN.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL{
|
||||
private:
|
||||
ArrayLLN<T> * head;
|
||||
public:
|
||||
ArrayLL(){ head = NULL; }
|
||||
~ArrayLL(){ delete head; }
|
||||
int length(){
|
||||
int cnt = 0;
|
||||
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext());
|
||||
return cnt;
|
||||
}
|
||||
void remove(int i){
|
||||
for (int cnt = 0; cnt < i; cnt++, head = head->getnext());
|
||||
head = head->removeself();
|
||||
}
|
||||
T& operator[] (const int i){
|
||||
ArrayLLN<T>* P = head;
|
||||
for (int cnt = 0; cnt < i; cnt++, P = P->getnext());
|
||||
return P->getcontents();
|
||||
}
|
||||
void insert(int i, T stuff){
|
||||
if (i == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
|
||||
else if (i>0 && i < length()){
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (int j = 0; j < i-1; j++, Q = P, P = P->getnext());
|
||||
Q->setnext(new ArrayLLN<T>(stuff, P));
|
||||
}
|
||||
else if (i == length())
|
||||
if (head == NULL) head = new ArrayLLN<T>(stuff, NULL);
|
||||
else head->addback(stuff);
|
||||
else return;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing ArrayLLN.h...
|
||||
|
||||
//Ben Harris
|
||||
//List Node Template
|
||||
|
||||
#ifndef _ARRAYLLN_
|
||||
#define _ARRAYLLN_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLLN{
|
||||
private:
|
||||
T contents;
|
||||
ArrayLLN *next;
|
||||
public:
|
||||
|
||||
ArrayLLN(T t, ArrayLLN* n){
|
||||
contents = t;
|
||||
next = n;
|
||||
}
|
||||
~ArrayLLN(){delete next;}
|
||||
void setcontents(T s){contents = s;}
|
||||
T& getcontents(){return contents;}
|
||||
ArrayLLN * getnext(){return next;}
|
||||
void setnext(ArrayLLN* s){next = s;}
|
||||
|
||||
ArrayLLN * removeself(){
|
||||
ArrayLLN<T>*temp = next;
|
||||
this->setnext(nullptr);
|
||||
delete this;
|
||||
return temp;
|
||||
}
|
||||
void addback(T stuff){
|
||||
if (next == NULL) next = new ArrayLLN<T>(stuff, nullptr);
|
||||
else next->addback(stuff);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG5.cpp...
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
string input = "";
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
ArrayLL<string> list;
|
||||
|
||||
while (converttolower(input).substr(0, 4) != "exit"){
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
|
||||
if (lower.substr(0, 3) == "add"){
|
||||
|
||||
bool found = false;
|
||||
string lv = converttolower(input.substr(5, input.length() - 6));
|
||||
|
||||
for (int i = 0; i < list.length(); i++)
|
||||
if (converttolower(list[i]) == lv){
|
||||
cout << "That title already exists in the library." << endl;
|
||||
found = true;
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
|
||||
//for (; converttolower(list[j]) < lv;) j++;
|
||||
//cout << j << endl;
|
||||
|
||||
if (!found) list.insert(list.length(), input.substr(5, input.length() - 6));
|
||||
|
||||
}
|
||||
|
||||
if (lower.substr(0, 5) == "print"){
|
||||
cout << endl;
|
||||
|
||||
//sorts(list, 0, list.length());
|
||||
|
||||
for (int i = 0; i < list.length(); i++)
|
||||
cout << list[i] << endl;
|
||||
|
||||
cout << endl << "There are " << list.length() << " items in the library." << endl;
|
||||
}
|
||||
|
||||
if (lower.substr(0, 6) == "remove"){
|
||||
string lv = lower.substr(8, input.length() - 9);
|
||||
for (int i = 0; i < list.length(); i++)
|
||||
if (list[i].find(lv) >= 0 && list[i].find(lv) < list[i].length()){
|
||||
list.remove(i);
|
||||
cout << "Titles containing " << input.substr(8, input.length() - 9) << " were removed from the library." << endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (lower.substr(0, 5) == "clear"){
|
||||
list = {};
|
||||
cout << endl << "Library cleared." << endl << endl;
|
||||
}
|
||||
|
||||
|
||||
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "clear \t\t\t--clears the entire library" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
//delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sorts(ArrayLL<string> arr, int left, int right){
|
||||
int i = left, j = right;
|
||||
string tmp;
|
||||
string pivot = arr[(left + right) / 2];
|
||||
|
||||
while (i <= j){
|
||||
while (arr[i] < pivot) i++;
|
||||
while (arr[j] > pivot) j--;
|
||||
if (i <= j)
|
||||
{
|
||||
tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
if (left < j)sorts(arr, left, j);
|
||||
if (i < right)sorts(arr, i, right);
|
||||
}
|
||||
|
||||
string converttolower(string s){for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;}
|
||||
|
||||
|
||||
Listing PG5.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG5
|
||||
|
||||
#ifndef _PG5_
|
||||
#define _PG5_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Returns a version of the input string all lower case.
|
||||
void sorts(ArrayLL<string> arr, int left, int right); // Quick sort ArrayLL
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG5.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG5.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG5.exe
|
||||
PG5.cpp
|
||||
PG5.cpp(61) : error C2059: syntax error : '{'
|
||||
PG5.cpp(61) : error C2143: syntax error : missing ';' before '{'
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG5.exe < "C:\Grading\Classes\CS201-01-14F\PG5\PG5-1.in"
|
||||
'PG5.exe' is not recognized as an internal or external command,
|
||||
operable program or batch file.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
DOES NOT COMPILE.
|
||||
|
||||
5/50.
|
||||
|
||||
Fix by Friday 21 November 2014.
|
|
@ -0,0 +1,269 @@
|
|||
Listing ArrayLL.h...
|
||||
|
||||
//Ben Harris
|
||||
//Template for an linked list that can be used as a array.
|
||||
|
||||
#ifndef _ARRAYLL_
|
||||
#define _ARRAYLL_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "ArrayLLN.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL{
|
||||
private:
|
||||
ArrayLLN<T> * head;
|
||||
public:
|
||||
ArrayLL(){ head = NULL; }
|
||||
~ArrayLL(){ delete head; }
|
||||
int length(){
|
||||
int cnt = 0;
|
||||
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext());
|
||||
return cnt;
|
||||
}
|
||||
T remove(int i){
|
||||
for (int cnt = 0; cnt < i; cnt++, head = head->getnext());
|
||||
T tempcontents = head->getcontents();
|
||||
head = head->removeself();
|
||||
return tempcontents;
|
||||
}
|
||||
T& operator[] (const int i){
|
||||
ArrayLLN<T>* P = head;
|
||||
for (int cnt = 0; cnt < i; cnt++, P = P->getnext());
|
||||
return P->getcontents();
|
||||
}
|
||||
void insert(int i, T stuff){
|
||||
if (i == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
|
||||
else if (i>0 && i < length()){
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (int j = 0; j < i-1; j++, Q = P, P = P->getnext());
|
||||
if (Q) Q->setnext(new ArrayLLN<T>(stuff, P));
|
||||
else P->setnext(new ArrayLLN<T>(stuff, P->getnext()));
|
||||
}
|
||||
else if (i == length())
|
||||
if (head == NULL) head = new ArrayLLN<T>(stuff, NULL);
|
||||
else head->addback(stuff);
|
||||
else return;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing ArrayLLN.h...
|
||||
|
||||
//Ben Harris
|
||||
//List Node Template
|
||||
|
||||
#ifndef _ARRAYLLN_
|
||||
#define _ARRAYLLN_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLLN{
|
||||
private:
|
||||
T contents;
|
||||
ArrayLLN<T> *next;
|
||||
public:
|
||||
|
||||
ArrayLLN(T t, ArrayLLN<T>* n){
|
||||
contents = t;
|
||||
next = n;
|
||||
}
|
||||
~ArrayLLN(){delete next;}
|
||||
void setcontents(T s){contents = s;}
|
||||
T& getcontents(){return contents;}
|
||||
ArrayLLN<T> * getnext(){return next;}
|
||||
void setnext(ArrayLLN<T>* s){next = s;}
|
||||
|
||||
ArrayLLN<T> * removeself(){
|
||||
ArrayLLN<T>*temp = next;
|
||||
this->setnext(nullptr);
|
||||
delete this;
|
||||
return temp;
|
||||
}
|
||||
void addback(T stuff){
|
||||
if (next == NULL) next = new ArrayLLN<T>(stuff, nullptr);
|
||||
else next->addback(stuff);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG5.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//PG5 Main
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
string input = "";
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
ArrayLL<string> list;
|
||||
|
||||
while (converttolower(input).substr(0, 4) != "exit"){
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
|
||||
if (lower.substr(0, 3) == "add"){
|
||||
|
||||
bool found = false;
|
||||
string lv = converttolower(input.substr(5, input.length() - 6));
|
||||
|
||||
for (int i = 0; i < list.length(); i++) // Make sure no duplicates are entered
|
||||
if (converttolower(list[i]) == lv){
|
||||
cout << "That title already exists in the library." << endl;
|
||||
found = true;
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
for (j = 0; j < list.length(); j++){ // Find where in the list that it needs to be inserted
|
||||
if (converttolower(list[j]) > lv){
|
||||
break;
|
||||
}
|
||||
}
|
||||
cout << j << endl;
|
||||
if (!found) list.insert(j, input.substr(5, input.length() - 6));
|
||||
|
||||
}
|
||||
|
||||
if (lower.substr(0, 5) == "print"){
|
||||
cout << endl;
|
||||
|
||||
//sorts(list, 0, list.length());
|
||||
|
||||
for (int i = 0; i < list.length(); i++)
|
||||
cout << list[i] << endl;
|
||||
|
||||
cout << endl << "There are " << list.length() << " items in the library." << endl;
|
||||
}
|
||||
|
||||
if (lower.substr(0, 6) == "remove"){
|
||||
string lv = lower.substr(8, input.length() - 9);
|
||||
int l = list.length();
|
||||
for (int i = 0; i < list.length(); i++){
|
||||
string ll = converttolower(list[i]);
|
||||
if (ll.find(lv) >= 0 && ll.find(lv) < ll.length()){
|
||||
cout << "\"" << list.remove(i) << "\"" << " was removed from the library." << endl;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lower.substr(0, 5) == "clear"){
|
||||
list = {};
|
||||
cout << endl << "Library cleared." << endl << endl;
|
||||
}
|
||||
|
||||
|
||||
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "clear \t\t\t--clears the entire library" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
//delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sorts(ArrayLL<string> arr, int left, int right){
|
||||
int i = left, j = right;
|
||||
string tmp;
|
||||
string pivot = arr[(left + right) / 2];
|
||||
|
||||
while (i <= j){
|
||||
while (arr[i] < pivot) i++;
|
||||
while (arr[j] > pivot) j--;
|
||||
if (i <= j)
|
||||
{
|
||||
tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
if (left < j)sorts(arr, left, j);
|
||||
if (i < right)sorts(arr, i, right);
|
||||
}
|
||||
|
||||
string converttolower(string s){for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;}
|
||||
|
||||
|
||||
Listing PG5.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG5
|
||||
|
||||
#ifndef _PG5_
|
||||
#define _PG5_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Returns a version of the input string all lower case.
|
||||
void sorts(ArrayLL<string> arr, int left, int right); // Quick sort ArrayLL
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG5.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG5.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG5.exe
|
||||
PG5.cpp
|
||||
PG5.cpp(69) : error C2059: syntax error : '{'
|
||||
PG5.cpp(69) : error C2143: syntax error : missing ';' before '{'
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG5.exe < "C:\Grading\Classes\CS201-01-14F\PG5\PG5-1.in"
|
||||
'PG5.exe' is not recognized as an internal or external command,
|
||||
operable program or batch file.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
DOES NOT COMPILE.
|
||||
|
||||
5/50.
|
||||
|
||||
Fix by Friday 12 December 2014.
|
||||
|
||||
|
||||
Fix by F
|
|
@ -0,0 +1,388 @@
|
|||
Listing ArrayLL.h...
|
||||
|
||||
//Ben Harris
|
||||
//Template for an linked list that can be used as a array.
|
||||
|
||||
#ifndef _ARRAYLL_
|
||||
#define _ARRAYLL_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "ArrayLLN.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL{
|
||||
private:
|
||||
ArrayLLN<T> * head;
|
||||
public:
|
||||
ArrayLL(){ head = NULL; }
|
||||
~ArrayLL(){ delete head; }
|
||||
int length(){
|
||||
int cnt = 0;
|
||||
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext());
|
||||
return cnt;
|
||||
}
|
||||
T remove(int i){
|
||||
for (int cnt = 0; cnt < i; cnt++, head = head->getnext());
|
||||
T tempcontents = head->getcontents();
|
||||
head = head->removeself();
|
||||
return tempcontents;
|
||||
}
|
||||
T& operator[] (const int i){
|
||||
ArrayLLN<T>* P = head;
|
||||
for (int cnt = 0; cnt < i; cnt++, P = P->getnext());
|
||||
return P->getcontents();
|
||||
}
|
||||
void insert(int i, T stuff){
|
||||
if (i == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
|
||||
else if (i>0 && i < length()){
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (int j = 0; j < i-1; j++, Q = P, P = P->getnext());
|
||||
if (Q) Q->setnext(new ArrayLLN<T>(stuff, P));
|
||||
else P->setnext(new ArrayLLN<T>(stuff, P->getnext()));
|
||||
}
|
||||
else if (i == length())
|
||||
if (head == NULL) head = new ArrayLLN<T>(stuff, NULL);
|
||||
else head->addback(stuff);
|
||||
else return;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing ArrayLLN.h...
|
||||
|
||||
//Ben Harris
|
||||
//List Node Template
|
||||
|
||||
#ifndef _ARRAYLLN_
|
||||
#define _ARRAYLLN_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLLN{
|
||||
private:
|
||||
T contents;
|
||||
ArrayLLN<T> *next;
|
||||
public:
|
||||
|
||||
ArrayLLN(T t, ArrayLLN<T>* n){
|
||||
contents = t;
|
||||
next = n;
|
||||
}
|
||||
~ArrayLLN(){delete next;}
|
||||
void setcontents(T s){contents = s;}
|
||||
T& getcontents(){return contents;}
|
||||
ArrayLLN<T> * getnext(){return next;}
|
||||
void setnext(ArrayLLN<T>* s){next = s;}
|
||||
|
||||
ArrayLLN<T> * removeself(){
|
||||
ArrayLLN<T>*temp = next;
|
||||
this->setnext(nullptr);
|
||||
delete this;
|
||||
return temp;
|
||||
}
|
||||
void addback(T stuff){
|
||||
if (next == NULL) next = new ArrayLLN<T>(stuff, nullptr);
|
||||
else next->addback(stuff);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG5.cpp...
|
||||
|
||||
//Ben Harris
|
||||
//PG5 Main
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
string input = "";
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
ArrayLL<string> list;
|
||||
|
||||
while (converttolower(input).substr(0, 4) != "exit"){
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
|
||||
if (lower.substr(0, 3) == "add"){
|
||||
|
||||
bool found = false;
|
||||
string lv = converttolower(input.substr(5, input.length() - 6));
|
||||
|
||||
for (int i = 0; i < list.length(); i++) // Make sure no duplicates are entered
|
||||
if (converttolower(list[i]) == lv){
|
||||
cout << "That title already exists in the library." << endl;
|
||||
found = true;
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
for (j = 0; j < list.length(); j++){ // Find where in the list that it needs to be inserted
|
||||
if (converttolower(list[j]) > lv){
|
||||
break;
|
||||
}
|
||||
}
|
||||
cout << j << endl;
|
||||
if (!found) list.insert(j, input.substr(5, input.length() - 6));
|
||||
|
||||
}
|
||||
|
||||
if (lower.substr(0, 5) == "print"){
|
||||
cout << endl;
|
||||
|
||||
//sorts(list, 0, list.length());
|
||||
|
||||
for (int i = 0; i < list.length(); i++)
|
||||
cout << list[i] << endl;
|
||||
|
||||
cout << endl << "There are " << list.length() << " items in the library." << endl;
|
||||
}
|
||||
|
||||
if (lower.substr(0, 6) == "remove"){
|
||||
string lv = lower.substr(8, input.length() - 9);
|
||||
int l = list.length();
|
||||
for (int i = 0; i < list.length(); i++){
|
||||
string ll = converttolower(list[i]);
|
||||
if (ll.find(lv) >= 0 && ll.find(lv) < ll.length()){
|
||||
cout << "\"" << list.remove(i) << "\"" << " was removed from the library." << endl;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if (lower.substr(0, 5) == "clear"){
|
||||
// list = {};
|
||||
// cout << endl << "Library cleared." << endl << endl;
|
||||
//}
|
||||
|
||||
|
||||
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "clear \t\t\t--clears the entire library" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
//delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sorts(ArrayLL<string> arr, int left, int right){
|
||||
int i = left, j = right;
|
||||
string tmp;
|
||||
string pivot = arr[(left + right) / 2];
|
||||
|
||||
while (i <= j){
|
||||
while (arr[i] < pivot) i++;
|
||||
while (arr[j] > pivot) j--;
|
||||
if (i <= j)
|
||||
{
|
||||
tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
if (left < j)sorts(arr, left, j);
|
||||
if (i < right)sorts(arr, i, right);
|
||||
}
|
||||
|
||||
string converttolower(string s){for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;}
|
||||
|
||||
|
||||
Listing PG5.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG5
|
||||
|
||||
#ifndef _PG5_
|
||||
#define _PG5_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Returns a version of the input string all lower case.
|
||||
void sorts(ArrayLL<string> arr, int left, int right); // Quick sort ArrayLL
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG5.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG5.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG5.exe
|
||||
PG5.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG5.exe
|
||||
/OUT:PG5.exe
|
||||
PG5.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG5.exe < "C:\Grading\Classes\CS201-01-14F\PG5\PG5-1.in"
|
||||
cLibrary Version 1.0 -- type help for a list of commands
|
||||
Enter command:
|
||||
|
||||
There are 0 items in the library.
|
||||
Enter command: Enter command: 0
|
||||
Enter command: 1
|
||||
Enter command: 0
|
||||
Enter command: 2
|
||||
Enter command: 3
|
||||
Enter command: 5
|
||||
Enter command: 4
|
||||
Enter command: 1
|
||||
Enter command: 2
|
||||
Enter command: 3
|
||||
Enter command: 0
|
||||
Enter command: 6
|
||||
Enter command: 1
|
||||
Enter command: 3
|
||||
Enter command: 4
|
||||
Enter command: 5
|
||||
Enter command: 6
|
||||
Enter command: 7
|
||||
Enter command: 8
|
||||
Enter command: 19
|
||||
Enter command: 18
|
||||
Enter command: 1
|
||||
Enter command: 18
|
||||
Enter command: 1
|
||||
Enter command: 11
|
||||
Enter command: 10
|
||||
Enter command: That title already exists in the library.
|
||||
16
|
||||
Enter command: That title already exists in the library.
|
||||
22
|
||||
Enter command: That title already exists in the library.
|
||||
4
|
||||
Enter command: That title already exists in the library.
|
||||
16
|
||||
Enter command: That title already exists in the library.
|
||||
19
|
||||
Enter command:
|
||||
A
|
||||
b
|
||||
c
|
||||
D
|
||||
F
|
||||
G
|
||||
H
|
||||
J
|
||||
K
|
||||
m
|
||||
L
|
||||
n
|
||||
e
|
||||
o
|
||||
p
|
||||
i
|
||||
S
|
||||
r
|
||||
t
|
||||
u
|
||||
v
|
||||
q
|
||||
X
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
There are 26 items in the library.
|
||||
Enter command: "A" was removed from the library.
|
||||
Enter command: "b" was removed from the library.
|
||||
Enter command: "c" was removed from the library.
|
||||
Enter command: "D" was removed from the library.
|
||||
Enter command: "e" was removed from the library.
|
||||
Enter command: Enter command: Enter command: "i" was removed from the library.
|
||||
Enter command: Enter command:
|
||||
S
|
||||
r
|
||||
t
|
||||
u
|
||||
v
|
||||
q
|
||||
X
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
There are 10 items in the library.
|
||||
Enter command: 3
|
||||
Enter command: 4
|
||||
Enter command: 0
|
||||
Enter command: 3
|
||||
Enter command: 1
|
||||
Enter command: 0
|
||||
Enter command: "And Another Thing..." was removed from the library.
|
||||
"Life, The Universe, And Everything" was removed from the library.
|
||||
"So Long, And Thanks For All The Fish" was removed from the library.
|
||||
Enter command: "The Hitchhiker's Guide To The Galaxy" was removed from the library.
|
||||
Enter command:
|
||||
The Restaurant At The End Of The Universe
|
||||
t
|
||||
u
|
||||
v
|
||||
q
|
||||
X
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
There are 9 items in the library.
|
||||
Enter command: "The Restaurant At The End Of The Universe" was removed from the library.
|
||||
"t" was removed from the library.
|
||||
"u" was removed from the library.
|
||||
"v" was removed from the library.
|
||||
"q" was removed from the library.
|
||||
"X" was removed from the library.
|
||||
"w" was removed from the library.
|
||||
"y" was removed from the library.
|
||||
"z" was removed from the library.
|
||||
Enter command:
|
||||
|
||||
There are 0 items in the library.
|
||||
Enter command:
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Wrong answers. Doesn't look like everything gets added.
|
||||
|
||||
25/50.
|
|
@ -0,0 +1,349 @@
|
|||
Listing ArrayLL.h...
|
||||
|
||||
//Ben Harris
|
||||
//Template for an linked list that can be used as a array.
|
||||
|
||||
#ifndef _ARRAYLL_
|
||||
#define _ARRAYLL_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "ArrayLLN.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL{
|
||||
private:
|
||||
ArrayLLN<T> * head;
|
||||
public:
|
||||
ArrayLL(){ head = NULL; }
|
||||
~ArrayLL(){ delete head; }
|
||||
int length(){
|
||||
int cnt = 0;
|
||||
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext());
|
||||
return cnt;
|
||||
}
|
||||
T remove(int i){
|
||||
for (int cnt = 0; cnt < i; cnt++, head = head->getnext());
|
||||
T tempcontents = head->getcontents();
|
||||
head = head->removeself();
|
||||
return tempcontents;
|
||||
}
|
||||
T& operator[] (const int i){
|
||||
ArrayLLN<T>* P = head;
|
||||
for (int cnt = 0; cnt < i; cnt++, P = P->getnext());
|
||||
return P->getcontents();
|
||||
}
|
||||
void insert(int i, T stuff){
|
||||
if (i == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
|
||||
else if (i>0 && i < length()){
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (int j = 0; j < i-1; j++, Q = P, P = P->getnext());
|
||||
if (Q) Q->setnext(new ArrayLLN<T>(stuff, P));
|
||||
else P->setnext(new ArrayLLN<T>(stuff, P->getnext()));
|
||||
}
|
||||
else if (i == length())
|
||||
if (head == NULL) head = new ArrayLLN<T>(stuff, NULL);
|
||||
else head->addback(stuff);
|
||||
else return;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing ArrayLLN.h...
|
||||
|
||||
//Ben Harris
|
||||
//List Node Template
|
||||
|
||||
#ifndef _ARRAYLLN_
|
||||
#define _ARRAYLLN_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLLN{
|
||||
private:
|
||||
T contents;
|
||||
ArrayLLN<T> *next;
|
||||
public:
|
||||
|
||||
ArrayLLN(T t, ArrayLLN<T>* n){
|
||||
contents = t;
|
||||
next = n;
|
||||
}
|
||||
~ArrayLLN(){delete next;}
|
||||
void setcontents(T s){contents = s;}
|
||||
T& getcontents(){return contents;}
|
||||
ArrayLLN<T> * getnext(){return next;}
|
||||
void setnext(ArrayLLN<T>* s){next = s;}
|
||||
|
||||
ArrayLLN<T> * removeself(){
|
||||
ArrayLLN<T>*temp = next;
|
||||
this->setnext(nullptr);
|
||||
delete this;
|
||||
return temp;
|
||||
}
|
||||
void addback(T stuff){
|
||||
if (next == NULL) next = new ArrayLLN<T>(stuff, nullptr);
|
||||
else next->addback(stuff);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG5.cpp...
|
||||
|
||||
// Ben Harris
|
||||
// PG5 Main
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
string input = "";
|
||||
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
|
||||
ArrayLL<string> list;
|
||||
|
||||
while (converttolower(input).substr(0, 4) != "exit"){
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
|
||||
if (lower.substr(0, 3) == "add"){
|
||||
|
||||
bool found = false;
|
||||
string lv = converttolower(input.substr(5, input.length() - 6));
|
||||
|
||||
for (int i = 0; i < list.length(); i++) // Make sure no duplicates are entered
|
||||
if (converttolower(list[i]) == lv){
|
||||
cout << "That title already exists in the library." << endl;
|
||||
found = true;
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
for (j = 0; j < list.length(); j++){ // Find where in the list that it needs to be inserted
|
||||
if (converttolower(list[j]) > lv){
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) list.insert(j, input.substr(5, input.length() - 6));
|
||||
|
||||
}
|
||||
|
||||
if (lower.substr(0, 5) == "print"){
|
||||
cout << endl;
|
||||
|
||||
//sorts(list, 0, list.length());
|
||||
|
||||
for (int i = 0; i < list.length(); i++)
|
||||
cout << list[i] << endl;
|
||||
|
||||
cout << endl << "There are " << list.length() << " items in the library." << endl;
|
||||
}
|
||||
|
||||
if (lower.substr(0, 6) == "remove"){
|
||||
string lv = lower.substr(8, input.length() - 9);
|
||||
int l = list.length();
|
||||
for (int i = 0; i < list.length(); i++){
|
||||
string ll = converttolower(list[i]);
|
||||
if (ll.find(lv) >= 0 && ll.find(lv) < ll.length()){
|
||||
cout << "\"" << list.remove(i) << "\"" << " was removed from the library." << endl;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if (lower.substr(0, 5) == "clear"){
|
||||
// list = {};
|
||||
// cout << endl << "Library cleared." << endl << endl;
|
||||
//}
|
||||
|
||||
|
||||
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
//delete list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sorts(ArrayLL<string> arr, int left, int right){
|
||||
int i = left, j = right;
|
||||
string tmp;
|
||||
string pivot = arr[(left + right) / 2];
|
||||
|
||||
while (i <= j){
|
||||
while (arr[i] < pivot) i++;
|
||||
while (arr[j] > pivot) j--;
|
||||
if (i <= j)
|
||||
{
|
||||
tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
if (left < j)sorts(arr, left, j);
|
||||
if (i < right)sorts(arr, i, right);
|
||||
}
|
||||
|
||||
string converttolower(string s){for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;}
|
||||
|
||||
|
||||
Listing PG5.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG5
|
||||
|
||||
#ifndef _PG5_
|
||||
#define _PG5_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Returns a version of the input string all lower case.
|
||||
void sorts(ArrayLL<string> arr, int left, int right); // Quick sort ArrayLL
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG5.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG5.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG5.exe
|
||||
PG5.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG5.exe
|
||||
/OUT:PG5.exe
|
||||
PG5.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG5.exe < "C:\Grading\Classes\CS201-01-14F\PG5\PG5-1.in"
|
||||
cLibrary Version 1.0 -- type help for a list of commands
|
||||
Enter command:
|
||||
|
||||
There are 0 items in the library.
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: That title already exists in the library.
|
||||
Enter command: That title already exists in the library.
|
||||
Enter command: That title already exists in the library.
|
||||
Enter command: That title already exists in the library.
|
||||
Enter command: That title already exists in the library.
|
||||
Enter command:
|
||||
A
|
||||
b
|
||||
c
|
||||
D
|
||||
F
|
||||
G
|
||||
H
|
||||
J
|
||||
K
|
||||
m
|
||||
L
|
||||
n
|
||||
e
|
||||
o
|
||||
p
|
||||
i
|
||||
S
|
||||
r
|
||||
t
|
||||
u
|
||||
v
|
||||
q
|
||||
X
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
There are 26 items in the library.
|
||||
Enter command: "A" was removed from the library.
|
||||
Enter command: "b" was removed from the library.
|
||||
Enter command: "c" was removed from the library.
|
||||
Enter command: "D" was removed from the library.
|
||||
Enter command: "e" was removed from the library.
|
||||
Enter command: Enter command: Enter command: "i" was removed from the library.
|
||||
Enter command: Enter command:
|
||||
S
|
||||
r
|
||||
t
|
||||
u
|
||||
v
|
||||
q
|
||||
X
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
There are 10 items in the library.
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: "And Another Thing..." was removed from the library.
|
||||
"Life, The Universe, And Everything" was removed from the library.
|
||||
"So Long, And Thanks For All The Fish" was removed from the library.
|
||||
Enter command: "The Hitchhiker's Guide To The Galaxy" was removed from the library.
|
||||
Enter command:
|
||||
The Restaurant At The End Of The Universe
|
||||
t
|
||||
u
|
||||
v
|
||||
q
|
||||
X
|
||||
w
|
||||
y
|
||||
z
|
||||
|
||||
There are 9 items in the library.
|
||||
Enter command: "The Restaurant At The End Of The Universe" was removed from the library.
|
||||
"t" was removed from the library.
|
||||
"u" was removed from the library.
|
||||
"v" was removed from the library.
|
||||
"q" was removed from the library.
|
||||
"X" was removed from the library.
|
||||
"w" was removed from the library.
|
||||
"y" was removed from the library.
|
||||
"z" was removed from the library.
|
||||
Enter command:
|
||||
|
||||
There are 0 items in the library.
|
||||
Enter command:
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Still not everything is getting added.
|
||||
|
||||
25/50.
|
|
@ -0,0 +1,342 @@
|
|||
Listing ArrayLL.h...
|
||||
|
||||
//Ben Harris
|
||||
//Template for an linked list that can be used as a array.
|
||||
|
||||
#ifndef _ARRAYLL_
|
||||
#define _ARRAYLL_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "ArrayLLN.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL{
|
||||
private:
|
||||
ArrayLLN<T> * head;
|
||||
public:
|
||||
ArrayLL(){ head = nullptr; }
|
||||
~ArrayLL(){ delete head; }
|
||||
void sethead(ArrayLLN<T>* t){ head = t; }
|
||||
|
||||
int length(){ // returns length of the list
|
||||
int cnt = 0;
|
||||
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext());
|
||||
return cnt;
|
||||
}
|
||||
T remove(int pos){ // removes the node at pos
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (int cnt = 0; cnt < pos; cnt++, Q = P, P = P->getnext());
|
||||
T tempcontents = P->getcontents();
|
||||
P->removeself(P, Q, this);
|
||||
return tempcontents;
|
||||
}
|
||||
T& operator[] (const int pos){ // overloads [], which returns a reference to the node at pos
|
||||
ArrayLLN<T>* P = head;
|
||||
for (int cnt = 0; cnt < pos; cnt++, P = P->getnext());
|
||||
return P->getcontents();
|
||||
}
|
||||
void insert(int pos, T stuff){ // inserts a node at pos with stuff contents
|
||||
if (pos == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
|
||||
else if (pos > 0 && pos < length()){ // if inserting not at the head or tail
|
||||
ArrayLLN<T> *P = head, *Q = nullptr;
|
||||
for (int i = 0; i < pos; i++, Q = P, P = P->getnext());
|
||||
if (Q) Q->setnext(new ArrayLLN<T>(stuff, P));
|
||||
else P->setnext(new ArrayLLN<T>(stuff, P->getnext()));
|
||||
}
|
||||
else if (pos == length()) // if inserting at the tail.
|
||||
if (head == NULL) head = new ArrayLLN<T>(stuff, NULL);
|
||||
else head->addback(stuff);
|
||||
else return;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing ArrayLLN.h...
|
||||
|
||||
//Ben Harris
|
||||
//List Node Template
|
||||
|
||||
#ifndef _ARRAYLLN_
|
||||
#define _ARRAYLLN_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
template <class T> class ArrayLL;
|
||||
|
||||
template <class T> class ArrayLLN{
|
||||
private:
|
||||
T contents;
|
||||
ArrayLLN<T> *next;
|
||||
public:
|
||||
|
||||
ArrayLLN(T t, ArrayLLN<T>* n){ // Constructor
|
||||
contents = t;
|
||||
next = n;
|
||||
}
|
||||
~ArrayLLN(){delete next;} // Destructor
|
||||
void setcontents(T t){contents = t;} // contents mutator
|
||||
T& getcontents(){return contents;} // contents accessor
|
||||
ArrayLLN<T> * getnext(){return next;} // next ptr accessor
|
||||
void setnext(ArrayLLN<T>* n){next = n;} // next ptr mutator
|
||||
|
||||
void removeself(ArrayLLN<T>* curr, ArrayLLN<T>* prev, ArrayLL<T>* l){ // deletes curr using prev, sets l's head if !prev
|
||||
ArrayLLN<T>*temp = curr->getnext();
|
||||
if (prev) prev->setnext(temp);
|
||||
else l->sethead(temp);
|
||||
curr->setnext(nullptr);
|
||||
delete curr;
|
||||
}
|
||||
|
||||
void addback(T stuff){ // adds new node at the end of the list.
|
||||
if (!next) next = new ArrayLLN<T>(stuff, nullptr);
|
||||
else next->addback(stuff);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Listing PG5.cpp...
|
||||
|
||||
// Ben Harris
|
||||
// PG5 Main
|
||||
// This program recreates PG4 using a template of a Linked List.
|
||||
// The ArrayLL template interfaces with the linkedlist as if it were an array.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){ // PG5 main
|
||||
|
||||
string input = "";
|
||||
cout << "cLibrary Version 2.0 -- type help for a list of commands" << endl;
|
||||
ArrayLL<string> list;
|
||||
|
||||
while (converttolower(input).substr(0, 4) != "exit"){ // quit program when exit command is found
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
|
||||
if (lower.substr(0, 3) == "add"){ // insert a new node in alphabetical order, not allowing duplicates (case-insensitive)
|
||||
|
||||
bool found = false;
|
||||
string linputstr = converttolower(input.substr(5, input.length() - 6));
|
||||
|
||||
for (int i = 0; i < list.length(); i++) // Make sure no duplicates are entered
|
||||
if (converttolower(list[i]) == linputstr)
|
||||
found = true;
|
||||
|
||||
int j = 0;
|
||||
for (j = 0; j < list.length(); j++) // Find where in the list that it needs to be inserted
|
||||
if (converttolower(list[j]) > linputstr)
|
||||
break;
|
||||
|
||||
if (!found) list.insert(j, input.substr(5, input.length() - 6));
|
||||
|
||||
}
|
||||
|
||||
if (lower.substr(0, 5) == "print"){ // prints the entire list, which should be in alphabetical order
|
||||
cout << endl;
|
||||
for (int i = 0; i < list.length(); i++)
|
||||
cout << list[i] << endl;
|
||||
cout << endl << "There are " << list.length() << " titles in the library." << endl;
|
||||
}
|
||||
|
||||
if (lower.substr(0, 6) == "remove"){ // removes all the items in the library that contain search string
|
||||
string lv = lower.substr(8, input.length() - 9);
|
||||
for (int i = 0; i < list.length(); i++){
|
||||
string ll = converttolower(list[i]);
|
||||
if (ll.find(lv) >= 0 && ll.find(lv) < ll.length()){
|
||||
cout << "\"" << list.remove(i) << "\"" << " was removed from the library." << endl;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){ // prints info for each function
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
string converttolower(string s){for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;}
|
||||
|
||||
|
||||
Listing PG5.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for PG5
|
||||
|
||||
#ifndef _PG5_
|
||||
#define _PG5_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Returns a version of the input string all lower case.
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG5.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG5.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG5.exe
|
||||
PG5.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG5.exe
|
||||
/OUT:PG5.exe
|
||||
PG5.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG5.exe < "C:\Grading\Classes\CS201-01-14F\PG5\PG5-1.in"
|
||||
cLibrary Version 2.0 -- type help for a list of commands
|
||||
Enter command:
|
||||
|
||||
There are 0 titles in the library.
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
|
||||
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
|
||||
|
||||
There are 26 titles in the library.
|
||||
Enter command: "A" was removed from the library.
|
||||
Enter command: "b" was removed from the library.
|
||||
Enter command: "c" was removed from the library.
|
||||
Enter command: "D" was removed from the library.
|
||||
Enter command: "e" was removed from the library.
|
||||
Enter command: "F" was removed from the library.
|
||||
Enter command: "H" was removed from the library.
|
||||
Enter command: "i" was removed from the library.
|
||||
Enter command: "J" was removed from the library.
|
||||
Enter command:
|
||||
G
|
||||
K
|
||||
L
|
||||
m
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
S
|
||||
t
|
||||
u
|
||||
v
|
||||
w
|
||||
X
|
||||
y
|
||||
z
|
||||
|
||||
There are 17 titles in the library.
|
||||
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: "And Another Thing..." was removed from the library.
|
||||
"Life, The Universe, And Everything" was removed from the library.
|
||||
"So Long, And Thanks For All The Fish" was removed from the library.
|
||||
Enter command: "G" was removed from the library.
|
||||
"The Hitchhiker's Guide To The Galaxy" was removed from the library.
|
||||
Enter command:
|
||||
K
|
||||
L
|
||||
m
|
||||
Mostly Harmless
|
||||
n
|
||||
o
|
||||
p
|
||||
q
|
||||
r
|
||||
S
|
||||
t
|
||||
The Restaurant At The End Of The Universe
|
||||
u
|
||||
v
|
||||
w
|
||||
X
|
||||
y
|
||||
z
|
||||
|
||||
There are 18 titles in the library.
|
||||
Enter command: "K" was removed from the library.
|
||||
"L" was removed from the library.
|
||||
"m" was removed from the library.
|
||||
"Mostly Harmless" was removed from the library.
|
||||
"n" was removed from the library.
|
||||
"o" was removed from the library.
|
||||
"p" was removed from the library.
|
||||
"q" was removed from the library.
|
||||
"r" was removed from the library.
|
||||
"S" was removed from the library.
|
||||
"t" was removed from the library.
|
||||
"The Restaurant At The End Of The Universe" was removed from the library.
|
||||
"u" was removed from the library.
|
||||
"v" was removed from the library.
|
||||
"w" was removed from the library.
|
||||
"X" was removed from the library.
|
||||
"y" was removed from the library.
|
||||
"z" was removed from the library.
|
||||
Enter command:
|
||||
|
||||
There are 0 titles in the library.
|
||||
Enter command:
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
50/50.
|
|
@ -0,0 +1,72 @@
|
|||
// Ben Harris
|
||||
// PG5 Main
|
||||
// This program recreates PG4 using a template of a Linked List.
|
||||
// The ArrayLL template interfaces with the linkedlist as if it were an array.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "PG5.h"
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){ // PG5 main
|
||||
|
||||
string input = "";
|
||||
cout << "cLibrary Version 2.0 -- type help for a list of commands" << endl;
|
||||
ArrayLL<string> list;
|
||||
|
||||
while (converttolower(input).substr(0, 4) != "exit"){ // quit program when exit command is found
|
||||
|
||||
cout << "Enter command: ";
|
||||
getline(cin, input);
|
||||
string lower = converttolower(input);
|
||||
|
||||
if (lower.substr(0, 3) == "add"){ // insert a new node in alphabetical order, not allowing duplicates (case-insensitive)
|
||||
|
||||
bool found = false;
|
||||
string linputstr = converttolower(input.substr(5, input.length() - 6));
|
||||
|
||||
for (int i = 0; i < list.length(); i++) // Make sure no duplicates are entered
|
||||
if (converttolower(list[i]) == linputstr)
|
||||
found = true;
|
||||
|
||||
int j = 0;
|
||||
for (j = 0; j < list.length(); j++) // Find where in the list that it needs to be inserted
|
||||
if (converttolower(list[j]) > linputstr)
|
||||
break;
|
||||
|
||||
if (!found) list.insert(j, input.substr(5, input.length() - 6));
|
||||
|
||||
}
|
||||
|
||||
if (lower.substr(0, 5) == "print"){ // prints the entire list, which should be in alphabetical order
|
||||
cout << endl;
|
||||
for (int i = 0; i < list.length(); i++)
|
||||
cout << list[i] << endl;
|
||||
cout << endl << "There are " << list.length() << " titles in the library." << endl;
|
||||
}
|
||||
|
||||
if (lower.substr(0, 6) == "remove"){ // removes all the items in the library that contain search string
|
||||
string lv = lower.substr(8, input.length() - 9);
|
||||
for (int i = 0; i < list.length(); i++){
|
||||
string ll = converttolower(list[i]);
|
||||
if (ll.find(lv) >= 0 && ll.find(lv) < ll.length()){
|
||||
cout << "\"" << list.remove(i) << "\"" << " was removed from the library." << endl;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){ // prints info for each function
|
||||
cout << endl << "These are the commands that this program accepts: " << endl << endl;
|
||||
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
|
||||
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
|
||||
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
|
||||
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
string converttolower(string s){for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;}
|
|
@ -0,0 +1,16 @@
|
|||
// Ben Harris
|
||||
// Header for PG5
|
||||
|
||||
#ifndef _PG5_
|
||||
#define _PG5_
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include "ArrayLL.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv); // Main...
|
||||
string converttolower(string s); // Returns a version of the input string all lower case.
|
||||
|
||||
#endif
|
|
@ -0,0 +1,154 @@
|
|||
Listing PG6.cpp...
|
||||
|
||||
// Ben Harris
|
||||
// Main: Program6
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include "PG6.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string fn = "";
|
||||
cout << "Enter the name of the file that you want to sort." << endl << "<<";
|
||||
getline(cin, fn);
|
||||
ifstream f (fn);
|
||||
char ch = ' ';
|
||||
char* s = new char[5];
|
||||
f.read(s, 4);
|
||||
s[4] = 0;
|
||||
int sortsz = atoi(s);
|
||||
f.seekg(0, f.end);
|
||||
int end = (int)f.tellg();
|
||||
int cct = end - sortsz;
|
||||
int stringcnt = cct / sortsz;
|
||||
|
||||
f.seekg(4, f.beg);
|
||||
|
||||
char * allrecords = new char[sortsz];
|
||||
int pos = 0;
|
||||
while (true){
|
||||
allrecords[pos] = f.get();
|
||||
if (f.eof())break;
|
||||
pos++;
|
||||
}
|
||||
string allrecordstr = (string)allrecords;
|
||||
string * record_a = new string[stringcnt];
|
||||
for (int i = 0, p = 0; i < cct; p++, i += sortsz){
|
||||
record_a[p] = allrecordstr.substr(i, sortsz);
|
||||
}
|
||||
shellsort(record_a, stringcnt);
|
||||
for (int r = 0; r < stringcnt; r++){
|
||||
f >> record_a[r];
|
||||
}
|
||||
/*int d = stringcnt;
|
||||
while (d > 1){
|
||||
d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2;
|
||||
for (int i = d; i < stringcnt; i++)
|
||||
for (int j = i - d; j >= 0; j -= d);
|
||||
for (int q = 0; q < sortsz; q++);
|
||||
}*/
|
||||
|
||||
cout << "There were " << stringcnt << " records in the file, each of size " << sortsz << endl;
|
||||
|
||||
system("pause");
|
||||
}
|
||||
|
||||
void shellsort(string* s, int sz){
|
||||
int d = sz;
|
||||
while (d > 1){
|
||||
d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2;
|
||||
for (int i = d; i < sz; i++)
|
||||
for (int j = i - d; j >= 0 && s[j] > s[j + d]; j -= d){
|
||||
string t = s[j];
|
||||
s[j] = s[j + d];
|
||||
s[j + d] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Listing PG6.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for program 6
|
||||
|
||||
#ifndef _PG6_
|
||||
#define _PG6_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv);
|
||||
void shellsort(string* s, int sz);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG6.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG6.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG6.exe
|
||||
PG6.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG6.exe
|
||||
/OUT:PG6.exe
|
||||
PG6.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test1.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-1.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
<<
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test1.txt C:\Grading\Classes\CS201-02-14F\PG6\test1.out
|
||||
Files differ on line 1:5.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test2.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-2.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
<<There were 88 records in the file, each of size 1
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test2.txt C:\Grading\Classes\CS201-02-14F\PG6\test2.out
|
||||
Files differ on line 1:5.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test3.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-3.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
<<
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test3.txt C:\Grading\Classes\CS201-02-14F\PG6\test3.out
|
||||
Files differ on line 1:5.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Crashes every time.
|
||||
|
||||
5/50.
|
|
@ -0,0 +1,143 @@
|
|||
Listing PG6.cpp...
|
||||
|
||||
// Ben Harris
|
||||
// Main: Program6
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include "PG6.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string fn = "";
|
||||
streampos fsz;
|
||||
cout << "Enter the name of the file that you want to sort." << endl << "File Name: ";
|
||||
getline(cin, fn);
|
||||
fstream f (fn, ios::in | ios::out | ios::binary);
|
||||
if (f.is_open()){
|
||||
|
||||
char * recordsz = new char[4];
|
||||
f.read(recordsz, (streamsize) 4);
|
||||
int rsz = atoi(recordsz);
|
||||
f.seekg(0, f.end);
|
||||
fsz = f.tellg();
|
||||
int recordcnt = ((int)fsz-4) / rsz;
|
||||
f.seekg(0, f.beg);
|
||||
//cout << "File size: " << fsz << " Record size: " << rsz << " Number of records: " << recordcnt << endl;
|
||||
int d = recordcnt;
|
||||
while (d > 1){
|
||||
d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2;
|
||||
for (int i = d; i < recordcnt; i++)
|
||||
for (int j = i - d; j >= 0 && readstring(f,j,rsz) > readstring(f,j + d,rsz); j -= d){
|
||||
string t = readstring(f,j,rsz);
|
||||
writestring(f, j, rsz, readstring(f,j + d,rsz));
|
||||
writestring(f, j+d, rsz, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
else cout << "Error: file could not be opened or does not exist." << endl;
|
||||
system("pause");
|
||||
}
|
||||
|
||||
string readstring(fstream &f, int recordpos, int len){
|
||||
char* t = new char[len];
|
||||
f.seekg(recordpos*len + 4, ios::beg);
|
||||
f.read(t, len);
|
||||
return (string)t;
|
||||
}
|
||||
|
||||
void writestring(fstream &f, int recordpos, int len, string s){
|
||||
f.seekg(recordpos*len + 4, ios::beg);
|
||||
char * t = new char[s.length()];
|
||||
for (unsigned int i = 0; i < s.length(); i++) t[i] = s[i];
|
||||
f.write(t, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Listing PG6.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for program 6
|
||||
|
||||
#ifndef _PG6_
|
||||
#define _PG6_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv);
|
||||
//void shellsort(string* s, int sz);
|
||||
string readstring(fstream &f, int recordpos, int len);
|
||||
void writestring(fstream &f, int recordpos, int len, string s);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG6.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG6.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG6.exe
|
||||
PG6.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG6.exe
|
||||
/OUT:PG6.exe
|
||||
PG6.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test1.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-1.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
File Name: Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test1.txt C:\Grading\Classes\CS201-02-14F\PG6\test1.out
|
||||
Files are the same.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test2.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-2.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
File Name: Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test2.txt C:\Grading\Classes\CS201-02-14F\PG6\test2.out
|
||||
Files are the same.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test3.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-3.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
File Name: Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test3.txt C:\Grading\Classes\CS201-02-14F\PG6\test3.out
|
||||
Files are the same.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
Undercommented.
|
||||
|
||||
45/50.
|
|
@ -0,0 +1,147 @@
|
|||
Listing PG6.cpp...
|
||||
|
||||
// Ben Harris
|
||||
// Main: Program6
|
||||
// This program reads a file, uses the first four bytes as an integer record size and sorts the rest of the file in recordsz chunks.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include "PG6.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string fn = "";
|
||||
streampos fsz;
|
||||
cout << "Enter the name of the file that you want to sort." << endl << "File Name: ";
|
||||
getline(cin, fn); // Get file name from user
|
||||
fstream f (fn, ios::in | ios::out | ios::binary); // Open file from input location for read and write and as binary
|
||||
if (f.is_open()){
|
||||
|
||||
char * recordsz = new char[4];
|
||||
f.read(recordsz, (streamsize) 4); // Read first four bytes
|
||||
int rsz = atoi(recordsz); // Store first four bytes as int record size
|
||||
f.seekg(0, f.end);
|
||||
fsz = f.tellg(); // Get length of file
|
||||
int recordcnt = ((int)fsz-4) / rsz; // Get # of records
|
||||
f.seekg(0, f.beg);
|
||||
|
||||
// Begin shell sort
|
||||
int d = recordcnt;
|
||||
while (d > 1){
|
||||
d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2;
|
||||
for (int i = d; i < recordcnt; i++) // Use readstring and writestring methods to get and put strings in the file
|
||||
for (int j = i - d; j >= 0 && readstring(f,j,rsz) > readstring(f,j + d,rsz); j -= d){
|
||||
string t = readstring(f,j,rsz);
|
||||
writestring(f, j, rsz, readstring(f,j + d,rsz));
|
||||
writestring(f, j+d, rsz, t);
|
||||
}
|
||||
}
|
||||
cout << "Sort successful." << endl;
|
||||
f.close();
|
||||
}
|
||||
else cout << "Error: file could not be opened or does not exist." << endl;
|
||||
system("pause");
|
||||
}
|
||||
|
||||
string readstring(fstream &f, int recordpos, int len){ // This method seeks to the position of recordpos * len, reads len bytes and returns that string
|
||||
char* t = new char[len];
|
||||
f.seekg(recordpos*len + 4, ios::beg);
|
||||
f.read(t, len);
|
||||
return (string)t;
|
||||
}
|
||||
|
||||
void writestring(fstream &f, int recordpos, int len, string s){ // Writes string s to f at position recordpos*len
|
||||
f.seekp(recordpos*len + 4, ios::beg);
|
||||
char * t = new char[s.length()];
|
||||
for (unsigned int i = 0; i < s.length(); i++) t[i] = s[i];
|
||||
f.write(t, len);
|
||||
}
|
||||
|
||||
|
||||
Listing PG6.h...
|
||||
|
||||
// Ben Harris
|
||||
// Header for program 6
|
||||
// This program reads a file, uses the first four bytes as an integer record size and sorts the rest of the file in recordsz chunks.
|
||||
|
||||
#ifndef _PG6_
|
||||
#define _PG6_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv);
|
||||
string readstring(fstream &f, int recordpos, int len);
|
||||
void writestring(fstream &f, int recordpos, int len, string s);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG6.exe
|
||||
Microsoft Windows [Version 6.1.7601]
|
||||
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
||||
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG6.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG6.exe
|
||||
PG6.cpp
|
||||
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
/out:PG6.exe
|
||||
/OUT:PG6.exe
|
||||
PG6.obj
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
||||
Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test1.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-1.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
File Name: Sort successful.
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test1.txt C:\Grading\Classes\CS201-02-14F\PG6\test1.out
|
||||
Files are the same.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test2.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-2.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
File Name: Sort successful.
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test2.txt C:\Grading\Classes\CS201-02-14F\PG6\test2.out
|
||||
Files are the same.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test3.txt" .
|
||||
1 file(s) copied.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-3.in"
|
||||
Enter the name of the file that you want to sort.
|
||||
File Name: Sort successful.
|
||||
Press any key to continue . . .
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>cmp test3.txt C:\Grading\Classes\CS201-02-14F\PG6\test3.out
|
||||
Files are the same.
|
||||
|
||||
C:\Users\apoe\Desktop\Grading Folder>
|
||||
C:\Users\apoe\Desktop\Grading Folder>exit
|
||||
|
||||
50/50.
|
|
@ -0,0 +1,59 @@
|
|||
// Ben Harris
|
||||
// Main: Program6
|
||||
// This program reads a file, uses the first four bytes as an integer record size and sorts the rest of the file in recordsz chunks.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include "PG6.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
string fn = "";
|
||||
streampos fsz;
|
||||
cout << "Enter the name of the file that you want to sort." << endl << "File Name: ";
|
||||
getline(cin, fn); // Get file name from user
|
||||
fstream f (fn, ios::in | ios::out | ios::binary); // Open file from input location for read and write and as binary
|
||||
if (f.is_open()){
|
||||
|
||||
char * recordsz = new char[4];
|
||||
f.read(recordsz, (streamsize) 4); // Read first four bytes
|
||||
int rsz = atoi(recordsz); // Store first four bytes as int record size
|
||||
f.seekg(0, f.end);
|
||||
fsz = f.tellg(); // Get length of file
|
||||
int recordcnt = ((int)fsz-4) / rsz; // Get # of records
|
||||
f.seekg(0, f.beg);
|
||||
|
||||
// Begin shell sort
|
||||
int d = recordcnt;
|
||||
while (d > 1){
|
||||
d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2;
|
||||
for (int i = d; i < recordcnt; i++) // Use readstring and writestring methods to get and put strings in the file
|
||||
for (int j = i - d; j >= 0 && readstring(f,j,rsz) > readstring(f,j + d,rsz); j -= d){
|
||||
string t = readstring(f,j,rsz);
|
||||
writestring(f, j, rsz, readstring(f,j + d,rsz));
|
||||
writestring(f, j+d, rsz, t);
|
||||
}
|
||||
}
|
||||
cout << "Sort successful." << endl;
|
||||
f.close();
|
||||
}
|
||||
else cout << "Error: file could not be opened or does not exist." << endl;
|
||||
system("pause");
|
||||
}
|
||||
|
||||
string readstring(fstream &f, int recordpos, int len){ // This method seeks to the position of recordpos * len, reads len bytes and returns that string
|
||||
char* t = new char[len];
|
||||
f.seekg(recordpos*len + 4, ios::beg);
|
||||
f.read(t, len);
|
||||
return (string)t;
|
||||
}
|
||||
|
||||
void writestring(fstream &f, int recordpos, int len, string s){ // Writes string s to f at position recordpos*len
|
||||
f.seekp(recordpos*len + 4, ios::beg);
|
||||
char * t = new char[s.length()];
|
||||
for (unsigned int i = 0; i < s.length(); i++) t[i] = s[i];
|
||||
f.write(t, len);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
// Ben Harris
|
||||
// Header for program 6
|
||||
// This program reads a file, uses the first four bytes as an integer record size and sorts the rest of the file in recordsz chunks.
|
||||
|
||||
#ifndef _PG6_
|
||||
#define _PG6_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv);
|
||||
string readstring(fstream &f, int recordpos, int len);
|
||||
void writestring(fstream &f, int recordpos, int len, string s);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue