数据结构

作者在 2010-11-21 19:05:10 发布以下内容
为什么 这个程序无法写入文件中?我忙了一个下午没找着错误原因  谁能帮下忙
谢谢了!!

//Application.cpp
#include <iostream>       //引用输入输出流库函数的头文件
#include "SeqList.cpp"      //引用顺序表类SeqList
#include <cstring>       //引用输入输出流库函数的头文件
#include<iomanip>
#include<fstream>
using namespace std;

struct Employee
{
    char name[20];
    char addr[20];
    char tel[20];
    int pay;
    int id;
};


void Employee_display(SeqList <Employee> a)
{
    int i;
    Employee x;
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    cout<<i<<" :name "<<x.name<<"  addr "<<x.addr<<"  tel "<<x.tel<<"  pay "<<x.pay<<"  id "<<x.id<<endl;
    }
}

void Employee_findbyname(SeqList <Employee> a)
{
    int k=0;
    int i;
    char name[20];
    Employee x;
    cin.get();  
    cout<<"To find which one? Input it's Name: "<<endl;
    cin.getline(name,20,'\n');    
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    if(strcmp(x.name,name)==0)
        {
            cout<<i<<" :name "<<x.name<<"  addr "<<x.addr<<"  tel "<<x.tel<<"  pay "<<x.pay<<"  id "<<x.id<<endl;    
            k++;
        }
        
    }
    if(k>=0)cout<<"Found in "<<k<<endl;else cout<<"not found"<<endl;
}

void Employee_findbyid(SeqList <Employee> a)
{
    int i,id,k=0;
    Employee x;
    cin.get();
    cout<<"To find which one? Input it's id: "<<endl;
    cin>>id;    
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    if(x.id==id)
        {
            cout<<i<<" :name "<<x.name<<"  addr "<<x.addr<<"  tel "<<x.tel<<"  pay "<<x.pay<<"  id "<<x.id<<endl;    
            k++;
        }
    }
    if(k>=0)cout<<"Found in "<<k<<endl;else cout<<"not found"<<endl;
}

void Employee_findbypay(SeqList <Employee> a)
{
    int k,i,pay1,pay2;
    Employee x;
    cin.get();
    cout<<"To find which one? Input it's pay range,for example,100 200 :"<<endl;
    cin>>pay1;
    cin>>pay2;        
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    if(x.pay>=pay1&&x.pay<=pay2)
        {
            cout<<i<<" :name "<<x.name<<"  addr "<<x.addr<<"  tel "<<x.tel<<"  pay "<<x.pay<<"  id "<<x.id<<endl;    
        k++;
        }
    }
    if(k>=0)cout<<"Found in "<<k<<endl;else cout<<"not found"<<endl;
}

void Employee_insert(SeqList <Employee> a)
{
    int k;
    Employee x;
    cin.get();
    cout<<"Input an employee to be Insert:"<<endl;
    cout<<"name=";    cin.getline(x.name,20,'\n');
    cout<<"addr=";    cin.getline(x.addr,20,'\n');
    cout<<"tel=";    cin.getline(x.tel,20,'\n');
    cout<<"pay=";    cin>>x.pay;
    cout<<"id=";    cin>>x.id;
    k=a.Length()+1;
    a.Insert(k,x);
    Employee_display(a);
}

void Employee_delet(SeqList <Employee> a)
{
    int k;
    cout<<"Input the employee position to be deleted: ";
    cin>>k;
    cout<<"Delete a Employee in "<<k<<endl;
    a.Delete(k);
    Employee_display(a);
}

void Write2File(SeqList <Employee> a)
{
    int i;
    Employee x;
    ofstream f1("e:\\Employee.txt");
//ofstream f1("e:\\Employee.txt");        //打开文件用于写,若文件不存在就创建

    if(!f1)return;                            

    //打开文件失败则结束运行
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    f1<<x.name<<endl;
    f1<<x.addr<<endl;        
    f1<<x.tel<<" "<<x.pay<<" "<<x.id<<endl;
    }
    f1.close();
}

