#include<stdio.h>
#include<string.h>
#include<malloc.h>
#pragma pack(1)
struct Node
{
int no; //编号
char name[13]; //姓名,长度最大不超过12个字符
int luck_no; //幸运数字
char date[11]; //出生日期,格式是形如“yyyy-mm-dd”的字符串
struct Node *next;
};
void main ()
{
int input=0;
char data[1000];//存储整个文件的数据。
long tag[18]; //存储换行符的位置,依据此得到各条信息。
int j=0;
int numOfLF=0;//存储的是稳健的换行符,分居换行符得到具体行数。
long fileLength;//获取文件大小。
FILE * streamReader ;//文件指针。
long label[100];//存储的是空格符号,且空格符前面有数据,即前一个字符不是空格符。
int p=1;//表示数组数组索引。
int numOfNode=0; //存储整个文件数据的条数,也就是人数,节点数。
int numOfSpace;//存储文件中的空格数目。
Node * head;//在链表中移动
Node*open;//链表的头指针。
int position=0;//表示空格所在位置
int posiRead;//姓名部分有效数,后面是空格。即使“tom ”,长度是13个。
if ((streamReader = fopen("E:\\data.txt", "r")) == NULL)
{
printf("打开文件失败!");
return;
}
fseek(streamReader,0L,SEEK_END);//将文件指针放在文件末尾
fileLength=ftell(streamReader);//获取文件的长度,即指针位置
rewind(streamReader);//文件指针指向文件头
fread(data,fileLength,1,streamReader);//读取数据到data字符数组中
rewind(streamReader);
//该循环获取文件的换行符位置
for(int i=0;i<fileLength;i++)
{
if(data[i]==(char)10)
{
tag[j]=i;
j++;
}
}
numOfLF=j;
j=0;
//获取的是空格符号。
for(long n=1;n<fileLength;n++)
{
if((data[n]==(char)32)&&(data[n-1]!=char(32)))
{
label[j]=n;
j++;
}
}
numOfSpace=j;
open=(Node *)malloc(44);//动态分配第一个头节点。
head=open;
//构造链表
for(int t=0;t<=numOfLF;t++)
{
if(t>0)
p=tag[t-1]+2;//p只是的是每条数据在data数组中的其实位置。是根据回车符“\n”标记。
head->no=t+1;//no数据采用的是逐步增加,并为按照文件中编号读取。
if(t>=9)
p+=2;//当大于等于9是,编号是2个字符,需要向后移动两个字符,否则是1个。
else
{
++p;
}
for(int i=0;i<(label[position+1]-label[position]-1);i++,p++)
{
head->name[i]=data[p];//读取姓名部分。
posiRead=i;
}
for(int j=1;j<=13-(label[position+1]-label[position]-2);j++)
{
head->name[posiRead+j]=(char)32;//姓名全部整合成13个字符,后面是空格。ASCII码32.
}
head->name[12]=(char)0;//字符数组最后一位是空。
head->luck_no=(data[++p]-48);//化字符为数字
p=p+2;
for(int k=0;k<10;k++)
{
head->date[k]=data[p];//读取出生日期部分。
p++;
}
head->date[10]=(char)0;
position+=4;//用于规定读取姓名的位置。
if(t==numOfLF)
{
head->next=NULL;//链表末尾是空。
}
else
{
head->next=(Node*)malloc(36);//构造新的节点。
head=head->next; //节点移动。
}
}
head=open;
puts("................幸运数字.............");
puts(" 1...分析 2...统计");
puts(" 3...打印 4...添加");
puts(" 5...删除 6...修改");
puts(" 7...查询 0...退出");
while(1)
{
printf("请输入一个数字进行选择:");
scanf("%d",&input);
switch(input)
{
case 1:
{
int bornDate=0;//存储出生年日。
head=open;
while(head!=NULL)
{
bornDate=(head->date[8]-48)*10+head->date[9]-48;
if(bornDate==head->luck_no)
printf(" %d %s%d %s\n",head->no,head->name,head->luck_no,head->date);
head=head->next;
}
break;
}
case 2:
{
int num[9]={0,0,0,0,0,0,0,0,0};
int total=0;
head=open;
while(head!=NULL)
{
switch (head->luck_no)
{
case 1:
num[0]+=1;break;
case 2:
num[1]+=1;break;
case 3:
num[2]+=1;break;
case 4:
num[3]+=1;break;
case 5:
num[4]+=1;break;
case 6:
num[5]+=1;break;
case 7:
num[6]+=1;break;
case 8:
num[7]+=1;break;
case 9:
num[8]+=1;break;
default:
puts("产生错误!");
}
head=head->next;
}
for(int i=1;i<=9;i++)
{
printf(" 拥有幸运数字%d的人数是%d\n",i,num[i-1]);
}
for(int i=0;i<9;i++)
{
total+=num[i];
}
printf(" 统计人数是:%d\n",total);
break;
}
case 3:
head=open;
char nameInterest[13];
printf("请输入你感兴趣人的姓名:");
scanf("%s",nameInterest);
for(int i=0;i<12;i++)
{
if(!((nameInterest[i]>=65&&nameInterest[i]<=90)||(nameInterest[i]>=97&&nameInterest[i]<=122)))
nameInterest[i]=char(32);
}
nameInterest[12]=(char)0;
while(head!=NULL) //while循环遍历链表。
{
if(strcmp(head->name,nameInterest)==0)
{
printf(" %d %s%d %s\n",head->no,head->name,head->luck_no,head->date);
break;
}
head=head->next;
}
break;
case 4:
{
//类似于读取文件数据,关键在于判断各项信息在字符数组中位置。
char newPerson[36];
int posSpace[10];
j=0;
int k;
int m=0;
head=open;
while(head->next!=NULL)
{
head=head->next;
}
head->next=(Node*)malloc(36);
head=head->next;
head->next=NULL;
printf("请输入各项信息:编号 姓名 幸运数字 出生年月(1920-03-10),\n");
getchar();
gets(newPerson);
for(int i=0;i<36;i++)
{
if(newPerson[i]==(char)32||newPerson[i]==(char)0)
{
posSpace[j]=i;
j++;
}
}
if(posSpace[0]==1)
{
head->no=(newPerson[0]-48);
m=2;
}
else
{
head->no=(newPerson[0]-48)*10+newPerson[1]-48;
m=3;
}
for( k=0;k<(posSpace[1]-posSpace[0]-1);k++)
{
head->name[k]=newPerson[m+k];
}
for(int p=0;p<12-(posSpace[1]-posSpace[0]-1);p++)
{
head->name[k+p]=(char)32;
}
head->name[12]=(char)0;
head->luck_no=newPerson[posSpace[1]+1]-48;
strncpy(head->date,&newPerson[posSpace[2]+1],11);
puts("增加节点成功!");
break;
}
case 5:
{
Node* pre=open;
head=open;
int noNotInterest=0;
printf("请输入你不感兴趣人的编号:");
scanf("%d",&noNotInterest);
while(head!=NULL)
{
if(noNotInterest==open->no)
{
open=open->next;
free(head);
break;
}
else if(head->no==noNotInterest)
{
pre->next=head->next;
free(head);
break;
}
pre=head;
head=head->next;
}
break;
}
case 6:
{
head=open;
int numModify=0;
int newLuckNo=0;
printf("输入某个需要修改数字的人的编号:");
scanf("%d",&numModify);
while(head!=NULL)
{
if(head->no==numModify)
{
printf(" %d %s%d %s\n",head->no,head->name,head->luck_no,head->date);
printf("请输入新的幸运数字:");
scanf("%d",&newLuckNo);
head->luck_no=newLuckNo;
break;
}
head=head->next;
}
break;
}
case 7:
{ int numQuery;
printf("请输入查询的幸运数字:");
scanf("%d",&numQuery);
head=open;
while(head!=NU
C语言版的位图彩色变黑白
4星 · 超过85%的资源 需积分: 10 161 浏览量
2010-12-29
21:33:28
上传
评论 1
收藏 4KB RAR 举报
xiaoyou66
- 粉丝: 1
- 资源: 2
最新资源
- 基于C++实现二叉树的创建,遍历,添加,查找与删除
- 基于C语言实现二叉树的基本操作
- 毕业设计基于STM32的测量温度与压力的数据处理设计C语言完整源码+论文.zip
- 基于MATLAB的PCA算法人脸识别项目源码+GUI界面+说明文档.zip
- 基于STM32的测量温度与压力的数据处理设计源码+论文(毕业设计).zip
- Vision Transformer 网络对不同氨气氧气浓度轨迹RAS 图像数据集的分类,包含训练权重和数据集、迁移学习
- 基于C51带字库LCD12864(ST7920)的keil工程源码,只支持8位并口通讯(不支持串口),可显示中文.zip
- 基于SI4463射频模块433MD-SMA无线模块软硬件技术资料及(SI4463)IC技术资料文档.zip
- (GPS+北斗+GSM)HLK-GS2503模块软硬件开发资料包硬件参考设计(原理图PCB)+技术文档资料.zip
- 基于BERT+Biaffine结构的关系抽取模型源码+文档说明.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