Hi, I have a code for subtraction of a larger number to a smaller number. It works for most numbers but I've found error with
1: 100-(1 to 9) = 99
2. the program doesn't output answers that are single digits.
I'm using functions to multiply and output answer, and the output function works fine to output single digits. I've also included functions called erase (to erase leading zeros) and flip (to flip the vectors, because the initial result is stored backwards).
// this function outputs a vector
void output (vector <char> v)
{
for (i=0; i <= v.size(); i++ )
cout << v[i];
}
// this function flips the order of a vector
vector<char>flip(vector<char>num)
{
vector<char>backwards(num.size());
for (size_t i=0; i<num.size(); i++)
backwards[i] = num[num.size() - i - 1];
return backwards;
}
// this function erases leading zeros
vector <char> erase (vector <char> number)
{
i=0;
number = flip(number);
while (number.size() > 1 && number[number.size()-1] == '0')
number.pop_back();
number = flip(number);
number.pop_back();
return number;
}// end of erase function
// this function substracts a larger number to a smaller number
vector <char> substraction(const vector <char> &max, const vector <char> &min)
{
int mini=0;
int maxi=0;
int lmax = max.size();
int lmin = min.size();
int pretimes = 0;
int carry = 0;
char times;
vector <char> result;
for (i = lmin-1; i >= 0; i--)
{
// the following is to properly cast the character in each slot of vector that is to be used into integer
// the desired integer is the difference between the ask value of the character in question and the character '0'
mini = static_cast<int> (min[i]) - static_cast<int>('0');
maxi = static_cast<int> (max[i+lmax-lmin]) - static_cast<int>('0');
// do substraction for overlapping digits
if ((maxi-carry) >= mini)
{
pretimes = maxi - carry - mini;
carry = 0;
}
else
{
pretimes = (maxi -carry + 10) - mini;
carry = 1;
}
times = pretimes + '0';
result.push_back(times);
}
if (lmax > lmin)
{
int z = lmax - lmin - 1;
for (z = lmax - lmin - 1; z >= 0; z--)
{
if ((static_cast<int>(max[z]) - static_cast<int>('0') - carry) >= 0)
{
pretimes = (static_cast<int>(max[z]) - static_cast<int>('0')) - carry;
carry = 0;
}
else
{
pretimes = pretimes = (static_cast<int>(max[z]) - static_cast<int>('0') + 10) - carry;
carry = 1;
}
times = pretimes + '0';
result.push_back(times);
}
}
result = flip(result);
result = erase(result);
return result;
}
Removed the extra calls to flip() and removed a call to number.pop_back() to get it to work. Look for the comments with CHANGED and REMOVED. It looks like it is working now, but test it some more.
#include <vector>
#include <iostream>
usingnamespace std;
// this function outputs a vector
void output (vector <char> v)
{
// CHANGED, added int infront of i
for (int i=0; i < v.size(); i++ )
cout << v[i];
// CHANGED, added endl
cout << endl;
}
// this function flips the order of a vector
vector<char>flip(vector<char>num)
{
vector<char>backwards(num.size());
for (size_t i=0; i<num.size(); i++)
backwards[i] = num[num.size() - i - 1];
return backwards;
}
// this function erases leading zeros
vector <char> erase (vector <char> number)
{
// CHANGED, added int infront of i
int i=0;
// REMOVED
//number = flip(number);
while (number.size() > 1 && number[number.size()-1] == '0'){
number.pop_back();
}
number = flip(number);
// REMOVED
//number.pop_back();
return number;
}// end of erase function
// this function substracts a larger number to a smaller number
vector <char> substraction(const vector <char> &max, const vector <char> &min)
{
int mini=0;
int maxi=0;
int lmax = max.size();
int lmin = min.size();
int pretimes = 0;
int carry = 0;
char times;
vector <char> result;
for (int i = lmin-1; i >= 0; i--)
{
// the following is to properly cast the character in each slot of vector
// that is to be used into integer the desired integer is the difference
// between the ask value of the character in question and the character '0'
mini = static_cast<int> (min[i]) - static_cast<int>('0');
maxi = static_cast<int> (max[i+lmax-lmin]) - static_cast<int>('0');
// do substraction for overlapping digits
if ((maxi-carry) >= mini)
{
pretimes = maxi - carry - mini;
carry = 0;
}
else
{
pretimes = (maxi -carry + 10) - mini;
carry = 1;
}
times = pretimes + '0';
result.push_back(times);
}
if (lmax > lmin)
{
int z = lmax - lmin - 1;
for (z = lmax - lmin - 1; z >= 0; z--)
{
if ((static_cast<int>(max[z]) - static_cast<int>('0') - carry) >= 0)
{
pretimes = (static_cast<int>(max[z]) - static_cast<int>('0')) - carry;
carry = 0;
}
else
{
// CHANGED
pretimes = (static_cast<int>(max[z]) - static_cast<int>('0') + 10) - carry;
carry = 1;
}
times = pretimes + '0';
result.push_back(times);
}
}
// REMOVED
//result = flip(result);
result = erase(result);
return result;
}
int main(){
vector<char> a,b;
a.push_back('1');
a.push_back('0');
a.push_back('0');
b.push_back('9');
b.push_back('7');
output(a);
output(b);
cout << "------" << endl;
output(substraction(a,b));
return 0;
}