Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
11 kB
2
Indexable
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 100
// define stucture for student
typedef struct student 
{
	int student_id;
	char first_name[MAX];
	char last_name[MAX];
	char gender[10];
	char day_of_birth[10];
	char class_name[10];
	char country[MAX];
}STUDENT;
// define stucture for course
typedef struct subject
{
	int subject_id;
	char subject_name[MAX];
	int credit;
}SUBJECT;
// define stucture for score
typedef struct score
{
	int student_id;
	int subject_id;
	float score;
}SCORE;
// define structure for student id and avg score
typedef struct avg_score
{
	int student_id;
	float score;
}AVG_SCORE;
// find number of rows
int row_count(const char* filename)
{
	FILE *fp;
    int count = 0;  // Line counter (result)
    char c;  // To store a character read from file
    // Open the file
    fp = fopen(filename, "r");
    // Check if file exists
    if (fp == NULL)
    {
        printf("Could not open file %s", filename);
        return 0;
    }
  
    // Extract characters from file and store in character c
    for (c = getc(fp); c != EOF; c = getc(fp))
        if (c == '\n') // Increment count if this character is newline
            count++;
  
    // Close the file
    fclose(fp);
    return count;
}
// read subject csv
void read_subject_csv(SUBJECT subject[], const char* path)
{
	FILE* fp = fopen(path, "r");
  	
    if (!fp)
        printf("Can't open file\n");
  
    else 
	{
        // Here we have taken size of
        // array 1024 you can modify it
        char buffer[1024];
  
        int row = 0;
        int column = 0;
  
        while (fgets(buffer,1024, fp)) 
		{
			
			if (row == 0)
			{
				row++;
                continue;
			}
			column = 0;

            //printf("%d\n", row);
            // Splitting the data
            char* value = strtok(buffer, ",");
  
            while (value) 
			{
                // Column 1
                if (column == 0) 
				{
                    //printf("%s\n", value);
                    subject[row-1].subject_id = atoi(value);
                	value = strtok(NULL, ",");
                }
  
                // Column 2
                if (column == 1) 
				{
                   	//printf("%s\n", value);
                    strcpy(subject[row-1].subject_name, value);
                	value = strtok(NULL, ",");
                }
  
                // Column 3
                if (column == 2) 
				{
                    //printf("\tLast name :");
                    //printf("%s\n", value);
                    subject[row-1].credit = atoi(value);
                	value = strtok(NULL, ",");
                }
                column++;
            }
            //printf("\n");
            row++;
        }
    }
    // Close the file
    fclose(fp);
}
// read score csv
void read_score_csv(SCORE score[], const char* path)
{
	FILE* fp = fopen(path, "r");
  
    if (!fp)
        printf("Can't open file\n");
  
    else 
	{
        // Here we have taken size of
        // array 1024 you can modify it
        char buffer[1024];
  
        int row = 0;
        int column = 0;
  
        while (fgets(buffer,1024, fp)) 
		{
			
			if (row == 0)
			{
				row++;
                continue;
			}
			column = 0;

            //printf("%d\n", row);
            // Splitting the data
            char* value = strtok(buffer, ",");
  
            while (value) 
			{
                // Column 1
                if (column == 0) 
				{
                    //printf("Student ID :");
                    //printf("%s", value);
                    score[row-1].student_id = atoi(value);
                	value = strtok(NULL, ",");
                }
  
                // Column 2
                if (column == 1) 
				{
                    //printf("\tFirst name :");
                    //printf("%s", value);
                    score[row-1].subject_id = atoi(value);
                	value = strtok(NULL, ",");
                }
  
                // Column 3
                if (column == 2) 
				{
                    //printf("\tLast name :");
                    //printf("%s", value);
                    score[row-1].score = atof(value);
                	value = strtok(NULL, ",");
                }
                column++;
            }
            //printf("\n");
            row++;
        }
    }
    // Close the file
    fclose(fp);
}
// read student csv
void read_student_csv(STUDENT student[], const char* path)
{
	FILE* fp = fopen(path, "r");
  
    if (!fp)
        printf("Can't open file\n");
  
    else 
	{
        // Here we have taken size of
        // array 1024 you can modify it
        char buffer[1024];
  
        int row = 0;
        int column = 0;
  
        while (fgets(buffer,1024, fp)) 
		{
			
			if (row == 0)
			{
				row++;
                continue;
			}
			
			column = 0;
            //row++;
  
            // To avoid printing of column
            // names in file can be changed
            // according to need
            
            //printf("%d\n", row);
            // Splitting the data
            char* value = strtok(buffer, ",");
  
            while (value) 
			{
                // Column 1
                if (column == 0) 
				{
                    //printf("Student ID :");
                    //printf("%s", value);
                    student[row-1].student_id = atoi(value);
                	value = strtok(NULL, ",");
                }
  
                // Column 2
                if (column == 1) 
				{
                    //printf("\tFirst name :");
                    //printf("%s", value);
                    strcpy(student[row-1].first_name, value);
                	value = strtok(NULL, ",");
                }
  
                // Column 3
                if (column == 2) 
				{
                    //printf("\tLast name :");
                    //printf("%s", value);
                    strcpy(student[row-1].last_name, value);
                	value = strtok(NULL, ",");
                }
                if (column == 3) {
                    //printf("\tGender :");
                    //printf("%s", value);
                    strcpy(student[row-1].gender, value);
                	value = strtok(NULL, ",");
                }
                if (column == 4) 
				{
                    //printf("\tDate of birth :");
                    //printf("%s", value);
                    strcpy(student[row-1].day_of_birth, value);
                	value = strtok(NULL, ",");       
                }
                if (column == 5) 
				{
                    //printf("\tClassname :");
                    //printf("%s", value);
                    strcpy(student[row-1].class_name, value);
               	 	value = strtok(NULL, ",");      
                }
                if (column == 6) 
				{
                    //printf("\tCountry :");
                    //printf("%s", value);
                    strcpy(student[row-1].country, value);
                	value = strtok(NULL, ",");    
                }
                column++;
            }
            //printf("\n");
            row++;
        }
    }
    // Close the file
    fclose(fp);
}
void list(char* class_name, STUDENT student_array[], int student_rows)
{
	FILE* fp = fopen("result.csv", "w");
	for (int i=0;i<student_rows;i++)
	{
		if (strcmp(student_array[i].class_name, class_name)==0)
		{
			fprintf(fp, "%d,%s,%s,%s,%s,%s,%s", student_array[i].student_id, student_array[i].first_name, 
			student_array[i].last_name, student_array[i].gender, student_array[i].day_of_birth, student_array[i].class_name, student_array[i].country);
		}
	}
	fclose(fp);
}
void count(char* gender, STUDENT student_array[], int student_rows)
{
	FILE* fp = fopen("result.csv", "w");
	for (int i=0;i<student_rows;i++)
	{
		if (strcmp(student_array[i].gender, gender)==0)
		{
			fprintf(fp, "%d,%s,%s,%s,%s,%s,%s", student_array[i].student_id, student_array[i].first_name, 
			student_array[i].last_name, student_array[i].gender, student_array[i].day_of_birth, student_array[i].class_name, student_array[i].country);
		}
	}
	fclose(fp);
}
void search(char* name, STUDENT student_array[], int student_rows)
{
	FILE* fp = fopen("result.csv", "w");
	for (int i=0;i<student_rows;i++)
	{
		if (strcasecmp(strcat(strcat(student_array[i].first_name, " "),student_array[i].last_name), name)==0)
		{
			fprintf(fp, "%d,%s,%s,%s,%s,%s,%s", student_array[i].student_id, student_array[i].first_name, 
			student_array[i].last_name, student_array[i].gender, student_array[i].day_of_birth, student_array[i].class_name, student_array[i].country);
		}
	fclose(fp);
	}
}
void swap(AVG_SCORE *x, AVG_SCORE *y)
{
    AVG_SCORE temp = *x;
    *x = *y;
    *y = temp;
}
  
