I don't understand why that code would work. The multiplication by 10 seems completely wrong.
If you could post a small compilable program, I'll give it a try and see if I can help you fix your problems.
Simple division is too slow
Quote:Original post by alvaro
I don't understand why that code would work. The multiplication by 10 seems completely wrong.
It was completely wrong, and so I deleted the post. Perhaps the numbers were not in base 2^16 after all.
Here is my program playing around with both encoding and adding of these numbers. It's completely wrong, but I think my main problem might be with the encoding method.
#include <iostream>#include <vector>using namespace std;typedef unsigned int digit;#define DIGIT_MAX 65535std::ostream& operator<< (std::ostream& out, std::vector<digit> v){ int numV = int(v.size()) - 1; for(int i=numV;i>=0;--i) { out<<v<<" "; } return out;}int main(){ std::vector<digit> numbers; std::vector<digit> numbers2,numbers3; char* str = "11223344556677889900"; int strLen = (int)strlen(str); unsigned int multiplier; //encoding for(int i=strLen-1;i>=0;--i) { if(numbers.empty() || (numbers.back() + (str - '0') * multiplier) > DIGIT_MAX) { numbers.push_back(0); multiplier = 1; } numbers.back() = numbers.back() + (str - '0') * multiplier; multiplier *= 10; } numbers2 = numbers; std::cout<<numbers<<std::endl; std::cout<<numbers2<<std::endl; //adding (completely wrong) digit carry = 0; for(size_t i=0;i<numbers.size();i++) { digit sum = numbers + numbers2; if(carry) { if(sum<DIGIT_MAX+1) sum = sum*10 + carry; else sum = sum + carry; } carry = sum / (DIGIT_MAX+1); numbers3.push_back(sum % (DIGIT_MAX+1)); } numbers3.push_back(carry); std::cout<<numbers3<<std::endl;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement