fork download
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3.  
  4. typedef enum rule {
  5. first, total
  6. } Rule;
  7.  
  8. int fund(Rule rule, int v1, ...){
  9. switch(rule){
  10. case total:
  11. {
  12. int total = v1, value;
  13. if(v1 == -1) return 0;
  14. va_list ap;
  15.  
  16. va_start(ap, v1);
  17. value = va_arg(ap, int);
  18. while(value != -1){
  19. total += value;
  20. value = va_arg(ap, int);
  21. }
  22. va_end(ap);
  23.  
  24. return total;
  25. }
  26. break;
  27. case first:
  28. return v1;
  29. }
  30. return -1;
  31. }
  32.  
  33. int main(void){
  34. printf("first:%d\n", fund(first, 1, 2, 3, 4, -1));//first:1
  35. printf("total:%d\n", fund(total, 7, 5, 3, 1, -1));//total:16
  36. }
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
first:1
total:16