1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/******************************************
* Project: BigNCalc
* File: BigN.h
******************************************/
#ifndef BIGN_H
#define BIGN_H
#include <string>
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
class BigN {
public:
/**
* Basic constructor.
*/
BigN();
/**
* Adds the current BigN to the inputed BigN
*/
bool operator+(const BigN right);
/**
* Mltiplys the current BigN to the inputed BIgN
*/
bool operator*(const BigN right);
/**
* Assignes the value of a BigN
*/
// bool operator=(string val);
// define in and output //
friend ostream & operator<< (ostream & out, BigN right);
friend istream & operator>> (istream & in, BigN right);
private:
// Variables //
static const int MAX = 10;
bool overFlow;
int digits[MAX];
// Methods //
/**
* Finds the first non-zero of the string of
*/
int FirstNZ();
/**
* Assigns a new value to the BigN
* @param val,
*/
bool setBigN(const string val);
/**
* Adds two BigN's
*/
void add(BigN left, BigN right, BigN &answer);
/**
* Multiplies two BigN's
*/
void mult(BigN left, BigN right, BigN &answer);
/**
* Prints out the BigN
* @param width, the number of characters to print
*/
void print(int width);
};
#endif /* BIGN_H */
/********************************************
* Project: BigNCalc
* File: BigN.cpp
********************************************/
/**
* Class File for the BigN Class
*/
#include "BigN.h"
#include <string>
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
/**
* Constructor to make a BigN full of 0's
*/
BigN::BigN() {
for (int i = 0; i < MAX; i++) {
digits[i] = 0; //sets the arrays to 0
}
}
/**
* Constructs a BigN with a value
*/
BigN::BigN(const string val) {
int nZ = val.find_first_of("123456789");
if (nZ != -1) {
for (int c = val.length() - 1, j = 0; c >= nZ && !overFlow; c--, j++) {
if (j == MAX) { //numbers are too big
overFlow = true;
return;
} else { //else within size limits
char l = val.at(c);
digits[j] = l - '0'; //assgin given value
}
}
}
}
/**
* Finds the first non-zero character of the BigN to trim leading 0's away
*/
int BigN::FirstNZ(const BigN me) {
bool found = false;
int rtv = 0;
int i = MAX - 1;
while (!found) {
if (me.digits[i] != 0) {
rtv = i; //cycle tell find value greater than 0
found = true;
}
--i;
if (i == 0) {
found = true; //sets array as 0
}
}
return rtv;
}
/**
* Adds two BigN's together.
*/
void BigN::add(BigN left, BigN right) {
BigN temp;
int carry = 0;
for (int i = 0; i <= MAX; i++) {
int val = left.digits[i] + right.digits[i] + carry;
int dig = val % 10; //determines value
carry = val / 10; //determines carry
temp.digits[i] = dig;
}
for (int j = 0; j <= MAX; j++) { //reassign values
digits[j] = temp.digits[j];
}
if (carry != 0) { //if number is too big
overFlow = true;
}
}
/**
* Multiplies two BigN's together
* @pre values are >= 0
* @param left, value on the left side of the opperator
* @param right, value on the right side of the opperator
* @param answer, resault of multiplying the left and right values
* @return the Answer in a BigN
*/
void BigN::mult(BigN left, BigN right) {
int firstDigit = FirstNZ(left);
int secondDigit = FirstNZ(right);
if (firstDigit + secondDigit >= MAX + 1) {
overFlow = true;
return;
}
int carry = 0;
for (int d = 0; d <= firstDigit + 1; d++) {
BigN temp;
for (int sD = 0; sD <= secondDigit; sD++) {
int val = (left.digits[d] * right.digits[sD]) + carry;
int rD = val % 10; //determines value
carry = val / 10; //determines carry
temp.digits[sD + d] = rD;
}
add(*this, temp);
if (overFlow) { //if number is too big
return;
}
}
}
/**
* Detects the Multiply opperator calls the mult method.
* @param right, the BigN to multiply this one by
*/
bool BigN::operator*(const BigN right) {
mult(digits, right);
}
/**
* Detects the Add opperator calls the add method.
* @param right, the BigN to add to this one
*/
bool BigN::operator+(const BigN right) {
add(digits, right);
}
/**
*
* @param val
* @return
*/
//bool BigN::operator=(const string val){
setBigN(val);
}
/**
* Sets the value of a BigN
* @param val, inputed value as a string
*/
bool BigN::setBigN(const string val) {
bool set = false;
int nZ = val.find_first_of("123456789");
if (nZ != -1) {
for (int c = val.length() - 1, j = 0; c >= nZ && !overFlow; c--, j++) {
if (j == MAX) {
overFlow = true; //if number is too big
return;
} else {
char l = val.at(c);
digits[j] = l - '0'; //sets value
}
}
}
}
/**
* Prints out the BigN
* @param width, the number of characters to print
*/
void BigN::print(int width) {
for (int i = width; i >= 0; i--) { //trims 0
cout << digits[i]; //print numbers
}
}
/********************************************
* Project: BigNCalc
* File: main.cpp
********************************************/
#include <cstdlib>
#include "BigN.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/**
* Finds the first non-zero digit in the string.
* @param entry, given string.
* @return int possation of the first digit > 0.
*/
int findFirstDigit(string entry) {
bool found = false;
int rtv = 0;
int i = entry.length() - 1;
while (!found) {
if (entry[i] != 0) { //trims leading 0's
rtv = i;
found = true;
}
--i;
if (i == 0) {
found = true;
}
}
return rtv;
}
int main() {
// Variables //
char opp;
char again = 'Y';
// Interface //
cout << endl;
cout << "Hello, I am a calculator for large numbers, up to 500 digits, but allas though I can "
<< "only add and multiply. I take input in this fashion ####### + or * ##########."
<< endl;
do {
BigN first;
BigN second;
BigN answer;
cin >> left >> opp >> right;
cout << endl << endl;
// Process input //
switch (opp) { //devise operator
case '+':
answer = first + second;
break;
case '*':
answer = first * second;
break;
default:
cout << "!!ERROR!! Sorry, I can only add and multiply." << endl;
}
// Output //
cout << " ";
cout << first;
cout << endl; //format output
cout << opp << " ";
cout << second;
cout << endl;
cout << "===================";
cout << endl;
cout << answer;
cout << endl << endl;
cout << "Wanna do another one, Y or N?? "; //reset program to do another
cin >> again;
cout << endl;
} while (again == 'y' || again == 'Y');
return 0;
}
|