| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 702 人关注过本帖
标题:c对txt的添加 修改 和 条件查询。。。
只看楼主 加入收藏
woshihsz
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-2
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:3 
c对txt的添加 修改 和 条件查询。。。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct guest
{
 string id;
 string name;
 string address;
 string date;
 string ps;
 guest(string i,string p,string a,string d,string s):id(i),name(p),address(a),date(d),ps(s) {next = NULL;}
 guest* next;};

 class guest_mgr{
private:
 guest* head;
 public:
  guest_mgr();
  bool add_people(string id,string name,string address,string date,string ps);
  bool alt_people(string id,string name,string address,string date,string ps);
  bool look_file(string filename);
  void print_people();
  ~guest_mgr();
}



bool guest_mgr::add_people(string id,string name,string address,string date,string ps){
 if(NULL==head)
  return false;
 guest* p = head;
 while(NULL!=p->next){
  p = p->next;
 }
 guest* newguest = new guest(id,name,address,date,ps);
 p->next = newguest;
 newguest->next = NULL;
 return true;
}

bool guest_mgr::look_file(string filename)
{
 if stream inobj(filename.c_str());
 if(!inobj){
  cout<<"open file failed.\n";
  return false;
 }
 guest *t,*p;
 string sid,sname,saddress,sdate,sps;
 p = head;
 while(!inobj.eof()){  
  inobj>>sid>>sname>>saddress>>sdate>>sps;
  t = new guest(sid,sname,saddress,sdate,sps);
  p->next = t;
  p = t;
 }
 t->next = NULL;
 return true;
}

bool guest_mgr::chk_people(string id){
 if(NULL==head)
  return false;
 guest *p = head;
 while(NULL!=p->next&&id!=p->id){
  p = p->next;  
 }
 if(NULL!=p->next){
  return true;
 }
 else if(NULL==p->next&&id==p->id)
  return true;
 return false;
}

bool guest_mgr::chk_people(string id,string name,string address,string date,string ps){
 if(NULL==head)
  return false;
 guest *p = head;
 while(NULL!=p->next&&id!=p->id){
  p = p->next;  
 }
 if(NULL!=p->next){
  if(name==p->name)
   return true;
 }
 else if(NULL==p->next&&id==p->id)
  if(name==p->name)
   return true;
 return false;
}


void guest_mgr::print_people(){
 if(NULL==head){
  cout<<"no guest in the list.\n";
  return;
 }
 guest* p = head;
 while(NULL!=p){
  cout<<p->id<<" "<<p->name<<" "<<p->address" "<<p->date" "p->ps<<"\n";
  p=p->next;
 }
 cout<<endl;
}

guest_mgr* mylist;//在菜单函数中要用到,不要移动它
string fn = "data.txt";//同上

void add(){
 cout<<"\n-------------------Add Guest----------------------\n";
 string id,name,address,date,contents,ps;
  cout<<"ID:";
 cin>>id;
  cout<<"name:";
 cin>>name;
  cout<<"Address:";
 cin>>address;
  cout<<"Date:";
 cin>>date;
  cout<<"Contents:";
 cin>>contents;
  cout<<"Ps:";
 cin>>ps;
 if(mylist->add_people(id,name,address,date,contents,ps)){
  cout<<"\nadd guest successful.\n";
 }
 else{
  cout<<"sorry,the system is wrong.\n";
 }
 cout<<"\n-------------------Add Guest----------------------\n";
}



void menu(){
 int c;
 cout<<"\n****************************Main Menu****************************\n";
 cout<<"1.添加信息\t2.修改信息\t3.查询信息\t4.退出";
 cout<<"\n****************************Main Menu****************************\n";
 cin>>c;
 switch(c){
 case 1:add();break;
 case 2:look();break;
 case 3:look();break;
 default:mylist->write_file(fn);exit(0);
 }
}

void main(){
 mylist = new guest_mgr();
 mylist->look_file(fn);
 while(1)
  menu();
 cout<<endl;
}


