【问题标题】:3-digit integer number program won't execute3位整数程序不执行
【发布时间】:2014-03-10 03:40:24
【问题描述】:

是的,这是一个基本的 C 编码作业问题。不,我不只是在找人为我做这件事。考虑到这是我的第一堂编程课,我不能让它工作并不奇怪,而且我敢肯定它有很多问题。我只是想要一些帮助,指出我的代码中的问题和缺少的东西,以便我自己修复它们。

家庭作业问题:

编写一个只读取一个整数的程序(你的输入必须是 一个从 100 到 999 的 3 位数字),并将一个数字视为 是 ABC(其中 A、B 和 C 是数字的 3 位数字)。现在, 将数字组成为 ABC、BCA 和 CAB,然后找出 这三个数除以 11 的余数。 假设余数分别为 X、Y 和 Z 并将它们相加 向上为 X+Y、Y+Z 和 Z+X。现在,如果这些总和中的任何一个是奇数 数字,如果总和加上 11 小于 20,则将其增加 11, 否则将总和减少 11(此求和操作 必须是正数但小于 20)。最后,将每个 总和的一半。现在,打印出所有结果数字。

我的代码:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    //Declare all variables
    int OrigNumber;
    int x, y, z;
    int number;
    number = x, y, z;
    int sum;
    //
    printf("Input a three digit number");
    //
    int c;
    c = OrigNumber %10;
    //
    int b;
    b=((OrigNumber - c) % 100)/10;
    //
    int a;
    a = (OrigNumber - (b + c))/100;
    //
    int abc, bca, cab;
    abc = (a*100) + (10*b) + c;
    bca = (10*b) + c + (a*100);
    cab = c + (a*100) + (10*b);
    //
    if((number % 2) == 1)
        {
            if(number + 11 < 20)
                number += 11;
            else if((100 - 11 > 0) && (100 - 11 < 20))
                number -= 11;
        }
    //
    x = abc/11;
    y = bca/11;
    z = cab/11;
    //
    sum = (x + y),
          (y + z),
          (z + x);
}

【问题讨论】:

  • 大技巧是从小而简单的东西开始,然后逐步建立。你首先想要什么?一个接受用户号码的程序?能把279分解成2、7、9的程序吗?可以做 +/-11 事情的程序?
  • 鉴于这个问题,我认为最好是整理一下,可以允许输入三位数字,因为那是程序崩溃的地方。
  • 好吧,如果您在文本中查找“输入”(或谷歌“C 输入”),您会发现 scanf 非常适合,就像这样:scanf("%d", &amp;OrigNumber);跨度>
  • 两件事:首先是没有声明会导致任何输入被捕获;第二个是“number = x, y, z;”这一行可能并没有像你想象的那样做。
  • 你要一个号码,但从来没有读过任何东西。您的abc 含义不明确。对于 123,它们是 100、20 和 3;还是 1、2 和 3?使用轻微优化和所有典型警告(对于 GCC,-O -Wall)编译程序,然后了解并修复每个警告或确保确保你是对的并且编译器感到困惑(这种情况发生得比你想象的要少得多)。

标签: c


【解决方案1】:

首先,您需要读取输入。从包含回车的提示开始:

printf("Input a three digit number: \n");

由于它是一个三位数,您可以添加以下行来读取输入:

scanf("%3d", &OrigNumber);

下一段代码运行良好,直到您到达 if (number % 2),这毫无意义,因为您没有真正定义 number - 好吧,您定义了,但是行

number = x, y, z;

不做你认为它做的事。如果你添加

printf("So far I have abc=%d, bca=%d, cab=%d\n", abc, bca, cab);

在您第一次读入数字并计算出这三个数字后,您会发现一切顺利。

注意

number = x, y, z;

使用一种叫做“逗号运算符”的东西。所有的东西 (a,b,c) 都被“评估”了,但它们的值不会被返回。无论如何,在你有那条线的地方,你还没有给 x、y 和 z 赋值。

这足以让你开始吗?

更新既然您已经有几个小时来考虑这个问题,这里还有一些建议。

您对abc, cab, bca 的计算毫无意义。我将只向您展示其中之一:

cab = c*100 + a*10 + b;

接下来,您需要计算 x、y 和 z 中的每一个。同样,这里是三个之一:

y = bca%11;

现在你必须算数 - 我称它们为 xyyzzx。只有其中之一:

zx = z + x;

接下来,处理指令:“现在,如果这些总和中的任何一个是奇数,如果总和加上 11 小于 20,则将其增加 11,否则将总和减少 11:

if(xy % 2 == 1) {
  if(xy + 11 < 20) xy += 11; else xy -= 11;
}

