运算优先顺序/循环

发布于 2024-11-02 14:27:50 字数 354 浏览 0 评论 0原文

在C编程语言中:

  1. 这个我不明白。是不是说(例如)如果语句中 += 在 -= 之后,则首先评估 += ?或者如果 * 在 - 之前,则 - 首先执行?我需要了解优先级。

  2. 有人能给我写两个或三个复杂的循环,其中包括:一些计数器变量、彼此之间的 3 或 4 或 5 个循环、数组、printf 和字符串之类的东西吗?如果我想在编程课程或明天的考试中取得好成绩,我需要学会在纸上手动完成循环。

这是 nit 作业,即没有什么可交的,只是为明天的 C 考试做准备。

In the C programming language:

  1. This I do not understand. Is it saying that (for example) if += is after -= in a statement, the += is evaluated first? Or if * is before a -, the - is executed first? I need to understand precedence.

  2. Can someone write me two or three complicated loops which include: a few counter variables, 3 or 4 or 5 loops within eachother, arrays, printf's and strings and stuff? I need to learn to manually go through loops on paper really well if I want to ace my programming course, or the exam I have tomorrow..

This is nit homework, i.e., nothing to hand in, just preparing for my exam in C tomorrow.

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

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

发布评论

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

评论(3

享受孤独 2024-11-09 14:27:50

优先级图具有垂直分量(优先级)和水平分量(关联性)。

基本上,列表中较高位置的操作首先完成,因此 a + b * c 的计算结果为 a + (b * c)。请注意,这并不意味着 b * ca 之前计算,只是 * 操作在 + 之前完成。实现可以首先计算a然后乘以bc并将其添加到已经计算的<代码>a。

对于简单的表达式,这没有什么区别,但如果表达式的其中一项除了提供简单值之外还具有副作用,那么它可能会咬伤您。我的意思是像 i++ 这样的东西,它具有递增 i 的副作用,或者调用修改全局变量的函数,或者将信息写入数据库。

如果两个运算符具有相同的优先级,则结合性将起作用。这规定了具有相同优先级组的操作如何组合在一起。

因此 +- (具有从左到右的关联性)意味着 a + b - c 的计算结果为 (a + b ) - c.

另一方面,+=-= 具有从右到左的关联性,因此 a += b -= c 的计算结果为 <代码>a += (b -= c)。


就循环而言,您可以从以下内容开始:

#include <stdio.h>
#define WIDTH 5
#define HEIGHT 7
int main (void) {
    int num[WIDTH*HEIGHT];
    int counter = 100;
    for (int i = 0; i < HEIGHT; i++)
        for (int j = 0; j < WIDTH; j++)
            num[i*WIDTH+j] = --counter;
    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            printf ("%3d ", num[i*WIDTH+j]);
        }
        printf ("\n");
    }
    return 0;
}

我建议您快速尝试理解它,然后输入它进行编译和运行。

The precedence chart has a vertical component (precedence) and a horizontal component (associativity).

Basically, operations higher in the list are done first, so a + b * c is evaluated as a + (b * c). Note that this doesn't mean that b * c is calculated before a, just that the * operation is done before the +. An implementation is free to calculate a first, then multiply b and c and add that to the already calculated a.

For simple expressions, this makes no difference but it can bite you if one of the terms of your expression has a side-effect beyond supplying a simple value. By that, I means things like i++ which has the side-effect of incrementing i, or a call to a function which modifies global variables, or writes information to a database.

Where two operators have the same precedence, associativity takes over. This dictates how operations with the same precedence group together.

So + and - (which have associativity of left to right) means that a + b - c evaluates as (a + b) - c.

On the other hand, += and -= have right-to-left associativity so that a += b -= c evaluates as a += (b -= c).


In terms of loops, you can start with the following:

#include <stdio.h>
#define WIDTH 5
#define HEIGHT 7
int main (void) {
    int num[WIDTH*HEIGHT];
    int counter = 100;
    for (int i = 0; i < HEIGHT; i++)
        for (int j = 0; j < WIDTH; j++)
            num[i*WIDTH+j] = --counter;
    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            printf ("%3d ", num[i*WIDTH+j]);
        }
        printf ("\n");
    }
    return 0;
}

