C语言 如何将主函数的内容移动到不同的函数并保留功能?

sg3maiej  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(94)

我目前正在尝试将“main”函数的内容移动到它自己的函数中,这样我就可以重复操作,也可以有更多的选项来选择何时发生什么。一旦函数完成,我还想在每次购买时增加一个整数,这样就可以用某种库存命令来检查它。
我犯了一个错误,把我以前在main中的东西复制粘贴到我想成为它自己的函数中。不幸的是,这实际上是我第一次尝试声明一个不是这个网站上其他人给我的函数,所以我真的不知道如何声明它们。我猜声明不正确/缺少数据,导致问题。
代码:

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

//Attempting to declare the function early, which is the only thing CS50 has taught me about custom functions thus far.
void itemshop(int buckaroonies);

//Struct given to me in another post
struct item
    {
        string name;
        int price;
        string description;
    };

//Part of aformentioned struct
int find_item(string item_name, struct item items[], size_t num_items)
{
    for (size_t i = 0; i < num_items ; i++)
    {
        if (0 == strcmp(items[i].name, item_name))
        {
            return i;
        }
    }
    //Item not available
    return -1;
}

int main(void)
{
    //Item struct
     struct item items[] = {
        {"Matchbox", 50, "They're cheap, and they'll keep you warm."},
        {"Wool Hat", 125, "A nice, handknit, cozy hat. Thank my ma for that!"},
        {"Heavy Coat", 250, "This coat'll keep you cozy through the night no matter the situation!"},
        {"Canned Food", 25, "It don't taste too good, but it'll last you a lifetime. Eat up!"}
    };
    const size_t num_items = sizeof(items) / sizeof(items[0]);

    //Integers for items once they are purchased
    int buckaroonies = 500;
    int matchb = 0;
    int woolh = 0;
    int heavyc = 0;
    int canf = 0;

    itemshop(int buckaroonies);
}

//This is probably the worst part, I just copy-pasted what I had in main into this function, and likely declared it incorrectly as well
void itemshop(int buckaroonies)
{
    //Dialogue and selection
    printf("Hey chum, welcome to the item shop. You've got a handful 'o coin on ya, huh?\nYou came to the right place, we got the best wares in town!\n\n");
    printf("Store:\n\nMatchbox - 50\nWool Hat - 125\nHeavy Coat - 250\nCanned Food - 25\n");
    string select = get_string ("What'll it be? You have %i buckaroonies, pal.\n", buckaroonies);
    string description[] = { "They're cheap, and they'll keep you warm.\n", "A nice, handknit, cozy hat. Thank my ma for that!\n", "This coat'll keep you cozy through the night no matter the situation!\n", "It don't taste too good, but it'll last you a lifetime. Eat up!" };

    //Get item details from selection, error comes from here
    int index = find_item(select, items, num_items);

    //Return dialogue based on selected item
    if (-1 == index)
    {
        printf("We aint got that, pal.\n");
    }
    else
        {
            char c = get_char ("That %s'll cost ya %i buckaroonies. %s You want it?\n", items[index].name, items[index].price, items[index].description);

            if(c == 'y' || c == 'Y')

                {
                    printf("Pleasure doing business with you, pal. -%i Buckeroonies\n", items[index].price);
                    buckaroonies = buckaroonies - items[index].price;
                }
            else if (c == 'n' || c == 'N')
                {
                    printf("Alright, feel free to keep browsing.\n");
                }
            else
                {
                    printf("Hey, uh... I don't know what that means.\n");
                }
        }
}

字符串
错误:

itemshop.c:48:14: error: expected expression
    itemshop(int buckaroonies);
             ^

6jygbczu

6jygbczu1#

main()的内容移动到从main()调用的函数中的基本技术是更改函数的名称并编写新的main()。由于main()程序忽略所有命令行参数,因此更改如下所示:

之前

int main(void)
{
    …declarations and code…
}

字符串

之后

static int old_main(void)
{
    …declarations and code…
    return 0;   // Necessary - only main() gets special treatment
}

int main(void)
{
    return old_main();
}


然后,您可以进行更改,将适当的代码从old_main()移动到main(),并向old_main()添加参数,以便它可以访问需要处理的数据。
如果你的程序接受命令行参数,你会用(int argc, char **arg)替换每一个(void)。但是,你最有可能希望在修改后的main()中处理命令行参数,调用函数来处理命令行上命名的文件(或命令行上指定的字符串)。
当我处理一个问题时,我经常从一个包含太多内容的main()函数开始,最后我基于原始的main()编写了一个测试工具函数,可以从修改后的main()重复调用。GitHub上的(堆栈溢出问题)存储库-例如,src/so-4715-1134子目录中的文件mds41.cmds43.c。该存储库中还有许多其他示例。

wfsdck30

wfsdck302#

你在main的底部写了void itemshop();。这是一个声明,而不是一个函数调用。当你调用一个函数时,你不要直接在函数名之前写一个类型(即voidint)。仔细比较这一行和你代码中所有其他函数调用(即当你调用printfstrcmp时)。
删除void,然后解决编译报告给您的下一个错误,即您忘记包含函数的参数。
您的代码中可能存在其他问题,您可以通过仔细阅读所有编译器警告和错误消息和/或使用调试器来诊断。

相关问题