C: scanf 添加到总计

发布于 2024-08-17 08:29:17 字数 296 浏览 4 评论 0原文

一个简单的问题是,有没有一种方法可以执行这 3 个操作:

while(...) {
fscanf(input, "%lf %lf", &t, &y);
tTotal += t;
yTotal += y;
}

在一个操作中,t 和 y 在 scanf 语句内分别将自己添加到 tArray 和 yArray 中?谢谢

fscanf(input, "%lf %lf", ...code..., ...code...); 

,阿什。

A quick question, is there a way to perform these 3 operations:

while(...) {
fscanf(input, "%lf %lf", &t, &y);
tTotal += t;
yTotal += y;
}

in one operation where t and y add themselves to tArray and yArray respectively, inside the scanf statement? Something of the form

fscanf(input, "%lf %lf", ...code..., ...code...); 

Thanks, Ash.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

莫相离 2024-08-24 08:29:17

无法将加法放入 fscanf 函数的参数中,因为 fscanf 接受指针并简单地写入它们的位置。但是,您仍然可以像这样在同一语句中进行加法部分:

while(...) {
    fscanf(input, "%lf %lf", &t, &s), tTotal += t, sTotal += s;
}

请注意,该语句的结果是最后一个表达式的值。因此,例如,如果您想记录 scanf 的返回值,您应该这样做:

while(...) {
    int res;
    useResult((res = fscanf(input, "%lf %lf", &t, &s), tTotal += t, sTotal += s, res));
}

... 这很丑陋但有效。

(免责声明:我尚未编译此代码)

There is no way to put the addition into the parameters of the fscanf function because fscanf accepts pointers and simply writes to their locations. However, you could still make the addition part of the same statement like this:

while(...) {
    fscanf(input, "%lf %lf", &t, &s), tTotal += t, sTotal += s;
}

Note that the result of this statement is the value of the last expression. So, for example, if you wanted to record the return value of scanf, you should do:

while(...) {
    int res;
    useResult((res = fscanf(input, "%lf %lf", &t, &s), tTotal += t, sTotal += s, res));
}

... which is ugly but works.

(Disclaimer: I haven't compiled this code)

定格我的天空 2024-08-24 08:29:17

这样做需要 fscanf() 知道您想要的操作以及存储结果的位置(并且在变量尚未初始化的情况下它将是未定义的)。

可以围绕fscanf()编写一个包装器来完成此操作,但这对于非常具体的用例来说只是大量工作,并且最终会比仅仅执行以下操作要慢+=之后。

Doing that would require fscanf() to know the operation that you wanted in addition to where to store the result (and it would be undefined in the case where the variable hadn't yet been initialized).

You could write a wrapper around fscanf() to do it, but that's just a lot of work for a very specific use case, and would ultimately be slower than just doing the += after.

○闲身 2024-08-24 08:29:17

这是不可能的。 fscanf 只是将扫描的值写入指针指定的内存位置,并且无法使该操作执行任何加法。

It is not possible. fscanf simply writes the scanned values to the memory locations given by the pointers, and there is no way to make that operation perform any addition.

老街孤人 2024-08-24 08:29:17

指针算术来拯救!

while(fscanf(input, "%lf %lf", t, y)) {t++; y++;}

其中 ty 最初指向其数组的开头。

当然,您需要添加自己的边界检查。

Pointer arithmetic to the rescue!

while(fscanf(input, "%lf %lf", t, y)) {t++; y++;}

Where t and y point to the start of their arrays initially.

Of course, you'll need to add your own bounds checks.

誰認得朕 2024-08-24 08:29:17

在 C++ 中,这相当容易,但在 C 中,这可能相当具有挑战性(充其量)。

In C++ this would be fairly easy, but C it's likely to be pretty challenging (at best).

天赋异禀 2024-08-24 08:29:17

问题是:你想要实现什么目标?
如果您只是想减少混乱,请创建一个内联函数,例如:

int GetData(FILE *fp, double *ptTotal, double *pyTotal)
{
    double t, y;
    if (2 == fscanf(fp, "%lf %lf", &t, &y))
    {
         *ptTotal += t;
         *pyTotal += y;
         return 1;
    }
    else
    {
        return 0;
    }
}

并在循环中调用它。

但是,如果您希望加快循环速度,那么我不喜欢您的机会。 fscanf 可能比数学慢很多倍,所以我认为如果不切换到批量读取方式,您不会加快速度,这将使程序变得更加复杂。

The question is: what are you trying to achieve?
If you just want to reduce clutter, create an inline function something like:

int GetData(FILE *fp, double *ptTotal, double *pyTotal)
{
    double t, y;
    if (2 == fscanf(fp, "%lf %lf", &t, &y))
    {
         *ptTotal += t;
         *pyTotal += y;
         return 1;
    }
    else
    {
        return 0;
    }
}

and call that in a loop.

However if you have hopes of speeding up the loop, then I don't like your chances. fscanf is probably many times slower than the maths, so I don't think you'll speed it up much without switching to a bulk reading style, which will make the program much more complex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文