【问题标题】:How would i make this C++ Program work with negative Numbers?我如何让这个 C++ 程序使用负数?
【发布时间】:2016-11-10 02:07:10
【问题描述】:
#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++


    【解决方案1】:
    #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】:

      我能想到的最优雅的:

      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】:

        这个;

        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
        

        容纳负数

        【讨论】:

        • tempnumber1&gt;0 || tempnumber1 &lt; 0,或者我们有时写的:tempnumber1 != 0 ;)
        • halving[i] % 2 != 0?
        【解决方案4】:

        我认为你可以使用这个(如果我错了,请纠正我);

        while (isdigit(tempnumber1)==0);
        

        【讨论】:

        • 你错了! isdigit(int) 检查字符是否为十进制数字。如果true and 0`,如果false,则返回非零值。因此,您只是在执行块中的内容,而用户输入的内容不是十进制数字。这不是该计划的目标。
        猜你喜欢
        • 2022-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多