| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 552 人关注过本帖
标题:数据结构的一个应用 无法写入和查看
取消只看楼主 加入收藏
蔽目聆雨
Rank: 1
来 自:济南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-5-11
收藏
 问题点数:0 回复次数:7 
数据结构的一个应用 无法写入和查看
为什么 这个程序无法写入文件中?我忙了一个下午没找着错误原因  谁能帮下忙
谢谢了!!

//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
搜索更多相关主题的帖子: 数据结构 应用 
2010-11-21 20:36
蔽目聆雨
Rank: 1
来 自:济南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-5-11
收藏
得分:0 
一共是三个文件
2010-11-21 20:37
蔽目聆雨
Rank: 1
来 自:济南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-5-11
收藏
得分:0 
是没有用return的原因吗?
2010-11-22 08:41
蔽目聆雨
Rank: 1
来 自:济南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-5-11
收藏
得分:0 
是插入函数没有写调用   弄了一个下午 终于明白了!!!!
2010-11-22 12:40
蔽目聆雨
Rank: 1
来 自:济南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-5-11
收藏
得分:0 
/*
*
*本程序共包含三个文件SeqList.h 、SeqList.cpp 和 Application.cpp
*txt文件建立在e盘
*/
//===========文件一=======================

//SeqList.h
#ifndef SeqList_H
#define SeqList_H
const int MaxSize=100;  
template <class T>      
class SeqList
{
public:
   SeqList( );      
   SeqList(T a[], int n);     
   ~SeqList();           
   int Length();           
   T Get(int i);         
   int Locate(T x);      
   void Insert(int i, T x);  
   T Delete(int i);      
   void PrintList();      
private:
   T data[MaxSize];      
   int length;           
};
#endif
2010-11-25 13:23
蔽目聆雨
Rank: 1
来 自:济南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-5-11
收藏
得分:0 
//===============文件二====================
//SeqList.cpp  
#include "SeqList.h"

template <class T>
SeqList<T>:: SeqList( )
{
  length=0;
}

template <class T>
SeqList<T>:: SeqList(T a[], int n)
{
  if (n>MaxSize) throw "参数非法";
  for (int i=0; i<n; i++)  
    data[i]=a[i];
  length=n;
}

template <class T>
SeqList<T>:: ~SeqList( )
{
}

template <class T>
void SeqList<T>::Insert(int i, T x)
{
        int j;
  if (length>=MaxSize) throw "上溢";
    if (i<1 || i>length+1) throw "位置";
  for (j=length; j>=i; j--)
  data[j]=data[j-1];   
  data[i-1]=x;
  length++;
}


template <class T>
T SeqList<T>::Delete(int i)
{
        T x;
        int j;
  if (length==0) throw "下溢";
  if (i<1 || i>length) throw "位置";
  x=data[i-1];
  for (j=i; j<length; j++)
    data[j-1]=data[j];   
  length--;
  return x;
}

template <class T>
int SeqList<T>::Length()
{
         return length;
}

template <class T>
T SeqList<T>::Get(int i)
{
  if (i<1 || i>length) throw "查找位置非法";
    else return data[i-1];
}


template <class T>
int SeqList<T>::Locate(T x)
{     
          for (int i=0; i<length; i++)
           if (strcmp(data[i].cellno,x.cellno)==0)
             return i+1 ;   
          return 0;   
}

template <class T>
void SeqList<T>::PrintList()
{
        for(int i=0;i<length;i++)
        cout<<data[i]<<endl;
}
2010-11-25 13:23
蔽目聆雨
Rank: 1
来 自:济南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-5-11
收藏
得分:0 
//==================文件三==============
//Application.cpp
#include <string>
#include <iostream>
#include "SeqList.cpp"
#include <cstring>      
#include<iomanip>
#include<fstream>
using namespace std;

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

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 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 i,k=0;
        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 i,pay1,pay2,k=0;
        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)
{
        string str;
        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');
    while(1)   
        {
                 cout<<"pay=";
                 cin>>str;
                   for(int i=0;i<str.size();i++)
                 {
                           if (str[i]>='0' && str[i]<='9')
                           {

                        }
                           else
                        {
                                    cout<<"输入了非整数!请重新输入:"<<endl;
                                    break;
                        }
                 }
                    if( i==str.size()) break ;
        }
    x.pay=atoi(str.c_str());
    while(1)   
        {
                 cout<<"id=";
                 cin>>str;
                   for(int i=0;i<str.size();i++)
                 {
                           if (str[i]>='0' && str[i]<='9')
                           {

                        }
                           else
                        {
                                    cout<<"输入了非整数!请重新输入:"<<endl;
                                    break;
                        }
                 }
                    if( i==str.size()) break ;
        }
    x.id=atoi(str.c_str());
        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");

        if(!f1)return;                                                        
        for(i=1;i<=a.Length();i++)
        {
        x=a.Get(i);
        f1<<x.name<<endl;
        f1<<x.addr<<" "<<x.tel<<" "<<x.pay<<" "<<x.id<<endl;
        }
        f1.close();
}


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,' ');
                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<<"    你的选择是:";
                        while(1)
                        {
                                cin>>select;
                                if(select<=105&&select>=97)break;
                                cout<<"选择错误!请重新选择:"<<endl;
                        }
                        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;
                }
        }
}
2010-11-25 13:23
蔽目聆雨
Rank: 1
来 自:济南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-5-11
收藏
得分:0 
上面三个帖子是刚修改完的  欢迎指正
2010-11-25 13:24
快速回复:数据结构的一个应用 无法写入和查看
数据加载中...
 
   



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

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