DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Solution for Big Integer used Linked - List. (http://www.daniweb.com/forums/thread92618.html)

vincent551987vn Oct 11th, 2007 3:53 pm
Solution for Big Integer used Linked - List.
 
main.cpp:

#include <stdio.h>

#include "VeryLongInt.h"

int InitializeFromFile(char* fileName, VeryLongInt* &x, VeryLongInt* &y);
//void ShowScreen();

int main(int argc, char* argv[]){
        char* inputFile;
        char* outputFile;
        if (argc < 2){
                printf("Usage: LongIntDataType.exe [InputFile] [OutputFile]\n");
                printf("Using default configuration: LongIntDataType.exe input.txt output.txt\n\n");
                inputFile = "input.txt";
                outputFile = "output.txt"; // "output/output.txt"
        } else {
                inputFile = argv[1];
                if (argc == 2)
                        outputFile = "output.txt";
                else
                        outputFile = argv[2];
        }

        VeryLongInt *x, *y, *z;
        int succ = InitializeFromFile(inputFile, x, y);
        if (!succ) return 0;

        z = VeryLongInt::Add(x, y);
        z->SaveToFile(outputFile, 0);
        delete z;

        z = VeryLongInt::Subtract(x, y);
        z->SaveToFile(outputFile, 1);
        delete z;

        z = VeryLongInt::Multiply(x, y);
        z->SaveToFile(outputFile, 1);
        delete z;

        z = VeryLongInt::Div(x, y);
        if (z != NULL){
                z->SaveToFile(outputFile, 1);
                delete z;
        } // else FW: write NULL to file for checking students' result

        z = VeryLongInt::Mod(x, y);
        if (z != NULL) {
                z->SaveToFile(outputFile, 1);
                delete z;
        } // else FW: write NULL to file for checking students' result

        // Calculate x^10
        z = VeryLongInt::Power(x, 10);
        z->SaveToFile(outputFile, 1);
        delete z;

        delete x;
        delete y;

//        ShowScreen();

        return 1;
}

// Note: I have not optimized this function yet. Anyways, it works
// It's easier to use fgets function but it's not efficient for memory.
int InitializeFromFile(char* fileName, VeryLongInt* &x, VeryLongInt* &y){
        FILE *fptr;
        char* buffer;
        char c;
        fopen_s(&fptr, fileName, "r");
        if (fptr == NULL){
                printf("Can't open file [%s]", fileName);
                return 0;
        }
        // Read the first number
        int count = 0;
        int ok = 1;
        while (!feof(fptr) && ok){
                c = fgetc(fptr);
                if (c != '\n')
                        count++;
                else ok = 0;
        }
        fseek(fptr,0, 0);
        buffer = new char[count+1];// +1 for NULL character
        buffer[count] = '\0';
        count = 0;
        ok = 1;
        while (!feof(fptr) && ok){
                c = fgetc(fptr);
                if (c != '\n'){
                        buffer[count] = c;
                        count++;
                } else ok = 0;
        }
        x = VeryLongInt::Parse(buffer);
        delete buffer;

        // Read the second number
        int secondCount = 0;
        ok = 1;
        while (!feof(fptr) && ok){
                c = fgetc(fptr);
                if (c != '\n'){
                        secondCount++;
                } else ok = 0;
        }
        buffer = new char[secondCount+1];// +1 for NULL character
        buffer[secondCount] = '\0';

        fseek(fptr, count+2, 0);
        secondCount = 0;
        ok = 1;
        while (!feof(fptr) && ok){
                c = fgetc(fptr);
                if (c != '\n'){
                        buffer[secondCount] = c;
                        secondCount++;
                } else ok = 0;
        }
        y = VeryLongInt::Parse(buffer);
        delete buffer;
        fclose(fptr);
        return 1;
}


void ShowScreen(){
        VeryLongInt *x = new VeryLongInt(998);
        VeryLongInt *y = new VeryLongInt(9);
        VeryLongInt *z;

        printf("Simulation of operators between 2 very long integers\n");
        printf("Sum:\t\t");
        z = VeryLongInt::Add(x, y);
        x->Print(); printf(" + "); y->Print();
        printf(" = "); z->Print(); printf("\n");
        delete z;

        printf("Subtract:\t");
        z = VeryLongInt::Subtract(x, y);
        x->Print(); printf(" - "); y->Print();
        printf(" = "); z->Print(); printf("\n");
        delete z;

        printf("Multiply:\t");
        x->Print(); printf(" * "); y->Print();
        z = VeryLongInt::Multiply(x, y);
        printf(" = "); z->Print(); printf("\n");
        delete z;

        printf("DIV:\t\t");
        x->Print(); printf(" / "); y->Print();
        z = VeryLongInt::Div(x, y);
        printf(" = "); z->Print(); printf("\n");
        delete z;

        printf("MOD:\t\t");
        z = VeryLongInt::Mod(x, y);
        x->Print(); printf(" %% "); y->Print();
        printf(" = "); z->Print(); printf("\n");
        delete z;

        delete x;
        delete y;

        x = VeryLongInt::Parse("-23234");
        y = VeryLongInt::Parse("256");

        if (VeryLongInt::Compare(x, y) == -1){
                printf("First number is less than second number.\n");
        } else
                printf("First number isnot less than second number.\n");

        z = VeryLongInt::Add(x, y);
        printf("Sum:\t\t");
        x->Print(); printf(" + "); y->Print(); printf(" = "); z->Print();
        printf("\n");
        delete z;
        printf("Subtract:\t");
        z = VeryLongInt::Subtract(x, y);
        x->Print(); printf(" - "); y->Print(); printf(" = "); z->Print();
        printf("\n");
        delete z;

        x->Print(); printf("^10 = ");
        z = VeryLongInt::Power(x, 10);
        char *temp = z->ToString();
        printf("%s\n", temp);
        delete temp;

        delete z;

        delete x;
        delete y;
}

and the VeryLongInt.h

class VeryLongInt{
public:
        VeryLongInt();
        VeryLongInt(int val); // convert an integer into VeryLongInt object
        ~VeryLongInt();

        VeryLongInt* Clone(); // return another copy of current object (replicate the current object)
        int GetSign(); // return sign of current number
        void ToggleSign(); // toggle sign of current number
        char* ToString(); // convert current object into string
        void Print(); // output current number to console by using printf() function
        int SaveToFile(char* fileName, int appendFlag);// output to file. appendFlag: +1: append, 0: overwrite

        static VeryLongInt* Add(VeryLongInt *x, VeryLongInt *y); // x + y
        static VeryLongInt* Subtract(VeryLongInt *x, VeryLongInt *y); // x - y
        static VeryLongInt* Multiply(VeryLongInt *x, VeryLongInt *y); // x * y
        static VeryLongInt* Div(VeryLongInt *x, VeryLongInt *y); // x div y
        static VeryLongInt* Mod(VeryLongInt *x, VeryLongInt *y); // x % y
        static VeryLongInt* Power(VeryLongInt *x, int n); // x^n
        static int EqualTo(VeryLongInt *x, VeryLongInt *y); // x == y?
        static int Compare(VeryLongInt *x, VeryLongInt *y); // return +1 if x > y; 0 if x = y; -1 if x < y
        static VeryLongInt* Parse(char *str); // convert a string into a very long integer
        // Add your neccessary methods here ...
};


Many things i dont know up to now, but i seem so usual.

iamthwee Oct 11th, 2007 5:04 pm
Re: Solution for Big Integer used Linked - List.
 
So is that actually your program or did you copy it of someone.

Narue Oct 11th, 2007 5:16 pm
Re: Solution for Big Integer used Linked - List.
 
>So is that actually your program or did you copy it of someone.
I'd wager that since he went from nothing (knowing nothing) to that in two hours, he copied it from somewhere.

iamthwee Oct 11th, 2007 5:25 pm
Re: Solution for Big Integer used Linked - List.
 
That's what I though from looking at his previous threads. He he. So I guess that means we aren't going to point out all of his mistakes. Ha ha.

vincent551987vn Oct 12th, 2007 12:00 am
Re: Solution for Big Integer used Linked - List.
 
He he. Maybe, but nowhere i can copy that, and this my doing's Big Brother ^^. I only want to take ideas from around ^^.

===========
To open ur eyes to see more and more intensively.


All times are GMT -4. The time now is 5:40 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC