CS106B Assignment #2 Part A : Random writing

编程入门 行业动态 更新时间:2024-10-22 11:38:12

CS106B <a href=https://www.elefans.com/category/jswz/34/1769111.html style=Assignment #2 Part A : Random writing"/>

CS106B Assignment #2 Part A : Random writing

CS106B Assignment #2 Part A : Random writing and Markov models of language

任务地址:.pdf

基于马尔可夫链的随机文段产生,而且可以自己设置马尔可夫阶数

#include <iostream>
#include "genlib.h"
#include "simpio.h"
#include "map.h"
#include "vector.h"
#include "random.h"void CreateMap(string &line);
string GetSeed();
void GenerateText(string seed);const int MAX_CHARS = 2000;Map<Vector<char> > seedMap;
Vector<string> allseeds; // 获取全部seed用于GetSeed函数
int orderK;void main() {Randomize();//获得马尔可夫链阶数kwhile(true) {cout << "What order of analysis? (a number from 1 to 10): ";orderK = GetInteger();if (orderK >= 1 && orderK <= 10) break;cout << "Invalid number" << endl;}//自己输入的textstring text;text="We all stood watching, smiling and laughing as they darted past the cars and. They held their shopping bags over their heads just in case. They got soaked. But they were followed by a few who screamed and laughed like children all the way to their cars. And yes, I did. I ran. I got wet. I needed washing.Circumstances or people can take away your material possessions, they can take away your money, and they can take away your health. But no one can ever take away your precious memories. So, don't forget to make time and take the opportunities to make memories every day!Now some would laugh it off and scold her for being silly. Some might even ignore what was said. But this was a moment of affirmation in a young child's life. Time when innocent trust can be nurtured so that it will bloom into faith. Honey, you are absolutely right. Let's run through the rain. If get wet, well maybe we just needed washing.Mom said. Then off they ran.The entire crowd stopped dead silent. I swear you couldn't hear anything but the rain. We all stood silently. No one came or left in the next few minutes. Mom paused and thought for a moment about what she would say.Don't you remember? When you were talking to Daddy about his cancer, you said, If God can get us through this, he can get us through anything!";//创建MapCreateMap(text);//选择最初始的seedstring seed = GetSeed();//把seed输入,生成文章GenerateText(seed);// clearseedMap.clear();allseeds.clear();
}void CreateMap(string &line) {for(int i= 0; i< line.size() - orderK ; i++){string seed = line.substr(i, orderK); //种子allseeds.add(seed); //将种子加入全部种子的vector中if(i< line.size() - orderK){   //在for循环里还需判断防止越界Vector<char> temp;   // 中间变量char c= line[i+orderK];  //vector 的 待加 valueif(seedMap.containsKey(seed)){   //如果map已经有,则需要取出vector加入c进去,用temp做中间变量temp = seedMap.getValue(seed);temp.add(c);seedMap.add(seed,temp);}else{temp.add(c);seedMap.add(seed,temp);   //如果map没有,则直接加入map}}}
}string GetSeed() {Map<int> seedcount;int count;string seed;//制作包含次数的map 操作不变,仍是如果有,取出加1,没有则直接加入for (int i = 0;i< allseeds.size ();i++){if (seedcount.containsKey(allseeds[i])){count = seedcount.getValue(allseeds[i]);count++;seedcount.add(allseeds[i],count);}else{seedcount.add(allseeds[i],1);}}//用迭代器,找出seedcount中value最大的key,  也可以用for循环遍历Map<int>::Iterator itr = seedcount.iterator();int max=0;while (itr.hasNext()) {string key = itr.next();if(seedcount.getValue(key) > max){max = seedcount.getValue(key);seed = key;}}return seed;
}void GenerateText(string seed) {Vector<char> seedvector;  //用雨取出key的valuecout << seed;for(int i=0; i<MAX_CHARS; i++){if(seedMap.containsKey(seed))seedvector = seedMap.getValue(seed);else break;  //说明是结尾,没有下一个值int size = seedvector.size();if (size==0) break;int rand = RandomInteger(0,size-1); //随机选择下一个字符char c = seedvector[rand];cout << c;seed.erase(0,1); //删除首字符seed+= c;        //添加尾字符}cout<< endl <<endl;
}

结果展示:
 

What order of analysis? (a number from 1 to 10): 4they can take time and take time and. They can take away you remember? When ignore what washing.Circumstances or people cars and scold her forget to their health. But no one can take away your health. But no one cars and take away your material possessions, their health. But no one can take time and the can ever the rain. I got soaked. But no one can take away you are absolutely right ever they got soaked. But this washing bags over their shopping bags over their shopping and scold her forget to they held they got soaked. But no one can take time and take away your health. But no one cars and. They got soaked.
What order of analysis? (a number from 1 to 10): 5can take away your material possessions, they can take away your material possessions, they darted past the rain. If get wet. I needed washing.Circumstances or people can get wet. I needed washing.Circumstances or people can be nurtured so that it will bloom into faith. Honey, and scold her for being silly. Some might even ignore what was said. But no one can take away your material possessions, they darted past they can take away your heads just in case. They held their shopping bags over their shopping bags over their heads just in case. They held their heads just in case. They held their heads just in case. They held their shopping bags over their shopping bags over their heads just in case.

更多推荐

CS106B Assignment #2 Part A : Random writing

本文发布于:2024-03-14 15:12:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1736743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Assignment   CS106B   Part   writing   Random

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!