gpt4 book ai didi

c - 如果第一个参数仅指示枚举值,如何找出参数的数量(使用变量参数)?

转载 作者:行者123 更新时间:2023-11-30 20:20:38 24 4
gpt4 key购买 nike

所以我得到了一个枚举值列表。该函数将返回整数值。它接受一个枚举值,其余的后面是整数值。如果枚举类型称为总计并返回总计,我应该获得以下整数值的总和。

最佳答案

像这样

#include <stdio.h>
#include <stdarg.h>

typedef enum rule {
first, total
} Rule;

int fund(Rule rule, int v1, ...){
switch(rule){
case total:
{
int total = v1, value;
if(v1 == -1) return 0;
va_list ap;

va_start(ap, v1);
value = va_arg(ap, int);
while(value != -1){
total += value;
value = va_arg(ap, int);
}
va_end(ap);

return total;
}
break;
case first:
return v1;
}
return -1;
}

int main(void){
printf("first:%d\n", fund(first, 1, 2, 3, 4, -1));//first:1
printf("total:%d\n", fund(total, 7, 5, 3, 1, -1));//total:16
}

关于c - 如果第一个参数仅指示枚举值,如何找出参数的数量(使用变量参数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com