| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 950 人关注过本帖
标题:为什么运行之后显示无法打开文件
只看楼主 加入收藏
xiaoxu531
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-6-25
收藏
 问题点数:0 回复次数:2 
为什么运行之后显示无法打开文件
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <cmath>
using namespace std;
#define Maxsize 57
struct record
{char name[20];
char tel[20];
char add[20];};
typedef record *precord;
struct HashTable
{ int elem[Maxsize]; //存放数组a[]的下标
int count;};
typedef HashTable * pHashTable;
int Number; //统计当前数组a[]中的记录总数
void Getdata(precord a) //从文件telphone.txt中读取数据存放到数组a[]
{ Number=0;
ifstream infile("telphone.txt",ios::in|ios::binary);
if(!infile) {cout<<"文件打开失败!\n"; exit(1);}
while(!infile.eof() && infile.get()!=EOF) //文件不为空并且文件指针没有指到结束符
{infile.seekg(Number*sizeof(a[Number]),ios::beg); //定位文件指针
infile.read((char *)&a[Number],sizeof(a[Number]));
Number++;}
infile.close();}
void Add(precord a) //添加记录
{ int i,num;
cout<<"当前文件内已有"<<Number<<"条记录\n";
cout<<"请输入添加的个数:";
cin>>num;
ofstream ofile("telphone.txt",ios::app);
if(!ofile) {cout<<"文件打开失败!"; exit(1);}
for(i=0;i<num;i++)
{cout<<"请输入第"<<Number+1<<"个人的姓名"<<endl;
cin>>a[Number].name;
cout<<"请输入第"<<Number+1<<"个人的电话"<<endl;
cin>>a[Number].tel;
cout<<"请输入第"<<Number+1<<"个人的地址"<<endl;
cin>>a[Number].add;
ofile.seekp(ios::end);
ofile.write((char *)&a[Number],sizeof(a[Number]));
Number++;}
ofile.close();}
void Print(precord a) //显示所有记录
{ int i;
for(i=0;i<Number;i++)
{cout<<" 姓名:"<<a[i].name;
cout<<" 电话:"<<a[i].tel;
cout<<" 地址:"<<a[i].add<<endl;}
}
int Hash(char str[]) //除留取余
{ long val=0;char p[20],*p1;
strcpy(p,str);
p1=p;
while(*p1!='\0')
val=val+*p1++; //将字符串中的所有字符对应的ASCII值相加
return(val%Maxsize);
}
int derter; //线性增量
int Line_Sollution(int address) //采用线性探测解决冲突
{
derter++;
if(derter==Maxsize) return(-1);
else return((address+derter)%Maxsize);
}
int n;
int Square_Sollution(int address) //采用平方探测法解决冲突
{ int j;derter++;
if(derter==Maxsize) return -1;
n=n*(-1);
j=(int(pow((float)derter,2))*n+address)%Maxsize;
return(j);}
void Init_Hash(pHashTable h) //初始化哈希表
{ int i;
for(i=0;i<Maxsize;i++)
h->elem[i]=-1;
}
int menu;
void Creathash_Name(pHashTable h,precord a)//以员工姓名为关键字创建哈希表
{cout<<"  ------------------------------------------------------------------------\n";
cout<<"                    1----以线性探测建表\n";
cout<<"                    2----以平方探测建表\n";
cout<<"       ------------------------------------------------------------------------\n";
int i,address;
cout<<"请选择:";
cin>>menu;
Init_Hash(h);
for(i=0;i<Number;i++)
{ derter=0;n=-1;
address=Hash(a[i].name);
while(h->elem[address]!=-1)
{if(menu==1) address=Line_Sollution(address);
else address=Square_Sollution(address);
if(address==-1) break;}
if(address!=-1) { h->elem[address]=i; h->count++;}
}
cout<<"姓名哈希表已成功建立!\n";
}
void Search_Name(pHashTable h,precord a) //查找并显示指定姓名的记录
{ cout<<"请输入要查找的姓名:";
char nam[20];int address,i=1;
cin>>nam;
address=Hash(nam);
derter=0;n=-1;
while(h->elem[address]!=-1 && strcmp(nam,a[h->elem[address]].name)!=0)
{ if(menu==1) address=Line_Sollution(address);
else address=Square_Sollution(address);
i++;
if(address==-1) break;}
if(h->elem[address]!=-1 && strcmp(nam,a[h->elem[address]].name)==0)
{ cout<<"你要查找的信息为:\n";
cout<<" 姓名:"<<a[h->elem[address]].name<<endl;
cout<<" 电话:"<<a[h->elem[address]].tel<<endl;
cout<<" 地址:"<<a[h->elem[address]].add<<endl;
cout<<"比较次数为"<<i<<endl;}
else cout<<"无此姓名,查找失败!";
}
void Creathash_tel(pHashTable h,precord a) //以电话号为关键字创建哈希表
{cout<<"        ---------------------------------------------------------\n";
cout<<"                     1----以线性探测建表\n";
cout<<"                     2----以平方探测建表\n";
cout<<"      ---------------------------------------------------------\n";
int i,address;
cout<<"请选择:";
cin>>menu;
Init_Hash(h);
for(i=0;i<Number;i++)
{ derter=0;n=-1;
address=Hash(a[i].tel);
while(h->elem[address]!=-1)
{if(menu==1) address=Line_Sollution(address);
else address=Square_Sollution(address);
if(address==-1) break;}
if(address!=-1) { h->elem[address]=i; h->count++;}
}
cout<<"电话号码哈希表已成功建立!\n";}
void Search_tel(pHashTable h,precord a)//查找并显示指定电话号的记录
{ cout<<"请输入要查找的电话:";
char telphone[20];int address,i=1; //i统计比较次数
cin>>telphone;
address=Hash(telphone);
derter=0; n=-1; //初始化线性增量
while(h->elem[address]!=-1&& strcmp(telphone,a[h->elem[address]].tel)!=0)
{ if(menu==1) address=Line_Sollution(address);
else address=Square_Sollution(address);
i++;
if(address==-1) break;}
if(h->elem[address]!=-1 && strcmp(telphone,a[h->elem[address]].tel)==0)
{ cout<<"你要查找的信息为:\n";
cout<<" 姓名:"<<a[h->elem[address]].name<<endl;
cout<<" 电话:"<<a[h->elem[address]].tel<<endl;
cout<<" 地址:"<<a[h->elem[address]].add<<endl;
cout<<"比较次数为"<<i<<endl;}
else cout<<"无此电话,查找失败!";
}
void Delet(pHashTable h,precord a)
{cout<<"         ---------------------------------------------------------\n";
cout<<"                     1----按电话号码删除\n";
cout<<"                     2----按姓名删除\n";
cout<<"         ---------------------------------------------------------\n";
int m;
cout<<"请选择:";
cin>>m;
if(m==1)
{cout<<"请输入要删除的电话:";
char telphone[20];
int address,i,j;
cin>>telphone;
address=Hash(telphone);
derter=0; n=-1; //初始化线性增量
while(h->elem[address]!=-1 && strcmp(telphone,a[h->elem[address]].tel)!=0)
{ if(menu==1) address=Line_Sollution(address);
else address=Square_Sollution(address);
if(address==-1) break;}
if(h->elem[address]!=-1&& strcmp(telphone,a[h->elem[address]].tel)==0)
{j=h->elem[address];
h->elem[address]=-1;
}
for (i=j;i<Number-1;i++)
{strcpy(a[i].name,a[i+1].name);
strcpy(a[i].tel,a[i+1].tel);
strcpy(a[i].add,a[i+1].add);}
Number=Number-1;
}
if(m==2)
{cout<<"请输入要删除的姓名:";
char nam[20];int address,i,j;
cin>>nam;
address=Hash(nam);
derter=0;n=-1;
while(h->elem[address]!=-1 && strcmp(nam,a[h->elem[address]].name)!=0)
{ if(menu==1) address=Line_Sollution(address);
else address=Square_Sollution(address);
i++;
if(address==-1) break;}
if(h->elem[address]!=-1 && strcmp(nam,a[h->elem[address]].name)==0)
{ j=h->elem[address];h->elem[address]=-1;}
for (i=j;i<Number-1;i++)
{strcpy(a[i].name,a[i+1].name);
strcpy(a[i].tel,a[i+1].tel);
strcpy(a[i].add,a[i+1].add);}
Number=Number-1;
}
}
void Menu() //功能菜单函数
{cout<<endl;
cout<<"                             员工管理查询系统\n";
cout<<'\n';
cout<<"         ★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆\n";
cout<<"         ★ 1-------添加                                   ☆\n";                           
cout<<"         ☆ 2-------显示所有                               ★\n";                        
cout<<"         ★ 3-------以姓名建立哈希表                       ☆\n";               
cout<<"         ☆ 4-------以电话号码建立哈希表                   ★\n";            
cout<<"         ★ 5-------按员工姓名查找                         ☆\n";                  
cout<<"         ☆ 6-------按电话号码查找                         ★\n";                  
cout<<"         ★ 7-------删除员工信息                           ☆\n";
cout<<"         ☆ 0-------退出                                   ★\n";
cout<<"         ★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆\n";
cout<<"         使用说明:\n";
cout<<"         1.添加新纪录后,如要进行查找请先进行3或4操作\n";
cout<<"         2.按员工姓名查找之前,请先进行3操作建立用户名哈希表\n";
cout<<"         3.按电话号码查找之前,请先进行4操作建立电话号码哈希表\n";}
void exit()
{ int i;
for(i=1;i<=4;i++)
cout<<endl;
cout<<"         ◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◇◆◇\n";
cout<<"                                                             ◆\n";
cout<<"                   员 工 管 理 查 询 系 统                ◇\n";
cout<<"                                                             ◆\n";
cout<<"                   谢 谢 您 的 使 用 !                    ◇\n";
cout<<"                                                             ◆\n";
cout<<"         ◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆◇◆\n";
system("pause");}
int main()
{ record a[Maxsize];
pHashTable H=new HashTable;
Getdata(a); //将文件中的数据读入到数组a中
system("color BD");
start:
Menu();
cout<<"请选择:";
int menu1;
cin>>menu1;
switch(menu1)
{ case 0:system("cls");exit();break;
case 1:Add(a);system("pause");system("cls");goto start;break;
case 2:Print(a);system("pause");system("cls");goto start;break;
case 3:Creathash_Name(H,a);system("pause");system("cls");goto start;break;
case 4:Creathash_tel(H,a);system("pause");system("cls");goto start;break;
case 5:Search_Name(H,a);system("pause");system("cls");goto start;break;
case 6:Search_tel(H,a);system("pause");system("cls");goto start;break;
case 7:Delet(H,a);system("pause");system("cls");goto start;break;
default:cout<<"请输入正确的操作选项!\n";system("cls");goto start;break;}
return 0;
}
搜索更多相关主题的帖子: include record count 记录 统计 
2014-06-25 20:41
陆金泉
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-7-14
收藏
得分:0 
你默认的路径下面没有telphone.txt这个文件吧
2014-07-15 13:01
zhuix7788
Rank: 1
等 级:新手上路
帖 子:4
专家分:7
注 册:2014-7-7
收藏
得分:0 
创建一个telphone.txt文件,里面输入员工信息就ok了
2014-07-15 15:32
快速回复:为什么运行之后显示无法打开文件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022231 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved