danixxx89
26-04-2009, 14:25
Non riesco a capire come mai non vengono memorizzati i dati nell'array di double definito nella struttura arithmetic_op. Inoltre la funzione operation mi provoca un segmentation fault.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
typedef struct arithmetic_op{
char operation;
int num_of_op;
double operands[SIZE];
struct arithmetic_op *next;
}arithmetic_op;
void struct_data_storage(arithmetic_op *nptr);
double operation(arithmetic_op *nptr, double opr[], int n);
int main()
{
arithmetic_op *head=NULL;
struct_data_storage(head);
system ("pause");
return 0;
}
void struct_data_storage(arithmetic_op *nptr)
{
int n=0, i=0, a, opr[SIZE], arr[SIZE];
nptr=(arithmetic_op*)malloc(sizeof(arithmetic_op));
while(n<=SIZE){
nptr->operands[n]=0.0;
n++;
}
n=0;
if(nptr!=NULL){
do
{
printf("Inserire operatore aritmetico: ");
scanf("%c", &nptr->operation);
printf("%c\n", nptr->operation);
printf("Inserire il numero di operandi: ");
scanf("%d", &nptr->num_of_op);
printf("%d\n", nptr->num_of_op);
while(n!=nptr->num_of_op){
printf("Inserire gli operatori: ");
scanf("%f", &nptr->operands[n]); /*non memorizza come dovrebbe le
informazioni nella struttura*/
printf("%f\n", nptr->operands[n]);
opr[n]=0;
opr[n]=nptr->operands[n];
printf("%d", opr[n]);
arr[i]=operation(nptr, opr[n], n); /*la funzione operation mi
provoca un segmentation fault*/
n++;
}
i++;
printf("\n");
printf("Inserire il carattere di End of File per terminare,\n"
"altrimenti premere Invio: ");
a=getc(stdin);
} while((a=getchar())!=EOF);
}
else {
printf("Memoria non sufficiente\n");
}
nptr->next=NULL;
free(nptr);
printf("I risultati delle operazioni a partire dalla num. %d sono: ", i);
while(i!=0)
{
printf("%f\n", arr[i]);
i--;
}
return;
}
double operation(arithmetic_op *nptr, double opr[], int n)
{
double result=0;
if((nptr->operation)=='+')
result+=opr[n];
if((nptr->operation)=='-')
result-=opr[n];
if((nptr->operation)=='*')
result*=opr[n];
if((nptr->operation)=='/')
result/=opr[n];
return result;
}
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
typedef struct arithmetic_op{
char operation;
int num_of_op;
double operands[SIZE];
struct arithmetic_op *next;
}arithmetic_op;
void struct_data_storage(arithmetic_op *nptr);
double operation(arithmetic_op *nptr, double opr[], int n);
int main()
{
arithmetic_op *head=NULL;
struct_data_storage(head);
system ("pause");
return 0;
}
void struct_data_storage(arithmetic_op *nptr)
{
int n=0, i=0, a, opr[SIZE], arr[SIZE];
nptr=(arithmetic_op*)malloc(sizeof(arithmetic_op));
while(n<=SIZE){
nptr->operands[n]=0.0;
n++;
}
n=0;
if(nptr!=NULL){
do
{
printf("Inserire operatore aritmetico: ");
scanf("%c", &nptr->operation);
printf("%c\n", nptr->operation);
printf("Inserire il numero di operandi: ");
scanf("%d", &nptr->num_of_op);
printf("%d\n", nptr->num_of_op);
while(n!=nptr->num_of_op){
printf("Inserire gli operatori: ");
scanf("%f", &nptr->operands[n]); /*non memorizza come dovrebbe le
informazioni nella struttura*/
printf("%f\n", nptr->operands[n]);
opr[n]=0;
opr[n]=nptr->operands[n];
printf("%d", opr[n]);
arr[i]=operation(nptr, opr[n], n); /*la funzione operation mi
provoca un segmentation fault*/
n++;
}
i++;
printf("\n");
printf("Inserire il carattere di End of File per terminare,\n"
"altrimenti premere Invio: ");
a=getc(stdin);
} while((a=getchar())!=EOF);
}
else {
printf("Memoria non sufficiente\n");
}
nptr->next=NULL;
free(nptr);
printf("I risultati delle operazioni a partire dalla num. %d sono: ", i);
while(i!=0)
{
printf("%f\n", arr[i]);
i--;
}
return;
}
double operation(arithmetic_op *nptr, double opr[], int n)
{
double result=0;
if((nptr->operation)=='+')
result+=opr[n];
if((nptr->operation)=='-')
result-=opr[n];
if((nptr->operation)=='*')
result*=opr[n];
if((nptr->operation)=='/')
result/=opr[n];
return result;
}