| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 487 人关注过本帖
标题:各位帮帮忙,我实在想不出错哪了!
只看楼主 加入收藏
编程菜鸟2009
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-8-29
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:4 
各位帮帮忙,我实在想不出错哪了!
#include <stdio.h>
#include <stdlib.h>
#include < malloc.h>
#include <string.h>
#define maxlen 20


typedef struct lnode
{ /*通讯录结构中结点的定义*/
   char name[20];
   char e_addr[20];
   char tel_no[15];
   char QQ_no[10];
   char arch;
   struct lnode *next;
}listnode,*linklist;

linklist head=NULL,r=NULL; /*定义头指针和尾指针*/
listnode *s,*p0,*ptest; /*定义节点指针*/
char name1[maxlen]; //定义结构体中间变量
///*
void test()
{
ptest=head;
while(ptest!=NULL)
{
printf("\n");
printf("\t%s\n",ptest->name);
printf("\t%s\n",ptest->tel_no);
printf("\t%s\n",ptest->QQ_no);
ptest=ptest->next;
}
}
//*/
void creat()
{
   s=(linklist)malloc(sizeof(listnode));
}
void Find()
{
   printf("\n\n\t请输入姓名:");
   scanf("%s",&name1);
   p0=head;
   while(strcmp(name1,p0->name)!=0&&p0!=NULL)
      p0=p0->next;
   if(p0==NULL)
       printf("要查找的联系人不存在!");
   else
   {
       printf("\t姓名:%s\n",p0->name);
       printf("\t电子邮件:%s\n",p0->e_addr);
       printf("\t电话号码:%s\n",p0->tel_no);
       printf("\tQQ号码:%s\n",p0->QQ_no);
       printf("\t类别:%c\n",p0->arch);
       printf("\n\n\n");
   }


}

void Delete() /*定义一个删除的函数*/
{
    printf("\n\n\t请输入要删除用户的姓名:");
    scanf("%s",name1);
    p0=head;
    if(p0->next!=NULL)
    {
        if(strcmp(p0->name,name1)==0)
        {
            ptest=p0;
            p0=p0->next;
            free(ptest);
        }
        else
            p0=p0->next;
    }
    else
        printf("找不到该联系人!");
}

void Input() /*向通讯录中输入一个人的信息*/
{
   s=(linklist)malloc(sizeof(listnode));
   printf("\n\n\t请输入该用户的信息:\n");
   printf("\t姓名:");
   scanf("%s",s->name);
   printf("\t电话号码:");
   scanf("%s",s->tel_no);
   printf("\tE-mail:");
   scanf("%s",s->e_addr);
   printf("\tQQ号码:");
   scanf("%s",s->QQ_no);
   getchar();
   printf("\t组别(A朋友  B同事  C同学  D家人):");
   scanf("%c",&s->arch);
   test();
   if(head==NULL){printf("\n\n"); head=s;r=head;head->next=NULL;}
   else
   {
       p0=head;
       while(p0!=NULL&&strcmp(s->name,p0->name)!=0&&strcmp(s->tel_no,p0->tel_no)!=0)
       p0=p0->next;
       if(p0!=NULL)
       {
           printf("\t您添加的用户已存在!");
           free(s);
       }
       else
       {
           r->next=s;
           r=s;r->next=NULL;
       }
}

//test();
}

void Alter() /*改变一个人的信息*/
{
    printf("\n\n\t请输入姓名:");
    scanf("%s",name1);
    p0=head;
    while(strcmp(name1,p0->name)!=0&&p0!=NULL)
    {
        p0=p0->next;
    }
    if(p0==NULL)
        printf("找不到该联系人!");
    else
    {
        printf("要修改的联系人的姓名:");
        scanf("%s",&name1);
        strcpy(p0->name,name1);
        printf("要修改的联系人的电子邮件:");
        scanf("%s",&name1);
        strcpy(p0->e_addr,name1);
        printf("要修改的联系人的电话号码:");
        scanf("%s",&name1);
        strcpy(p0->tel_no,name1);
        printf("要修改的联系人的QQ号码:");
        scanf("%s",&name1);
        strcpy(p0->QQ_no,name1);
/*        printf("要修改的联系人的类别:");
        scanf("%s",&name1);
        strcpy(p0->arch,name1);*/
    }
}



void main()
{
   char ch;
do
{
    printf("\n\n\n\n\t欢迎使用您的通讯录!");/*显示提示的信息*/
    printf("\n\n\t\t请选择操作:");
    printf("\n\t\t1.查找通讯录");
    printf("\n\t\t2.删除通讯录");
    printf("\n\t\t3.添加通讯录");
    printf("\n\t\t4.编辑通讯录");
    printf("\n\t\t5.退出");
    printf("\n\n\n");
    printf("\t请选择:");
    scanf("%c",&ch);
    void creat();
switch(ch)
{
    case '1': Find(); /*用单条件多选择语句实现调用与循环*/
    break;
    case '2': Delete();
    break;
    case '3': Input();
    break;
    case '4': Alter();
    break;
    case '5': exit(0);
    break;
    default:
    printf("\n\t————————————————\n");
    printf("\n\t 请输入1-5的数字!!! \n");
    printf("\n\t————————————————\n");
    break;
}
    getchar();
}while(1);
}
2009-08-29 12:24
mengfp
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:45
专家分:141
注 册:2009-8-5
收藏
得分:35 
#include < malloc.h> 多了一个空格,其他就没有了。
2009-09-03 11:48
编程菜鸟2009
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-8-29
收藏
得分:0 
空格多不多没所谓

