Как правильно вызвать функцию? - C (СИ) (69637)
Формулировка задачи:
как вызвать функцию?
Это полный код
int sum_last(list_ptr a) {
list_ptr ptr;
assert(NULL != a);
assert(NULL != a->next);
for (ptr = a; ptr->next->next; ptr = ptr->next)
{
}
return ptr->data + ptr->next->data;
}#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <assert.h>
typedef int bool;
#define true 1
#define false 0
typedef struct list {
int data;
struct list *next;
} *list_ptr;
char ch1, str[100];
int i, count = 0;
list_ptr x = NULL;
int sum_last(list_ptr a) {
list_ptr ptr;
assert(NULL != a);
assert(NULL != a->next);
for (ptr = a; ptr->next->next; ptr = ptr->next)
{
}
return ptr->data + ptr->next->data;
}
void show(struct list *a) {
while (a != NULL) {
printf(" %d\t", a->data);
a = a->next;
}
}
bool isnumber(const char*s) {
char* e = NULL;
(void)strtol(s, &e, 0);
return e != NULL && *e == (char)0;
}
void add(list_ptr *a, int newdata) {
if (*a == NULL) {
*a = (list_ptr)malloc(sizeof(list_ptr));
(*a)->data = newdata;
(*a)->next = NULL;
return;
}
list_ptr ptr;
for (ptr = *a; ptr->next; ptr = ptr->next)
{
}
ptr->next = (struct list *)malloc(sizeof(struct list));
ptr = ptr->next;
ptr->data = newdata;
ptr->next = NULL;
}
int main()
{
char c = 0, c1;
int i, j, min, number;
while (c != 27)
{
system("cls");
printf(" Enter - run the program.\n");
printf(" Esc - exit.\n");
printf(" Any other key - information about program.\n");
c = getch();
system("cls");
switch (c)
{
case 27:
break;
case 13:
{
system("cls");
printf("It\'s time to fill the list (to end the filling eneter ***)\n\n");
for (;;) {
printf("Enter the element:\n>>");
do
{
gets(str);
if (!isnumber(str) && strcmp(str, "***") != 0) {
printf("Please, enter an integer number:\n>> ");
continue;
}
break;
} while (true);
if (strcmp(str, "***") == 0) break;
number = atoi(str);
add(&x, number);
count++;
}
if (count == 0) printf("\nThe first list is empty.\n");
else printf("Elements in your first list are:\n");
show(x);
x = NULL;
printf("\n\n\nPress \'Enter\' to continue");
getch();
printf("The sum of the last and the penultimate number: ");
printf("\n\tEsc - exit.\n");
printf("\tAny other - go to the main page.\n");
c = getch();
break;
}
default:
{
printf("\n Finds the sum of the last and penultimate elements of the list L containing at least two elements.\n\n");
printf(" Esc - exit.\n");
printf(" Any other - go to the main page.\n");
c = getch();
break;
}
}
}
return 0;
}Решение задачи: «Как правильно вызвать функцию?»
textual
Листинг программы
if (count == 0) printf("\nThe first list is empty.\n"); // 104 строка
else printf("Elements in your first list are:\n");
show(x);
x = NULL;
Объяснение кода листинга программы
- Проверяется условие count == 0.
- Если условие истинно, то выводится сообщение
The first list is empty.. - Если условие ложно, то выводится сообщение
Elements in your first list are:. - Вызывается функция show(x).
- Значение x присваивается NULL.