?? 新建 文本文檔.txt
字號:
#include <iostream.h> // cin 及 cout
#include <malloc.h> // 用到申請內存函數 malloc() 和釋放內存函數 free()
#include <string.h> // 字符串處理
#include <stdio.h> // 文件操作(讀文件)
#include <stdlib.h>
#define Y 1
#define N 0
struct worker
{
char name[20];
char sex[10];
int num;
char birthday[100];
char partment[40];
char workyear[10];
char E-mail[50];
char telphone[10];
char graduate[20];
char address[40];
char duty[20];
char in_time[20];
};
typedef struct linknode
{
struct worker someone;
bool flag;
struct linknode* next;
}nodetype;
struct file
{
int Dat;
char num[6];
char date_t[20];
char depart[20];
char duty[20];
char notice[50];
int leave_er;
int come_la;
int sick_le;
int affair_le;
int over_time;
int out;
int holiday;
}File_one;
typedef struct LNode
{
struct File_one data;
struct LNode *next;
}LNode, *pLinkList;
class LinkList {
private:
pLinkList m_pList;
int m_listLength;
public:
LinkList();
~LinkList();
bool InitList ();
bool DestroyList ();
bool ClearList();
bool IsEmpty ();
int GetLength ();
bool GetNode(int position, LNode** node);
int LocateElem(int elem);
bool SetNodeData(int position, int newData);
bool GetNodeData(int position, int &data);
bool InsertNode(int beforeWhich);
bool Insert_Node(int beforeWhich, int data);
bool DeleteNode(int position);
boos load_file( )
int leaveer(int, int);
int comela();
int sickle();
int affairle();
int overtime();
int Out();
int holiday();
};
class List
{
nodetype* head;
public:
List();
List::~List();
linknode* creatlist(int); //創建鏈表
int listlen(); //返回鏈表長度
nodetype* findnode(int); //通過查找序號返回節點的指針
nodetype* find(char c[]); //通過查找姓名返回節點的指針
int find2(char c[]); //通過查找姓名返回節點的序號
nodetype* insnode(int); //插入節點
void delnode(int); //刪除節點
nodetype* load(); //初始化:從外部讀入數據
void readstr(FILE *f,char *string); //讀行函數
bool check(char *a, char *b); //對比兩個字符串是否相等
void help(); //顯示幫助菜單
void editworker(nodetype*); //編輯個人說明
void dispname(); //顯示所有姓名
void dispnode(nodetype* p); //顯示一個職工的所有信息
void dispworker(nodetype*); //顯示一個職工的個人說明
};
Cpp文件:LinkList.cpp
----------------------------------------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include "time.h"
#include "list.h"
List::List()
{
head = NULL;
}
bool List::check(char *a, char *b) //對比兩個字符串是否相等
{
int i;
int j=strlen(b);
for(i=0; i<j; i++)
{
if(*a==*b)
{
a++;
b++;
}
else
return 0;
}
return 1;
}
nodetype* List::creatlist (int n) //創建鏈表
{
nodetype *h=NULL, *s, *t;
int i=1;
for(int j=0; j<n; j++)
{
if(i==1) //創建第一個節點
{
h=(nodetype*)malloc(sizeof(nodetype));
h->next=NULL;
t=h;
}
else //創建其余節點
{
s=(nodetype*)malloc(sizeof(nodetype));
s->next=NULL;
t->next=s;
t=s; //t 始終指向生成的單鏈表的最后一個節點
}
i++;
}
head=h;
return h;
}
/*
nodetype* List::creatlist (int n) //創建鏈表
{
nodetype *h=NULL;
int i=1;
for(int j=0; j<n; j++)
{
h=insnode(0);
}
head=h;
return h;
}
*/
void List::readstr(FILE *f,char *string)
{
do
{
//①: 先讀入一行文本
fgets(string, 255, f); //fgets(): 從文件 f 讀入長度為 255-1 的字符串
// 并存入到 string 中
} while ((string[0] == '/') || (string[0] == '\n'));
return;
}
nodetype* List::load()
{
FILE *fp;
nodetype *p;
char c[255];
int num;
if((fp=fopen("student.txt", "r"))==NULL)
{
cout<<"打開文件失敗"<<endl;
return 0;
}
readstr(fp, c);
sscanf(c, "The Length Of Link: %d", &num); //獲取鏈表長度
p=creatlist(num); //創建鏈表
for(int i=0; i<num; i++)
{
readstr(fp, c);
strcpy(p->someone.name, c);
readstr(fp, c);
strcpy(p->someone.sex, c);
readstr(fp, c);
strcpy(p->someone.num, c);
readstr(fp, c);
strcpy(p->someone.birthday, c);
readstr(fp, c);
strcpy(p->someone.partment, c);
readstr(fp, c);
strcpy(p->someone.workyear, c);
readstr(fp, c);
strcpy(p->someone.E-mail, c);
readstr(fp, c);
strcpy(p->someone.telphone, c);
readstr(fp, c);
strcpy(p->someone.graduate, c);
readstr(fp, c);
strcpy(p->someone.address, c);
readstr(fp, c);
strcpy(p->someone.duty, c);
readstr(fp, c);
strcpy(p->someone.notice, c);
readstr(fp, c);
strcpy(p->someone.in_time, c);
p=p->next;
}
fclose(fp);
return p;
}
void List::dispnode(nodetype* p) //顯示一個職工的所有信息
{
if(p!=NULL)
{
dispworker(p);
}
}
void List::dispname() //顯示所有職工姓名
{
nodetype* p=head;
cout<<"現有的職工: "<<endl;
if(p==NULL)
cout<<"沒有任何職工數據"<<endl;
while(p!=NULL)
{
cout<<"姓名: "<<p->someone.name;
p=p->next;
}
}
int List::listlen() //返回鏈表長度
{
int i=0;
nodetype* p=head;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}
nodetype* List::findnode (int i) //通過查找序號返回節點的指針
{
nodetype* p=head;
int j=1;
if( i>listlen()||i<=0 ) // i 上溢或下溢
return NULL;
else
{
while( p!=NULL && j<i ) //查找第 i 個節點并由 p 指向該節點
{
j++;
p=p->next;
}
return p;
}
}
nodetype* List::find(char c[]) //通過查找姓名返回節點的指針
{
nodetype* p=head;
int j=1;
strcat(c, "\n"); //從外部讀入的字符串末尾都帶了一個換行符
while( p!=NULL && !(check(c, p->someone.name))) //查找第 i 個節點并由 p 指向該節點
{
j++;
p=p->next;
}
return p;
}
int List::find2(char c[]) //通過查找姓名返回節點的序號
{
nodetype* p=head;
int j=1;
strcat(c, "\n"); //從外部讀入的字符串末尾都帶了一個換行符
while( p!=NULL && !(check(c, p->pe.name))) //查找第 i 個節點并由 p 指向該節點
{
j++;
p=p->next;
}
return j;
}
nodetype* List::insnode(int i)
{
nodetype *h=head, *p, *s;
s=(nodetype*)malloc(sizeof(nodetype)); //創建節點 s
s->next=NULL;
if(i==0) //i=0 時 s 作為該單鏈表的第一個節點
{
s->next = h;
h=s; //重新定義頭節點
}
else
{
p=findnode(i); //查找第 i 個節點,并由 p 指向該節點
if(p!=NULL)
{
s->next=p->next;
p->next=s;
}
else cout<<"輸入的 i 值不正確"<<endl;
}
head=h;
return s;
}
void List::delnode(int i) //刪除第 i 個節點
{
nodetype *h=head, *p=head, *s;
int j=1;
if(i==1) //刪除第一個節點
{
h=h->next;
free(p);
}
else
{
p=findnode(i-1); //查找第 i-1 個節點,并由 p 指向這個節點
if(p!=NULL && p->next!=NULL)
{
s=p->next; // s 指向要刪除的節點
p->next=s->next;
free(s);
}
else
cout<<"輸入的 i 值不正確"<<endl;
}
head=h;
}
void List::editworker(nodetype* p)
{
char c[100];
cout<<"請輸入姓名: "<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.name, c);
cout<<"請輸入性別:"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.sex, c);
cout<<"請輸入編號:"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.num, c);
cout<<"請輸入生日(格式舉例:1982-1-1): "<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.birthday, c);
cout<<"請輸入部門:"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.partment, c);
cout<<"請輸入工作年限:"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.workyear, c);
cout<<"請輸入E-mail"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.E-mail, c);
cout<<"請輸入電話"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.telphone, c);
cout<<"請輸入學歷"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.graduate, c);
cout<<"請輸入地址"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.address, c);
cout<<"請輸入職務"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.duty, c);
cout<<"請輸入進入時間"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->someone.in_time, c);
cout<<"編輯個人信息完成!"<<endl;
dispworker(p);
}
void List::dispworker(nodetype* p)
{
cout<<"姓名: "<<p->someone.name;
cout<<"性別: "<<p->someone.sex;
cout<<"電話: "<<p->someone.telphone;
cout<<"進公司時間: "<<p->someone.in_time;
cout<<"出生日期: "<<p->someone.birthday;
cout<<"家庭住址: "<<p->someone.address;
cout<<"編號: "<<p->someone.num;
cout<<"職務: "<<p->someone.duty;
cout<<"部門: "<<p->someone.partment;
cout<<"工作年限: "<<p->someone.workyear;
cout<<"E-mail: "<<p->someone.E-mail;
}
void List::help()
{
cout<<endl<<endl;
cout<<"----------------------------------------------------------"<<endl;
cout<<"1: 編輯職工信息"<<endl;
cout<<"2: 顯示個人信息"<<endl;
cout<<"3: 顯示該職工所有信息"<<endl;
cout<<"4: 幫助菜單"<<endl;
cout<<"5: 返回上一級菜單"<<endl;
cout<<"----------------------------------------------------------"<<endl;
}
List::~List()
{
nodetype *pa=head, *pb;
if(pa!=NULL)
{
pb=pa->next;
if(pb==NULL)
free(pa);
else
{
while(pb!=NULL)
{
free(pa);
pa=pb;
pb=pb->next;
}
free(pa);
}
}
}
LinkList::LinkList() {
m_pList = NULL;
m_listLength = 0;
InitList();
}
LinkList::~LinkList() {
if (!DestroyList()) {
DestroyList();
}
}
//初始化,分配一個頭節點。
bool LinkList::InitList() {
if (!(m_pList = new LNode)) {
return false;
}
m_pList->next = NULL;
return true;
}
//銷毀鏈表。
bool LinkList::DestroyList() {
if (!ClearList()) {
return false;
}
delete m_pList;
return true;
}
//判斷鏈表是否為空。若為空,返回true,否則返回false。
bool LinkList::IsEmpty() {
if (m_pList->next == NULL) {
return true;
}
return false;
}
//返回鏈表的中當前節點數。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -