Guest Posted June 18, 2012 at 08:31 PM Report Share #463817 Posted June 18, 2012 at 08:31 PM Boas, estou a começar a resolver os exercicios da USACO. Estou neste momento no segundo,Greedy Gift Givers, elaborei a minha solução mas estou com um erro de excepçao de virgula flutuante. Codigo: #include<stdio.h> #include<string.h> #include<stdlib.h> #define MAXNAME 15 struct person{ char name[MAXNAME]; int money; }; /* Get the index of the person */ int getname(char name[MAXNAME], struct person people[], int n_people) { int i; for(i=0 ; i<n_people; i++) if((strcmp(name,people[i].name)) == 0) return i; } void distribute_money(FILE *fp, struct person people[], int n_people) { int total_amount = 0; int total_people = 0; int amount_per_people = 0; char name[MAXNAME]; int i; /* Get the name */ fscanf(fp,"%s",name); /* Get the amount of money */ fscanf(fp,"%d %d",&total_amount, &total_people); /* How much money does each one gets */ if(total_amount != 0 || total_people != 0) amount_per_people = total_amount / total_people; /* Reads the amount of people */ for(i=0; i<total_people; i++){ /* Reads the people name */ fscanf(fp,"%s",name); /* Search for the people name and add the money in there */ people[getname(name,people,n_people)].money = amount_per_people; } } int main(void) { FILE *fp = fopen("gift1.in","r"); int n_people; /* Get the number of people that will contribute to the gift */ fscanf(fp,"%d",&n_people); /* Declare an array of structs */ struct person *people = malloc(n_people); int i; char name[15]; /* Puts the name in the structs */ for(i=0; i<=n_people; i++) fscanf(fp,"%s",people[i].name); for(i=0; i<=n_people;i++) distribute_money(fp,people,n_people); fclose(fp); fp = fopen("gift1.out","w"); for(i=0; i<n_people; i++) fprintf(fp,"%s %d\n",people[i].name,people[i].money); fclose(fp); return 0; } Com o gdb identifiquei o problema como sendo esta linha amount_per_people = total_amount / total_people; Mas não estou a ver uma forma de resolução possivel. Desde já agredeço o tempo dispendido. Link to comment Share on other sites More sharing options...
dardevelin Posted June 18, 2012 at 09:16 PM Report Share #463849 Posted June 18, 2012 at 09:16 PM Boas, vou dar uma dica, a divisão esta a ser feita sobre inteiros, que tal esta ? Link to comment Share on other sites More sharing options...
Guest Posted June 18, 2012 at 09:19 PM Report Share #463851 Posted June 18, 2012 at 09:19 PM Well this is embarassing. Podia jurar que tinha declarado como float. Link to comment Share on other sites More sharing options...
dardevelin Posted June 18, 2012 at 09:25 PM Report Share #463854 Posted June 18, 2012 at 09:25 PM Outros pontos: Em getname strcmp(name,people.name) < usa antes strncmp visto que strcmp não têm bounds checking e pode fazer leituras a overflow e underflow não faças comparações quando a key é maior que a word distribute_money fscanf(fp,"%s",name); < não esta protegido de buffer overflow fscanf(fp,"%d %d",&total_amount, &total_people); ^ -> risco de não ser um inteiro amount_per_people = total_amount / total_people; ^ divisão entre inteiros (float ou double se os decimais interessam) fscanf(fp,"%s",name); < overflow ? int main(void) FILE *fp = fopen("gift1.in","r"); < sem verificação de erros fscanf(fp,"%d",&n_people); < mesma historia de sempre struct person *people = malloc(n_people); < sem verificação de erro scanf(fp,"%s",people.name); < preciso dizer mais ? fclose(fp); < não houve verificação, sera que esta aberto ? fp = fopen("gift1.out","w"); < sem verificação outra vez fclose(fp); < n houve verificação, sera que esta aberto ? Link to comment Share on other sites More sharing options...
Guest Posted June 18, 2012 at 09:30 PM Report Share #463855 Posted June 18, 2012 at 09:30 PM Não fiz verificação de erros pois como é um exercicio da USACO eles é que me dão o input por isso foi um bocado mais perguiçoso. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now