【问题标题】:Program outputting incorrect answers for even and odd program为偶数和奇数程序输出错误答案的程序
【发布时间】:2018-01-29 04:24:17
【问题描述】:

我需要创建一个程序来获取 6 个随机整数输入(3 个偶数,3 个奇数;以任意顺序),然​​后显示输入的最小偶数和奇数整数。我的代码几乎完全做到了这一点,除非您为第一个变量输入一个数字,例如 -6 并一直到最后一个变量为 -1,最小的偶数被计为 -4,最小的奇数是 -5。究竟是什么问题导致了这种情况发生?

short int a, b, c, d, e, f, smallEven=0, smallOdd=0;


 cout<<"Enter a number: ";          //test a-c: even or odd; is there a small even or small odd; if there is, is new input smaller than old?
cin>>a;
if (a%2==0){
   if (smallEven==0)
      smallEven=d;
   else if (a<smallEven)
        smallEven=a;
}
else if (a%2== 1 || a%2== -1){
     if (smallOdd==0)
        smallOdd=a;
     else if (a<smallOdd)
          smallOdd=a;} 

cout<<"Enter a number: ";
cin>>b;
if (b%2==0){
   if (smallEven==0)
      smallEven=b;
   else if (b<smallEven)
        smallEven=b;
}
else if (b%2==1 || b%2== -1){
     if (smallOdd==0)
        smallOdd=b;
     else if (b<smallOdd)
          smallOdd=b;}

cout<<"Enter a number: ";
cin>>c;
if (c%2==0){
   if (smallEven==0)
      smallEven=c;
   else if (c<smallEven)
        smallEven=c;
}
else if (c%2== 1 || c%2== -1){
     if (smallOdd==0)
        smallOdd=c;
     else if (c<smallOdd)
          smallOdd=c;}


cout<<"Enter a number: ";           //at this point only need to ask if input is even or odd, and if its the smallest
cin>>d;
if (d%2==0){
   if (d<smallEven)
        smallEven=d;
}
else if (d%2== 1 || d%2== -1){
     if (d<smallOdd)
          smallOdd=d;}

 cout<<"Enter a number: ";
cin>>e;
if (e%2==0){
   if (e<smallEven)
        smallEven=e;}
else if (e%2== 1 || e%2== -1){
     if (e<smallOdd)
          smallOdd=e;}

cout<<"Enter a number: ";
cin>>f;
if (f%2==0){
   if (f<smallEven)
        smallEven=f;}
else if (f%2== 1 || f%2== -1){
     if (f<smallOdd)
          smallOdd=f;}

【问题讨论】:

  • 不要一遍又一遍地重复相同的代码,而是创建一个执行重复代码的函数。
  • 教授希望我们只使用我们在课堂上学到的东西,这意味着没有函数也没有循环
  • 您应该了解如何使用调试器。它将允许您暂停程序执行并执行诸如查看存储在变量中的值之类的操作。搜索教程或获取某种 C++ 书籍。这里不是让别人为你调试程序的地方。
  • @edmonda7 你是 YAIT 的受害者。
  • 什么?没有功能?你认为int main 是什么?或者cin&gt;&gt;n 是什么?这都是功能。学习没有函数的 C++ 就像学习在空中游泳一样。

标签: c++ algorithm logic


【解决方案1】:
void smallest(int* even, int* odd, int n){
        if(n%2 == 0 && n < *even) *even = n;
        else if(abs(n%2) == 1 && n < *odd) *odd = n;
}

那么你可以做这样的事情

int n, even, odd;

while(cin >> n){
 smallest(&even, &odd, n); 
}

如果您需要以这种方式进行分配,逻辑似乎是正确的,但要非常仔细地检查变量。我在第一块看到一些错别字。

【讨论】:

  • 当调用 "smallest()" 时,假设 "*even" == -10 和 "*odd" == 零和 "n" == -6 ... “*奇”在电话之后..??您的第一个“if”语句中有两个条件,因此您的第二个“if”需要测试“n”是否为“奇数”...
【解决方案2】:

大量重复代码的问题在于,您在复制代码时经常会出现剪切粘贴错误。如果您是cin &gt;&gt; **a**,然后是if (smallEven==0) smallEven=**d**,这在您的第一部分中很明显。

这样做的理想方法是将其分解为函数,但是,由于您声明由于课程限制而不允许这样做,您仍然可以通过意识到您不这样做来大大清理您的代码 需要很多变量。

int num, smallEven, smallOdd, countEven = 0, countOdd = 0;

while ((countEven < 3) && (countOdd < 3)) {
    cin >> num;

    // Select even or odd.

    if ((num % 2) == 0) {
        // Check allowed.

        if (countEven == 3) {
            cout << "No more even numbers allowed\n";
            continue;
        }

        // Store if first or lesser, add to count.

        if (countEven == 0) {
            smallEven = num;
        } else {
            if (num < smallEven) {
                smallEven = num;
            }
        }
        countEven++;
    } else {
        // Check allowed.

        if (countOdd == 3) {
            cout << "No more odd numbers allowed\n";
            continue;
        }

        // Store if first or lesser, add to count.

        if (countOdd == 0) {
            smallOdd = num;
        } else {
            if (num < smallOdd) {
                smallOdd = num;
            }
        }
        countOdd++;
    }

有了函数,这变得更加容易:

void apply(const char *type, const int num, int &count, int &small) {
    if (count == 3) {
        cout << "No more " << type << " numbers allowed\n";
        return;
    }

    // Store if first or lesser.

    if (count == 0) {
        small = num;
    } else {
        if (num < small) {
            small = num;
        }
    }
    count++;
}

:

int num, smallEven, smallOdd, countEven = 0, countOdd = 0;

while ((countEven < 3) && (countOdd < 3)) {
    cin >> num;

    // Select even or odd.

    if ((num % 2) == 0)
        apply("even", num, countEven, smallEven);
    else
        apply("odd", num, countOdd, smallOdd);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多