cs201/PG1/PG1-1.out

177 lines
5.5 KiB
Plaintext

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.