csdn.txt中的数据是这样的:
1.main.c
#include "list.h"
int isoktosscanf(char *str) { char *p = strstr(str, "#"); if (p!=NULL) { if (strstr(p+1,"#")!=NULL) { return 1; } else { return 0; } } else { return 0; } } void eatspace(char *str) { int i = 0; int j = 0; while ((str[i]=str[j++])!='\0')//双指针错位 { if (str[i]!=' ') { i++; } } } void fileload() { FILE *pf = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\csdn.txt", "r"); while (!feof(pf)) { char str[100] = { 0 }; char password[100] = { 0 }; fgets(str, 100, pf); char*p1 = strstr(str, "#"); char*p2 = strstr(p1+1, "#"); *p1 = '\0'; *p2 = '\0'; strcpy(password, p1 + 1);//拷贝字符串 eatspace(password); if (isin(phead, password) == 0) { phead = addback(phead, password); } } fclose(pf); sortbyci(phead); writetofile(phead, "C:\\ci.txt"); sortbypass(phead); writetofile(phead, "C:\\pass.txt"); } void main() { fileload(); system("pause"); } void main1() { PINFO phead = NULL; char *str[10] = { "123", "1234", "456", "789", "2345", "123", "456", "789", "1234", "123" }; for (int i = 0; i < 10;i++) { if (isin(phead,str[i])==0) { phead=addback(phead, str[i]); } } show(phead); sortbyci(phead); printf("\n\n"); show(phead); sortbypass(phead); printf("\n\n"); show(phead); writetofile(phead, "C:\\x.txt"); system("C:\\x.txt"); system("pause"); }
2.list.h
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h>
#include <Windows.h> PINFO phead = NULL;
typedef struct passinfo { char password[20]; int ci; struct passinfo *pNext; }INFO ,*PINFO; PINFO addback(PINFO phead, char password[20]);//尾部插入 PINFO sortbyci(PINFO phead);///排序 PINFO sortbypass(PINFO phead); PINFO show(PINFO phead);//显示 int isin(PINFO phead, char password[20]);//判断存在与否 void writetofile(PINFO phead,char path[100]);//写入到文件
3.list.c
#include "list.h" PINFO addback(PINFO phead, char password[20])//尾部插入 { PINFO pnew = calloc(1, sizeof(INFO));//开辟内存 strcpy(pnew->password, password);//拷贝密码 pnew->ci = 1; pnew->pNext = NULL; if (phead ==NULL) { phead = pnew;//直接插入 } else { pnew->pNext = phead; phead = pnew;//头插 } return phead; } PINFO sortbyci(PINFO phead)///排序 { for (PINFO p1 = phead; p1 != NULL;p1=p1->pNext) { for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext) { if (p2->pNext!=NULL) { if (p2->ci < p2->pNext->ci) { int citemp = p2->ci;//交换数据 p2->ci = p2->pNext->ci; p2->pNext->ci = citemp; char passtemp[100]; strcpy(passtemp, p2->password); strcpy(p2->password, p2->pNext->password); strcpy(p2->pNext->password, passtemp); } } } } } PINFO sortbypass(PINFO phead) { for (PINFO p1 = phead; p1 != NULL; p1 = p1->pNext) { for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext) { if (p2->pNext != NULL) { if (strcmp(p2->password, p2->pNext->password) < 0) { int citemp = p2->ci;//交换数据 p2->ci = p2->pNext->ci; p2->pNext->ci = citemp; char passtemp[100]; strcpy(passtemp, p2->password); strcpy(p2->password, p2->pNext->password); strcpy(p2->pNext->password, passtemp); } } } } } PINFO show(PINFO phead)//显示 { if (phead==NULL) { return; } else { printf("%s,%d\n", phead->password, phead->ci); show(phead->pNext); } return phead; } int isin(PINFO phead, char password[20])//判断存在与否 { PINFO p = phead; while (p!=NULL) { if (strcmp(p->password,password)==0) { p->ci += 1; return 1;//存在 } p = p->pNext; } return 0;//不在链表内部 } void writetofile(PINFO phead, char path[100])//写入到文件 { FILE *pf = fopen(path, "w"); PINFO p = phead; while (p != NULL) { fprintf(pf, "%s %d\n", p->password, p->ci);//打印到文件 p = p->pNext; } fclose(pf); }