我想实现 c对txt的添加 修改 和 条件查询。。。。
怎么改?
搜索更多相关主题的帖子: private address include public 
2011-09-02 15:01
QQ785468931
Rank: 2
等 级:论坛游民
帖 子:43
专家分:50
注 册:2011-8-30
收藏
得分:20 
程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
struct customer_node {
    int key;
    char name[20];
    char sex[5];
    char ID[20];
    float total_consumption;
    float bonus_points;
    customer_node *next;
};
int count_customer_node(customer_node *head) {                                            //统计现有的客户总数,对链表进行操作,返回一个int型的客户总数
    customer_node *p;
    int count = 0;
    p = head->next;
    while(p != NULL) {
        count++;
        p = p->next;
    }
    return count;
}
void sort_customer_node(customer_node *head) {                                            //排序函数,对链表进行操作,采用冒泡法
    int count,i;
    i = 0;
    count = count_customer_node(head);
    customer_node *p,*k,*h,*m;

    while(i < count) {
        h = head;
        k = h->next;
        p = k->next;
        while(p != NULL) {
            if(k->bonus_points < p->bonus_points) {
                k->next = p->next;
                p->next = k;
                h->next = p;
                m = p;
                p = k;
                k = m;
           
            }
            p = p->next;
            k = k->next;
            h = h->next;
        }
        i++;
    }
}
void save_customer_msg(customer_node *head) {                                         //信息存储操作函数,接受customer_node类型的指针作为参数,将链表中的信息存储到customer_msg.txt中
    customer_node *k;
    FILE *fp;
    fp = fopen("customer_msg","ab+");
    k = head->next;
    while(k != NULL) {
        fwrite(k,sizeof(customer_node),1,fp);
        k = k->next;
    }
    fclose(fp);
}
void cover_save_customer_msg(customer_node *head) {                                      //信息覆盖存储操作函数,接受customer_node类型的指针作为参数,将链表中的信息存储到customer_msg.txt中(会覆盖原来的信息)
    customer_node *h;
    FILE *fp;
    fp = fopen("customer_msg","wb");
    h = head->next;
    while(h != NULL) {
        fwrite(h,sizeof(customer_node),1,fp);
        h = h->next;
    }
    fclose(fp);
}
void empty_customer_msg(customer_node *head) {
    if(head->next == NULL)
        printf("这里没有任何顾客信息!!你需要!!\n");
    else {
        customer_node *k;
        k = head->next;
        while(k != NULL) {
            head->next = k->next;
            k = k->next;
        }
        cover_save_customer_msg(head);
        printf("删除客户信息成功!\n");
    }
}
float display_discount(float points) { //计算客户折扣函数,接受一个float型的数作为参数,输出对应的折扣
    float discount;
    if(points == 0)
        discount = 0;
    if(points > 0&&points <= 50)
        discount = 9.8;
    if(points > 50&&points <= 100)
        discount = 9.5;
    if(points > 100&&points <= 150)
        discount = 9.2;
    if(points > 150&&points <= 200)
        discount = 9.0;
    if(points > 200&&points <= 300)
        discount = 8;
    else if(points > 300)
        discount = 7;
    return discount;
}
void change_customer_msg(customer_node *head) {                                          //客户信息修改函数
    if(head->next == NULL)
        printf("这里没有任何顾客信息!!你需要新建!!\n");
    else {
        customer_node *k;
        k = head->next;
        char name[20];
        float consumption;
        printf("请输入你想修改的客户的名字!!: ");
        fflush(stdin);
        gets(name);
        if(k == NULL)
            printf("这里没有任何顾客信息!!请创建客户信息!\n");
        else {
            while(strcmp(name,k->name)) {
                k = k->next;
                if(k == NULL) {
                    printf("这里没有任何顾客信息!!你可以在新菜单创建!!\n");
                    break;
                }
            }
            if(k != NULL) {
                printf("%s\t\t%5.2f\t\t%5.2f\n",k->name,k->total_consumption,k->bonus_points,k->ID,k->sex);
                display_discount(k->bonus_points);
                printf("现在请输入总的消费额:");
                scanf("%f",&consumption);
                k->bonus_points = k->bonus_points = (consumption);
                k->total_consumption = k->total_consumption = consumption;
                sort_customer_node(head);
                cover_save_customer_msg(head);
            }
        }
    }
}
void seach_customer_msg(customer_node *head) {                                                 //查询函数,接受一个customer_node型的指针作为参数,交互式操作,根据名称查找客户信息
    if(head->next == NULL)
        printf("这里没有任何客户的信息,请返回!!\n");
    else {
        char name[20];
        customer_node *k;
        float discount;
        k = head->next;
        printf("请输入你要查询的客户的名字:");
        fflush(stdin);
        gets(name);
        if(k == NULL) {
            printf("这里没有任何客户信息!!请在这里创建!\n");
        }
        else {
            while(k != NULL) {
                if(!strcmp(name,k->name))
                    break;
                else
                    k = k->next;
            }
            if(k != NULL) {
                discount = display_discount(k->bonus_points);
                printf("姓名\t总消费金额\t总积分\t\t折扣\tID\t\t性别\n");
                printf("%s\t%5.2f\t\t%5.2f\t\t%2.1f\t%s\t\t%s\n",k->name,k->total_consumption,k->bonus_points,discount,k->ID,k->sex);
           
            }
            else
                printf("没有这个客户! !\n");
        }
    }
}
void display_customer_msg(customer_node *head) { //显示所有客户的信息
    if(head->next == NULL)
        printf("这里没有任何客户信息!!你需要新建一个主菜单!!\n");
    else {
        int count = 0;
        count = count_customer_node(head);
        customer_node *k;
        float discount;
        k = head->next;
        printf("现在系统保存了 %d 个客户的用户资料!!!\n",count);
        printf("姓名\t总消费金额\t总积分\t\t折扣\tID\t\t性别\n");
        while(k != NULL) {
            discount = display_discount(k->bonus_points);
            printf("%s\t%5.2f\t\t%5.2f\t\t%2.1f\t%s\t\t%s\n",k->name,k->total_consumption,k->bonus_points,discount,k->ID,k->sex);
            k = k->next;
        }
    }
}
void add_customer(customer_node *head) {
    FILE *fp;
    customer_node *k,*h,*p;
    k = head;
    char w = 'y';
    while(w == 'y')
    {
        p = head;
        h = (customer_node*)malloc(sizeof(customer_node));
        printf("请输入新客户名字:");
        scanf("%s",&h->name);
        while(p != NULL)
        {
            if(!strcmp(h->name,p->name)) {
                printf("这个客户已经存在!!你可以在主菜单修改他的信息!是否继续增加客户信息(y or n):");
                break;
            }
            p = p->next;
        }
        if(p == NULL) {
            char ch[4]={0};
            char a[4]="";
            char b[4]="";
            char id[7]={0};
            int flag=0;
            int i=0;
             while(!flag)
     {
      flag=1;  
      printf("请输入新客户的6位数ID:");
      scanf("%6s",id);//输入大于6位,其后自动抛弃
     for(i=0;i<6;i++)
        { //输入的不是数字或少于6位报错,从新输入
          if(id[i]<'0'||id[i]>'9'||id[i]==0)
          {
             printf("输入非法,请输入数字\n");
           flag=0;       //设标志位,重新输入
           i=7;          //退出for循环
              fflush(stdin);//清除键盘缓存        
           }
        }
    
     }
    printf("输入成功\n");
    strcpy(h->ID,id);
    fflush(stdin);
R:            printf("请输入新客户的性别:");
            scanf("%s",&ch);
                if(strcmp(ch,a)==0||strcmp(ch,b)==0)
                {
                    strcpy(h->sex,ch);
                }
                else
                {
                    printf("输入有误请重新输入“男”或“女”两字:");
                    goto R;
                }
            printf("请输入新客户的总消费额:");
            scanf("%f",&h->total_consumption);
            printf("请输入新客户的总积分:");
            scanf("%f",&h->bonus_points);
            h->next = k->next;
            k->next = h;
                printf("N/n完成输入或Y/y继续输入");
        }
        fflush(stdin);
        w = getchar();
    }
    sort_customer_node(head);
    fp = fopen("customer_msg","wb");
    k = head->next;
    while(k != NULL)
    {
        fwrite(k,sizeof(customer_node),1,fp);
        k = k->next;
    }
    fclose(fp);
}
void del_customer_msg(customer_node *head) {                                                         //删除操作函数,接受customer_node型指针作为参数
    if(head->next == NULL)
        printf("这里没有任何客户信息!!\n");
    else {
        char w = 'y';
        char name[20];
        customer_node *p,*k;
        while(w == 'y') {
            k = head->next;
            p = head;
            printf("在你删除客户信息后.假如你想继续删除客户信息,你可以输入y+Enter或n+Enter选择继续或退出\n\n");
            printf("请输入你要删除人的名字:");
            fflush(stdin);
            gets(name);
            while(k != NULL) {
                if(!strcmp(name,k->name))
                    break;
                else {
                    k = k->next;
                    p = p->next;
                }
           
            }printf("删除成功\n");fflush(stdin);
            if(k == NULL) {
                printf("已经没有这个客户!!!\n");
                printf("你想继续吗!(y or n):");
            }
            else {
                p->next = k->next;
            }
            fflush(stdin);
            w = getchar();
        }
        sort_customer_node(head);
        cover_save_customer_msg(head);
    }
}
int main() {
    FILE *fp;
    customer_node *head,*k,*p;
    head = (customer_node*)malloc(sizeof(customer_node));
    head->next = NULL;
    k = head;
    fp = fopen("customer_msg","ab+");
    while(1) {
        p = (customer_node*)malloc(sizeof(customer_node));
        fread(p,sizeof(customer_node),1,fp);
        if(feof(fp)){
            break;
        }
        else {
            p->next = NULL;
            k->next = p;
            k = p;
        }
    }
    system("cls");
    fclose(fp);
    int i,m;
TT:    system("cls");printf("\n\n\n\n              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printf("                                 客户信息管理系统 \n");
    printf("              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printf("                                 1.添加客户信息\n");
    printf("                                 2.查询客户信息 \n");
    printf("                                 3.删除客户信息 \n");
    printf("                                 4.修改客户信息 \n");
    printf("                                 5.显示所有客户信息\n");
    printf("                                 6.清空所有客户信息 \n");
    printf("                                 7.退出客户信息管理系统 \n");
    printf("               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    while (i=1) {
        printf("\n\n");
        printf("请输入你要选择的功能选项,按回车键:");
        fflush(stdin);
        scanf("%d",&m);
        fclose(fp);
        switch (m) {
            case 1:system("cls");add_customer(head);system("PAUSE");goto TT;
            case 2:system("cls");seach_customer_msg(head);system("PAUSE");goto TT;
            case 3:system("cls");del_customer_msg(head);system("PAUSE");goto TT;
            case 4:system("cls");change_customer_msg(head);system("PAUSE");goto TT;
            case 5:system("cls");display_customer_msg(head);system("PAUSE");goto TT;
            case 6:system("cls");empty_customer_msg(head);system("PAUSE");goto TT;
            case 7:system("cls");cover_save_customer_msg(head);exit(0);
            default:printf("输入非法!!请重新输入!!");continue;
        }
    }
    fflush(stdin);
    return 0;
}//你参考参考,或许对你有用!
2011-09-02 15:35
woshihsz
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-2
收藏
得分:0 
回复 2楼 QQ785468931
额 多谢了
2011-09-02 16:27
QQ785468931
Rank: 2
等 级:论坛游民
帖 子:43
专家分:50
注 册:2011-8-30
收藏
得分:0 
回复 3楼 woshihsz
不用,相互学习,帮助应该的
2011-09-02 18:46
快速回复:c对txt的添加 修改 和 条件查询。。。
数据加载中...
 
   



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

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