2016-11-09 28 views
0
#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++の でロシアの農民の乗算作業を使用することでしたが、それを振り返ると、負の数値では機能しないことに気付きました。どうすればこのC++プログラムを負の数値で動作させることができますか?

答えて

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 

私は考えることができる最もエレガントな負の数

+2

'tempnumber1> 0 || tempnumber1 <0'、またはときどき次のように書きます: 'tempnumber1!= 0';) – besc

+1

' halving [i]%2!= 0'? – stefaanv

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; 
} 
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. 
    } 
-1

私はあなたが(私が間違っているなら、私を修正)これを使用することができると思います。

while (isdigit(tempnumber1)==0); 
+0

あなたは間違っています! 'isdigit(int)'は、文字が10進数字かどうかを調べます。 '真であれば非ゼロを、'偽であれば '0を返します。だから、あなたが入力したものが10進数字ではない間にブロック内のものを実行するだけです。それはプログラムの目的ではありません。 –

関連する問題