标题:C语言随机生成字符串并进行冒泡排序
只看楼主
z15039649647
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-10-26
结帖率:0
已结贴  问题点数:20 回复次数:3 
C语言随机生成字符串并进行冒泡排序
C语言随机生成字符串并进行冒泡排序
搜索更多相关主题的帖子: 生成 冒泡排序 随机 字符串 C语言 
2021-10-26 19:09
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:10 
https://blog.
+
https://blog.
2021-10-26 19:24
z15039649647
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-10-26
得分:0 
这两个怎样结合
2021-10-26 19:36
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
回复 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编辑过]

2021-10-26 20:12
z15039649647
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-10-26
得分:0 
,优秀
2021-10-26 20:20
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
//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编辑过]

2021-10-27 07:19
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
得分:10 
以下是引用自由而无用在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扩展

https://zh.
2021-10-27 09:56
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
回复 7楼 lin5161678
Thank you for your help
2021-10-27 10:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-507301-1-1.html




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

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