如何在c语言编程中使用函数

3ks5zfa0  于 2023-04-11  发布在  其他
关注(0)|答案(2)|浏览(116)
void result_1(){
    
    int score = 0;
    char answer[8];

    do
    {
        scanf("%s", answer);
     } while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);

    
    if (strcmp(answer, "c") == 0)
    {
        score += 1;
        printf("Correct \tYour score is now %d\n", score);
    }
    else
    {
        printf("Wrong \tYour score is still %d\n", score);
    }
}

void result_2(){
    
    int score = 0;
    char answer[8];

    do
    {
        scanf("%s", answer);
     } while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
    

    if (strcmp(answer, "a") == 0)
    {
        score += 1;
        printf("Correct \tYour score is now %d\n", score);
    }
    else
    {
        printf("Wrong \tYour score is still %d\n", score);
    }
}

void result_3(){
    int score = 0;
    char answer[8];

    do
    {
        scanf("%s", answer);
     } while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
    
    if (strcmp(answer, "c") == 0)
    {
        score += 1;
        printf("Correct \tYour score is now %d\n", score);
    }
    else
    {
        printf("Wrong \tYour score is still %d\n", score);
    }
}

void result_4(){
    int score = 0;
    char answer[8];

    do
    {
        scanf("%s", answer);
     } while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
    
    if (strcmp(answer, "d") == 0)
    {
        score += 1;
        printf("Correct \tYour score is now %d\n", score);
    }
    else
    {
        printf("Wrong \tYour score is still %d\n", score);
    }
}

void result_5(){
    int score = 0;
    char answer[8];

    do
    {
        scanf("%s", answer);
    } while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
    
    if (strcmp(answer, "c") == 0)
    {
        score += 1;
        printf("Correct \tYour score is now %d\n", score);
    }
    else
    {
        printf("Wrong \tYour score is still %d\n", score);
    }
}

int main(void)
{
    int score = 0;
    char answer[8];

    printf("Welcome to the C brain scan\n");
    printf("I will ask you some questions...\n");
    printf("Select the best answer and type in the corresponding letter.\n");

    printf("1. What does the == operator do?\n");
    printf("(a) calculates an arithmetic solution.\n");
    printf("(b) assigns a value to a variable.\n");
    printf("(c) checks for equality\n(d) draws the '=' character\n");
    printf("Your answer: \n");
    
    result_1();

    printf("2. Which is NOT a C keyword?\n");
    printf("(a) when\n");
    printf("(b) const\n");
    printf("(c) unsigned\n(d) do\n");
    printf("Your answer: \n");
    
    result_2();

    printf("3. In function call, the actual parameters are separated by\n");
    printf("(a) semicolons\n");
    printf("(b) colons\n");
    printf("(c) commas\n(d) space\n");
    printf("Your answer: \n");
    
    result_3();

    printf("4. What is the keyword for a global variable?\n");
    printf("(a) global\n");
    printf("(b) namespace\n");
    printf("(c) external\n(d) there is no keyword. to be global the variable must be declared outside of main()\n");
    printf("Your answer: \n");
    
    result_4();

    printf("5. A function prototype always contains:\n");
    printf("(a) a void type\n");
    printf("(b) an integer parameter\n");
    printf("(c) a function name\n(d) a comma\n");
    printf("Your answer: \n");

    result_5();
}

以上是我的完整代码
结果是这样的:
如果答案正确,得1分,否则得分不变。
我的问题:
我想把重复代码放在函数内部,使主函数看起来更干净。
在测验1中,如果我回答正确,我得到1分,但当我回答测验2时,即使我回答正确,也没有加分。

ffscu2ro

ffscu2ro1#

您的代码不完整,main的底部缺失。
在提供的代码中,你只运行了一次测验,如果你回答了c,那么你得到一分。如果(我在这里假设),完整的代码有另一个对result_1()的调用,并且你再次正确回答,输出仍然只有1。
为什么?因为你的score变量是局部的,在每次函数调用后就失去了作用域。而且,它在每次函数调用中都被初始化为0,但主要的收获是它是局部的。
请提供完整的代码,以便我们能够提供帮助。

thigvfpy

thigvfpy2#

