fork download
  1. #include <stdio.h>
  2.  
  3. int acc(int x) { // acc関数の内部のみ修正対象
  4. static int sum = 0;
  5. static int count = 0;
  6. int y = 0;
  7.  
  8. if (x == -1) {
  9. sum = 0;
  10. count = 0;
  11. y = 0;
  12. } else if (x == -2) {
  13. y = count;
  14. } else if (x == 0) {
  15. y = sum;
  16. } else if (x > 0) {
  17. sum += x;
  18. count++;
  19. y = sum;
  20. }
  21. return y;
  22. }
  23.  
  24. int main() {
  25. int score, num, i;
  26. printf("数字の個数を入力してください:");
  27. scanf("%d", &num);
  28. printf("%d\n", num);
  29.  
  30. for (i = 0; i < num; i++) {
  31. printf("正の整数を入力してください:");
  32. scanf("%d", &score);
  33. printf("%d\n", score);
  34. acc(score);
  35. }
  36.  
  37. printf("数字の個数は%dです。\n", acc(-2));
  38. printf("合計値は%dです。\n", acc(0));
  39.  
  40. acc(-1); // リセット
  41. acc(3); // 3を追加
  42.  
  43. printf("数字の個数は%dです。\n", acc(-2));
  44. printf("合計値は%dです。\n", acc(0));
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0.01s 5316KB
stdin
3 4 5 6 
stdout
数字の個数を入力してください:3
正の整数を入力してください:4
正の整数を入力してください:5
正の整数を入力してください:6
数字の個数は3です。
合計値は15です。
数字の個数は1です。
合計値は3です。