代碼如下
/*
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
英語一般,所以在命名變量的時候可能有點語法錯誤,請勿見笑
因為是初學,所以還請各位詳細指點!!
不是x86環境下的程序?
http://blog.chinaunix.net/uid-20178794-id-3494278.html