有了#include <stdlib.h>可以去掉#include < malloc.h>的

2009-09-04 14:15
missiyou
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:35 
#ifndef SNODELIST_H
#define SNODELIST_H

#include <stdio.h>

template<class DataType>class SListNode;
template<class DataType>class SLinkedList;
template<class DataType>class SListIterator;


template<class DataType>
class SListNode
{
   
   
public:   
    DataType m_data;
    SListNode *m_next;
   
    SListNode()
    {
        m_next = 0;
    }
    void InsertAffter(DataType p_item)
    {
        SListNode<DataType> *newnode = new SListNode<DataType>;
        newnode->m_data = p_item;
        newnode->m_next = m_next;
        m_next = newnode;
        
        
    }
   
};


template< class  DataType >
class SLinkedList
{
public:
    SListNode<DataType> *head;
    SListNode<DataType> *tail;
    int count;
   
   
    SLinkedList()
    {
        head = 0;
        tail = 0;
        count = 0;
        
    }
   
    ~SLinkedList()
    {
        
        SListNode<DataType> *itr = head;
        SListNode<DataType> *next;
        while (itr != 0)
        {
            cout << "delete" <<endl;
            next = itr->m_next;
            
            delete itr;
            
            itr = next;
        }
    }
   
    void Append(DataType p_item)
    {
        
        if ( head == 0)
        {
            SListNode<DataType> * newnode = new SListNode<DataType>;
            newnode->m_data = p_item;
            head = tail =  newnode;
        }
        else
        {
            tail ->InsertAffter(p_item);
            tail  = tail ->m_next;
        }
        count ++;
        
    }

    void prepend(DataType p_item)
    {
        SListNode<DataType> *newnode = new SListNode<DataType>;
        newnode ->m_data  = p_item;
        newnode->m_next = head;

        head = newnode;
        if ( tail == 0 )
        {
            tail = head ;
        }
        count ++;

    }
    void Insert(SListIterator<DataType> &p_Iterator, DataType p_item)
    {
         if ( p_Iterator.m_list != this )
         {
             return ;
         }

        if ( p_Iterator.m_node != 0 )
        {

            p_Iterator.m_node->InsertAffter( p_item );

            if ( p_Iterator.m_node == tail )
            {
                tail = p_Iterator.m_node->m_next;
            }

            count ++;

        }
        else
        {
            Append( p_item );
            
        }


    }

    void Remove(SListIterator< DataType > &p_iterator )
    {
        



    }

    void RemoveHead()
    {

        SListNode<DataType> *node = 0;

        if ( head != 0 )
        {

            node = head->m_next ;
            delete head;
            head = node;


            if ( head == 0)
            {
                tail = 0;

            }
            count --;

        }


    }

    void RemoveTail()
    {

        SListNode<DataType> *node = 0;

        if ( head != 0 )
        {

            if ( head == tail )
            {
                delete tail;
                head = tail = 0 ;
            }
            else
            {

            

                node = head;
                while ( node->m_next != tail)
                {
                    node = node->m_next;
                }
                tail = node;
                delete node ->m_next;
               
            }

            count --;
        }
    }
SListIterator<DataType> GetItrerator()
     {
         
         return SListIterator<DataType>(this, head);

     }

     int size()
     {

         return count;

     }

     bool SaveToDisk(const char *name )
     {

         FILE *outfile = 0;
         SListNode<DataType> *itr = head;

         outfile = fopen( name, "wb");

         if ( outfile == 0)
         {
             return false;

         }

         fwrite( &count, sizeof(int), 1,outfile );

         while ( itr != 0 )
         {
             fwrite(&(itr->m_data), sizeof(DataType), 1, outfile );
             itr = itr ->m_next;
         }

         fclose( outfile );
         return true;


     }
     //


   
   
   
   
};

template<class DataType>
class SListIterator
{
    public:
        SListNode<DataType> *m_node;
        SLinkedList<DataType> *m_list;


        SListIterator(SLinkedList<DataType> *p_list, SListNode<DataType> * p_node)
        {
            m_list = p_list;
            m_node = p_node;
        }

        void Start()
        {
            if ( m_list != 0 )
            {
                m_node =  m_list->head ;
            }
        }
        void Forth()
        {

            if ( m_node != 0 )
            {

                m_node = m_node->m_next ;

            }

        }
        bool  Valid()
        {

           return (m_node != 0) ;

        }


        DataType &Item()
        {

        
            return m_node ->m_data;


        }







};

#endif
#include "SLinkedList.h"
#include <iostream.h>

void main()
{

     SLinkedList<int> list;
     SListNode<int> *p = NULL;
    SListIterator<int> it(0, 0);
     list.Append( 10 );
     list.Append ( 20 );
     list.Append ( 30 );
     list.prepend ( 5 );
     list.prepend ( 4 );


   


it= list.GetItrerator();

     for (it.Start();  it.Valid(); it.Forth())
     {
          cout << it.Item() << "\n" << endl;
         
     }
     list.SaveToDisk(" LoveYou.txt");


     while(1);







}
2009-09-04 21:50
missiyou
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:0 
还是C++好啊, 想想学C语言时,代码真的不想写,晕死了,


2009-09-04 21:52
快速回复:各位帮帮忙,我实在想不出错哪了!
数据加载中...
 
   



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

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