如何在C中使用函数?函数声明/原型的一般格式是:例如<Return type> <function name> <arguments>;void quiz1(void);将不返回任何值,也不接受任何参数,而void get_n_check_answer(enum quizid id);将接受1个参数,与上一个示例相反。在原型之后,您只需在函数体中定义函数应该做什么。C中的函数体看起来像<Return type> <function name> <arguments> {... BODY ....}因此,quiz 1()的函数体看起来像这样void quiz1(void) {...Body of code....}
现在让我们进入代码:

#include <stdio.h>
#include <stdlib.h>
/** Seems like you are a beginner in C
 *  For this problem you have declared multiple of the same variable.
 *  you only need to declare score once.
 *  You can declare it in Main fucntion and pass it by reference to a function
 *  or to keep things simple you can make it global
 *  In this solution I will make it global to keep things easier
 */
enum quizid{
    q1,
    q2,
    q3,
    q4,
    q5,
};
int score =0;
char answers[5] = {'c','a','c','d','c'};

/** Function prototype
 *  function declaration entails in C:
 *  <Return type> <function name> <arguments> 
 */
void welcome_msg(void);
void quiz1(void);
void quiz2(void);
void quiz3(void);
void quiz4(void);
void quiz5(void);
void get_n_check_answer(enum quizid id);

void get_n_check_answer(enum quizid id) {
    printf("Your answer: ");
    char user_answer = getchar(); // this reads the answers
    getchar(); // this ignores the line feed. 
    if(user_answer==answers[id]) { // here compare it for correctness and print result 
        score++;
        printf("Correct \tYour score is now %d\n", score);
    } else {
        printf("Wrong \tYour score is still %d\n", score);
    }
}

void welcome_msg(void) {
    printf("Welcome to the C brain scan\n");
    printf("I will ask you some questions...\n");
    printf("Select the best answer and type in the corresponding letter.\n");
}

void quiz1(void) {
    printf("1. What does the == operator do?\n");
    printf("(a) calculates an arithmetic solution.\n");
    printf("(b) assigns a value to a variable.\n");
    printf("(c) checks for equality\n(d) draws the '=' character\n");
    get_n_check_answer(q1);
}

void quiz2(void) {
    printf("2. Which is NOT a C keyword?\n");
    printf("(a) when\n");
    printf("(b) const\n");
    printf("(c) unsigned\n(d) do\n");
    get_n_check_answer(q2);
}

void quiz3(void) {
    printf("3. In function call, the actual parameters are separated by\n");
    printf("(a) semicolons\n");
    printf("(b) colons\n");
    printf("(c) commas\n(d) space\n");
    get_n_check_answer(q3); 
}

void quiz4(void) {
    printf("4. What is the keyword for a global variable?\n");
    printf("(a) global\n");
    printf("(b) namespace\n");
    printf("(c) external\n(d) there is no keyword. to be global the variable must be declared outside of main()\n");
    get_n_check_answer(q4); 
}

void quiz5(void) {
    printf("5. A function prototype always contains:\n");
    printf("(a) a void type\n");
    printf("(b) an integer parameter\n");
    printf("(c) a function name\n(d) a comma\n");
    get_n_check_answer(q5); 
}

int main(void) {

    welcome_msg(); // welcome the user
    quiz1(); // do quiz 1
    quiz2(); // do quiz 2
    quiz3(); // do quiz 3
    quiz4(); // do quiz 4
    quiz5(); // do quiz 5
    exit(0); // exit program
}

提供的代码的输出:

Codes/C/Stackoverflow via C v12.2.0-gcc 
> ./a.out                                                                         
Welcome to the C brain scan
I will ask you some questions...
Select the best answer and type in the corresponding letter.
1. What does the == operator do?
(a) calculates an arithmetic solution.
(b) assigns a value to a variable.
(c) checks for equality
(d) draws the '=' character
Your answer: c
Correct         Your score is now 1
2. Which is NOT a C keyword?
(a) when
(b) const
(c) unsigned
(d) do
Your answer: a
Correct         Your score is now 2
3. In function call, the actual parameters are separated by
(a) semicolons
(b) colons
(c) commas
(d) space
Your answer: c
Correct         Your score is now 3
4. What is the keyword for a global variable?
(a) global
(b) namespace
(c) external
(d) there is no keyword. to be global the variable must be declared outside of main()
Your answer: d
Correct         Your score is now 4
5. A function prototype always contains:
(a) a void type
(b) an integer parameter
(c) a function name
(d) a comma
Your answer: c
Correct         Your score is now 5

希望这对你有帮助。如果你有任何问题,不要犹豫,问。

相关问题