//输入一个文件名或两个文件名(成绩表),解析内部学生成绩,然后计算总分,
//如果两个文件名,需要汇总两个表,然后按照成绩高低排列输出整表。
//(PS : 成绩表为CSV格式,已逗号分隔,暂不考虑异常输入。)
#include "count_grade.h"
#include <stdio.h>
#include<string.h>
#include <windows.h>
int main(int argc, char* argv[])
{
FILE* fp = NULL;
char* filename = NULL;
char* file1_buf = NULL;
char* line_buf = NULL;
struct student stu[LSIZE] = { 0 };
char ch = 0;
int rows = 0;
int first_line_len = 0;
int line = 1;
Item temp = { 0 };
List student = NULL;
FILE* fp2 = NULL;
char* file2name = NULL;
char* file2_buf = NULL;
char* line2_buf = NULL;
struct student stu2[LSIZE] = { 0 };
char ch2 = 0;
int rows2 = 0;
int first_line_len2 = 0;
int line2 = 1;
Item temp2 = { 0 };
List student2 = NULL;
if (argc != 3)
{
printf("Usage:%s filemane\n", argv[0]);
exit(EXIT_FAILURE);
}
fp = fopen(argv[1], "rb");
if (NULL == fp)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);
file1_buf = (char*)malloc(size + 1);
memset(file1_buf, 0, size + 1);
line_buf = (char*)malloc(size + 1);
memset(line_buf, 0, size + 1);
filename = argv[1];
fp = fopen(filename, "rb");
if (NULL == fp)
{
fprintf(stderr, "Can't open %s\n", filename);
exit(EXIT_FAILURE);
}
//计算行数
while (EOF != (ch = getc(fp)))
{
if ('\n' == ch)
{
++rows;
}
}
rewind(fp);
//计算第一行的字符数
while ('\n' != (ch = getc(fp)))
{
++first_line_len;
}
//让句柄偏移到第二行
fseek(fp, first_line_len + 1, SEEK_SET);
#if 0
int index = 0;
while (line < rows)
{
read_line(fp, line_buf, size); //读出每一行
parse_line(line_buf, &(stu[index]), line);//把每一行写入结构体
++line;
++index;
}
for (index = 0; index < rows - 1; ++index)
{
calculate_total_grade(&(stu[index]), rows);
}
#endif
int index = 0;
int valid_line_num = 0;
while (line < rows)
{
read_line(fp, line_buf, size); //读出每一行
valid_line_num += parse_line(line_buf, &(stu[index]));//把每一行写入结构体
++line;
++index;
}
InitializeList(&student);//初始化链表;
for (int z = 0; z < valid_line_num; z++)
{
AddItem(&student, &stu[z]);
}
printf("show the information of file1: \n");
//Showstudent(temp, &student);
calculate_total_grade(&student);
//打开第二个文件
fp2 = fopen(argv[2], "rb");
if (NULL == fp2)
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
fseek(fp2, 0L, SEEK_END);
int size2 = ftell(fp2);
file2_buf = (char*)malloc(size2 + 1);
memset(file2_buf, 0, size2 + 1);
line2_buf = (char*)malloc(size2 + 1);
memset(line2_buf, 0, size2 + 1);
file2name = argv[2];
fp2 = fopen(file2name, "rb");
if (NULL == fp2)
{
fprintf(stderr, "Can't open %s\n", file2name);
exit(EXIT_FAILURE);
}
//计算行数
while (EOF != (ch2 = getc(fp2)))
{
if ('\n' == ch2)
{
++rows2;
}
}
rewind(fp2);
//计算第一行的字符数
while ('\n' != (ch2 = getc(fp2)))
{
++first_line_len2;
}
//让句柄偏移到第二行
fseek(fp2, first_line_len2 + 1, SEEK_SET);
int index2 = 0;
int valid_line_num2 = 0;//读取的行数
while (line2 < rows2)
{
read_line(fp2, line2_buf, size2); //读出每一行
valid_line_num2 += parse_line(line2_buf, &(stu2[index2]));//把每一行写入结构体
++line2;
++index2;
}
InitializeList(&student2);//初始化链表;
for (int z = 0; z < valid_line_num2; z++)
{
AddItem(&student, &stu2[z]);
}
printf("show the information of file2: \n");
//Showstudent(temp2, &student);
calculate_total_grade( &student);//合并后计算总分
printf("print combine file: \n");
//Showstudent(temp, &student);
Sort(&student);//排序
printf("sort from high to low:\n");
Showstudent(temp, &student);
//单链表逆序打印
Node* NewList = PrintReverse(&student);
printf("sort from low to high:\n");
Showstudent(temp, &NewList);
//双向链表逆序打印
//创建一个双向链表
student = NULL;
DListInitializeList(&student);
for (int z = 0; z < valid_line_num; z++)
{
DListAddItem(&student, &stu[z]);
}
printf("Dlist:\n");
DShowstudent(temp, &student);
for (int z = 0; z < valid_line_num2; z++)
{
DListAddItem(&student, &stu2[z]);
}
printf("Dlist combine:\n");
DShowstudent(temp, &student);
Dcalculate_total_grade(temp, &student);//合并后计算总分
//排序
DSort(&student);
printf("sort from high to low:\n");
DShowstudent(temp, &student);
//逆序打印
DPrintReverse(temp, &student);
printf("Dsort from low to high:\n");
Showstudent(temp, &student);
int DelineGrade = 0;
printf("Input the lowest number:");
scanf("%d", &DelineGrade);
EraseStudent(&student, DelineGrade);
printf("more than %d:\n",DelineGrade);
Showstudent(temp, &student);
WriteF(temp, &student);
ListDestroy(&student2);
ListDestroy(&student);
fclose(fp);
fclose(fp2);
free(file1_buf);
file1_buf = NULL;
free(file2_buf);
file2_buf = NULL;
free(line_buf);
line_buf = NULL;
free(line2_buf);
line2_buf = NULL;
system("pause");
return 0;
}//输入一个文件名或两个文件名(成绩表),解析内部学生成绩,然后计算总分,
//如果两个文件名