User Tools

Site Tools


user:skinney1:portfolio:cprogproject3

Project: BIG NUM REDUX

A project for COURSENAME by YOUR NAME during the SEMESTER YEAR.

This project was begun on DATE and is anticipated to take TIME UNIT to complete. Project was completed on MONTH DAY, YEAR.

Objectives

State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?

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

Background

State the idea or purpose of the project. What are you attempting to pursue?

You'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK.

Providing any links to original source material, such as from a project page, is a good idea.

You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.)

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

Code for my reworked calc

#include <stdio.h>
#include <math.h>
#include <float.h>
 
int main()
{
        float y, x;
        char z;
 
        printf("___SHANES's CALC___\n");
 
        int t = star();
 
        printf("FiRsT nUmBeR      ||\n");
                scanf("%f",&y);
                getchar();
 
        printf("SiGiN?            ||\n");
                scanf("%c",&z);
                getchar();
 
        printf("sEcOnD nUmBeR     ||\n");
                scanf("%f",&x);
                getchar();
 
        while (1==1)
                {
        if (z=='*')
                {
                y=y*x;
                printf("-------------------\n");
                printf("%.1f\n",y);
                int i = print();
                break;
                }
        if (z=='/')
                {
                y=y/x;
                printf("-------------------\n");
                printf("%.1f\n",y);
                printf("-------------------\n");
                int s = ship();
                break;
                }
        if (z=='+')
                {
                y=y+=x;
                printf("-------------------\n");
                printf("%.1f\n",y);
                printf("-------------------\n");
                break;
                }
        if (z=='-')
                {
                y=y-=x;
                printf("-------------------\n");
                printf("%f\n",y);
                printf("-------------------\n");
                break;
                }
        if (z=='^')
                {
                y= pow(y,x);
                printf("-------------------\n");
                printf("%1f\n",y);
                printf("-------------------\n");
                break;
                }
        if (z=='%')//modulus
                {
                y=y-x*(y/x);
                printf("-------------------\n");
                printf("The Mod is: %.1f\n",y);
                printf("-------------------\n");
                int u = skull();
                break;
                }
 
        }
        return(0);
}

Execution

The code running

lab46:~/src/cprog/classproject$ ./tester.out
___SHANES's CALC___
VVVVVVVVVVVVVVVVVVVV
____________________
FiRsT nUmBeR      ||
12
SiGN?             ||
*
sEcOnD nUmBeR     ||
2
-------------------
24.0
-------------------

The Code running with some fun add ons

lab46:~/src/cprog/classproject$ ./tester.out
___SHANES's CALC___
VVVVVVVVVVVVVVVVVVVV
____________________
FiRsT nUmBeR      ||
12
SiGiN?            ||
/
sEcOnD nUmBeR     ||
2
-------------------
6.0
-------------------
                  _.--| CCC ||
                 <____|.----||
                        .---''---,
                         ;..__..'    _...
                       ,'/  :|/ .--''   :
                     ,'_/.-/':         :   :
                _..-'''/     |         : _|/|
                :     /- /_  ;        ,;'    :
               , :   /  :  `: |        /    `:`.
             ,'   : /-._;   | :    : ::    ,.   .
           ,'     ::   /`-._| |    | || ' :  `.`.:
        _,'       |:._:: |  | |    | `|   :    `'
      ,'   `.     /   |`-:_ : |    |  |  :
      `--.   )   /|-._:    :          |   :
         /  /   :_|   :`-._:   __..--':    :
        /  (    :|: _  _    -:'o |   /     ' :
       /  , :._ _ _  --'' _|:|___|_,'        :
      :  /   `'-'--'----'---------'          :
      | :     O ._O   O_. O ._O   O_.      : :
      : `.       /    /     /     /     ,' :
    ~~~`.______ /____/_____/_____/_______,'~
               /    / ~   /    /
       ~~   _ /   _/    _/ ~ _/     ~
     ~     / /   / /   / /   / /  ~      ~~
          ~~~   ~~~   ~~~   ~~~

Reflection

First i have to say my code from the last project just… was the wrong way to go about it. I re-wrote my code and found that floating points really helped slim things out. Second, i made some ASCII art to be called out from within the code and set them into their own files. I ended with 5 files all named as test. My program started out this way and i just never changed the names. SO main is test, then testa,b,c, etc.

lab46:~/src/cprog/classproject$ gcc -lm test.c testa.c testb.c testd.c testc.c -c lab46:~/src/cprog/classproject$ gcc -lm test.o testa.o testb.o testd.o testc.o -o tester.out

My code did not meet the requirement of being able to handle any larger numbers as the float data type cannot do it. I did complete the project with everything but that.. After meeting i worked on the code for awhile and came to a standpoint. Needing to move on to the next assignment. I am posting what i have completed and plan to return when i can.

#include <stdio.h>
#include <stdlib.h>
int main()
{
 char length;
 char pos;
 char carry;
 char sign;
 char junk;
 unsigned char *value1;
 value1 = (unsigned char *) malloc (sizeof(unsigned char) * length);
 unsigned char *value2;
 value2 = (unsigned char *) malloc (sizeof(unsigned char) * length);
 unsigned char *value3;
 value3 = (unsigned char *) malloc (sizeof(unsigned char) * length);


        printf("how many place vaules?:\n");
                scanf("%c", &length);

        printf("First number:\n");

                scanf("%d");

                for(pos=0;pos++;)
                        {
                        *(value1+pos)=getchar();
                        junk = length+1; getchar();
                        }

        printf("Second number:\n");

                scanf("%d");

                for(pos=0;pos++;)
                        {
                        *(value2+pos)=getchar();
                        junk = (length+1); getchar();
                        }
        printf("whatcha wanna do?:\n");
                scanf("%d", &sign);

//add it and junk - line # 32
        if(sign = '+')
                {
                carry=0;
                for(pos=length-1;pos>=0;pos--)
                        *(value3+pos)=*(value1+pos)+*(value2+pos)+carry-96;
                        if(*(value3+pos)>9)
                                {
                                carry=1;
                                *(value3+pos)=*(value3+pos)-10;
                                }
                else
                        {
                        carry=0;
                        }
        printf("%hhd\n", value3);

                }

                return (0);
}

References

In performing this project, the following resources were referenced:

user/skinney1/portfolio/cprogproject3.txt · Last modified: 2012/04/28 22:13 by skinney1