-2

通过这个程序,我试图实现一个看起来像这样的输出

A+B+C= 7

xMin = 3

xMax = 8

3----10

4----11

5----12

6----13

7----14

8----15

相反,我通常会得到这样的东西

4----0

5----0

6----0

7----0

8----0

只有当我硬编码 xMin 或 xMax 以显示时,它才会改变,所有 in-bewteens 都不会显示。

#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;
}
4

2 回答 2

0

您的for循环错误(您没有更新上限),将其更改为:

y = a + b + c;

for (int count = xMin; count <= xMax; count++)
{
    cout << count << "\t" << count + y << "\n";
}
于 2014-07-25T10:32:26.657 回答
0
 #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 。

于 2014-07-25T12:23:16.223 回答