#include <iostream>
#include <vector>
using namespace std;
int main()
{
int value1; // these holds the original numbers inputted by the users
int value2;
int result;
// this holds the answer to be compared against the answer provided by using the algorithm
cout << "Please Enter the first number to be multiplied"<< endl;
cin >> value1;
cout << "Please Enter the second number to be multiplied"<< endl;
cin >> value2;
int tempnumber1 {value1}; //create a temp variable for halving while keeping main numbers stored for later use.
vector <int> halving; // this opens this vector halving which the algorithm uses
cout << "This is the Halving Step" << endl;
do
{
halving.push_back(tempnumber1);
cout <<tempnumber1 << endl;
tempnumber1/=2;
}
while (tempnumber1>0);
cout << " This is the Doubling stage" <<endl;
int tempnumber2 {value2};
for (int i=0; i<halving.size(); i++)
{
cout << tempnumber2 << endl;
tempnumber2*=2;
}
int total{0};
int doubling = value2;
for (int i =0; i < halving.size(); i++)
{
if (halving [i] %2==1)
{
cout << doubling << " Is Added to total" << endl;
total += doubling;
}
doubling *= 2; // this is used to avoid having to use two vectors.
}
//total /= 2;
result = value1*value2; // this provides the check value
cout << "The result is:" << total;
cout << "[Check Value:" << result << "]" << endl;
}
嗨,这是我几个月前通过的一项大学任务。 任务是使用C ++中的俄罗斯农民增殖工作 但回过头来看,我意识到它不会与负数一起工作,我怎样才能使这个程序与负数一起工作?
答案 0 :(得分:0)
此;
while (tempnumber1>0);
应改为此;
while (tempnumber1>0 || tempnumber1 < 0);
//Or
while (tempnumber1 != 0); //Thanks @besc
而且这个;
if (halving [i] %2==1)
应改为此;
if (halving [i] %2==1 || halving [i] %2==-1)
//Or
if(halving[i] % 2 != 0); //Thanks @stefaanv
容纳负数
答案 1 :(得分:0)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int value1; // these holds the original numbers inputted by the users
int value2;
int result;
// this holds the answer to be compared against the answer provided by using the algorithm
cout << "Please Enter the first number to be multiplied"<< endl;
cin >> value1;
cout << "Please Enter the second number to be multiplied"<< endl;
cin >> value2;
int tempnumber1 {value1}; //create a temp variable for halving while keeping main numbers stored for later use.
vector <int> halving; // this opens this vector halving which the algorithm uses
cout << "This is the Halving Step" << endl;
do
{
halving.push_back(tempnumber1);
cout <<tempnumber1 << endl;
tempnumber1/=2;
}
while ((tempnumber1>0 && value1>0) ||(tempnumber1<0 && value1<0));
cout << " This is the Doubling stage" <<endl;
int tempnumber2 {value2};
for (int i=0; i<halving.size(); i++)
{
cout << tempnumber2 << endl;
tempnumber2*=2;
}
int total{0};
int doubling = value2;
for (int i =0; i < halving.size(); i++)
{
if (abs(halving [i]) % 2==1)
{
cout << doubling << " Is Added to total" << endl;
total += doubling;
}
doubling *= 2; // this is used to avoid having to use two vectors.
}
//total /= 2;
result = value1*value2; // this provides the check value
cout << "The result is:" << total;
cout << "[Check Value:" << result << "]" << endl;
}
答案 2 :(得分:0)
我能想到的最优雅:
cout << "This is the Halving Step" << endl;
do {
halving.push_back(tempnumber1);
cout << tempnumber1 << endl;
tempnumber1 /= 2;
} while (tempnumber1 != 0);
int total{0};
int doubling = value2;
int sign{0};
for (int i = 0; i < halving.size(); i++) {
if ((sign = halving[i] % 2) != 0) {
cout << doubling*sign << " Is Added to total" << endl;
total += doubling*sign;
}
doubling *= 2; // this is used to avoid having to use two vectors.
}
答案 3 :(得分:-1)
我认为你可以使用它(如果我错了,请纠正我);
while (isdigit(tempnumber1)==0);