【问题标题】:Trouble understanding Functions in C难以理解 C 中的函数
【发布时间】:2020-10-31 00:05:40
【问题描述】:

我正在完成一项编程任务。我在 main 中进行了密钥验证,但决定尝试使其成为一个单独的函数。我还不太了解函数,所以我看不出哪里出错了。每当我运行程序时,即使我知道它不是,我总是只会得到“密钥有效”。正如我所说,程序在 main 中运行良好。

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int validate (int c, string v[]); //prototpe validate function

int main (int argc, string argv[])
{
    int validate (int argc, string argv[]); //run validate for argc and argv

    printf("Key is valid\n"); //if valid, print message

}

int validate (int c, string v[])
{
    //Validate that only one Command Line Argument was entered
    if (c != 2) //Check the number if entered commands at execution
    {
        //If more than one, print error message
        printf("Key must be the only Command Line Argument\n");
        return 1; //Return false
    }

    //Validate that Key length is 26 characters
    if (strlen(v[1]) != 26) //Don't forget null 0
    {
        printf("Key must contain exactly 26 characters\n");
        return 1; //Return false
    }

    //Validate that all Key characters are alphabetic
    for (int i = 0, n = strlen(v[1]); i < n; i++)
    {
        //Check each element of the array
        if (isalpha(v[1][i]))
        {
            continue; //Continue if alphabetic
        }
        else
        {
            //if non-alphabetic, print error code
            printf("Key must contain only alphabetic characters\n");
            return 1; //Return false
        }
    }

    //Validate that each Key character is unique
    for (int x = 0, z = strlen(v[1]) - 1; x < z; x++)
    {
        //Create a second loop to compare every element to the first before incrementing the first
        for (int y = x + 1; y < z; y++)
        {
            //Cast array elements to int, check if each element equals next element in array
            if (v[1][x] == v[1][y])
            {
                printf("Key must contain exactly 26 unique characters\n");
                return 1;
            }
        }
    }

    return 0; //Key is valid, so return true
}

【问题讨论】:

    标签: c function cs50


    【解决方案1】:

    您只是声明函数validate,而不是运行该函数并无条件打印Key is valid

    要运行函数validate并仅在返回0时打印Key is validmain函数应该是这样的:

    int main (int argc, string argv[])
    {
        if (validate (argc, argv) == 0) //run validate for argc and argv and check its response
        {
            // put printing inside if statement so that it runs only if the condition is true
            printf("Key is valid\n"); //if valid, print message
        }
    }
    

    【讨论】:

    • 成功了!谢谢你。因此,将它作为 if 语句运行允许我检查条件,而如果我只是运行一个函数,例如,做一些数学运算,我可以像我一样运行它,然后我的程序就会得到结果功能向前发展?还是我还在搞砸。只是想更好地理解。
    • @apreynolds1989 目前尚不清楚“就像我在做的那样”是什么意思,但你不能通过声明它们来运行函数。
    • 你需要捕获函数的返回值。否则你的代码没有函数执行的结果,除非你使用指针。
    • 好的。我认为只需调用它会运行的函数,然后如果该函数在任何时候返回 false,那么它将结束程序,但我发现情况并非如此。谢谢你们!
    • @apreynolds1989 调用函数将使函数运行。 声明函数不会使函数运行。
    【解决方案2】:

    函数声明提示编译器我将返回一个特定的数据类型,并且我将接受参数部分中的给定数据类型。例如,

    int check(int a, int b); -> return type is int and the function will accept 2 integer parameters.
    int mul(int a, float b); -> return type is int and the function will accept 1 integer parameter and 1 float.
    void check(); -> returns nothing, accepts nothing
    

    函数调用就像你的代码正在调用要执行的函数。

    int c = check(2, 3);
    int b = mul(3, 0.12);
    check();
    

    您的函数正在返回一些值。您必须获取该值并根据以下值执行其余代码。

    if (validate (argc, argv) == 0) {
        printf("key is valid");
    } else {
        printf("key is not valid");
    }
    

    【讨论】:

    • void check(); 没有说明它在 C 中接受什么。void check(void); 说明它什么都不接受。
    • @MikeCAT:感谢您的澄清。但通常我们会将函数声明为 void check();如果它不接受任何参数。如果我错了,请纠正我。
    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多