int string2int(char chr[]){
int i=0,s=0,t=1;
while(chr[i]!='\0')i++;
for(i--;i>=0;i--)
{
    s=s+(chr[i]-48)*t;
    t=t*10;
}
return s;
}
void ReadFile(SeqList <Employee> &a)
{
    int k;
    Employee x;
    char chg1[80],chg2[80];
    ifstream f("e:\\Employee.txt");
    if(!f)return;
    while(!f.eof())
    {
        f.getline(x.name,80,'\n');
        f.getline(x.addr,80,'\n');
        f.getline(x.tel,80,' ');
        f.getline(chg1,80,' ');
        f.getline(chg2,80,'\n');
        x.pay=string2int(chg1);
        x.id=string2int(chg2);
        if(strcmp(x.name,"")!=0)
        {
            k=a.Length()+1;
            a.Insert(k,x);
        }
    }
    f.close();
    Employee_display(a);
}



void main( )
{
        int k=0;
        char select;
        SeqList <Employee> a;
        while(1){
        cout<<"\n      主选单:"<<endl;
        cout<<"    a.查看"<<endl;
        cout<<"    b.按姓名查找"<<endl;
        cout<<"    c.按工号查找"<<endl;
        cout<<"    d.按年薪查找"<<endl;
        cout<<"    e.插入1个"<<endl;
        cout<<"    f.删除1个"<<endl;
        cout<<"    g.从文件读入"<<endl;
        cout<<"    h.写入文件"<<endl;
        cout<<"    i.退出"<<endl;
        cout<<"    你的选择是:";
        cin>>select;
        switch(select){
        case 'a':
            Employee_display(a);
            break;
        case 'b':
            Employee_findbyname(a);
            break;
        case 'c':
            Employee_findbyid(a);
            break;
        case 'd':Employee_findbypay(a);
            break;
        case 'e':Employee_insert(a);
            break;
        case 'f':Employee_delet(a);
            break;
        case 'g':
            ReadFile(a);
            break;
        case 'h':
            Write2File(a);
            break;
        case 'i':
            exit(0);
            break;
        }
    }
}
 //Application.cpp
#include <iostream>       //引用输入输出流库函数的头文件
#include "SeqList.cpp"      //引用顺序表类SeqList
#include <cstring>       //引用输入输出流库函数的头文件
#include<iomanip>
#include<fstream>
using namespace std;

struct Employee
{
    char name[20];
    char addr[20];
    char tel[20];
    int pay;
    int id;
};


void Employee_display(SeqList <Employee> a)
{
    int i;
    Employee x;
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    cout<<i<<" :name "<<x.name<<"  addr "<<x.addr<<"  tel "<<x.tel<<"  pay "<<x.pay<<"  id "<<x.id<<endl;
    }
}

void Employee_findbyname(SeqList <Employee> a)
{
    int k=0;
    int i;
    char name[20];
    Employee x;
    cin.get();  
    cout<<"To find which one? Input it's Name: "<<endl;
    cin.getline(name,20,'\n');    
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    if(strcmp(x.name,name)==0)
        {
            cout<<i<<" :name "<<x.name<<"  addr "<<x.addr<<"  tel "<<x.tel<<"  pay "<<x.pay<<"  id "<<x.id<<endl;    
            k++;
        }
        
    }
    if(k>=0)cout<<"Found in "<<k<<endl;else cout<<"not found"<<endl;
}

void Employee_findbyid(SeqList <Employee> a)
{
    int i,id,k=0;
    Employee x;
    cin.get();
    cout<<"To find which one? Input it's id: "<<endl;
    cin>>id;    
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    if(x.id==id)
        {
            cout<<i<<" :name "<<x.name<<"  addr "<<x.addr<<"  tel "<<x.tel<<"  pay "<<x.pay<<"  id "<<x.id<<endl;    
            k++;
        }
    }
    if(k>=0)cout<<"Found in "<<k<<endl;else cout<<"not found"<<endl;
}

void Employee_findbypay(SeqList <Employee> a)
{
    int k,i,pay1,pay2;
    Employee x;
    cin.get();
    cout<<"To find which one? Input it's pay range,for example,100 200 :"<<endl;
    cin>>pay1;
    cin>>pay2;        
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    if(x.pay>=pay1&&x.pay<=pay2)
        {
            cout<<i<<" :name "<<x.name<<"  addr "<<x.addr<<"  tel "<<x.tel<<"  pay "<<x.pay<<"  id "<<x.id<<endl;    
        k++;
        }
    }
    if(k>=0)cout<<"Found in "<<k<<endl;else cout<<"not found"<<endl;
}

