【问题标题】:How to change the globale variables to local variables in C?如何在C中将全局变量更改为局部变量?
【发布时间】:2020-12-12 16:13:35
【问题描述】:

我已经完成了我的任务,一切都按照我的意愿进行,但问题是我不允许在这个项目中使用 globale 变量,因此每件事都应该在函数中。由于我是 C 新手,我真的不知道如何完成这项工作,我的意思是如何将我的全局变量更改为本地变量。

感谢您的帮助!

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

int tal[99] = { -1 };
int bubbles, byte, c, d, val;

/* Function for number gen*/
int talserie() {
    srand(time(NULL));
    for (c = 0; c < 100; c++) {

        tal[c] = rand() % 901;
        printf(" %d ", tal[c]);

        if ((c + 1) % 10 == 0)
            printf("\n");
    }
}
/* Funktion för bubble sort*/
int bubbel() {
    for (c = 0; c < (99); c++)
    {
        for (d = 0; d < 99 - c; d++)
        {
            if (tal[d] > tal[d + 1])
            {
                byte = tal[d];
                tal[d] = tal[d + 1];
                tal[d + 1] = byte;
            }
        }
    }

    for (c = 0; c < 100; c++) {
        printf(" %d ", tal[c]);
        if ((c + 1) % 10 == 0)
            printf("\n");
    }
}

/* Funktion för median-, max/min- och medelvärde*/
int varde() {
    printf("\nMaxvärdet är: %d", tal[99]);
    printf("\nMinvärdet är: %d", tal[0]);

    int total = 0;
    for (c = 0; c < 100; c++) {
        total = total + tal[c];
    }

    printf("\nMedelvärdet är: %d", total / 100);
    printf("\nMedianvärdet är: %d", ((tal[49] + tal[50]) / 2));
}

/*Funktion leta siffra*/
int siffra() {
    printf("\nSkriv in en siffra: ");
    scanf("%d", &val);
    d = 0;
    for (c = 0; c < 100; c++) {
        if (tal[c] == val) {
            d = 1;
            printf("\nFinns i talföljden på plats: ");
            if (c <= 9)
                printf(" Rad 1 och Kolumn %d\n", c + 1);
            else if (c > 9 && c <= 19)
                printf(" Rad 2 och Kolumn %d\n", (c + 1) - 10);
            else if (c > 19 && c <= 29)
                printf(" Rad 3 och Kolumn %d\n", (c + 1) - 20);
            else if (c > 29 && c <= 39)
                printf(" Rad 4 och Kolumn %d\n", (c + 1) - 30);
            else if (c > 39 && c <= 49)
                printf(" Rad 5 och Kolumn %d\n", (c + 1) - 40);
            else if (c > 49 && c <= 59)
                printf(" Rad 6 och Kolumn %d\n", (c + 1) - 50);
            else if (c > 59 && c <= 69)
                printf(" Rad 7 och Kolumn %d\n", (c + 1) - 60);
            else if (c > 69 && c <= 79)
                printf(" Rad 8 och Kolumn %d\n", (c + 1) - 70);
            else if (c > 79 && c <= 89)
                printf(" Rad 9 och Kolumn %d\n", (c + 1) - 80);
            else if (c > 89 && c <= 99)
                printf(" Rad 10 och Kolumn %d\n", (c + 1) - 90);
            break;
        }
    }
    if (d == 0);
    {
        printf("\n%d Finns inte i talföljden", val);
    }
}
/* Main funktion med switch meny*/
int main()
{
    while (val != 5) {
        printf("\n1. Generera en talföljd med 100 tal mellan 0-900.");
        printf("\n2. Sortera talföljden i storleksordning.");
        printf("\n3. Räkna ut medelvärde, median och maxvärde.");
        printf("\n4. Sök efter valfritt tal.");
        printf("\n5. För att avsluta\n");
        printf("Skriv in ett val (1-5): ");



        scanf("%d", &val);

        switch (val) {

        case 1:
            talserie();
            break;

        case 2:
            if (tal[0] == -1)
                printf("\nFel! Generera en talföljd först!\n");
            else
                bubbel();
            break;

        case 3:
            if (tal[0] == -1) /* Arrayen innehåller -1 i [0] innan talföljden genereras"*/
                printf("\nFel! Generera en talföljd först!\n");
            else if (tal[0] <= tal[1] && tal[1] <= tal[2] && tal[2] <= tal[3])

                varde();
            else
                printf("\nFel! Sortera talföljden i storleksordning först!\n");
            break;

        case 4:
            if (tal[0] == -1)
                printf("\nFel! Generera en talföljd först!\n");
            else if (tal[0] <= tal[1] && tal[1] <= tal[2] && tal[2] <= tal[3])
                siffra();
            else
                printf("\nFel! Sortera talföljden i storleksordning först!\n");
            break;

        }
    }
    return 0;
}

【问题讨论】:

    标签: c global-variables local-variables


    【解决方案1】:

    我是笼统地说,然后你会在你的程序中做出特定的改变。

    假设您在函数foo() 中使用了一个全局变量。如果您希望变量不再是全局变量,则需要将其作为参数之一传递给 foo()

    因此,您可以在将调用 foo() 的函数中将该变量声明为局部变量

    void caller( void )
    {
        int local;
       
        foo( local );
    }
    
    void foo( int var )
    {
    ...Some code...
    }
    

    那是传值,如果你需要foo()修改变量那么你需要传引用

    void caller( void )
    {
        int local;
       
        foo( &local );
    }
    
    void foo( int *pointerToVar )
    {
    ...Some code...
    }
    

    编辑:这里有一个示例来回答您的评论。主要是你将拥有

    int main()
    {
        int tal[99], c;
    
        talserie(tal, c);
        ...
    }
    

    你的功能将是

    talserie(int tal[99], int c)
    {
        ...
    }
    

    但是如果你不需要在main中使用c,你可以直接将它声明成talserie并且你只传递tal

    【讨论】:

    • 是的,我试过了,但是在使用 switch 中的函数时出现错误。它说我必须编写 int 函数,在编写 int 后我在 int 上得到错误。
    • 我把返回类型void放在我的例子中,但是你应该放任何你需要放的东西。
    • 感谢您的宝贵时间,但我不明白,因为我对编程很陌生,这是我第一次使用函数。
    • 也许这可以帮助您了解更多stackoverflow.com/questions/176118/… 并且您似乎需要澄清有关传递和重新调整参数的功能。谷歌这些东西
    • 我试图将我的变量写入函数中,看起来像这样 void talserie(int tal[99], int c);主要是我有一个使用函数的开关,通过更改变量,我在这些函数上得到错误。我也尝试在 main 中声明变量,但不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    相关资源
    最近更新 更多