My code is working in all Different IDE’s . Then whats going wrong here
Whats wrong in the code...?
#include<bits/stdc++.h> using namespace std ; void string_to_vec(string &s , vector &num) { for(int i = s.length() - 1 ; i >= 0 ; i–) { int ans = s[i] - ‘0’; num.push_back(ans); } } void get_sq(vector &orig , vector &temp, vector &ans , int size) { int ans_pointer = 0 ; int j = 0; for(int i = 0 ; i < size ; i++) { int multiplier = orig[i]; int index = 0; int carry = 0 ; ans_pointer = j ; while(index < temp.size()) { int x = multiplier * temp[index] + carry ; carry = x / 10 ; x= x%10; if(ans[ans_pointer] == -1) ans[ans_pointer] = 0 ; ans[ans_pointer] += x ; ans_pointer++; index++; } if(carry != 0) { while(carry != 0) { ans[ans_pointer] = carry%10; ans_pointer++; carry /= 10 ; } } j++; } } int get_mul(int num , vector &v , int size) { int index = 0 ; int carry = 0 ; while(index < size) { int ans = v[index] * num + carry ; carry = ans/10; ans %= 10; v[index] = ans ; index++; } if(carry != 0 ) { while(carry != 0) { v[index] = carry%10; carry /= 10; index++; } } return index; } int get_add(vector &v1 , vector &v2 , vector &ans , int size1 , int size2) { int index = 0 ; int j = 0 ; int carry = 0 ; while(j < size1 && j < size2) { int num = v1[j] + v2[j] + carry ; carry = num/10; num %= 10; ans[index] = num ; j++; index++; } while(j<size1) { int num = v1[j] + carry ; carry = num/10; num %= 10; ans[index] = num ; j++; index++; } while(j<size2) { int num = v2[j] + carry ; carry = num/10; num %= 10; ans[index] = num ; j++; index++; } return index ; } int get_sub(vector &v1 , vector &v2 ,int size1 , int size2) { int index = 0 ; for(int i = size2 ; i < size1 ; i++) { v2[i] = 0; } for(int i = 0 ; i <size1 ; i++) { if(v1[i] >= v2[i]) { v1[i] -= v2[i]; continue; } else { int j = i+1 ; while(j < size1) { if(v1[j] > 1) { v1[j] -= 1; v1[i] += 10 ; break ; } j++; } if(v1[i] >v2[i] ) v1[i] -= v2[i]; else{ v1[i] -= v2[i]; return i ; } } } return size1 ; } int main() { string s ; vector num; cin >> s ; string_to_vec(s,num); //h(x) = f(x) + g(x); //f(x)=3x^2-x+10 //g(x)=4x^3+2x^2-5x+4 //h(x) = 4x^3 + 5x^2 - 6x + 14; vector cube(1000,-1),sq(1000,-1) , add(1000,0) ; get_sq(num,num,sq,s.length()); int sq_size = 0 ; for(int i = 0 ; sq[i] != -1 ; i++) { sq_size++; } get_sq(sq,num,cube,sq_size); int cube_size = 0; for(int i = 0 ; cube[i] != -1 ;i++) { cube_size++; } int mul_cube_size = get_mul(4,cube,cube_size); int mul_sq_size = get_mul(5,sq,sq_size); int mul_num = get_mul(6,num,s.length()); int add_size1 = get_add(cube,sq,add,mul_cube_size,mul_sq_size); int add_size2 = get_sub(add,num,add_size1,mul_num);//-6; int number = 14 ; int carry = 0 ; int index = 0 ; while(number != 0 ) { int ans = number%10 + add[index] + carry ; carry = ans / 10 ; ans = ans % 10 ; add[index] = ans ; index++; number /= 10 ; } if(carry != 0 ) { while(carry != 0) { add[index] = carry%10 ; index++; carry/=10 ; } } if(index - add_size2 > 0) { add_size2 += (index - add_size2 ); } for(int i = add_size2 - 1 ; i >= 0 ; i–) { cout << add[i]; } }
Its not getting share from coding blocks ide. can I mail you code
save your code in the ide
then copy the link in address bar…
and share the link here
the question is easy, we have to do only three things…
- string multiplication
- string addition
- minus 2 of a string
thats all we need
https://ide.codingblocks.com/s/295812....This is the link of my code. I converted string into array and manipulates it…Have a look at it
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.