65,212
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct node
{
struct node *next;
char name[10];
int grade;
}node;
node *sort_list(node *head)
{
node *pre = head;
node *cur = head;
if (NULL == head || NULL == head->next)
{
return head;
}
cur = cur->next;
while (cur != NULL)
{
if (cur->grade > pre->grade)
{
pre->next = cur->next;
if (cur->grade >= head->grade)
{
cur->next = head;
head = cur;
}
else
{
node *p;
for (p = head; p != pre; p = p->next)
{
if (p->next->grade <= cur->grade)
{
cur->next = p->next;
p->next = cur;
break;
}
}
}
cur = pre->next;
}
else
{
pre = cur;
cur = cur->next;
}
}
return head;
}
void free_list(node *head)
{
while (head != NULL)
{
node *tmp = head->next;
free(head);
head = tmp;
}
}
int main()
{
FILE *fp;
node *head = NULL, *cur = NULL, *tmp = NULL;
if (NULL == (fp = fopen("in.txt", "r")))
{
printf("file open error!");
return -1;
}
while (1)
{
tmp = malloc(sizeof(node));
tmp->next = NULL;
if (2 == fscanf(fp, "%s%d", &tmp->name, &tmp->grade))
{
if (NULL == head)
{
head = tmp;
cur = tmp;
}
else
{
cur->next = tmp;
cur = tmp;
}
}
else
{
free(tmp);
break;
}
}
fclose(fp);
head = sort_list(head);
do
{
if (NULL == (fp = fopen("out.txt", "w")))
{
printf("file open error!");
break;
}
for (cur = head; cur != NULL; cur = cur->next)
{
fprintf(fp, "%s %d\n", cur->name, cur->grade);
}
} while (0);
free_list(head);
if (fp != NULL)
{
fclose(fp);
}
system("pause");
return 0;
}
#include <Windows.h>
#include <stdio.h>
#include <string.h>
typedef struct _tagGradeInfo
{
char strName[10];//名字
int nGrade;//成绩
}GRADEINFO , *LPGRADEINFO;
enum
{
FILE_ERROR,
FILE_SUCCESS,
};
const char *strFile1 = "in.txt";
const char *strFile2 = "out.txt";
char strPath_in[260] ={0};
char strPath_out[260] ={0};
int fsort( const char*fin, const char * fout);
int main()
{
//先获取当前项目debug下面的in.txt和out.txt文本文件
char *pstr = NULL;
GetModuleFileNameA(NULL , strPath_in , 260);
pstr=strrchr(strPath_in , '\\');//从字符串的右边开始找
*pstr = '\0';//截断
strcat(strPath_in , "\\in.txt");
pstr = NULL;
GetModuleFileNameA(NULL , strPath_out , 260);
pstr=strrchr(strPath_out , '\\');//从字符串的右边开始找
*pstr = '\0';//截断
strcat(strPath_out , "\\out.txt");
//成绩排序整理
int nRes = fsort(strPath_in , strPath_out);
if(nRes > 0)
{
printf("The programa is over!");
}
getchar();
return 0;
}
int fsort(const char *fin, const char *fout)
{
FILE *pRead = NULL ,*pWrite = NULL;
int nCount ;
GRADEINFO pGradeInfo[100];//先定死 100个学生
char buff[128] = {0} ;
char stoken[2] =" ";//空格分割符标记
char *p = NULL ;//保存分割后的内容
pRead=fopen(fin , "r"); //读
pWrite = fopen(fout , "w");//写
nCount = 0;//记录学生数
memset(pGradeInfo , 0 , sizeof(pGradeInfo));//清空结构体数据
while(!feof(pRead))//如果文件没有结束
{
if(fgets(buff ,128 , pRead) == NULL)//读第一行数据
{
printf("read error !\n");
return FILE_ERROR ;
}
p = strtok(buff , stoken);//第一次分割出来姓名
strcpy(pGradeInfo[nCount].strName , p);
p = strtok(NULL , stoken);//再分一次分离成绩
pGradeInfo[nCount].nGrade = atoi(p);
nCount++ ;
}
printf("读出in.txt文件成功:\n\n");
for(int i = 0 ; i < nCount ; i++)//输出
{
printf("Name: %s Grade: %d\n\n" , pGradeInfo[i].strName , pGradeInfo[i].nGrade);
}
//冒泡排序
for(int i= 0 ; i < 10 ; i++)
{
for(int j = i + 1 ; j < 10 ;j++)
{
if(pGradeInfo[i].nGrade < pGradeInfo[j].nGrade)
{
GRADEINFO pData ;//临时结构体变量
pData = pGradeInfo[i];//保存下成绩低的结构体
pGradeInfo[i] = pGradeInfo[j];//小的自动往后走,大的往前走
pGradeInfo[j] = pData;
}
}
}
printf("\t------------------------------------------------\t\n\n");
for(int i = 0 ; i < nCount ; i++)//输出
{
printf("Name: %s Grade: %d\n\n" , pGradeInfo[i].strName , pGradeInfo[i].nGrade);
}
//将排序完的数据写入out.txt文件
int i = 0;
while(i < nCount)
{
if(pWrite)
{
char buff[3] = {0} ;
fputs(pGradeInfo[i].strName , pWrite);//写入名字
fputs(" " ,pWrite);//空格隔开
itoa(pGradeInfo[i].nGrade ,buff ,10);//int to char*
fputs(buff , pWrite);//写入成绩
fputs("\r\n" , pWrite);//写入换行符
}
i++ ;
}
printf("写入out.txt文件成功!\n\n");
return FILE_SUCCESS;
}