📄 outfile.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "OutFile.h"
#include <windows.h>
void Draw(HDC hdc,int x,int y,int with,int high,COLORREF pencolor,COLORREF bgcolor)
{
HPEN hPen;
HBRUSH hBrush;
hPen = CreatePen(PS_SOLID,2,pencolor);
hBrush = CreateSolidBrush(bgcolor);
SelectObject(hdc,hBrush);
SelectObject(hdc,hPen);
Rectangle(hdc,x,y,x+with,y+high);
DeleteObject(hBrush);
DeleteObject(hPen);
}
//将学生信息保存到节点中
stuNode * addstu(stuNode *pHead,stuInfo *stuAddInfo)
{
stuNode *pNew;
pNew=(stuNode *)malloc(sizeof(stuNode));
if(pNew!=NULL)
{
pNew->stu=stuAddInfo;
pHead=Insert(pHead,pNew);
}
else
{
printf("分配空间失败!");
return(NULL);
}
return(pHead);
}
//向链表中加入新学生节点
stuNode * Insert(stuNode *pHead,stuNode *pNew)
{
stuNode *pCur,*pPre;//pCur,pPre 分别是当前结点和上一个结点
pCur=pPre=pHead;
if(pHead==NULL||pNew->stu->stuNo <=pHead->stu->stuNo )
{//新节点作为头结点情况
pNew->next=pHead;
pHead=pNew;
return (pHead);
}
while(pCur!=NULL&&pCur->stu->stuNo <=pNew->stu->stuNo )
{
pPre=pCur;
pCur=pCur->next;
}
//将pNew插入在pPre和pCur之间
pNew->next=pCur;
pPre->next=pNew;
return (pHead);
}
//输出链表
void Output(HDC hdc,stuNode *pCur)
{
stuNode *pHead;
int i;
int n=0;
char *Title[]={"学号","姓名","性别","英语","C语言","C++","SDK","总分","所属小组"},b[10],d[100];
//printf("当前文件中存储的学生信息如下:\n");
SetBkMode(hdc,TRANSPARENT);
for(i=0;i<9;i++)
{
if(i==8)
Draw(hdc,i*70,1,200,20,RGB(255,255,50),RGB(50,255,155));
else
Draw(hdc,i*70,1,70,20,RGB(255,255,50),RGB(50,255,155));
TextOut(hdc,i*70+10,2,Title[i],strlen(Title[i]));//输出表列标题栏的文字
}
pHead=pCur;
while(pHead!=NULL)//统计学生人数
{
n++;
pHead=pHead->next;
}
free(pHead);
for(i=1;i<=n;i++)//链表没有结束
{
_itoa(pCur->stu->stuNo,b,10);//将链表中学号转换成字符串
Draw(hdc,0,i*20+1,70,20,RGB(50,255,150),RGB(255,0,0));
TextOut(hdc,10,i*20+2,b,strlen(b));
Draw(hdc,1*70,i*20+1,70,20,RGB(149,153,150),RGB(255,255,255));
TextOut(hdc,1*70+10,i*20+2,pCur->stu->stuName,strlen(pCur->stu->stuName));
Draw(hdc,2*70,i*20+1,70,20,RGB(149,153,150),RGB(255,255,255));
if(pCur->stu->stuSex=='f')
TextOut(hdc,2*70+10,i*20+2,"女",strlen("女"));
else
TextOut(hdc,2*70+10,i*20+2,"男",strlen("男"));
Draw(hdc,3*70,i*20+1,70,20,RGB(149,153,150),RGB(255,255,255));
sprintf(d,"%4.1f",pCur->stu->stuScore.gw);
TextOut(hdc,3*70+10,i*20+2,d,strlen(d));
Draw(hdc,4*70,i*20+1,70,20,RGB(149,153,150),RGB(255,255,255));
sprintf(d,"%4.1f",pCur->stu->stuScore.sj);
TextOut(hdc,4*70+10,i*20+2,d,strlen(d));
Draw(hdc,5*70,i*20+1,70,20,RGB(149,153,150),RGB(255,255,255));
sprintf(d,"%4.1f",pCur->stu->stuScore.rx);
TextOut(hdc,5*70+10,i*20+2,d,strlen(d));
Draw(hdc,6*70,i*20+1,70,20,RGB(149,153,150),RGB(255,255,255));
sprintf(d,"%4.1f",pCur->stu->stuScore.hhx);
TextOut(hdc,6*70+10,i*20+2,d,strlen(d));
Draw(hdc,7*70,i*20+1,70,20,RGB(149,153,150),RGB(255,255,255));
sprintf(d,"%4.1f",pCur->stu->stuScore.hhx+pCur->stu->stuScore.gw+pCur->stu->stuScore.rx+pCur->stu->stuScore.sj);
TextOut(hdc,7*70+10,i*20+2,d,strlen(d));
Draw(hdc,8*70,i*20+1,200,20,RGB(149,153,150),RGB(255,255,255));
TextOut(hdc,8*70+10,i*20+2,pCur->stu->stuAddress,strlen(pCur->stu->stuAddress));
pCur=pCur->next;
}
sprintf(d,"文件中共有%d条记录信息",n);
TextOut(hdc,590,i*20+2+15,d,strlen(d));
}
stuNode * SearchByNo(stuNode *pHead,int stuNo)
{
stuNode *pCur,*pStu=NULL;
int n=0;
pCur=pHead;
while(pCur!=NULL)
{
if(pCur->stu->stuNo==stuNo)
{
n=n+1;
pStu=pCur;
}
pCur=pCur->next ;
}
return pStu;
}
stuNode * SearchByName(stuNode *pHead,char stuName[30])
{
stuNode *pCur;
stuInfo *pStu=NULL;
int n=0;
pCur=pHead;
pHead=NULL;
while(pCur!=NULL)
{
if(strcmp(pCur->stu->stuName,stuName)==0)
{
pStu=pCur->stu;
pHead=addstu(pHead,pStu);
}
pCur=pCur->next ;
}
return pHead;
}
stuNode * SearchBySex(stuNode *pHead,char stuSex)
{
stuNode *pCur;
stuInfo *pStu=NULL;
int n=0;
pCur=pHead;
pHead=NULL;
while(pCur!=NULL)
{
if(pCur->stu->stuSex==stuSex)
{
pStu=pCur->stu;
pHead=addstu(pHead,pStu);
}
pCur=pCur->next ;
}
return pHead;
}
/*追加保存链表的结点信息*/
void Save(stuNode *pHead)
{
FILE *fp;
stuNode *pCur=pHead;
if((fp=fopen("data_list.txt","ab"))==NULL)//追加写入学生信息 ab为读写
{
printf("文件打开失败!\n");
return;
}
while(pCur!=NULL)//链表没有结束
{
fwrite(pCur->stu,sizeof(stuInfo),1,fp);
pCur=pCur->next;
}
fclose(fp);
}
//删除节点后覆盖保存数据
void SaveTwo(stuNode *pHead)
{
FILE *fp;
stuNode *pCur=pHead;
if((fp=fopen("data_list.txt","wb"))==NULL)
{
printf("文件打开失败!\n");
return;
}
while(pCur!=NULL)
{
fwrite(pCur->stu,sizeof(stuInfo),1,fp);
pCur=pCur->next;
}
fclose(fp);
}
//从文件中读取数据
stuNode * Read()
{
FILE *fp;
stuNode *pHead=NULL,*pNew=NULL;
stuInfo *pStu=NULL;
if((fp=fopen("data_list.txt","rb"))==NULL)
{
return NULL;
}
while(!feof(fp))//feof为检查文件的状态,若不到文件末尾则:
{
pStu=(stuInfo *)malloc(sizeof(stuInfo));
pNew=(stuNode *)malloc(sizeof(stuNode));
if(pStu==NULL)
{
printf("内存分配失败!");
}
if(fread(pStu,sizeof(stuInfo),1,fp)==1)
{
pNew->stu=pStu;
pHead=Insert(pHead,pNew);
}
else
{
free(pStu);
break;
}
}
fclose(fp);
return pHead;
}
//删除学生信息
stuNode * DelStu(stuNode *pHead,int stuNo)
{
stuNode *pCur,*pPre;
pCur=pPre=pHead;
while(pCur!=NULL&&pCur->stu->stuNo!=stuNo)//链表没有结束并且查找不成功
{
pPre=pCur;
pCur=pCur->next;
}
if(pCur!=NULL)//查找成功,pCur是指针删除的结点,pPre用来指向pCur的上一个结点
{
printf("查找成功!删除学号为(%d)的学生信息!\n ",stuNo);
if(pCur==pHead)
pHead=pCur->next;//删除链表表头的结点
else
pPre->next=pCur->next;//删除中间结点
free(pCur);
}
else
printf("查找不成功!\n");
return (pHead);
}
stuNode * stuModify(stuNode *pHead,stuInfo *stu)
{
stuNode *NewHead=NULL;
NewHead=DelStu(pHead,stu->stuNo);
NewHead=addstu(NewHead,stu);
return NewHead;
}
void Release(stuNode *pHead)
{
stuNode *pCur=pHead;
while(pCur!=NULL)//链表没有结束
{
pHead=pCur->next;//头指针下移一个结点
free(pCur);
pCur=pHead;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -