[ create a new paste ] login | about

Link: http://codepad.org/x5upRtZ3    [ raw code | output | fork ]

C, pasted on Nov 26:
#include <stdio.h>
#include <stdlib.h>

int main()
{ 
	FILE *fp;
	char *fname ="test.txt";
	int c,a,b,d;
	double** matrix;
	int candidates_x, votes_y; /* size_x 行 size_y 列 */
	int i,j,count=0;

	printf("候補者数と投票者数を入力 >");
	scanf("%d%d", &candidates_x, &votes_y);
	fp = fopen( fname, "r" );
	if( fp == NULL ){
		printf( "%sファイルが開けません\n", fname );
		return -1;
	}
	matrix = (double**)malloc(sizeof(double*)*candidates_x);
	if (matrix==NULL) exit(1);


	for (i=0 ; i < candidates_x ; i++) {
		matrix[i] = (double*)malloc(sizeof(double)*votes_y);
		if (matrix[i]==NULL) exit(1);
	}
	i = j = 0;
	while( (c = fgetc( fp )) != EOF ){
	
		if('\n' == c){
	
			i = 0;
			j++;
			continue;
		}
	
		if(j >= votes_y){
			break;
		}

		if(i < candidates_x){
			matrix[i][j]=c;
		
		}
		
		i++;
	}
	for (j=0 ; j < votes_y ; j++) {
		for (i=0 ; i < candidates_x ; i++) {
			printf("%c ",(int)matrix[i][j]);
		}
		printf("\n");
	}	
	i=0;	a=matrix[i][j]; 
	for(b=1;b<=candidates_x;b++){
	  for(j=0;j<=votes_y;j++){
	    if(a==b){count++;  }
	    
	  }  
	  printf("candidates%d=%d\n",b,count); count=0;
}
	for (i=0 ; i < candidates_x ; i++) {
		free(matrix[i]); 
	}
	
	free(matrix);

	fclose(fp);

	return 0;

}


Output:
1
2
3
候補者数と投票者数を入力 >test.txtファイルが開けません

Exited: ExitFailure 255


Create a new paste based on this one


Comments: