程序的语法 [英] syntax for a program

查看:53
本文介绍了程序的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我正在通过程序编写一些困难,例如我必须要求两个不同的数字供用户输入。一个数字开始计数和一个数字停止计数停止,但我还需要使它如果一个数字可被3整除它应该说不可分3如果数字可以被5整除它应该说不可分5 ,如果一个数字可以分为3和5,则应该说不能被3和5分割。我的程序如下

  #include   <   stdio.h  >  
int main( void
{
int x,y,i;
printf( 插入要计数的数字:);
scanf( %d,& x);
printf( 插入数字以停止计数:);
scanf( %d,& y);
for (i = x; i< = y; ++ i)
{
printf( %u \ n,i);
}
}











我的问题是,它只是打印出我想知道的数字,如果我输入了一个

  if (i / 3)
printf( not 3);





 如果 (i / 5)
printf( not 5);







 如果( i / 15)
printf( 可被3和5整除





所以我的qustion是如何改变我的程序,这样如果我用y代替20而x代入1,那我的输出就是,但如果我这样做,它只会跳过数字并打印不可分割和底部而不是它们所在的位置应该去。感谢任何帮助给予。

解决方案

你应该总是检查 scanf 返回值(请参阅文档 [ ^ ])。

请注意,为了理解整数是否完全除以另一个整数使用提醒操作符,例如:

  if (a% 5  ==  0 
{ // 这里a是5的倍数
}
else
{ // 此处a不是5的倍数
}


您可以先读取用户输入获取。您可以通知用户,当用户只是按Enter而不输入任何其他内容时可以获得的空行将被视为使用先前值。也就是说,您首先检查字符串是否为空字符串()。仅当字符串不为空时,尝试解析已经输入的字符串,假设这是一些数字输入,使用 sscanf 。如果解析失败,您也可以保留未修改的值,或者您也可以通知用户输入错误并要求用户使用正确的字符串重复输入。



请参阅:

http://www.tutorialspoint.com/c_standard_library/c_function_gets.htm [ ^ ],

http://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm [ ^ ]。



关注这些说明,你会得到你想要的代码。



现在,这可能是一个很好的练习,但你应该理解别的东西:在真实 console-only utilit ies,交互式输入根本不被使用,除了一些罕见的情况,当应用程序是一个完全成熟的命令解释器,一般或专业。在所有其他情况下,所有输入都在命令行中提供。如果某个输入参数太长而无法方便地将其放在命令行中,则某些命令行参数需要文件名,即指定详细数据元素的文本文件的名称。这比交互式输入方便得多。使用交互式输入时,一个小错误可能会强制用户全部键入,但在仅基于命令行的方法中,使用只需从命令缓冲区重复相同的命令并更改一个特定的细节。



-SA


hello everyone. I am having some difficulties writing by program for instance i have to ask for two different numbers for the user to input. A number to start counting at and a number to stop counting stop, but I also need to make it if a number is divisible by 3 it should say "not divisible 3" If the number is divisible by 5 it should say "not divisible 5", and if a number is dividsible by 3 and 5 is should say not diviisible by 3 and 5. My program is as follows

 #include <stdio.h>
int main(void)
{
	int x,y,i;
	printf("insert number to count to: ");
	scanf("%d",&x);
	printf("insert number to stop counting to: ");
	scanf("%d",&y);
	for (i = x; i <= y; ++i)
	{
		printf("%u\n",i);
	}
}






My problem with this is that it just prints out the number I was wondering if I put in a

if (i/3)
    printf("not 3");


and

if (i/5)
    printf("not 5);



and

if (i/15)
    printf("divisible by 3 and 5")



so my qustion is how can I change my program so that if I substitute y in for 20 and x in for 1, that my output would be, but if i do this will it just skip over the numbers and print the not divisible and the bottom instead of where they should go. Thanks for any help gived.

解决方案

You should always check scanf return value (see the documentation[^]).
Pease note, in order to understand if an integer is exactly divided by another integer you have to use the reminder operator, e.g.:

if ( a % 5 == 0)
{// here a is a multiple of 5
} 
else
{// here a is NOT a multiple of 5
}


You can read user input gets first. You can inform the user that empty line, which can be obtained when the user simply presses Enter not entering anything else, will be treated as "use previous value". That said, you have first check up the string for being empty string (""). Only if the string is not empty, try to parse already entered string assuming this is some numeric input, using sscanf. If parsing fails, you can also leave the value unmodified, or you can also inform the user of the bad input and ask the user to repeat input with correct string.

Please see:
http://www.tutorialspoint.com/c_standard_library/c_function_gets.htm[^],
http://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm[^].

Follow these instructions, and you will get the code doing what you want.

Now, this could be a good exercise, but you should understand something else: in "real" console-only utilities, interactive input is not used at all, excluding some rare cases when the application is a fully-fledged command interpreter, general or specialized. In all other cases, all the input is supplied in a command line. If some input parameter is too long to conveniently place it in a command line, some command-line parameters expect file name, the name of the text file where detailed data elements are prescribed. This is much more convenient than the interactive input. With interactive input, a small mistake may force the user into typing it all over, but in the approach based only on the command line, the use simply repeat the same command from the command buffer and change one particular detail.

—SA


这篇关于程序的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