古风自动造句器



小桥流水肯定有人家,
两人对坐必须在饮茶。
为何提笔再难描绘出风雅。  

一个C++古风自动写诗器。V1.1上线
【注意:有背景音乐!】

灵感来自
古风

今天看到这个,突然就想到文中提到的这个想法不难实现哇~那自己实现一个玩玩儿吧,主要就是个产生随机数的问题嘛~

V1.0

上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
void CPoetryDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
CStdioFile file;
CString strLine, strtemp;
char *single[68];
char *wanted[8];
if (!file.Open(_T("d:\\1.txt "), CFile::modeRead))
{
return;
}
int row = 0;
while (file.ReadString(strLine))
{
char *str = strLine.GetBufferSetLength(strLine.GetLength());
char *p;

int i = 0, k = 0;
char *buf;
if (strLine != "")
{
for (p = strtok_s(str, "、", &buf); p != NULL; p = strtok_s(NULL, "、", &buf))
{
single[i] = p;
i++;
}
}

CString strSum = "";
srand((unsigned)time(NULL));
for (int m = 0; m < 6; m++)
{
int j = random(68);
wanted[m] = single[j];
strtemp = wanted[m];
strSum= strSum + strtemp;
}
strSum = strSum + "\r\n";
for (int m = 0; m < 4; m++)
{
int j = random(68);
wanted[m] = single[j];
strtemp = wanted[m];
strSum = strSum + strtemp;
}
strSum = strSum + "\r\n";

for (int m = 0; m < 5; m++)
{
int j = random(68);
wanted[m] = single[j];
strtemp = wanted[m];
strSum = strSum + strtemp;
}
strSum = strSum + "\r\n";

for (int m = 0; m < 4; m++)
{
int j = random(68);
wanted[m] = single[j];
strtemp = wanted[m];
strSum = strSum + strtemp;
}
SetDlgItemText(IDC_EDIT2,strSum);
SetDlgItemText(IDC_EDIT1, "烟、倾、萧、墨、歌、弦、痴、泠、幽、离、覆、寂、情、思、恋、醉、浅、尘、念、葬、负、梦、韶、陌、落、琴、夜、君、殇、朱砂、韶华、江南、烟雨、迷离、红尘、寂寞、繁华、风华、苍老、红颜、天下、彼岸、伊人、青丝、白首、陌上、千年、倾城、断弦、长歌、悲欢、沧海、天涯、尘缘、情殇、忘川、碧落、奈何、指尖、长安、青鸾、凤凰、桃花、海棠、红妆、婆娑、轻狂、未央");

}

file.Close();


}

当然了,这个代码写的非常的粗糙,很多可以改进之处,我就是特别想先看到一个能运行的结果,后续再来慢慢改咯~丑陋的V1.0
1

2

P.S.哈哈哈哈哈哈好想笑这自动生成的诗句 这个时候背景音乐应该是浮夸

随便赋几首机器作诗:
未央忘川梦千年长安韶
尘缘君幽繁华
风华夜梦离红妆
海棠烟寂痴

海棠白首伊人悲欢负歌
红妆迷离苍老覆
韶华红妆尘缘萧寂寞
墨奈何歌白首

彼岸覆迷离浅烟雨伊人
长安倾城碧落倾
未央倾奈何韶华陌上
天下迷离朱砂千年

苍老覆红妆陌长安青鸾
繁华红尘落殇
迷离轻狂海棠思梦
恋倾凤凰浅

可以改进的地方:
1.分成动词、名词 单字、双字 放在多个txt中读取
2.四个暴力循环可以改一改……看着太扎心了
3.可以增加重复词审查过滤
4.扩大词库
5.按现代诗、古诗字数韵律等分配每个循环里的约束条件

V1.1

