需要一些简单的C代码程序的建议

时间:2018-10-28 10:12:50

标签: c

我对C有疑问。我刚开始学习它,所以请耐心等待。

我想编写一个简单的程序来:

  
      
  1. 处理错误
  2.   
  3. 执行简单的打印任务
  4.   

它只是在控制台中制作的。

#include <stdio.h>
main()
{
   int x, y, z, loop_one, loop_two;
   printf("Give a number \n");
   scanf("%d",&x);
   for(loop_one = 1; loop_one <= x; loop_one += 1)
   {
      for(loop_two = 1; loop_two <= x; loop_two += 1)
      {
         y = y + 1;
         printf("1/%d = ", y);
      }
   }
}

这是我的代码,但是现在我对逻辑有些迷惑了。我什至没有添加错误处理。

用户输入负数时均应出现错误。它应该打印“ retype”并继续进行直到用户输入正数为止。

第二个任务是程序需要从 1/1 + 1/2 .... 1 / x = sum 计算并像这样打印。

例如,如果用户给出5,则输出应为:

1/1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28

3 个答案:

答案 0 :(得分:0)

您可以尝试

#include <stdio.h>
int main()
{
int x,y,z,loop_one,loop_two;
printf("Give a number: ");
scanf("%d",&x);
float ans=0;
for(loop_one=1;loop_one<x;loop_one+=1)
{
    ans+=1/(float)loop_one;
    printf("1/%d + ",loop_one);

}
ans+=1/(float)x;
printf("1/%d = %f\n",x,ans);
}

实际上,您不需要两个循环即可计算最终值。

我已经在x之前终止了循环,否则它将打印出一个额外的“ +” 在1 / x之后签名。

如果您不了解类型转换(即(float)x),则可以使用它进行搜索。 在C上对整数进行除法总是返回一个整数, 因此1/2 +1/3 +1/4在C中为0!

因此整数已转换为浮点数

现在可以处理-ve个整数

您可以看看以下内容:

#include <stdio.h>
int main()
{
int x,y,z,loop_one,loop_two;
printf("Give a number: ");
scanf("%d",&x);
while(x<=0)
{
    printf("Error !, Enter a valid number: ");
    scanf("%d",&x);
}
float ans=0;
for(loop_one=1;loop_one<x;loop_one+=1)
{
    ans+=1/(float)loop_one;
    printf("1/%d + ",loop_one);

}
ans+=1/(float)x;
printf("1/%d = %f\n",x,ans);
}

答案 1 :(得分:0)

您的主要功能应始终为int main(void)int main(int argc, char *argv[]),这取决于您是否对命令行参数感兴趣。始终为#include <stdlib>,因此您可以使用return EXIT_SUCCESS;return EXIT_FAILURE;从主函数返回,或者使用exit(EXIT_SUCCESS);exit(EXIT_FAILURE);退出程序。

推荐的最简单的“你好,世界!”程序是

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

int main(void)
{
    printf("Hello, world!\n");
    return EXIT_SUCCESS;
}

使用它作为您自己的程序的基础。

  
      
  1. 处理错误
  2.   

scanf()函数家族返回成功转换的次数。因此,您确实应该有类似的东西

    if (scanf("%d", &x) < 1) {
        printf("That's not a number. Goodbye, you sneaky human.\n");
        return EXIT_FAILURE;
    }

检查输入是否为整数。

提示:试试

    printf("Please input a positive integer:\n");
    while ( (scanf("%d", &x) < 1) || (x < 1) ) {
        printf("No, a positive integer, please.\n");
    }

并查看其行为。 ||是C语言中的逻辑OR运算符。它是短路的,意味着首先评估左侧,如果为true,则根本不会检查右侧。

  
      
  1. 执行简单的打印任务
  2.   

首先学习在纸上画逻辑。尽管涂鸦对许多人有用,但由于手眼协调如何影响所涉及的思维模型,您很快就会学到如何做。有些人则改用橡皮鸭法,即大声说出来,好像在向其他人描述它(但与橡皮鸭说话);将想法转化为文字和线性语音也有助于组织思想。

Pseudocode通常是一种有用的涂鸦方法。在这种情况下,我们可以将输入部分描述为

Let  n  be some integer
Prompt user to input a positive integer
Scan n from input
While scan failed, or n is less than 1:
    Prompt user to input a positive integer
End While

我们可以写成的打印输出部分

Let  sum  be some double-precision floating-point number
Let  i  be some integer

Let  sum = 1.0
Print "1/1"
For i from 2 to n, inclusive:
    Print " + 1 / %d", i
    Let  sum = sum + 1.0 / i
End For

Print " = %.3f\n", sum

将每个分数加到总和时,我们使用1.0 / i,因为我们希望将其评估为浮点除法。 (我们可以改用1/(double)i1.0 / (double)i;但是只要一个(除数或除数)是浮点数,编译器就会进行浮点除法。)我们仅使用1/i,则编译器执行整数除法,对于所有大于i的{​​{1}}都会产生0。那不是我们想要的。

打印结果时,%f是使用浮点数的模式。 %.3f格式告诉C库在小数点后使用三个小数,例如printf("%.3f\n", 1.0)打印1.000。如您所知,\n是换行符:以下任何输出都将换行。

您现在已经获得了足够的帮助来完成作业。不要成为一个讨厌的人,尝试从某处复制 + 粘贴答案;努力学习这些知识,您会发现您唾手可得的强大工具集可以解决许多不同类型的问题,并从中获得乐趣。祝你好运!

答案 2 :(得分:0)

#include <stdio.h>

int main() /*main is a function that returns int*/
{

float x,y,loop_one;/*use float instead of int so you don't lose data */

x = -1;/*set x to be a neg number */

y = 0; /*if not init y = garbage */

while(x < 0)

{

    printf("Give a number \n");

    scanf("%f",&x); /*must advise not to use scanf as it is an unsafe function */
}
for(loop_one=1;loop_one<=x;loop_one+=1) /*you only need one for loop*/
{
    y += (1/loop_one);/*which is the same as y = y+(1/loop_one)*/

    if(loop_one != x)

    {

        printf("1/%.0f + ",loop_one);/*%.0f asks to print none after dec point */

    }

    else

    {

        printf("1/%.0f  ",loop_one); 

    }

}

printf(" = %.2f\n", y);/*%.2f asks to print 2 values after dec point */

return 0;  /*on success returns 0*/
}