Как правильно вызвать функцию? - C (СИ) (69637)

Узнай цену своей работы

Формулировка задачи:

как вызвать функцию?
Листинг программы
  1. int sum_last(list_ptr a) {
  2. list_ptr ptr;
  3. assert(NULL != a);
  4. assert(NULL != a->next);
  5. for (ptr = a; ptr->next->next; ptr = ptr->next)
  6. {
  7. }
  8. return ptr->data + ptr->next->data;
  9. }
Это полный код
Листинг программы
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. typedef int bool;
  7. #define true 1
  8. #define false 0
  9. typedef struct list {
  10. int data;
  11. struct list *next;
  12. } *list_ptr;
  13. char ch1, str[100];
  14. int i, count = 0;
  15. list_ptr x = NULL;
  16. int sum_last(list_ptr a) {
  17. list_ptr ptr;
  18. assert(NULL != a);
  19. assert(NULL != a->next);
  20. for (ptr = a; ptr->next->next; ptr = ptr->next)
  21. {
  22. }
  23. return ptr->data + ptr->next->data;
  24. }
  25. void show(struct list *a) {
  26. while (a != NULL) {
  27. printf(" %d\t", a->data);
  28. a = a->next;
  29. }
  30. }
  31. bool isnumber(const char*s) {
  32. char* e = NULL;
  33. (void)strtol(s, &e, 0);
  34. return e != NULL && *e == (char)0;
  35. }
  36. void add(list_ptr *a, int newdata) {
  37. if (*a == NULL) {
  38. *a = (list_ptr)malloc(sizeof(list_ptr));
  39. (*a)->data = newdata;
  40. (*a)->next = NULL;
  41. return;
  42. }
  43. list_ptr ptr;
  44. for (ptr = *a; ptr->next; ptr = ptr->next)
  45. {
  46. }
  47. ptr->next = (struct list *)malloc(sizeof(struct list));
  48. ptr = ptr->next;
  49. ptr->data = newdata;
  50. ptr->next = NULL;
  51. }
  52. int main()
  53. {
  54. char c = 0, c1;
  55. int i, j, min, number;
  56. while (c != 27)
  57. {
  58. system("cls");
  59. printf(" Enter - run the program.\n");
  60. printf(" Esc - exit.\n");
  61. printf(" Any other key - information about program.\n");
  62. c = getch();
  63. system("cls");
  64. switch (c)
  65. {
  66. case 27:
  67. break;
  68. case 13:
  69. {
  70. system("cls");
  71. printf("It\'s time to fill the list (to end the filling eneter ***)\n\n");
  72. for (;;) {
  73. printf("Enter the element:\n>>");
  74. do
  75. {
  76. gets(str);
  77. if (!isnumber(str) && strcmp(str, "***") != 0) {
  78. printf("Please, enter an integer number:\n>> ");
  79. continue;
  80. }
  81. break;
  82. } while (true);
  83. if (strcmp(str, "***") == 0) break;
  84. number = atoi(str);
  85. add(&x, number);
  86. count++;
  87. }
  88. if (count == 0) printf("\nThe first list is empty.\n");
  89. else printf("Elements in your first list are:\n");
  90. show(x);
  91. x = NULL;
  92. printf("\n\n\nPress \'Enter\' to continue");
  93. getch();
  94. printf("The sum of the last and the penultimate number: ");
  95. printf("\n\tEsc - exit.\n");
  96. printf("\tAny other - go to the main page.\n");
  97. c = getch();
  98. break;
  99. }
  100. default:
  101. {
  102. printf("\n Finds the sum of the last and penultimate elements of the list L containing at least two elements.\n\n");
  103. printf(" Esc - exit.\n");
  104. printf(" Any other - go to the main page.\n");
  105. c = getch();
  106. break;
  107. }
  108. }
  109. }
  110. return 0;
  111. }

Решение задачи: «Как правильно вызвать функцию?»

textual
Листинг программы
  1. if (count == 0) printf("\nThe first list is empty.\n");  // 104 строка
  2. else printf("Elements in your first list are:\n");
  3. show(x);
  4. x = NULL;

Объяснение кода листинга программы

  1. Проверяется условие count == 0.
  2. Если условие истинно, то выводится сообщение The first list is empty..
  3. Если условие ложно, то выводится сообщение Elements in your first list are:.
  4. Вызывается функция show(x).
  5. Значение x присваивается NULL.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.867 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы