双方的计票都中断了
Disruption in the count for both sides
本文关键字:中断 更新时间:2023-10-16
使用这个程序,我试图实现如下输出
A + B + C = 7
xMin = 3
3 - 10
4 - 11
5——126——13
7——14
8——15
我通常会得到这样的东西
4 - 0
5 - 0
6 - 0
7 - 0
8 - 0
只有当我硬编码xMin或xMax来显示时,它才会改变,所有介于两者之间的值都不会显示。
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int X = 0;
double a, b, c, xMin, xMax;
double y = 0;
cout << "#1(A): ";
cin >> a;
cout << "n#2(B): ";
cin >> b;
cout << "#3(C): ";
cin >> c;
cout << "Enter Xmin" << endl;
cin >> xMin;
cout << "Enter Xmax" << endl;
cin >> xMax;
y = a + b + c + X;
for (int count = xMin; count <= xMax; count++)
{
cout << count << "t" << y << "n";
}
return 0;
}
您的for
循环是错误的(您没有更新上界),将其更改为:
y = a + b + c;
for (int count = xMin; count <= xMax; count++)
{
cout << count << "t" << count + y << "n";
}
#include <iostream>
#include <cmath>
using namespace std;
struct updInt{
int xMax;
int xMin;
int inc;
int val;
bool flag;
friend ostream& operator<<(ostream& os, updInt& dt){
os <<dt.val;
dt.val+=dt.inc;
if(dt.val>dt.xMax)
dt.flag=true;
if(dt.val<dt.xMin)
dt.flag=true;
return os;
}
updInt(int a,updInt X,int inc=1){
this->val=a+X.val;
this->xMax = a + X.xMax;
this->xMin = a + X.xMin;
this->inc =inc;
flag=false;
}
updInt(int max,int min,int val,int inc=1){
this->val= val;
this->xMax = max;
this->xMin = min;
this->inc = inc;
flag=false;
}};
int main(){
int a, b, c, xMin, xMax;
cout << "#1(A): ";
cin >> a;
cout << "n#2(B): ";
cin >> b;
cout << "#3(C): ";
cin >> c;
cout << "Enter Xmin" << endl;
cin >> xMin;
cout << "Enter Xmax" << endl;
cin >> xMax;
updInt X(xMax,xMin,0);
updInt y(a + b + c , X);
for (int count = xMin; count <= xMax; count++)
{
cout << count << "t" << y << "n";
if(y.flag)
break;
}
return 0;
}
你需要写你的ostream操作符,当你想增加它而不触及循环,但我不知道为什么你不直接增加y。
相关文章:
- 类成员和中断
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 我的代码运行良好,但在游戏循环中中断
- 使用带有MCP23017的 pigpio 进行中断读取
- 为什么我在 AVR 中的中断无法正常工作?
- 使用 UDP 中断 while()-循环
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何在 CompleteAsyncIO 中访问 IOMemoryBufferDescriptor,该描述符通过中断 EP
- 如何为伺服电机创建中断或返回值?
- 开关:无外壳中断
- 如何使用中断停止循环?
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 在 Ubuntu 中编译 SuiteSparse,在安装 CHOLMOD 时中断
- 在PROGMEM中添加更多数据会中断Arduino Mega 2560上的SPI传输
- 附加到 gdb 会中断并且不会继续该过程
- 使用 shared_ptr 在中断时结束多线程循环
- 为什么STM32不中断?
- 如何使用 SIGINT 在第二个线程中断 getchar
- 使用个人C++库编译代码时,与头文件一起使用时会中断
- 为什么我的 while 循环对于特定输入中断,而对于其他输入工作正常?