This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:rmatsch:portfolio:cprogproject3 [2012/04/21 21:18] – [Code] rmatsch | user:rmatsch:portfolio:cprogproject3 [2012/04/29 19:06] (current) – [Objectives] rmatsch | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | |||
+ | A project for C++ by Robert Matsch during the spring 2012. | ||
+ | |||
+ | =====Objectives===== | ||
+ | Take c code and put into c++ code with multiple header files along with adding 2 new functions. | ||
+ | |||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * successful completion of project #1 and solid understanding of pertinent topics | ||
+ | * successful implementation of addition and subtraction from project #2 in working functions | ||
+ | * familiarity with memory allocation via **malloc(3)** | ||
+ | * familiarity with memory, accessing data via pointer dereferencing, | ||
+ | * familiarity with looking up C function parameters/ | ||
+ | * familiarity with C++ classes | ||
+ | * familiarity with functions, their parameters and return types | ||
+ | * familiarity with multi-file programs, how to make and build them | ||
+ | |||
+ | |||
+ | =====Scope===== | ||
+ | Project #2 was to be an awesome exploration of array manipulation and functions, built atop a comfortable yet easy foundation of simple mathematics. | ||
+ | |||
+ | As it turns out, procrastination and refusal to work out ideas on paper are killer obstacles. | ||
+ | |||
+ | This project will therefore ebrace and extend upon project #2, where you will finish implementing code to support the storage and manipulation of numbers outside of the established data types. And once you have that, we'll do some additional modifications to reflect concepts covered is class. | ||
+ | |||
+ | So, for this project I'd like for you to: | ||
+ | |||
+ | * have working addition, subtraction, | ||
+ | * definitely get multiplication and division working | ||
+ | * also implement a **modulus** and **exponent** function | ||
+ | * verify successful operation with numbers of length 8, 16, 24, and 32 | ||
+ | * split your code up into **multiple files** (have at least one header file, a main.c, and two additional C files with various functions in them) | ||
+ | * have these **multiple files** successfully compile and operate just as your monolithic code would | ||
+ | * ALSO (aka "in addition to" your C solution, I'd like you to also implement a **class-based solution** in monolithic and multiple files in **C++**). So you will have a pure C implementation AND a class-based C++ implementation. | ||
+ | |||
+ | Some helpful hints: | ||
+ | |||
+ | * WORK IT OUT ON PAPER. | ||
+ | * WORK IT OUT ON PAPER. | ||
+ | * WORK IT OUT ON PAPER. | ||
+ | * WORK IT OUT ON PAPER. | ||
+ | * get the C version working before you even start on the C++ (it'll make more sense) | ||
+ | * when you get to coding, be sure to use the debugger to see what is actually happening | ||
+ | * chars are just numbers | ||
+ | * strings can make things complicated | ||
+ | * just focus on chars being numbers | ||
+ | * if confused, WORK IT OUT ON PAPER. | ||
+ | |||
+ | If you don't understand what "WORK IT OUT ON PAPER" means, it means to go through several STEP-BY-STEP iterations BY HAND of some of the very math operations you'd expect your program to ultimately perform. | ||
+ | |||
+ | Try it out for yourself- pick two arbitrary 8-digit numbers, and ADD them together. BY HAND. Note how you calculate the individual sums and carries. Watch how the carries propagate from right to left. | ||
+ | |||
+ | Do the some for subtraction, | ||
+ | |||
+ | Can you define multiplication in terms of addition? | ||
+ | |||
+ | Can you define division in terms of subtraction? | ||
+ | |||
+ | =====Code===== | ||
+ | prog3.cc | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | int main() | ||
+ | { | ||
+ | functions bignum; | ||
+ | unsigned char i, x,counter, operation, *n1, *n2, *n3, z, a, y,choice; | ||
+ | n1=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | n2=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | n3=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | x = fgetc(stdin); | ||
+ | counter=0; | ||
+ | while(x != ' | ||
+ | *(n1+counter)= x; | ||
+ | x = fgetc(stdin); | ||
+ | counter++; | ||
+ | }printf(" | ||
+ | y = fgetc(stdin); | ||
+ | counter=0; | ||
+ | while(y != ' | ||
+ | *(n2+counter)= y; | ||
+ | y = fgetc(stdin); | ||
+ | counter++; | ||
+ | }printf(" | ||
+ | printf(" | ||
+ | choice=0; | ||
+ | operation = fgetc(stdin); | ||
+ | choice = operation-48; | ||
+ | while (3 >= choice >= 0){ | ||
+ | if (choice ==0){ | ||
+ | bignum.sum(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | } | ||
+ | }if (choice ==1){ | ||
+ | bignum.subtraction(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | } | ||
+ | }if (choice ==2){ | ||
+ | bignum.multiply(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | } | ||
+ | }if (choice ==3){ | ||
+ | bignum.divide(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | } | ||
+ | }if (choice ==4){ | ||
+ | bignum.modulus(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | }if (choice ==5){ | ||
+ | bignum.expo(n1, | ||
+ | for(a=0; | ||
+ | printf(" | ||
+ | }if (choice ==6){ | ||
+ | exit(1); | ||
+ | } | ||
+ | printf(" | ||
+ | choice=0; | ||
+ | operation = fgetc(stdin); | ||
+ | operation = fgetc(stdin); | ||
+ | choice = operation-48; | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | Functions.h header file | ||
+ | < | ||
+ | #ifndef _FUNCTIONS_H | ||
+ | #define _FUNCTIONS_H | ||
+ | |||
+ | |||
+ | |||
+ | class functions{ | ||
+ | public: | ||
+ | void sum(unsigned char*, | ||
+ | void subtraction(unsigned char*, | ||
+ | void multiply(unsigned char*, | ||
+ | void divide(unsigned char*, | ||
+ | int compare(unsigned char*, | ||
+ | void modulus(unsigned char*, | ||
+ | |||
+ | }; | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | and functions.cc | ||
+ | < | ||
+ | #include " | ||
+ | #include < | ||
+ | |||
+ | |||
+ | |||
+ | int functions:: | ||
+ | { | ||
+ | int a; | ||
+ | unsigned char flag; | ||
+ | flag=0; | ||
+ | for (a=23; a>=0; a--){ | ||
+ | if (*(n2+a)=*(zero+a)){ | ||
+ | flag=0; | ||
+ | }else{ | ||
+ | flag=1; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | return(flag); | ||
+ | } | ||
+ | |||
+ | void functions:: | ||
+ | { | ||
+ | int a, z, i, x; | ||
+ | for (a= 23; a>=0; a--){ | ||
+ | *(n3+a) = 0; | ||
+ | } | ||
+ | for(a=23; a >= 0; a--){ | ||
+ | z = *(n3+a) + (*(n1+a)+ *(n2+a))-96; | ||
+ | if (z>9){ | ||
+ | i = z-10; | ||
+ | n3[a] = i; | ||
+ | n3[a-1] = n3[a-1]+1; | ||
+ | }if (z<9){ | ||
+ | n3[a]=z; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | void functions:: | ||
+ | { | ||
+ | int a, z, i, x; | ||
+ | for (a= 23; a>=0; a--){ | ||
+ | *(n3+a) = 0; | ||
+ | } | ||
+ | for(a=23; a>=0; a--){ | ||
+ | |||
+ | z = *(n3+a) + (*(n1+a) - *(n2+a)); | ||
+ | if (z>=0){ | ||
+ | n3[a]=z; | ||
+ | }else{ | ||
+ | n3[a-1]=n3[a-1]-1; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | void functions:: | ||
+ | { | ||
+ | unsigned char *zero, *one, *num1value; | ||
+ | int a, z, i, x; | ||
+ | |||
+ | zero=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | one=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | num1value=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(num1value+a) = *(n1+a); | ||
+ | *(zero+a)=0; | ||
+ | *(n1+a)=0; | ||
+ | *(one+a)= 0; | ||
+ | } | ||
+ | *(one+23)= 1; | ||
+ | while (compare(n2, | ||
+ | sum(n1, | ||
+ | subtraction(n2, | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(n3+a)=*(n2+a); | ||
+ | }for (a=23; a>=0; a--){ | ||
+ | *(n3+a)=*(n2+a); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | void functions:: | ||
+ | { | ||
+ | |||
+ | | ||
+ | int a, z, i, x; | ||
+ | |||
+ | zero=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | one=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | num1value=(unsigned char *)malloc(sizeof(unsigned char)*24); | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(num1value+a) = *(n1+a); | ||
+ | *(zero+a)=0; | ||
+ | *(n1+a)=0; | ||
+ | *(one+a)= 0; | ||
+ | } | ||
+ | *(one+23)= 1; | ||
+ | while (compare(n2, | ||
+ | | ||
+ | subtraction(n2, | ||
+ | for (a=23; a>=0; a--){ | ||
+ | *(n3+a)=*(n2+a); | ||
+ | }for (a=23; a>=0; a--){ | ||
+ | *(n3+a)=*(n2+a); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | void funtions:: | ||
+ | { | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | void functions:: | ||
+ | { | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | </ | ||
+ | =====Execution===== | ||
+ | <cli> | ||
+ | lab46: | ||
+ | please enter a number up to 24 digits long padded with zeros: | ||
+ | example: 000000000000000000000008 = 8 | ||
+ | 000000000000000000000015 | ||
+ | please enter another number up to 24 digits long padded with zeros: | ||
+ | 000000000000000000000003 | ||
+ | Addition, subtraction, | ||
+ | Enter 0 for Addition, 1 for Subtraction, | ||
+ | 0 | ||
+ | 000000000000000000000018 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 1 | ||
+ | 000000000000000000000012 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 2 | ||
+ | 000000000000000000000045 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 3 | ||
+ | 000000000000000000000005 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 4 | ||
+ | 000000000000000000000000 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 5 | ||
+ | 000000000000000000003375 | ||
+ | if you would like to perform other operations: 0 for addition, 1 for subtraction, | ||
+ | 6 | ||
+ | lab46: | ||
+ | </ | ||
+ | =====Reflection===== | ||
+ | Comments/ | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | |||