注册 登录
编程论坛 C语言论坛

C语言随机生成字符串并进行冒泡排序

z15039649647 发布于 2021-10-26 19:09, 1733 次点击
C语言随机生成字符串并进行冒泡排序
3 回复
#2
自由而无用2021-10-26 19:24
https://blog.
+
https://blog.
#3
z150396496472021-10-26 19:36
这两个怎样结合
#4
自由而无用2021-10-26 20:12
回复 3楼 z15039649647
oh, good question, emmmm.......
lets suppose(just assumption) that you are now a famous director of a tv drama
if you want to make the drama to be enough excellent, high ratings, what should you do
set a campy plot? nah~~~
lets come back to the code again, of course, we have to master them separately
and then making them fall in love again, so pls dont fail to them, thats the way

[此贴子已经被作者于2021-10-26 20:15编辑过]

#5
z150396496472021-10-26 20:20
,优秀
#6
自由而无用2021-10-27 07:19
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>
#include <stdlib.h>


 /***********************************************************************

 * open source license: wtfpl                                           *

 * important declaration:                                               *

 * this section is only for fun but it should be taken away from:       *

 * I.   solo young c-player (under 18)                                  *

 * II.  non-experienced c-player (unable to master unpredictable cases) *

 * III. illegal using (out of law)                                      *

 * IV.  <Blank note for future>                                         *

 **********************************************************************
*/

#define INC_STR
#ifdef INC_STR
#define V_STR "abcdefghijklmnopqrstuvwxyz"
#endif
#ifdef INC_DIG
#define V_DIG "0123456789"
#endif
#ifdef INC_SPEC
#define V_SPEC "!@#$%^&*-_=+"
#endif

#define swap(x, y)      \
        {               \
            char t;     \
            t = x;      \
            x = y;      \
            y = t;      \
        }
#define FS2B
#ifdef FS2B
#define DIR(_p, _n) _p > _n
#else
#define DIR(_p, _n) _p < _n
#endif
#define STUB 0
#define g_data ((unsigned char *)__TIME__)[idx + _idx] % 26
#define m_data ((unsigned char *)__DATE__)[i] % 10

char* gen_sort(char *s, int len, int _idx)
{
    static int idx;
    int i, j;
   
    //gen
    for (i = 0; i < len; i++, idx -= len)
        s[i] = V_STR[g_data];
    //print
    puts(s);
    //bubble_sort
    for (i = 0; i < len - 1; i++)
        for (j = 0; j < len - i - 1; j++)
#define GCC_FEATURE(_expr) (_expr)
            DIR(s[j], s[j + 1]) ? GCC_FEATURE(swap(s[j], s[j + 1])) : STUB;
    //print
    puts(s);
    puts("---------------------");

    return s;
}

int main(int argc, char *argv[])
{
    static char s[10];
    int i;
#ifdef CHK_ASLR
    system("sysctl -n kernel.randomize_va_space");
    printf("addr_i = %p\n", &i);
#endif
    for (i = 0; i < 10; i++)
#define rand_s(_stack) ((_stack) & 0xff)
        gen_sort(s, sizeof(s) - 1,  m_data + rand_s((long int)&i));
   
    return 0;
}


output sample:
tdacanpfn
aacdfnnpt
---------------------
ylsbhugjc
bcghjlsuy
---------------------
fuajjcaca
aaaccfjju
---------------------
ydbiwahug
abdghiuwy
---------------------
rmrvbvysm
bmmrrsvvy
---------------------
rsbuyjurs
bjrrssuuy
---------------------
uurrayukk
akkrruuuy
---------------------
maaybiwkr
aabikmrwy
---------------------
jbhgvuanj
abghjjnuv
---------------------
vaqqmvydz
admqqvvyz
---------------------

[此贴子已经被作者于2021-10-27 10:09编辑过]

#7
lin51616782021-10-27 09:56
以下是引用自由而无用在2021-10-27 07:19:26的发言:

//online parser: https://www.bccn.net/run/
#include <stdio.h>
#include <stdlib.h>
 /***********************************************************************
 * open source license: wtfpl                                           *
 * important declaration:                                               *
 * this section is only for fun but it should be taken away from:       *
 * I.   solo young c-player (under 18)                                  *
 * II.  non-experienced c-player (unable to master unpredictable cases) *
 * III. illegal using (out of law)                                      *
 * IV.  <Blank note for future>                                         *
 ***********************************************************************/

#define INC_STR
#ifdef INC_STR
#define V_STR "abcdefghijklmnopqrstuvwxyz"
#endif
#ifdef INC_DIG
#define V_DIG "0123456789"
#endif
#ifdef INC_SPEC
#define V_SPEC "!@#$%^&*-_=+"
#endif

#define swap(x, y)      \
        {               \
            char t;     \
            t = x;      \
            x = y;      \
            y = t;      \
        }
#define FS2B
#ifdef FS2B
#define DIR(_p, _n) _p > _n
#else
#define DIR(_p, _n) _p < _n
#endif
#define STUB 0
#define g_data ((unsigned char *)__TIME__) % 26
#define m_data ((unsigned char *)__DATE__) % 10

char* gen_sort(char *s, int len, int _idx)
{
    static int idx;
    int i, j;
   
    //gen
    for (i = 0; i < len; i++, idx -= len)
        s = V_STR[g_data];
    //print
    puts(s);
    //bubble_sort
    for (i = 0; i < len - 1; i++)
        for (j = 0; j < len - i - 1; j++)
            DIR(s[j], s[j + 1]) ? (swap(s[j], s[j + 1])) : STUB;
    //print
    puts(s);
    puts("---------------------");

    return s;
}

int main(int argc, char *argv[])
{
    static char s[10];
    int i;
#ifdef CHK_ASLR
    system("sysctl -n kernel.randomize_va_space");
    printf("addr_i = %p\n", &i);
#endif
    for (i = 0; i < 10; i++)
#define rand_s(_stack) ((_stack) & 0xff)
        gen_sort(s, sizeof(s) - 1,  m_data + rand_s((long int)&i));
   
    return 0;
}

output sample:
tdacanpfn
aacdfnnpt
---------------------
ylsbhugjc
bcghjlsuy
---------------------
fuajjcaca
aaaccfjju
---------------------
ydbiwahug
abdghiuwy
---------------------
rmrvbvysm
bmmrrsvvy
---------------------
rsbuyjurs
bjrrssuuy
---------------------
uurrayukk
akkrruuuy
---------------------
maaybiwkr
aabikmrwy
---------------------
jbhgvuanj
abghjjnuv
---------------------
vaqqmvydz
admqqvvyz
---------------------

DIR(s[j], s[j + 1]) ? (swap(s[j], s[j + 1])) : STUB;
 (swap(s[j], s[j + 1])) 是gcc的表达式语句
这是gcc扩展
不是不能用
至少应该加个提示说明
讲清楚 这是gcc扩展
#8
自由而无用2021-10-27 10:08
回复 7楼 lin5161678
Thank you for your help
1