| | |
Solution for Big Integer used Linked - List.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
main.cpp:
and the VeryLongInt.h
Many things i dont know up to now, but i seem so usual.
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
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.
Say It Like It Is.
Có sao nói vậy.
Có sao nói vậy.
![]() |
Similar Threads
- Recrusive Split on a linked list (C)
- help me on linked list (C)
- sum of the linked list (C)
- Linked list (C++)
- To find whether Single Linked List is looped. (C)
- remove method linked list (C)
- Linked List Retrieve Method (C)
- Linked List & Objects (C++)
Other Threads in the C++ Forum
- Previous Thread: Compilation process
- Next Thread: c++ functions to compare numbers
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






