数据结构题目001

设字符集为字符和数字的集合,字符的顺序为A,B,C,…,Z,0,1,2,…,9,
请将下列字符串按字典顺序排列、存储:PAB,5C,PABC,CXY,CRSI,7,B899,B9
并分析可以采取的存储方案。

具体实现代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIZE 100

void swap(char *a,char *b)
{
    char swap;
    swap = *b;
    *b = *a;
    *a = swap;
}

int main()
{
    char a[MAXSIZE];
    char b[MAXSIZE] = {0};
    scanf("%s",a);
    int front = 0;
    int behind = MAXSIZE - 1;
    int i = 0;
    char c = a[0];
    do
    {
        if(c >= 'A' && c <= 'Z')
        {
                b[front] = c;  
                int a_front = front;
                while (c < b[front-1] && front >= 1)
                {
                    swap(&b[front-1],&c);
                    b[front] = c;
                    c = b[front - 1];
                    front--;
                }
                front = a_front + 1 ;
        }
        else if(c >= '0' && c <= '9')
        {
            b[behind] = c;
            int a_behind = behind;
            while (c >= b[behind + 1] && behind <= MAXSIZE - 2)
            {
                swap(&b[behind+1],&c);
                b[behind] = c;
                c = b[behind + 1];
                behind++;
            }
            behind = a_behind - 1 ;
        }
        else if(c == ',')
        {
            for(int j = 0;j < MAXSIZE;j++)
            {
                if(b[j] != 0)
                {
                    printf("%c",b[j]);
                    b[j] = 0;
                }
            }
            printf(",");
            front = 0;
            behind = MAXSIZE - 1;
        }
        else
        {
            printf("输入有误,请重新输入\n");
            system("pause");
        }
        i++;
        c = a[i];
    } while (c != '\0');
    for(int j = 0;j < MAXSIZE;j++)
    {
        if(b[j] != 0)
        {
            printf("%c",b[j]);
        }
    }
    system("pause");
    return 0;
}
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页