当前位置: 代码迷 >> C语言 >> 统计各种类型字符的个数
  详细解决方案

统计各种类型字符的个数

热度:408   发布时间:2008-06-16 13:37:07.0
统计各种类型字符的个数
编一程序:输入一段字符,并统计各种类型字符的个数,比如整形有多少个,字符形有多少个....
搜索更多相关的解决方案: 字符  类型  统计  

----------------解决方案--------------------------------------------------------
作业自己做,不懂就看书



[color=white]
----------------解决方案--------------------------------------------------------
给个小小的提示好吗?怎么让程序区分不同类型数据
----------------解决方案--------------------------------------------------------
你先循环每个字符,然后判断其是数字还是字母还是符号还是其他类型的数据,当遇到一个字符的类型与前一个不一样时,就作一个记号。(对16进制数要另外处理)
以此类推

[[it] 本帖最后由 flyue 于 2008-6-16 18:10 编辑 [/it]]
----------------解决方案--------------------------------------------------------
[quote][bo][un]flyue[/un] 在 2008-6-16 14:20 的发言:[/bo]

然后判断其是数字还是字母还是符号还是其他类型的数据对16进制树要另外处理)


怎么判断类型不同呢
----------------解决方案--------------------------------------------------------
哎,,这贴不能就这样沉下去啊,,
----------------解决方案--------------------------------------------------------
怎么判断?
你买了C语言的书后面一定有ASCII码表,你自己根据表来判断字符在哪个范围,从而知道了此字符是什么类型啊
----------------解决方案--------------------------------------------------------
用计数排序的思想做看看....
----------------解决方案--------------------------------------------------------
我的 代码着色器 就是按我4#那样做的,个人感觉效果不错:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define null 0

struct node/* The struct for the information of a student. */
{
    int num;
    char name[8];
    float score;
    struct node *next;
};
typedef struct node node;

node *findnum(node *head, int snum) /* Finding the needed num from the database*/
{
    node *t;
    t = head->next;
    while(t&&(t->num != snum))
        t = t->next;
    return t;
}

node *findname(node *head, char s[]) /* Finding the needed name from the database*/
{
    node *t;
    t = head->next;
    while(t&&strcmp(t->name, s))
        t = t->next;
    return t;
}

void findnumout(node *head, int snum)/*Find the record according to num and print it out*/
{
    node *t;
    int sign = 1;
    t = head->next;
    puts("\nFinding:");
    while(t)
    {
        if(t->num == snum)
        {
            printf("The student you want to find:  num:%d, name:%s, score:%f\n", t->num, t->name, t->score);
            sign = 0;
        }
        t = t->next;
    }
    if(sign)
        puts("It is wrong!Not find!\n");
}

void findnameout(node *head, char s[])/*Find the record according to name and print it out*/
{
    node *t;int sign = 1;
    t = head->next;
    puts("\nNow it is finding:");
    while(t)
    {
        if(!strcmp(t->name, s))
        {
            printf("The student you want to find:  num:%d, name:%s, score:%f\n", t->num, t->name, t->score);
            sign = 0;
        }
        t = t->next;
    }
    if(sign)
        puts("It is wrong!Not find!\n");
}

node *findp(node *head, node *sp)
{
    node *t;
    t = head;
    while(t&&t->next != sp)
        t = t->next;
    return t;
}

node *creatnode()   /* Creating a database .It is a linked-list with a header */
{
    node *head, *t, *p;char c;/* When the "name" received comes to "0", the creation is over.*/
    head = (node*)malloc(sizeof(node));
    head->next = null;
    t = head;
    p = (node*)malloc(sizeof(node));
    p->next = null;
    puts("Input the num:");
    scanf("%d", &p->num);
    puts("Input the name:(within 8 words)");
    scanf("%s", p->name);c = getchar();
    puts("Input the score:");
    scanf("%f", &p->score);
    while(p->name[0] != '0')/*The "name" equal to "0" stands for the creation-finished sign.*/
    {
        t->next = p;p->next = null;
        t = t->next;
        p = (node*)malloc(sizeof(node));
        puts("Input the num:");
        scanf("%d", &p->num);
        puts("Input the name:(within 8 words)");
        scanf("%s", p->name);c = getchar();
        puts("Input the score:");
        scanf("%f", &p->score);
    }
    return head;
}

void insert(node *head, int snum)/* Inserting a record into the database. */
{
    node *p, *t;char c; /* The function inserts the record into the database according to "num". */
    p = (node*)malloc(sizeof(node));
    p->next = null;
    t = findnum(head, snum); /* Find the record whose "num" equals to "snum".*/
    puts("Inserting!\n");
    puts("Input the num:");
    scanf("%d", &p->num);
    puts("Input the name:(within 8 words)");
    scanf("%s", p->name);c = getchar();
    puts("Input the score:");
    scanf("%f", &p->score);
    if(t)
    {
        if(t->next != null)
        {
            p->next = t->next;t->next = p;
        }
        else
            t->next = p;
    }
    else puts("\nIt is wrong!\n");
}

void deletenode(node *head, int snum)/* Deleting a record from the database. */
{
    node *t;                      /*The function deletes the record according to "num". */
    t = head;
    while(t->next&&(t->next->num != snum))/*Find the previous record of the "snum"-record */
        t = t->next;
    if(t->next)
        t->next = t->next->next;
    else puts("\nIt is wrong!\n");
}

void print(node *head)     /* Printing all the records. */
{
    node *t;t = head->next;
    while(t)
    {
        printf("The num:%d, name:%s, score:%f\n", t->num, t->name, t->score);t = t->next;
    }
    free(t);
}

void sortnum(node *head)/*Sort all the nodes using the algorithm of bubble*/
{
    node *a, *b;int sign;   /*According to the nums */
    while(a)
    {
        a = head->next;
        b = a->next;
        sign = 1;
        while(b)
        {
            if(a->num>b->num)
            {
                findp(head, a)->next = b;a->next = b->next;b->next = a;b = a->next;sign = 0;
            }
            else {
                a = b;b = b->next;
            }
        }
        if(sign)
            break;
    }
}

void sortscore(node *head)/*Sort all the nodes using the algorithm of bubble*/
{
    node *a, *b;int sign;     /*According to the score */
    while(a)
    {
        a = head->next;
        b = a->next;
        sign = 1;
        while(b)
        {
            if(a->score>b->score)
            {
                findp(head, a)->next = b;a->next = b->next;b->next = a;b = a->next;sign = 0;
            }
            else {
                a = b;b = b->next;
            }
        }
        if(sign)
            break;
    }
}

void main()
{
    node *head;
    head = creatnode();
    puts("\nPrevious database is:\n");
    print(head);
    insert(head, 12);/*Insert a record after the node whose num is 12*/
    printf("After the action of inserting, the database is:\n");
    print(head);
    deletenode(head, 11);/*Delete the record whose num is 11 */
    puts("\nHaving been deleted, the database now is:\n");
    print(head);
    puts("\nTake the action of Sorting according to the num:\n");
    sortnum(head);
    print(head);
    puts("\nTake the action of Sorting according to score:\n");
    sortscore(head);
    print(head);
    findnumout(head, 12);/*Check out the student whose num is 12. */
    findnameout(head, "huhl");/*Check out the student whose name is huhl */
}

----------------解决方案--------------------------------------------------------
呵呵,感觉颜色有点单调...
----------------解决方案--------------------------------------------------------