473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Math with strings

Hello,

I am trying to make a function which takes two very large integers inside of strings, subtracts them, and returns the result as a string.

One goal is to do this without using third-party libraries, so please don't tell me to use Boost, MathTT etc.

I was able to make a function which adds 1 to a string:

Expand|Select|Wrap|Line Numbers
  1. void IncrementString(string& s)
  2. {
  3.     string::reverse_iterator i = s.rbegin(), end = s.rend();
  4.     int carryOver = 1;
  5.     while (carryOver && i != end)
  6.     {
  7.         int val = (*i - '0') + carryOver;
  8.         carryOver = (val / 10);
  9.         *i = '0' + (val % 10);
  10.         ++i;
  11.     }
  12.     if (carryOver)
  13.         s.insert(0, "1");
  14. }
But my code to subtract strings doesn't work:

Expand|Select|Wrap|Line Numbers
  1. string StringSubtract(string& y, string& z) {
  2.     string::reverse_iterator i = z.rbegin(), iend = z.rend();
  3.     string::reverse_iterator j = y.rbegin(), jend = y.rend();
  4.     string answer;
  5.     bool borrow = false;
  6.     while (j != jend) {
  7.         int p, q;
  8.         p = *i - '0';
  9.         q = *j - '0';
  10.         if (borrow) {
  11.             p--;
  12.             borrow = false;
  13.         }
  14.         if (q > p) {
  15.             borrow = true;
  16.             p += 10;
  17.         }
  18.         int r = p - q;
  19.         answer = answer.insert(0,to_string(r)); //PROBLEM!
  20.         i++;
  21.         j++;
  22.     }
  23.     while (i != iend) {
  24.         if (borrow) {
  25.             *i--;
  26.             borrow = false;
  27.         }
  28.         const char* s = *i;
  29.         answer = answer.insert(0,&s);
  30.     }
  31.     return answer;
  32. }
  33.  
The error I get is with the string.insert() function - invalid conversion from char to const char*.

I've tried sprintf(), string_to() and using a char, nothing seems to work! Is there any other way to put an integer in the front of a string that is not a constant?
Apr 26 '13 #1
1 2552
weaknessforcats
9,208 Expert Mod 8TB
What do you mean by "very large integers"?

Using strings and iterators will be very very slow.

Instead consider an array of unsigned int that you create on the heap. Now you can work at the binary bit level so your addition and subtraction is very straightforward. An array is guaranteed contiguous allowing you to scan bit-by-bit the entire array.

You can aleays write a function to convert your array to a basic_string<char> (aka a string object).
Apr 26 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jean-Fran?ois Lacrampe | last post by:
Hello, I've got two random number/statistics questions I'd like you to review. My first question is not directly related to PHP, but will be implemented in PHP, as explained in my second...
50
by: dataangel | last post by:
I wrote a function to compare whether two strings are "similar" because I'm using python to make a small text adventure engine and I want to it to be responsive to slight mispellings, like...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
12
by: Janiek Buysrogge | last post by:
Hello, Does anyone know if there is a library or a sample project that can parse strings with mathematical expressions inside ? eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)"; parser...
0
by: Mike | last post by:
can anyone explain to me why, given: decimal dec = 84.50M; Convert.ToInt32(dec).ToString() and Math.Round(dec,0).ToString() both correctly output "84"
14
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have pointers on the same? Thanks, Avanti
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
2
by: erikcw | last post by:
Hi, I'm parsing xml data with xml.sax and I need to perform some arithmetic on some of the xml attributes. The problem is they are all being "extracted" as unicode strings, so whenever I try to...
8
by: brad | last post by:
How does one make the math module spit out actual values without using engineer or scientific notation? I get this from <code>print math.pow(2,64)</code>: 1.84467440737e+19 I want this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.