diff options
Diffstat (limited to 'Bachelor/Prog1/Prakt6/prg1p6_3/int40.cpp')
| -rw-r--r-- | Bachelor/Prog1/Prakt6/prg1p6_3/int40.cpp | 344 |
1 files changed, 344 insertions, 0 deletions
diff --git a/Bachelor/Prog1/Prakt6/prg1p6_3/int40.cpp b/Bachelor/Prog1/Prakt6/prg1p6_3/int40.cpp new file mode 100644 index 0000000..16e3f67 --- /dev/null +++ b/Bachelor/Prog1/Prakt6/prg1p6_3/int40.cpp @@ -0,0 +1,344 @@ +// Programmieren 1, Praktikum 6, Aufgabe 3
+// Author: Sven Eisenhauer
+// Date: 17.01.2005
+// File: int40.cpp
+// Description: Implementation of class Integer40
+
+#include "int40.h"
+#include <cstdlib>
+#include <ctime>
+#include <iostream>
+#include <new>
+
+using std::cout;
+using std::endl;
+using std::cin;
+using std::ostream;
+using std::istream;
+
+
+
+ostream& operator<<(ostream &out, Integer40 &right)
+{
+ right.write();
+
+ return out;
+}
+
+istream& operator>> (istream &in, Integer40 &right)
+{
+ right.read();
+
+ return in;
+}
+
+Integer40& operator+ (const Integer40& left, const Integer40& right)
+{
+ static Integer40 temp;
+
+ int sum=0;
+ int carry=0;
+
+ for (int i=ARRAYSIZE-1;i>=0;i--)
+ {
+ sum=(left.int40[i]+right.int40[i]+carry);
+ if (sum >= 10)
+ {
+ temp.int40[i]=sum%10;
+ carry=1;
+ }
+ else
+ {
+ temp.int40[i]=sum;
+ carry=0;
+ }
+ }
+
+ return temp;
+
+}
+Integer40& operator- (const Integer40& left, const Integer40& right)
+{
+ static Integer40 temp;
+
+ int diff=0;
+ int carry=0;
+
+ for (int i=ARRAYSIZE-1;i>=0;i--)
+ {
+ if (right.int40[i] > left.int40[i])
+ {
+ temp.int40[i]=(left.int40[i]+10)-right.int40[i]-carry;
+ carry=1;
+ }
+ else
+ {
+ temp.int40[i]=left.int40[i]-right.int40[i]-carry;
+ carry=0;
+ }
+
+ }
+
+ return temp;
+}
+
+Integer40::Integer40()
+{
+ int40=new int[ARRAYSIZE];
+ for (int n=0;n<ARRAYSIZE;n++)
+ int40[n]=0;
+// ++nrInt40;
+}
+
+Integer40::Integer40(const Integer40 &toCopy)
+{
+ int40=new int[ARRAYSIZE];
+ for (int n=0;n<ARRAYSIZE;n++)
+ int40[n]=toCopy.int40[n];
+}
+
+Integer40::~Integer40()
+{
+ delete[] int40;
+ //--nrInt40;
+}
+
+const Integer40& Integer40::read()
+{
+ char input[ARRAYSIZE];
+ int i=0;
+ int n=0;
+ int temp[ARRAYSIZE];
+ cout << endl << "Please enter Integer40: ";
+ cin.get(input,ARRAYSIZE);
+ cin.sync();
+ for (n=ARRAYSIZE-strlen(input);n>=0;n--)
+ {
+ temp[n]=0;
+ }
+ i=strlen(input)-1;
+ for (n=ARRAYSIZE-1;n>=ARRAYSIZE-strlen(input);n--)
+ {
+ int40[n]=input[i]-48;
+ i--;
+ }
+ return *this;
+}
+
+const Integer40& Integer40::write()
+{
+ cout << endl;
+ for (int n=0;n<ARRAYSIZE;n++)
+ cout << int40[n];
+ cout << endl;
+
+ return *this;
+}
+
+const Integer40& Integer40::add(const Integer40 &toAdd)
+{
+ int sum=0;
+ int carry=0;
+
+ for (int i=ARRAYSIZE-1;i>=0;i--)
+ {
+ sum=(int40[i]+toAdd.int40[i]+carry);
+ int40[i]=sum%10;
+ if (sum >= 10)
+ carry=1;
+ else
+ carry=0;
+ }
+ return *this;
+}
+
+/*const Integer40& Integer40::operator+(const Integer40 &toAdd)
+{
+ add(toAdd);
+
+ return *this;
+}
+*/
+const Integer40& Integer40::operator+=(const Integer40 &toAdd)
+{
+ add(toAdd);
+
+ return *this;
+}
+
+const Integer40& Integer40::substract(const Integer40 &toSub)
+{
+ int temp=0;
+
+ for (int i=ARRAYSIZE-1;i>=0;i--)
+ {
+ if (toSub.int40[i] > int40[i])
+ {
+ int40[i]=((int40[i])+10)-toSub.int40[i];
+ (int40[i-1])--;
+ }
+ else
+ int40[i]=((int40[i]))-toSub.int40[i];
+
+ }
+ return *this;
+}
+
+/*const Integer40& Integer40::operator-(const Integer40 &toSub)
+{
+ substract(toSub);
+
+ return *this;
+}*/
+
+const Integer40& Integer40::operator-=(const Integer40 &toSub)
+{
+ substract(toSub);
+
+ return *this;
+}
+
+const Integer40& Integer40::operator =(const Integer40 &right)
+{
+ if(&right!=this)
+ {
+ for (int n=0;n<ARRAYSIZE;n++)
+ int40[n]=right.int40[n];
+ }
+
+ return *this;
+}
+
+
+bool Integer40::isGreaterOrEqual(const Integer40 &toCompare) const
+{
+ int i=0;
+ bool retval=true;
+
+ while ((i<ARRAYSIZE)&&(int40[i]==toCompare.int40[i]))
+ {
+ i++;
+ }
+ if (int40[i] < toCompare.int40[i])
+ {
+ retval=false;
+ }
+ return retval;
+}
+
+bool Integer40::operator>=(const Integer40 &toCompare) const
+{
+ return isGreaterOrEqual(toCompare);
+}
+
+bool Integer40::isSmallerOrEqual(const Integer40 &toCompare) const
+{
+ int i=0;
+ bool retval=true;
+
+ while ((i<ARRAYSIZE)&&(int40[i]==toCompare.int40[i]))
+ {
+ i++;
+ }
+ if (int40[i] > toCompare.int40[i])
+ {
+ retval=false;
+ }
+ return retval;
+}
+
+bool Integer40::operator<=(const Integer40 &toCompare) const
+{
+ return isSmallerOrEqual(toCompare);
+}
+
+
+bool Integer40::isGreaterThan(const Integer40 &toCompare) const
+{
+ int i=0;
+ bool retval=true;
+
+ while ((i<ARRAYSIZE)&&(int40[i]==toCompare.int40[i]))
+ {
+ i++;
+ }
+ if (int40[i] <= toCompare.int40[i])
+ {
+ retval=false;
+ }
+ return retval;
+}
+bool Integer40::operator>(const Integer40 &toCompare) const
+{
+ return isGreaterThan(toCompare);
+}
+
+bool Integer40::isSmallerThan(const Integer40 &toCompare) const
+{
+ int i=0;
+ bool retval=true;
+
+ while ((i<ARRAYSIZE)&&(int40[i]==toCompare.int40[i]))
+ {
+ i++;
+ }
+ if (int40[i] >= toCompare.int40[i])
+ {
+ retval=false;
+ }
+ return retval;
+}
+
+bool Integer40::operator<(const Integer40 &toCompare) const
+{
+ return isSmallerThan(toCompare);;
+}
+
+bool Integer40::isEqual(const Integer40 &toCompare) const
+{
+ int i=0;
+ bool retval=true;
+
+ while ((i<ARRAYSIZE)&&(int40[i]==toCompare.int40[i]))
+ {
+ i++;
+ }
+ if (int40[i] != toCompare.int40[i])
+ {
+ retval=false;
+ }
+ return retval;
+}
+
+bool Integer40::operator ==(const Integer40 &toCompare) const
+{
+ return isEqual(toCompare);
+}
+
+bool Integer40::isNotEqual(const Integer40 &toCompare) const
+{
+ return !(*this==toCompare);
+}
+
+bool Integer40::operator !=(const Integer40 &toCompare) const
+{
+ return !(*this==toCompare);
+}
+
+bool Integer40::isZero()
+{
+ int i=0;
+ bool retval=true;
+
+ while (i<ARRAYSIZE)
+ {
+ if(this->int40[i]!=0)
+ {
+ retval=false;
+ break;
+ }
+ i++;
+ }
+
+ return retval;
+}
\ No newline at end of file |
