在阅读类型INT时,如何不阅读SCANF中的浮动类型
我需要将两个整数(没有其他)写入变量。进行验证,确保没有任何ARGS是字符串,它们不是空的,除了零以外除外,或者当我将float作为第一个ARG中,但是当我将float作为第二个参数时,它并不是例外。如何仅使用stdio.h
解决它?
#include <stdio.h>
void sum(int a, int b);
void dif(int a, int b);
void prod(int a, int b);
void qut(int a, int b);
int main() {
int x, z;
int digits = scanf("%d %d", &x, &z);
if (digits != 2) {
printf("n/a\n");
return 2;
}
else {
sum(x, z);
dif(x, z);
prod(x, z);
qut(x, z);
}
return 0;
}
void sum(int a, int b) {
printf("%d ", a + b);
}
void dif(int a, int b) {
printf("%d ", a - b);
}
void prod(int a, int b) {
printf("%d ", a * b);
}
void qut(int a, int b) {
if (a == 0 || b == 0) {
printf("n/a\n");
}
else {
printf("%d\n", a / b);
}
}
抱歉,我知道代码很安静,我的问题很安静愚蠢:) 谢谢!
I need to write two integers (and nothing else) to variables. It does the validation makes sure that none of args is a string and that they are not empty and excepts division by zero, or when i put float as a first arg but when i put float as a second argument it does not makes an exception. How can i solve it using only stdio.h
?
#include <stdio.h>
void sum(int a, int b);
void dif(int a, int b);
void prod(int a, int b);
void qut(int a, int b);
int main() {
int x, z;
int digits = scanf("%d %d", &x, &z);
if (digits != 2) {
printf("n/a\n");
return 2;
}
else {
sum(x, z);
dif(x, z);
prod(x, z);
qut(x, z);
}
return 0;
}
void sum(int a, int b) {
printf("%d ", a + b);
}
void dif(int a, int b) {
printf("%d ", a - b);
}
void prod(int a, int b) {
printf("%d ", a * b);
}
void qut(int a, int b) {
if (a == 0 || b == 0) {
printf("n/a\n");
}
else {
printf("%d\n", a / b);
}
}
Sorry, i understand that the code is quiet simple and my question is quiet dumb :)
Thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如注释中所述,
scanf
是此作业的错误工具。scanf
众所周知,错误处理错误。从理论上讲,使用
scanf
解决此问题是可能的。同样,可以用锤子将螺钉拧入一块木头。但这是一个可怕的主意。一位教他的学生使用锤子驾驶螺钉的老师将被解雇以获得无能。但是由于某种原因,我们可以在开始编程的老师中容忍这种无能。通常我在这里不做功课问题;通常这也是一个坏主意。通常,让您(学生)做工作并获得学习更有意义。但是,对于像这样的骨头作业,我对为您提供完整的解决方案没有任何疑问,因此您可以让您的无能的教练退缩,然后继续学习一些更有用的东西。这是基本思想:
fgets
读取文本线(完整行)。sscanf
分析该行,确保它包含一个数字和数字,而没有其他数字。%d
读取第一个整数,%d
读取第二个整数,和,然后我们将使用第三个%c
接下来要接下来的任何角色。如果该字符是标记行末尾的\ n
以外的其他任何内容,则表明用户输入了错误的内容,例如字符串或。
是浮点数的一部分。这基本上与用户3121023的解决方案相同。
另请参见我可以用什么用于输入转换而不是scanf?
脚注:这里的代码不幸的小毛刺:如果用户类型是用户类型第二个数字之后的空间,但是在新线之前,代码将用
n/a
拒绝它。有一些方法可以解决这个问题,但是我认为,对于这项练习,它们只是不值得的。他们属于“回报递减法”。如果您的用户抱怨,只要到处都像难以置信的软件供应商:提醒他们他们应该键入两个数字,而其他任何数字,第二个数字之后键入的空间是“其他东西”,所以这是他们的错,而不是您的错漏洞。 :-)As mentioned in the comments,
scanf
is the WRONG TOOL for this job.scanf
is notoriously bad at error handling.Theoretically it's possible — barely possible — to solve this problem using
scanf
. By the same token, it's possible to drive a screw into a piece of wood using a hammer. But it's a terrible idea. A woodshop teacher who taught his students to drive screws using a hammer would be fired for incompetence. But for some reason we tolerate this kind of incompetence in teachers of beginning programming.Normally I don't do homework problems here; normally that's a bad idea, too; normally it makes much more sense to have you, the student, do the work and acquire the learning. In the case of boneheaded assignments like this one, though, I have no qualms about giving you a fully-worked-out solution, so you can get your incompetent instructor off your back and go on to learn something more useful. Here is the basic idea:
fgets
.sscanf
, ensuring that it contains a number and a number, and nothing else.%d
to read the first integer, and%d
to read the second integer, and then we'll use a third%c
to pick up whatever character comes next. If that character is anything other than the\n
that marks the end of the line, it indicates that the user has typed something wrong, like a string, or the.
that's part of a floating-point number.This is basically the same as user3121023's solution.
See also What can I use for input conversion instead of scanf?
Footnote: The code here has one unfortunate little glitch: If the user types a space after the second number, but before the newline, the code will reject it with
n/a
. There are ways to fix that, but in my opinion, for this exercise, they're just not worth it; they fall under the "law of diminishing returns". If your users complain, just act like incorrigible software vendors everywhere: remind them that they were supposed to type two numbers and nothing else, and the space they typed after the second number is "something else", so it's THEIR FAULT, and not your bug. :-)扫描整数后,使用循环扫描一个空格字符。在新线上突破循环。
对于任何其他字符,扫描将返回
0
。After scanning for the integers, use a loop to scan for one whitespace character. Break out of the loop on a newline.
For any other character, the scan will return
0
.