因为数组不定长,换成vector实现。同时扩大了词库,换了种句式……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
CStdioFile file;
CString strLine, strtemp;
std::vector<CString>Word;
std::vector<CString>Selected;
if (!file.Open(_T("d:\\1.txt "), CFile::modeRead))
{
ASSERT(FALSE);
return;
}
while (file.ReadString(strLine))
{
int len = strLine.GetLength();

char *str = strLine.GetBufferSetLength(strLine.GetLength());
char *p;

int i = 0, k = 0;
char *buf;
if (strLine != "")
{
for (p = strtok_s(str, "、", &buf); p != NULL; p = strtok_s(NULL, "、", &buf))
{
Word.push_back(p);
i++;
}
}

CString strSum = "";
srand((unsigned)time(NULL));
for (int m = 0; m < 3; m++)
{
int j = random(i);
Selected.push_back(Word.at(j));
strtemp = Selected.at(m);
strSum= strSum + strtemp;
}
strSum = strSum + ",";
for (int m = 3; m < 7; m++)
{
int j = random(i);
Selected.push_back(Word.at(j));
strtemp = Selected.at(m);
strSum = strSum + strtemp;
}
strSum = strSum + ",";
strSum = strSum + "\r\n";
for (int m = 7; m < 10; m++)
{
int j = random(i);
Selected.push_back(Word.at(j));
strtemp = Selected.at(m);
strSum = strSum + strtemp;
}
strSum = strSum + ",";
for (int m = 10; m < 13; m++)
{
int j = random(i);
Selected.push_back(Word.at(j));
strtemp = Selected.at(m);
strSum = strSum + strtemp;
}
strSum = strSum + ",";
strSum = strSum + "\r\n";
for (int m = 13; m < 18; m++)
{
int j = random(i);
Selected.push_back(Word.at(j));
strtemp = Selected.at(m);
strSum = strSum + strtemp;
}
strSum = strSum + ",";
for (int m = 18; m < 20; m++)
{
int j = random(i);
Selected.push_back(Word.at(j));
strtemp = Selected.at(m);
strSum = strSum + strtemp;
}
strSum = strSum + ",";
strSum = strSum + "\r\n";
for (int m = 20; m < 23; m++)
{
int j = random(i);
Selected.push_back(Word.at(j));
strtemp = Selected.at(m);
strSum = strSum + strtemp;
}

strSum = strSum + "?";
strSum = strSum + "\r\n";
for (int m = 23; m < 25; m++)
{
int j = random(i);
Selected.push_back(Word.at(j));
strtemp = Selected.at(m);
strSum = strSum + strtemp;
}
strSum = strSum + "。";


SetDlgItemText(IDC_EDIT2,strSum);
TCHAR szBuf[1264];
FILE *pfile;
fopen_s(&pfile,"d:\\library.txt", "r");
fread(szBuf, 1, 1262, pfile);
SetDlgItemText(IDC_EDIT1, szBuf);

fclose(pfile);

}

file.Close();

V1.1

字符串尾的烫烫烫烫烫还没解决

随机散文诗:
环佩离海棠,悲欢情墨红颜,
古道回推敲,萧瑟繁华梳妆,
寂寥绮罗簌簌桑麻墨,青梅尘缘,
花蕊墨城郭?
白露陌上。

烟读碧绿,顾青鸾青丝古墓,
楚楚故里尺素,念情殇柳眉,
伊人亮尘缘挥毫古墓,簌簌伽蓝,
寂寥萧尘埃?
桑麻知否。

枝桠年华尘埃,经纬竹婆娑伽蓝,
一觅古筝笑靥,沧海寺轩窗,
泠五岳勾勒磐石江,桑麻寂,
一觅知否袅袅?
萧瑟瑶池。

羌笛绮罗剑鞘,枯藤乌艄断弦琴,
绮罗朱砂萧,寺参差寒蝉,
一抹长安水墨朱砂踌躇,寂凤凰,
渔樵红烛垂钓?
长歌芭蕉。

哈哈哈哈哈看起来好有文化的样子

附上一首吐槽古风乱象的歌:

文章目录
  1. 1. V1.0
  2. 2. V1.1
|