Chinaunix首页 | 论坛 | 博客
  • 博客访问: 498750
  • 博文数量: 187
  • 博客积分: 3011
  • 博客等级: 中校
  • 技术积分: 2092
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-28 17:08
文章分类

全部博文(187)

文章存档

2011年(1)

2010年(8)

2009年(178)

我的朋友

分类: C/C++

2009-11-26 02:33:31

1、将一个字符串逆序
2、将一个链表逆序
3、计算一个字节里(byte)里面有多少bit被置1
4、搜索给定的字节(byte)
5、在一个字符串中找到可能的最长的子字符串
6、字符串转换为整数
7、整数转换为字符串
 
 
 
 
 
************************************************************************************
 
本站网友 时间:2006-12-25 23:33:03 IP地址:210.21.224.★
1、char *strconv(char *p)
{
    int i,length;
    char temp;
    length = strlen(p);
    for(i = 0;i < length/2;i++)
    {
        temp = *(p + i);
        *(p + i) = *(p + length - 1 - i);
        *(p +length - 1 - i) = temp;
    }
    return p;
}

int main()
{
    char src[100];
    char *p;
    scanf("%s",src);
    p = strconv(src);
    printf("%s\n",p);
    return 0;
}

  本站网友 时间:2006-12-26 12:24:08 IP地址:210.21.224.★
3、int cal(int data) //calculation the number of bit in one byte
{
    int a;
    int count = 0;
    a = data % 100;
    while (a != 0)
    {
        count += a % 2;
        a /= 2;
    }
    return count;
}

int main()
{
    int d,count;
    scanf("%d",&d);
    count = cal(d);
    printf("%d of one\n",count);
    return 0;
}

  本站网友 时间:2006-12-26 12:59:51 IP地址:210.21.224.★
4、#include
#include

void findmax(char *p)
{
    int j = 0,max = 0;
    int count = 0;
    char record[200];
    char recordmax[200];

    for(int i = 0;;i++)
    {
        
        if((*(p + i) == ' ') || (*(p + i) == '\0'))
        {        
            if(count > max)
            {
                max = count;
                record[j] = '\0';
                strcpy(recordmax,record);            
            }
            count = 0;
            j = 0;
        }
        else
        {
            record[j] = *(p + i);
            count ++;
            j ++;
        }
        if(*(p + i) == '\0')
            break;
    }
    printf("%s\n",recordmax);
}
            

        
int main()
{
    char str[]="zeng weidsfdsaf langd hah";
    printf("%s\n",str);
    findmax(str);
    return 0;
}

  本站网友 时间:2006-12-26 23:48:21 IP地址:210.21.224.★
#include
#include
#include

typedef struct shopping
{
    char goods[100];
    struct shopping *next;
}SHOP;

SHOP *buildlink()            //创建链表
{
    char goods[100];
    SHOP *head,*p,*h;
    p = (SHOP *)malloc(sizeof(SHOP));
    head = p;
    head->next = NULL;
    printf("Input three goods:");
    for(int i = 0;i < 3;i++)
    {
        scanf("%s",goods);
        p = (SHOP *)malloc(sizeof(SHOP));
        strcpy(p->goods,goods);
        p->next = NULL;
        if(head->next == NULL)
        {
            head->next = p;
            h = p;
        }
        else
        {
            h->next = p;
            h = h->next;
        }
    }
    return head;
}

void showlink(SHOP *head)  //显示链表
{
    SHOP *p;
    p = head->next;
    while(p != NULL)
    {
        printf("%s ",p->goods);
        p = p->next;
    }
    printf("\n");
}

SHOP *revlink(SHOP *head)    //反转链表
{
    SHOP *p,*newp;
    p = head->next;
    head->next = NULL;
    while(p != NULL)
    {
        newp = p;
        p = p->next;
        newp->next = head->next;
        head->next = newp;    
    }
    return head;
}

int delnode(SHOP *head,char *str)
{
    //delete success return 1,else return 0
    SHOP *q,*p = head;
    while(p->next != NULL)
    {
        q = p;
        p = p->next;        
        if(!strcmp(str,p->goods))
        {
            q->next = p->next;
            free(p);
            //p = q->next;
            return 1;
        }
    }
    return 0;
}
        
int main()
{
    SHOP *head;
    SHOP *newhead;
    head = buildlink();
    showlink(head);

    newhead = revlink(head);
    showlink(newhead);

    showlink(head);

    printf("Delete a node:");
    char str[50];
    scanf("%s",str);
    int i;
    i = delnode(head,str);
    showlink(head);
    if(i == 1)
        printf("Delete successful!\n");
    return 0;
}

  本站网友 时间:2006-12-26 23:48:32 IP地址:210.21.224.★
#include
#include
#include

typedef struct shopping
{
    char goods[100];
    struct shopping *next;
}SHOP;

SHOP *buildlink()            //创建链表
{
    char goods[100];
    SHOP *head,*p,*h;
    p = (SHOP *)malloc(sizeof(SHOP));
    head = p;
    head->next = NULL;
    printf("Input three goods:");
    for(int i = 0;i < 3;i++)
    {
        scanf("%s",goods);
        p = (SHOP *)malloc(sizeof(SHOP));
        strcpy(p->goods,goods);
        p->next = NULL;
        if(head->next == NULL)
        {
            head->next = p;
            h = p;
        }
        else
        {
            h->next = p;
            h = h->next;
        }
    }
    return head;
}

void showlink(SHOP *head)  //显示链表
{
    SHOP *p;
    p = head->next;
    while(p != NULL)
    {
        printf("%s ",p->goods);
        p = p->next;
    }
    printf("\n");
}

SHOP *revlink(SHOP *head)    //反转链表
{
    SHOP *p,*newp;
    p = head->next;
    head->next = NULL;
    while(p != NULL)
    {
        newp = p;
        p = p->next;
        newp->next = head->next;
        head->next = newp;    
    }
    return head;
}

int delnode(SHOP *head,char *str)
{
    //delete success return 1,else return 0
    SHOP *q,*p = head;
    while(p->next != NULL)
    {
        q = p;
        p = p->next;        
        if(!strcmp(str,p->goods))
        {
            q->next = p->next;
            free(p);
            //p = q->next;
            return 1;
        }
    }
    return 0;
}
        
int main()
{
    SHOP *head;
    SHOP *newhead;
    head = buildlink();
    showlink(head);

    newhead = revlink(head);
    showlink(newhead);

    showlink(head);

    printf("Delete a node:");
    char str[50];
    scanf("%s",str);
    int i;
    i = delnode(head,str);
    showlink(head);
    if(i == 1)
        printf("Delete successful!\n");
    return 0;
}

 原文地址 http://blog.chinaunix.net/u/28908/showart_220884.html
发表于: 2007-02-28 ,修改于: 2007-02-28 23:59,已浏览5125次,有评论2条 推荐 投诉


网友评论
内容:
计算一个字节里(byte)里面有多少bit被置1 
这个拿手~~呵呵
int check(char byte)
{
    int i=0,num=0;
    while(i++ <8) if(byte &0x1<     return num;
}

int main(void)
{
    char c = 0xff;
    return printf("Have %d bits.\n"i,check(c));
}
本站网友评论于:2008-08-27 11:26:01 (117.22.22.★)
内容:
楼上的有点问题吧,if(byte &0x1<
阅读(584) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~