对所有三个总和使用类似的代码。然后“除以2”:

xy /= 2;

根据需要重复。

最后,打印出结果:

printf("xy: %d, yz: %d, zx: %d\n", xy, yz, zx);

令人惊奇的是,如果你做对了,你会得到原来的数字......

您可以通过使用值数组并循环遍历它来使代码更紧凑 - 而不是重复我在上面使用不同变量编写的代码 sn-ps。但我怀疑这远远超出了您目前应该知道的范围。

你能从这里拿走吗?

【讨论】:

  • 嗯,它至少让我能够输入 3 位数字并输入它。
  • @user3399871 - 我已经更新了我的答案。现在有更多(希望有用)信息。仍然让您自己弄清楚一些。无论你做什么 - 停止使用逗号运算符。在使用变量之前,请确保已为变量分配了值。 C 不像“符号数学”语言。
【解决方案2】:
#include <stdio.h>
int main()
{
    //Declare all variables
    int OrigNumber;
    int a, b, c;
    int abc, bca, cab;
    int x, y, z;
    int xplusy , yplusz, xplusz;

    printf(" A program to read ONLY one integer number.\n Input must be one 3 digit number from 100 to 999 : ");    
    scanf("%d", &OrigNumber);   // Get input from console

    if(OrigNumber > 999 || OrigNumber < 100) {
        printf("Invalid number. Quiting program. This is error handling. Important while learning programming.");   
    return 0;
    }

    c = OrigNumber %10; // digit at unit's place

    b=((OrigNumber) % 100)/10; //digit at the ten's place

    a = (OrigNumber)/100; //digit at the 100's place. Note: 734/100 = 7. NOT 7.34.

    printf("\n Three numbers say A,B, C : %d, %d , %d ", a, b, c);  

    abc = a*100 + 10*b + c;
    bca = 100*b + 10*c + a;
    cab = c*100 + a*10 + b;

    printf("\n Three numbers say ABC, BCA, CAB : %d, %d , %d ", abc, bca, cab);

    x = abc % 11;   // Reminder when divided by 11.
    y = bca % 11;
    z = cab % 11;

    printf("\n Three numbers say X, Y, Z : %d, %d , %d ", x, y, z);

    xplusy = x + y; // Adding reminders two at a time.
    yplusz = y + z;
    xplusz = x + z;

    printf("\n Three numbers  X+Y, Y+Z, X+Z : %d, %d , %d ", xplusy, yplusz, xplusz);

    if((xplusy % 2) == 1) {
        if(xplusy + 11 < 20)
            xplusy += 11;
        else
        xplusy -= 11;
    }

    if((yplusz % 2) == 1) {
        if(yplusz + 11 < 20)
            yplusz += 11;
        else
            yplusz -= 11;
    }

    if((xplusz % 2) == 1) {
        if(xplusz + 11 < 20)
            xplusz += 11;
        else
            xplusz -= 11;
    }

    xplusy /= 2; // Finally, divide each of the sum in half.
    yplusz /= 2;
    xplusz /= 2;

    printf("\n Now print out all the resulting digits :  %d, %d , %d \n", xplusy, yplusz, xplusz);

    return 0;
}

【讨论】:

  • 是的 - 这应该很有帮助。不过有一条评论——你并没有真正给 OP 留下太多“从实践中学习”的机会......
  • 这篇文章是为了承认错误处理的重要性,并让 OP 意识到编程的关键是“分而治之”。一次接受一项挑战。使用结果进行下一个挑战。每个程序本质上都有一些输入和输出。
【解决方案3】:
int abc, bca, cab;
abc = (a*100) + (10*b) + c;
bca = (10*b) + c + (a*100);
cab = c + (a*100) + (10*b);

我建议在代码中打印出此时的数字。

printf( "%d %d %d", abc, bca, cab );

我想你会看到一个你需要解决的问题。

【讨论】:

    【解决方案4】:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main() {
        int n, a, b, c, abc, bca, cab, x, y, z, p, q, r;
        scanf("%d", &n);
        c=n%10;
        b=(n/10)%10;
        a=n/100;
        abc=a*100+b*10+c;
        bca=b*100+c*10+a;
        cab=c*100+a*10+b;
        x=abc%11;
        y=bca%11;
        z=cab%11;
        p=x+y;
        q=y+z;
        r=z+x;
        return 0;
    }
    

    现在,如果这些总和中的任何一个是奇数,则将其增加 11,如果 总和加 11 小于 20,否则将总和减少 11(此求和运算必须为正数但小于 20)。最后,将每个总和分成两半。现在,打印出所有 结果数字。

    我没有得到最后的部分,你能解释清楚吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 2018-08-10
      相关资源
      最近更新 更多