// A function to implement bubble sort
void bubble_sort(AVG_SCORE avg_score[], int n)
{
   	for (int i = 0; i < n-1; i++)
       // Last i elements are already in place   
       	for (int j = 0; j < n-i-1; j++) 
        	if (avg_score[j].score < avg_score[j+1].score)
        		swap(&avg_score[j], &avg_score[j+1]);
}
// top n function
void top(SCORE score_array[], const int score_rows, STUDENT student_array[], const int student_rows, int n)
{
	FILE* fp = fopen("result.csv", "w");
	AVG_SCORE avg_score[student_rows];
	int num_subject = score_rows/student_rows;
	int t = 0;
	for (int j=0;j<score_rows;j+=num_subject)
	{
		printf("\n%d %d", j, t);
		avg_score[t].score = 0.0;
		avg_score[t].student_id = student_array[j].student_id;
		for (int k=j;k<j+num_subject;k++)
			avg_score[t].score+=score_array[k].score;
		avg_score[t].score/= num_subject;
		//printf(" %f", avg_score[t].score);
		++t;
	}	
	bubble_sort(avg_score, student_rows);
	//printf("%d", avg_score[n-1].student_id);
	for(int i=0;i<student_rows;i++)
	{
		if (avg_score[n-1].student_id == student_array[i].student_id)
		{
			printf("%d", student_array[i].student_id);
			fprintf(fp, "%d,%s,%s,%s,%s,%s,%s", student_array[i].student_id, student_array[i].first_name, 
			student_array[i].last_name, student_array[i].gender, student_array[i].day_of_birth, student_array[i].class_name, student_array[i].country);	
			break;
		}
	}
	fclose(fp);
}

int main()
{
    int score_rows, subject_rows, student_rows;
    // read student csv
    student_rows = row_count("dssv.csv");
    STUDENT student_array[student_rows];
    read_student_csv(student_array, "dssv.csv");
    // read score csv
    score_rows = row_count("diem.csv");
    SCORE score_array[score_rows];
    read_score_csv(score_array, "diem.csv");
    // read subject csv
    subject_rows = row_count("dsmh.csv");
    SUBJECT subject_array[subject_rows];
    read_subject_csv(subject_array, "dsmh.csv");
    printf("%d %d %d", score_rows, subject_rows, student_rows);
    //list("5203003", student_array, student_rows);
    //count("Male", student_array, student_rows);
    //search("last_name cách first_name", student_array, student_rows);
    top(score_array, score_rows, student_array, student_rows, 1);
    return 0;
}