User Tools

Site Tools


user:rmatsch:portfolio:cprogproject3

Project: BIG NUM REDUX

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/perform this project, the listed resources/experiences need to be consulted/achieved:

  • 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, and address calculation
  • familiarity with looking up C function parameters/information in the manual
  • 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, multiplication, and division functions that can easily and transparently work with numbers of any length
  • 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, multiplication, division, modulus, and exponent.

Can you define multiplication in terms of addition?

Can you define division in terms of subtraction?

Code

prog3.cc

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include "functions.h"
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("please enter a number up to 24 digits long padded with zeros:\n");
        printf("example: 000000000000000000000008 = 8\n");
        x = fgetc(stdin);
        counter=0;
        while(x != '\n'){
                *(n1+counter)= x;
                x = fgetc(stdin);
                counter++;
        }printf("please enter another number up to 24 digits long padded with zeros:\n");
        y = fgetc(stdin);
        counter=0;
        while(y != '\n'){
                *(n2+counter)= y;
                y = fgetc(stdin);
                counter++;
        }printf("Addition, subtraction, multiplying, Dividing, modulus, exponential\n");
        printf("Enter 0 for Addition, 1 for Subtractiom, 2 for Multiplying, and 3 for Division, 4 for modulus, 5 for exponential and  6 for exit :\n");
        choice=0;
        operation = fgetc(stdin);
        choice = operation-48;
        while  (3 >= choice >= 0){
                if (choice ==0){
                        bignum.sum(n1,n2,n3);
                        for(a=0;a<24;a++){
                                printf("%c",n3[a]+48);
                        }
                }if (choice ==1){
                        bignum.subtraction(n1,n2,n3);
                        for(a=0;a<24;a++){
                                printf("%c",n3[a]+48);
                        }
                }if (choice ==2){
                        bignum.multiply(n1,n2,n3);
                        for(a=0;a<24;a++){
                                printf("%c",n3[a]+48);
                        }
                }if (choice ==3){
                        bignum.divide(n1,n2,n3);
                        for(a=0;a<24;a++){
                                printf("%c",n3[a]+48);
                        }
                }if (choice ==4){
                        bignum.modulus(n1,n2,n3);
                        for(a=0;a<24;a++){
                                printf("%c",n3[a]+48);
                }if (choice ==5){
                        bignum.expo(n1,n2,n3);
                        for(a=0;a<24;a++){
                                printf("%c",n3[a]+48);
                }if (choice ==6){
                        exit(1);
                }
                printf("\nif you would like to perform other operations: 0 for addition, 1 for subtraction, 2 for multiply, 3 for division, 4 for modulus and  5 to exi$
                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*,unsigned char*,unsigned char*);
                void subtraction(unsigned char*,unsigned char*,unsigned char*);
                void multiply(unsigned char*,unsigned char*,unsigned char*);
                void divide(unsigned char*,unsigned char*,unsigned char*);
                int compare(unsigned char*,unsigned char*);
                void modulus(unsigned char*,unsigned char*);

};




#endif

and functions.cc

#include "functions.h"
#include <stdlib.h>



int functions::compare(unsigned char *n2,unsigned char *zero)
{
        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::sum(unsigned char *n1,unsigned char *n2, unsigned char *n3)
{
        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::subtraction(unsigned char *n1,unsigned char *n2,unsigned char *n3)
{
        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::multiply(unsigned char *n1,unsigned char *n2, unsigned char *n3)
{
        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,zero) != 0){
                sum(n1,num1value,n3);
                subtraction(n2,one,n3);
                for (a=23; a>=0; a--){
                        *(n3+a)=*(n2+a);
                }for (a=23; a>=0; a--){
                        *(n3+a)=*(n2+a);
                }
        }
}
void functions::divide(unsigned char *n1,unsigned char *n2, unsigned char *n3)
{

         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,zero) != 0){
 subtraction(n1,num1value,n3);
                        subtraction(n2,one,n3);
                for (a=23; a>=0; a--){
                        *(n3+a)=*(n2+a);
                }for (a=23; a>=0; a--){
                        *(n3+a)=*(n2+a);
                }
        }
}
void funtions::modulus(unsigned char *n1,unsigned char *n2, unsigned char *n3)
{





}
void functions::expo(unsigned char *n1,unsigned char *n2, unsigned char *n3)
{



}

Execution

lab46:~/src/cprog/project2$ ./prog2f
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, multiplying, Dividing,modulus, exponential
Enter 0 for Addition, 1 for Subtraction, 2 for Multiplying, and 3 for Division 4 for modulus 5 for exponential and 6 to exit program:
0
000000000000000000000018
if you would like to perform other operations: 0 for addition, 1 for subtraction, 2 for multiply, 3 for division,4 for modulus 5 for exponential and 6 to exit program:
1
000000000000000000000012
if you would like to perform other operations: 0 for addition, 1 for subtraction, 2 for multiply, 3 for division,4 for modulus 5 for exponential and 6 to exit program:
2
000000000000000000000045
if you would like to perform other operations: 0 for addition, 1 for subtraction, 2 for multiply, 3 for division,4 for modulus 5 for exponential and 6 to exit program:
3
000000000000000000000005
if you would like to perform other operations: 0 for addition, 1 for subtraction, 2 for multiply, 3 for division,4 for modulus 5 for exponential and 6 to exit program:
4
000000000000000000000000
if you would like to perform other operations: 0 for addition, 1 for subtraction, 2 for multiply, 3 for division,4 for modulus 5 for exponential and 6 to exit program:
5
000000000000000000003375
if you would like to perform other operations: 0 for addition, 1 for subtraction, 2 for multiply, 3 for division,4 for modulus 5 for exponential and 6 to exit program:
6
lab46:~/src/cprog/project2$

Reflection

Comments/thoughts generated through performing the project: How simple some task are when we do them but how difficult they become when programming them.

References

In performing this project, the following resources were referenced:

  • Google
user/rmatsch/portfolio/cprogproject3.txt · Last modified: 2012/04/29 15:06 by rmatsch