void Employee_insert(SeqList <Employee> a)
{
    int k;
    Employee x;
    cin.get();
    cout<<"Input an employee to be Insert:"<<endl;
    cout<<"name=";    cin.getline(x.name,20,'\n');
    cout<<"addr=";    cin.getline(x.addr,20,'\n');
    cout<<"tel=";    cin.getline(x.tel,20,'\n');
    cout<<"pay=";    cin>>x.pay;
    cout<<"id=";    cin>>x.id;
    k=a.Length()+1;
    a.Insert(k,x);
    Employee_display(a);
}

void Employee_delet(SeqList <Employee> a)
{
    int k;
    cout<<"Input the employee position to be deleted: ";
    cin>>k;
    cout<<"Delete a Employee in "<<k<<endl;
    a.Delete(k);
    Employee_display(a);
}

void Write2File(SeqList <Employee> a)
{
    int i;
    Employee x;
    ofstream f1("e:\\Employee.txt");
//ofstream f1("e:\\Employee.txt");        //打开文件用于写,若文件不存在就创建

    if(!f1)return;                            

    //打开文件失败则结束运行
    for(i=1;i<=a.Length();i++)
    {
    x=a.Get(i);
    f1<<x.name<<endl;
    f1<<x.addr<<endl;        
    f1<<x.tel<<" "<<x.pay<<" "<<x.id<<endl;
    }
    f1.close();
}

int string2int(char chr[]){
int i=0,s=0,t=1;
while(chr[i]!='\0')i++;
for(i--;i>=0;i--)
{
    s=s+(chr[i]-48)*t;
    t=t*10;
}
return s;
}
void ReadFile(SeqList <Employee> &a)
{
    int k;
    Employee x;
    char chg1[80],chg2[80];
    ifstream f("e:\\Employee.txt");
    if(!f)return;
    while(!f.eof())
    {
        f.getline(x.name,80,'\n');
        f.getline(x.addr,80,'\n');
        f.getline(x.tel,80,' ');
        f.getline(chg1,80,' ');
        f.getline(chg2,80,'\n');
        x.pay=string2int(chg1);
        x.id=string2int(chg2);
        if(strcmp(x.name,"")!=0)
        {
            k=a.Length()+1;
            a.Insert(k,x);
        }
    }
    f.close();
    Employee_display(a);
}



void main( )
{
        int k=0;
        char select;
        SeqList <Employee> a;
        while(1){
        cout<<"\n      主选单:"<<endl;
        cout<<"    a.查看"<<endl;
        cout<<"    b.按姓名查找"<<endl;
        cout<<"    c.按工号查找"<<endl;
        cout<<"    d.按年薪查找"<<endl;
        cout<<"    e.插入1个"<<endl;
        cout<<"    f.删除1个"<<endl;
        cout<<"    g.从文件读入"<<endl;
        cout<<"    h.写入文件"<<endl;
        cout<<"    i.退出"<<endl;
        cout<<"    你的选择是:";
        cin>>select;
        switch(select){
        case 'a':
            Employee_display(a);
            break;
        case 'b':
            Employee_findbyname(a);
            break;
        case 'c':
            Employee_findbyid(a);
            break;
        case 'd':Employee_findbypay(a);
            break;
        case 'e':Employee_insert(a);
            break;
        case 'f':Employee_delet(a);
            break;
        case 'g':
            ReadFile(a);
            break;
        case 'h':
            Write2File(a);
            break;
        case 'i':
            exit(0);
            break;
        }
    }
}
 //SeqList.h 声明类SeqList,文件名为SeqList.h
#ifndef SeqList_H
#define SeqList_H
const int MaxSize=100;  //100只是示例性的数据,可以根据实际问题具体定义
template <class T>      //定义模板类SeqList
class SeqList
{
public:
   SeqList( );       //无参构造函数
   SeqList(T a[], int n);       //有参构造函数
   ~SeqList();             //析构函数为空
   int Length();           //求线性表的长度
   T Get(int i);         //按位查找,取线性表的第i个元素
   int Locate(T x);       //按值查找,求线性表中值为x的元素序号
   void Insert(int i, T x);  //在线性表中第i个位置插入值为x的元素
   T Delete(int i);        //删除线性表的第i个元素
   void PrintList();       //遍历线性表,按序号依次输出各元素
private:
   T data[MaxSize];      //存放数据元素的数组
   int length;            //线性表的长度
};
#endif
 
默认分类 | 阅读 685 次
文章评论,共1条
变幻小子
2011-03-21 13:22
1
你好&nbsp;&nbsp;路过看看
游客请输入验证码
文章分类
文章归档