C++ 我如何制作这个C++;程序使用负数吗? #包括 #包括 使用名称空间std; int main() { int value1;//这些保存用户输入的原始数字 int值2; int结果; //这将保存要与使用该算法提供的答案进行比较的答案 cout值1; cout值2; int tempnumber1{value1};//创建一个用于减半的临时变量,同时保存主数字以供以后使用。 向量减半;//这将打开该算法使用的向量减半 不能

C++ 我如何制作这个C++;程序使用负数吗? #包括 #包括 使用名称空间std; int main() { int value1;//这些保存用户输入的原始数字 int值2; int结果; //这将保存要与使用该算法提供的答案进行比较的答案 cout值1; cout值2; int tempnumber1{value1};//创建一个用于减半的临时变量,同时保存主数字以供以后使用。 向量减半;//这将打开该算法使用的向量减半 不能,c++,C++,应该改成这样, while (tempnumber1>0); 应该改成这样, if (halving [i] %2==1) 要容纳负数#包括 if (halving [i] %2==1 || halving [i] %2==-1) //Or if(halving[i] % 2 != 0); //Thanks @stefaanv #包括 使用名称空间std; int main() { int value1;//这些保存用户输入的原始数字 int值2; int结果; //这将保存要与使用

应该改成这样,

while (tempnumber1>0);
应该改成这样,

if (halving [i] %2==1)
要容纳负数

#包括
if (halving [i] %2==1 || halving [i] %2==-1)
//Or
if(halving[i] % 2 != 0); //Thanks @stefaanv
#包括 使用名称空间std; int main() { int value1;//这些保存用户输入的原始数字 int值2; int结果; //这将保存要与使用该算法提供的答案进行比较的答案 cout值1; cout值2; int tempnumber1{value1};//创建一个用于减半的临时变量,同时保存主数字以供以后使用。 向量减半;//这将打开该算法使用的向量减半
我能想到的最优雅的:

#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;
}
cout我想你可以用这个(如果我错了,请纠正我)


tempnumber1>0 | | tempnumber1<0
,或者像我们有时写的那样:
tempnumber1!=0
将[i]%2!=0减半
?你错了!
isdigit(int)
检查字符是否为十进制数字。如果
为true,则返回非零;如果
为false,则返回非零。因此,您只执行块中的内容,而用户输入的内容不是十进制数字。这不是程序的目标。
if (halving [i] %2==1 || halving [i] %2==-1)
//Or
if(halving[i] % 2 != 0); //Thanks @stefaanv
#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;
}
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.
  }
while (isdigit(tempnumber1)==0);