chinanoahli 2015-04-20 01:50 采纳率: 100%
浏览 1922
已采纳

C語言 请教bus error 10,USACO Greedy Gift Givers

代碼如下

/*
LANG: C
TASK: gift1
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


FILE *input_file, *output_file;

int total_people, gift, numbers_of_recever, money_giving[9];

int scan_names_counter, scan_recever_counter, recever_in_names, money[9], money_receving[9], splid_the_gift, how_many_waves_gift_been_send;

char names[9][13], recever[9][13], the_giver[13], the_recever[13];


void scan_names(){
        fscanf(input_file, "%d", &total_people);

        scan_names_counter=0;
        while(scan_names_counter<total_people){
                fscanf(input_file, "%s", names[scan_names_counter]);
                scan_names_counter++;
        }
}


void givers_money(){
        scan_names_counter=0;
        fscanf(input_file, "%s", the_giver);
        fscanf(input_file, "%d%d", &gift, &numbers_of_recever);
        while(strcmp(the_giver, names[scan_names_counter])!=0){
                scan_names_counter++;
        }
        money_giving[scan_names_counter]=money_giving[scan_names_counter]-gift;
        if(numbers_of_recever!=0){
                splid_the_gift=gift/numbers_of_recever;
        }else{
                splid_the_gift=0;
        }
}


void receive_the_money(){
        scan_recever_counter=0;
        while(scan_recever_counter<numbers_of_recever){
                fscanf(input_file, "%s", the_recever);
                scan_names_counter=0;
                while(scan_names_counter<total_people){
                        if(strcmp(the_recever, names[scan_names_counter])==0){
                                money_receving[scan_names_counter]=money_receving[scan_names_counter]+splid_the_gift;
                        }
                        scan_names_counter++;
                }
                scan_recever_counter++;
        }
}


void money_counter(){
        scan_names_counter=0;
        while(scan_names_counter<total_people){
                money[scan_names_counter]=money_receving[scan_names_counter]+money_giving[scan_names_counter];
                scan_names_counter++;
        }
}


void print_gift_tab(){
        scan_names_counter=0;
        while(scan_names_counter<total_people){
                fprintf(output_file, "%s %d\n", names[scan_names_counter], money[scan_names_counter]);
                scan_names_counter++;
        }
}


int main(){
        input_file = fopen("gift1.in", "r");
        output_file = fopen("gift1.out", "w+");


        scan_names();
        how_many_waves_gift_been_send=0;
        while(how_many_waves_gift_been_send<total_people){
                givers_money();
                receive_the_money();
                how_many_waves_gift_been_send++;
        }
        money_counter();
        print_gift_tab();


        fclose(input_file);
        fclose(output_file);
        return 0;
} 

讀下面2個文件
文件1:正常結束並輸出正確的數據到文件

5
dave
laura
owen
vick
arm
dave
200 3
laura
owen
vick
owen
500 1
dave
arm
150 2
vick
owen
laura
0 2
arm
vick
vick
0 0 

文件2:不能正常輸出 bus error 10

10
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Spafford
Farmer
Venema
Linus
mitnik
300 3
Poulsen
Tanner
Baran
Poulsen
1000 1
Tanner
Spafford
2000 9
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Farmer
Venema
Linus
Tanner
1234 1
Poulsen
Stallman
536 3
Farmer
Venema
Linus
Ritchie
2000 1
mitnik
Baran
79 2
Tanner
Farmer
Farmer
0 0
Venema
12 9
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Spafford
Farmer
Linus
Linus
1000 1
mitnik 

英語一般,所以在命名變量的時候可能有點語法錯誤,請勿見笑
因為是初學,所以還請各位詳細指點!!

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-04-20 01:58
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看