Hej mam problem z działaniem programu.
Na wstępie pytanie czy jest jakaś różnica pomiędzy sprawdzaniem czy wskaźnik ==NULL czy wskaźnik==0 ?
Mój program nie liczy poprawnie dla liczby = 0;
Treść zadania:
Napisać program, który wykonuje operacje matematyczne (dodawanie, odejmowanie, mnożenie, dzielenie) na liczbach całkowitych wprowadzanych przez użytkownika. Program powinien wyświetlić w kolejnych liniach sumę, różnicę, iloczyn oraz iloraz wprowadzonych liczb, a w przypadku niemożliwości wykonania którejś z operacji, zamiast jej wyniku, program powinien wyświetlić komunikat "Error". Każda operacja powinna stanowić oddzielną funkcję, która zwraca wartość 1, gdy operacja została wykonana poprawnie, a 0 w przeciwnym wypadku. Prototypy funkcji powinny wyglądać następująco:
int add(const int *first, const int *second, int* result);
int subtract(const int *first, const int *second, int* result);
int multiply(const int *first, const int *second, int* result);
int divide(const int *first, const int *second, float* result);
KOD:
#include <time.h>
#include <stdlib.h>
int add(const int *first, const int *second, int* result);
int subtract(const int *first, const int *second, int* result);
int multiply(const int *first, const int *second, int* result);
int divide(const int *first, const int *second, float* result);
int main()
{
int a=0;
int b=0;
int c=0;
int *first=&a;
int *second=&b;
int *result=&c;
printf("\nPodaj pierwsza liczbe ");
scanf("%d",first);
printf("\nPodaj druga liczbe ");
scanf("%d",second);
add(first,second,result);
subtract(first,second,result);
multiply(first,second,result);
divide(first,second,(float*)result);
}
int add(const int *first, const int *second, int* result)
{
if(!*first || !*second)
{
printf("\nError");
return 0;
}
else if(first==0 || second==0)
{
*result=*first+*second;
printf("%d \n",*result);
return 1;
}
else
{
*result=*first+*second;
printf("%d \n",*result);
return 1;
}
}
int subtract(const int *first, const int *second, int* result)
{
if(!*first || !*second)
{
printf("\nError");
return 0;
}
else
{
*result=*first-*second;
printf("%d \n",*result);
return 1;
}
}
int multiply(const int *first, const int *second, int* result)
{
if(!*first || !*second)
{
printf("\nError\n");
return 0;
}
else
{
*result=*first * *second;
printf("%d \n",*result);
return 1;
}
}
int divide(const int *first, const int *second, float* result)
{
if(!*first || !*second)
{
printf("Error\n");
return 0;
}
else if( *second==0)
{
printf("Error\n");
return 0;
}
else
{
*result=(float)*first / (float)*second;
printf("%f \n",*result);
return 1;
}
}
Dzięki za pomoc