I would suggest you have a quick try in understanding it, then type it in to compile and run.

可是我不能没有你 2024-11-09 14:27:50

运算符优先级和结合性不指定 C 计算表达式的顺序。它指定子表达式如何组合在一起。

+=-= 具有相同的优先级,并且从右到左关联。这意味着在此表达式中:

a += b -= c

C 将其分组为:

a += (b -= c)

因此,从 b 中减去的值是 c,而与 a 相加的值code> 是表达式 b -= c 的结果(这是 b 的新值)。未指定实际发生的顺序。


试试这个嵌套循环 - 它在做什么?

int check(const char *entries[], size_t num_entries)
{
    int i, j;
    int count = 0;

    for (i = 1; i < num_entries; i++)
    {
        size_t ilen = strlen(entries[i]);

        for (j = 0; j < i; j++)
        {
            size_t jlen = strlen(entries[j]);

            if (jlen >= ilen && !memcmp(entries[i], entries[j], ilen))
            {
                count++;
                printf("%s@%d shadows %s@%d.\n", entries[j], j, entries[i], i);
            }
        }
    }

    return count;
}

Operator precedence and associativity does not specify the order in which C evaluates expressions. It specifies how subexpressions are grouped together.

+= and -= have equal precedence, and associate right-to-left. This means that in this expression:

a += b -= c

C groups it as:

a += (b -= c)

So the value that is subtracted from b is c, and the value that is added to a is the result of the expression b -= c (which is the new value of b). The order in which this actually occurs is not specified.


Try this nested loop out - what is it doing?

int check(const char *entries[], size_t num_entries)
{
    int i, j;
    int count = 0;

    for (i = 1; i < num_entries; i++)
    {
        size_t ilen = strlen(entries[i]);

        for (j = 0; j < i; j++)
        {
            size_t jlen = strlen(entries[j]);

            if (jlen >= ilen && !memcmp(entries[i], entries[j], ilen))
            {
                count++;
                printf("%s@%d shadows %s@%d.\n", entries[j], j, entries[i], i);
            }
        }
    }

    return count;
}
少女的英雄梦 2024-11-09 14:27:50

1) “图表中同一行上的运算符具有相同的优先级”,因此 a + b - c 的计算结果为 (a + b) - c。从上到下阅读该页面以获取操作顺序。因此,对于 a + b + (c * ++d),顺序是 d 加 1,乘以 c,然后将整个数量添加到数量 a + b< /代码>。

2) For 循环就像普通的书一样从上到下阅读。例如:(伪代码更正为正确的 C -zw)

int i, j, k;
for (i = 0; i < 100; i++)
    for (j = 0; j < 10; j++)
        for (k = 10; k > 0; k = k/2)
            printf("%d %d %d\n", k, j, i);

您从 i 为 0 开始,j 为 0,i 和 j 保持 0,而 k 为 10, 5, 2, 1,然后返回,j 为 1,而i 保持 0,k 为 10, 5, 2, 1。重复直到 j 为 10,然后返回并使 i 为 1。重复整个过程,直到 i 为 100。内部循环对于正在进行的循环的每个值都运行完成。

1) "Operators on the same line in the chart have the same precedence," so a + b - c evaluates as (a + b) - c. Read that page from top to bottom to get the order of operations. So for a + b + (c * ++d) the order would be add 1 to d, multiply that by c, and then add that whole quantity to the quantity a + b.

2) For loops just read through like a normal book, top to bottom. For example: (pseudocode corrected to proper C -zw)

int i, j, k;
for (i = 0; i < 100; i++)
    for (j = 0; j < 10; j++)
        for (k = 10; k > 0; k = k/2)
            printf("%d %d %d\n", k, j, i);

You start with i as 0, and j as 0, i and j stay 0 while k is 10, 5, 2, 1, then you go back up and j is 1 while i remains 0 and k is 10, 5, 2, 1. Repeat until j is 10, then go back up and make i 1. Repeat this whole thing until i is 100. An internal loop is run to completion for every value of the proceeding loop.

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