TianXieErYang 2015-09-11 04:48 采纳率: 0%
浏览 1452

文件读取不了,谁能解决一下

#include
#include
#include
#include
#include
using namespace std;
int key;
int key2;
int p;
class node
{
public:
char name[8];
char address[20];
char num[11];
node *next;
};
typedef node*pnode;
typedef node*mingzi;
node
*phone;
node**nam;
void hash(char num[11]) //以电话号码为关键字建立哈希函数,将电话号码的十一位全部加起来
{
int i = 3;
key = (int)num[2];
while (num[i] != NULL)
{
key += (int)num[i];
i++;
}
key = key % 20; //通过线性探测再散列方法处理冲突

}
void hash2(char name[8])
{
int i = 1;
key2 = (int)name[0];
while (name[i] != NULL)
{
key2 += (int)name[i];
i++;
}
key2 = key2 % 20; //通过线性探测再散列方法处理冲突
}
node input()
{
int i;
node*temp;
temp=new node;
FILE *fp;
//cout<<"请选择需要插入的学生信息"< fp=fopen("a.txt","r");
while(fscanf(fp,"%s%d%s",temp->name,&temp->num,temp->address)!=EOF)
{
i++;
}
fclose(fp);
/

node*temp;
temp=new node;
temp->next = NULL;
FILE fp;
if((fp=fopen("1.txt","w"))==NULL){
printf("cannot open file");
exit(0);
}
while(temp!=NULL){
fprintf(fp,"%s %d %s/n",temp->name,&temp->num,temp->address);
if(temp->next==NULL)break;
temp = temp->next;
}
fclose(fp);
*/
/

node*temp;
string line;
temp=new node;
int head=0;
temp->next = NULL;
ifstream ifs("1.txt");
//if(!ifs) return this;
while(getline(ifs,line))
{
//ifstringstream is(line);
if(head==0)
{
//跳过第一行的表头
head=1;
continue;
}
ifs>>temp->name>>temp->num>>temp->address;
}
ifs.close();
*/
/

node*temp;
temp=new node;
temp->next = NULL;
cout<<"请输入姓名:\n";
cin>>temp->name;
cout<<"请输入电话号码:\n";
cin>>temp->num;
cout<<"请输入详细地址:\n";
cin>>temp->address;
*/
return temp;//返回地址

}
void add()//添加结点
{
node*newphone;
node*newname;
newphone = input();
newname = newphone;
newphone->next = NULL;
newname->next = NULL;
hash(newphone->num);
hash2(newname->name);

newphone->next = phone[key]->next;//利用电话号码为关键字插入
phone[key]->next = newphone;//拉链法处理冲突的散列结构

newname->next = nam[key2]->next;
nam[key2]->next = newname;

}

void create() //添加手机号
{
int i;
phone = new pnode[20];
for (i = 0; i {
phone[i] = new node;
phone[i]->next = NULL;
}
}
void create2() //添加姓名
{
int i;
nam = new mingzi[20];
for (i = 0; i {
nam[i] = new node;
nam[i]->next = NULL;
}
}

void display() //显示
{
int i;
node*p = NULL;
for (i = 0; i {
p = nam[i]->next;
while (p)
{
cout<name<<"\t";
cout<address<<"\t";
cout<num<<"\n";
p = p->next;
}
}
}

void search_num(char num[]) //查找手机号
{
hash(num);
node*q = phone[key]->next;
while (q) //比较手机号关键字信息
{
if (strcmp(num, q->num) == 0) break;
q = q->next;
}
if (q) //查找成功则输出
{
cout<<"姓名\t地址\t电话号码\n";
cout<name<<"\t";
cout<address<<"\t";
cout<num<<"\n";
}
else //查找失败
cout<<"无此条记录!\n";
}

void search_name(char name[8]) //查找姓名
{
hash2(name);
node*q = nam[key2]->next;
while (q) //比较姓名关键字信息
{
if (strcmp(name, q->name) == 0) break;
q = q->next;
}
if (q) //查找成功则输出
{
cout<<"姓名\t地址\t电话号码\n";
cout<name<<"\t";
cout<address<<"\t";
cout<num<<"\n";
}
else //查找失败
cout<<"无此条记录!\n";
}

void menu() //菜单
{
cout<<"*******************\n";
cout<<"1.添加记录\n";
cout<<"2.查找记录\n";
cout<<"3.显示记录\n";
cout<<"4.清空记录\n";
cout<<"5.删除记录\n";
cout<<"6.退出\n";
cout<<"*******************\n";
}

void main()
{
cout<<"****************************\n";
cout<<"* \n";
cout<<"
电话号码查询系统 \n";
cout<<"
\n";
cout<<"
***************************\n";
char num[11];
char name[8];
create();
create2();
int choose;
while (1)
{
menu();
cin>>choose;
switch (choose)
{
case 1:
system("cls");
cout<<"请输入要添加的记录\n";
add();
break;

        case 2:
            system("cls");
        cout<<"1.按号码查询    2.按姓名查询\n";
        int b;
        cin>>b;
        if (b == 1)
        {
            cout<<"请输入电话号码\n";
            cin>>num;
            cout<<"输出查找的信息\n";
            search_num(num);
        }
        else if(b==2)
        {
            cout<<"请输入姓名\n";
            cin>>name;
            cout<<"输出查找的信息\n";
            search_name(name);
        }
        break;

        case 3: 
            {
                system("cls");
                cout<<"姓名\t地址\t电话号码\n";
                display(); 
            }
            break;

        case 4:
            {
                system("cls");
                cout<<"列表已清空\n";
                create();
                create2();
            }
            break;
        case 5:
            {}
            break;
        case 6:
            exit(0);
        default:
            cout<<"没有该选项请重新输入!\n";
    }
}

}

  • 写回答

3条回答 默认 最新

  • lx624909677 2015-09-11 11:33
    关注

    还有,读取不了,是什么意思?直接打开文件就失败?还是打开成功了但是读取数据读取不到,亦或是读取的数据是乱码等

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度