写这个例子其实就是为了复习一下数据结构,摆脱STL,写个小程序而已。指针操作的确容易出现不少bug,改了好几次... ...

其实大部分简单的文件管理系统都是这个模式 ,回想起大一时候的大作业... ...

1 #include<fstream>
2 #include<iostream>
3 #include<iomanip>
4 #include<string>
5 #include<cstdlib>
6
7 using namespace std;
8
9 namespace NameRecord{
10
11 struct friend_node{
12 char last_name[20];
13 char phone_num[12];
14 friend_node* next;
15 friend_node* prior;
16 };
17 friend_node *head_ptr;
18 friend_node *tail_ptr;
19 friend_node *current_ptr;
20 char pause;
21 class record{
22 public:
23 void UserChoice(int choice);
24 void InsertRecord();
25 void InsertNode(friend_node *new_ptr);
26 void InsertNodeAtHead(friend_node *new_ptr);
27 void InsertNodeAtEnd(friend_node *new_ptr);
28 void ShowList();
29 void DeleteRecord();
30 void DeleteNodeAtHead();
31 void DeleteNodeAtEnd();
32 void DeleteNodeAtMiddle();
33 int VerifyDelete();
34 void DeleteNode();
35 void DeleteList();
36 void SearchByLastName();
37 void SaveFile();
38 void LoadFile();
39 void Help();
40 void ModifyRecord();
41 void UserInput();
42 };
43 }
44
45 using namespace NameRecord;
46
47 int main(){
48 record myrecord;
49 cout<<"欢迎使用PhoneBook"<<endl;
50 cout<<"按Enter键继续"<<endl;
51 cin.get(pause);
52 system("cls");//清屏
53 int choice;
54 head_ptr = NULL;
55 tail_ptr = NULL;
56 myrecord.LoadFile();
57 do{
58 cout<<"1_新增记录"<<endl;
59 cout<<"2_显示所有记录"<<endl;
60 cout<<"3_按姓氏搜索记录"<<endl;
61 cout<<"4_删除记录"<<endl;
62 cout<<"5_修改记录"<<endl;
63 cout<<"6_帮助"<<endl;
64 cout<<"7_退出程序"<<endl;
65 cout<<"输入您的选择:";
66 cin>>choice;
67 myrecord.UserChoice(choice);//处理选择
68 }while(choice!=7);
69 return 0;
70 }
71
72 /***************************************************/
73 //处理选择的函数
74 //通过输入的选项进行函数调用
75 void record::UserChoice(int choice){
76 switch(choice){
77 case 1:
78 InsertRecord();
79 break;
80 case 2:
81 ShowList();
82 break;
83 case 3:
84 SearchByLastName();
85 break;
86 case 4:
87 DeleteRecord();
88 break;
89 case 5:
90 ModifyRecord();
91 break;
92 case 6:
93 Help();
94 break;
95 case 7:
96 SaveFile();
97 if(head_ptr==NULL)
98 DeleteList();
99 break;
100 default:
101 cout<<"选择无效"<<endl;
102 break;
103 }
104 }
105 /***************************************************/
106 //插入记录的函数
107 void record::InsertRecord(){
108 friend_node *new_ptr;
109 new_ptr = new friend_node;
110 if(new_ptr!=NULL){
111 system("cls");
112 cin.ignore(20,'\n');
113 cout<<"名字:";
114 cin.getline(new_ptr->last_name,15);
115 cout<<"电话号码:";
116 cin.getline(new_ptr->phone_num,15);
117 InsertNode(new_ptr);
118 }
119 else
120 cout<<"警告,申请存储空间失败,不能创建新结点。"<<endl;
121 system("cls");
122 }
123
124 void record::InsertNode(friend_node * new_ptr){
125 system("cls");
126 //friend_node *temp_ptr;
127 //情况一:双向链表为空
128 if(head_ptr==NULL){
129 new_ptr->next = NULL;
130 new_ptr->prior = NULL;
131 head_ptr = new_ptr;
132 tail_ptr = new_ptr;
133 return;
134 }
135 //情况二:如果链表中只有一个结点
136 if(head_ptr->next==NULL){
137 if(strcmp(new_ptr->last_name,head_ptr->last_name)<0)
138 InsertNodeAtHead(new_ptr);
139 else
140 InsertNodeAtEnd(new_ptr);
141 return;
142 }
143 //情况三:链表中不为一个链表
144 if(head_ptr->next!=NULL){
145 current_ptr = head_ptr;
146 while((current_ptr!=NULL)//这两个判断条件不能颠倒,否则会出现严重错误 !!!
147 &&(strcmp(new_ptr->last_name,current_ptr->last_name)>0))
148 current_ptr = current_ptr->next;
149
150 if(current_ptr == head_ptr)
151 InsertNodeAtHead(new_ptr);
152 else if(current_ptr == NULL )
153 InsertNodeAtEnd(new_ptr);
154 else//如果没有查找完整个表,并且满足排序条件推出,
155 //欲插入结点应该插入current_ptr之前
156 {
157 new_ptr->next = current_ptr;
158 new_ptr->prior = current_ptr->prior;
159 current_ptr->prior->next = new_ptr;
160 current_ptr->prior = new_ptr;
161 }
162 return ;
163 }
164 }
165
166 void record::InsertNodeAtHead(friend_node*new_ptr){
167 new_ptr->next = head_ptr;
168 new_ptr->prior = NULL;
169 head_ptr->prior = new_ptr;
170 head_ptr = new_ptr;
171 }
172
173 void record::InsertNodeAtEnd(friend_node*new_ptr){
174 new_ptr->prior = tail_ptr;
175 new_ptr->next = NULL;
176 tail_ptr->next = new_ptr;
177 tail_ptr = new_ptr;
178 }
179 /*****************************************************/
180 void record::ShowList(){
181 system("cls");
182 int n;
183 cout<<"请输入每屏显示的数目(不得大于20):"<<endl;
184 cin>>n;
185 system("cls");
186 int i;
187 current_ptr = head_ptr;
188 if(head_ptr ==NULL){
189 cout<<"记录为空,还未添加记录 !"<<endl;
190 return;
191 }
192 do{
193 i=1;//分屏显示
194 cout<<setw(20)<<"Name"<<setw(20)<<"Phone Number"<<endl;
195 do{
196 cout<<setw(20)<<current_ptr->last_name<<setw(20)<<current_ptr->phone_num<<endl;
197 current_ptr = current_ptr->next;
198 i++;
199 }while(current_ptr!=NULL&&i<=n);
200
201 cin.get(pause);
202 if(current_ptr!=NULL){
203 cout<<"请按Enter键继续";
204 cin.get(pause);
205 system("cls");
206 }
207 else cout<<"文件结束"<<endl;
208 }while(current_ptr!=NULL);
209
210 cin.get(pause);
211 cin.ignore(1,pause);
212 system("cls");
213 }
214 /*****************************************************/
215 void record::SearchByLastName(){
216 system("cls");
217 int flag =0;
218 char search_string[20];
219 current_ptr = head_ptr;
220 if(current_ptr==NULL)
221 cout<<"电话记录为空 !"<<endl;
222 else{
223 cin.ignore(20,'\n');
224 cout<<"输入您要搜索的名字:";
225 cin.getline(search_string,20);
226 while(current_ptr!=NULL){
227 if(strcmp(current_ptr->last_name,search_string)==0){
228 if(flag==0)cout<<"找到记录"<<endl;
229 flag = 1;
230 cout<<current_ptr->last_name<<"\t\t";
231 cout<<current_ptr->phone_num<<endl;
232 }
233 current_ptr=current_ptr->next;
234 }
235 if(flag == 0){
236 cout<<"无该记录"<<endl;
237 cin.get(pause);
238 system("cls");
239 }
240 }
241 }
1 /*****************************************************/
2 void record::DeleteRecord(){
3 system("cls");
4 char search_string[20];
5 current_ptr = head_ptr;
6 if(current_ptr ==NULL){
7 cout<<"没有要删除的记录"<<endl;
8 return;
9 }
10 cin.getline(search_string,20);
11 int flag = 0;
12 cout<<"\n输入您要删除的名字:";
13 cin.getline(search_string,20);
14 while(current_ptr!=NULL&&flag == 0){
15 if(strcmp(current_ptr->last_name,search_string)==0){
16
17 if(flag==0)cout<<"找到记录 !"<<endl;
18 cout<<current_ptr->last_name<<"\t\t"<<current_ptr->phone_num<<endl;
19 flag = 1;
20 if(VerifyDelete()){
21 cout<<current_ptr->last_name<<"\t"<<current_ptr->phone_num<<"\t记录已经删除\n";
22 DeleteNode();
23 }
24 else
25 cout<<current_ptr->last_name<<"\t"<<current_ptr->phone_num<<"\t该记录未删除"<<endl;
26 }
27 else//如果删除该记录,current_ptr会自动向下跳一格
28 current_ptr = current_ptr->next;
29 }
30 if(flag == 0)
31 cout<<"该记录不存在,请核实后继续操作 !"<<endl;
32 }
33 int record::VerifyDelete(){
34 char yesno;
35 cout<<"\n确认 ?(Y/N)";
36 cin>>yesno;
37 cin.ignore(20,'\n');
38 if(yesno == 'Y' || yesno =='y')
39 return 1;
40 else
41 return 0;
42 }
43 void record::DeleteNode(){
44 if(current_ptr == head_ptr)
45 DeleteNodeAtHead();
46 else{
47 if(current_ptr->next == NULL)
48 DeleteNodeAtEnd();
49 else
50 DeleteNodeAtMiddle();
51 }
52 }
53 void record::DeleteNodeAtHead(){
54 if(head_ptr->next ==NULL){//如果只有头结点
55 delete head_ptr;
56 head_ptr = tail_ptr = current_ptr = NULL;
57 return;
58 }
59 head_ptr = head_ptr->next;
60 head_ptr->prior = NULL;
61 delete current_ptr;
62 current_ptr = head_ptr;
63 return;
64 }
65 void record::DeleteNodeAtEnd(){
66 tail_ptr = tail_ptr->prior;
67 tail_ptr->next = NULL;
68 delete current_ptr;
69 current_ptr = NULL;
70 return;
71 }
72 void record::DeleteNodeAtMiddle(){
73 friend_node *temp_ptr;
74 temp_ptr = current_ptr;
75 current_ptr = current_ptr->next;//删除完自动跳到下一结点
76 temp_ptr->prior->next = current_ptr;
77 current_ptr->prior = temp_ptr ->prior;
78 delete temp_ptr;
79 return;
80 }
81 /*****************************************************/
82 void record::ModifyRecord(){
83 system("cls");
84 char search_string[20];
85 if(head_ptr==NULL){
86 cout<<"无记录可以修改 !"<<endl;
87 return ;
88 }
89 int flag = 0;
90 friend_node*new_ptr;
91 current_ptr = head_ptr;
92 cin.ignore(20,'\n');
93 cout<<"请输入你想修改记录的名字 :";
94 cin.getline(search_string,20);
95 while(current_ptr!=NULL&&flag == 0){
96 if(strcmp(current_ptr->last_name,search_string)==0){
97 flag = 1;
98 cout<<"找到该记录 !"<<endl;
99 cout<<"是否修改记录:\t"<<current_ptr->last_name<<"\t"<<current_ptr->phone_num<<" ?(y/n)";
100 char yesno;
101 cin>>yesno;
102 if(yesno =='y'||yesno =='Y'){
103 record::DeleteNode();
104 new_ptr = new friend_node;
105 cout<<"请输入新记录的名字:";
106 //cin.getline(current_ptr->last_name,20);
107 cin>>new_ptr->last_name;
108 cout<<"\n请输入新记录的电话: ";
109 //cin.getline(current_ptr->phone_num,15);
110 cin>>new_ptr->phone_num;
111 record::InsertNode(new_ptr);
112 cout<<"\n该记录修改完毕 !"<<endl;
113 }
114 else
115 cout<<"该记录未进行修改 !"<<endl;
116 }
117 if(flag==0)
118 current_ptr = current_ptr->next;
119 }
120 return ;
121 }
122 /*****************************************************/
123 void record::Help(){
124 }
125 /*****************************************************/
126 void record::SaveFile(){
127 ofstream outfile;
128 outfile.open("FRIENDS.dat",ios::out);
129 if(outfile){
130 current_ptr = head_ptr;
131 while(current_ptr!=NULL){
132 outfile<<current_ptr->last_name<<endl;
133 outfile<<current_ptr->phone_num<<endl;
134 current_ptr = current_ptr->next;
135 }
136 outfile<<"文件结束"<<endl;
137 outfile.close();
138 }
139 else
140 cout<<"打开文件失败 !"<<endl;
141 }
142 /*****************************************************/
143 void record::LoadFile(){
144 friend_node *new_ptr;
145 ifstream infile;
146 int end_loop = 0;
147 infile.open("FRIENDS.dat",ios::in);
148 if(infile){
149 do{
150 new_ptr = new friend_node;
151 if(new_ptr!=NULL){
152 infile.get(new_ptr->last_name,20);
153 infile.ignore(20,'\n');
154 if(strcmp(new_ptr->last_name,"文件结束")!=0
155 &&strcmp(new_ptr->last_name,"")!=0){
156 infile.get(new_ptr->phone_num,15);
157 infile.ignore(20,'\n');
158 InsertNode(new_ptr);
159 }
160 else{
161 delete new_ptr;
162 end_loop = 1;
163 }
164 }
165 else{
166 cout<<"没有成功从磁盘导入文件 !"<<endl;
167 end_loop =1;
168 }
169 }while(end_loop ==0);
170 infile.close();
171 }
172 else
173 cout<<"没有可用的数据文件 !"<<endl;
174 }
175 void record::DeleteList(){
176 cout<<"是否要清空电话簿 ?(Y/N)"<<endl;
177 char yesno;
178 cin>>yesno;
179 if(yesno =='Y'||yesno =='y'){
180 while(head_ptr != NULL){
181 current_ptr = head_ptr;
182 head_ptr = head_ptr->next;
183 delete current_ptr;
184 }
185 head_ptr = tail_ptr = current_ptr;
186 cout<<"电话簿已经清空 !"<<endl;
187 }
188 }
posted on 2011-07-11 15:49  geeker  阅读(708)  评论(0编辑  收藏  举报