我如何使这个c++程序与负数一起工作?
How would i make this C++ Program work with negative Numbers?
#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++中使用俄罗斯农民乘法工作但是回头看,我意识到它不能处理负数,我该如何让这个程序处理负数呢?
#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.
}
This;
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
容纳负数
我想你可以用这个(如果我错了请纠正我);
while (isdigit(tempnumber1)==0);
相关文章:
- 如何使我的 sizeof sum 结构与空参数包一起工作
- SFINAE:它如何与派生类一起工作?
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- 尝试让条形码扫描仪与Arduino一起工作
- 从书本中学习C++无法使该程序与类一起工作
- 让类与运算符一起工作更简单的方法
- 如何使lambda与std::nullopt一起工作
- 当我使用长整型时,我的代码不起作用,它与 int 一起工作得很好
- 返回对象如何与分配运算符一起工作
- GLM :: perspective()和glm :: lookat()一起工作
- SFML OpenGL:如何同时与他们一起工作
- 使QTConcurrent ::映射与Lambdas一起工作
- 如何与C 字符串中的非ASCII字符一起工作
- 我如何适应擦除式习惯与矢量元组一起工作
- 需要帮助使 VOID 代码与总工资一起工作
- 我怎样才能让这个刽子手代码与 wxDev-C++ 一起工作
- Googletest 在 bazel test 中永远不会失败(在它应该失败的地方),但与 cmake & clion 一起工作
- 常量变量初始化仅与成员初始化列表一起工作
- feal_ptr与unique_ptr一起工作
- OpenGL Ping Pong可与一张通行证一起工作,而不是两个通行证