cs106b2017spring Word Ladder

括号灾难。。不堪卒读

#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include "console.h"
#include "lexicon.h"
#include "filelib.h"
#include "queue.h"

using namespace std;

string promptForFilename(string prompt);
void promptUserForWord(string &w1, string &w2, Lexicon &english);

int main() {
    cout<<"Welcome to CS 106B Word Ladder.\n"
            "Please give me two English words, and I will change the\n"
         <<"first into the second by changing one letter at a time.\n";
    string filename;
    filename = promptForFilename("\nDictionary file name? ");
    Lexicon english(filename);
    string w1, w2;
    while (true){
    promptUserForWord(w1, w2, english);

    //创建一个queue,内部类型是stack
    Queue<Stack<string>> queue;
    Stack<string> stack1;
    stack1.push(w1);
    queue.enqueue(stack1);
    Lexicon track;
    Stack<string> stack_copy;
    Stack<string> partial_ladder_stack;
    Stack<string> ladder;//最短路径
    string top_copy;
    while (!(queue.isEmpty())){
        partial_ladder_stack = queue.dequeue();//取出队列尾部的stack
        string top = partial_ladder_stack.peek();//从上一个探索的节点开始继续
        for (int i=0;i<top.length();i++){
            top_copy=top;//对单词副本进行字母替换
            for (char j=97;j<123;j++){
                top_copy[i]=j;//挨个替换单词字母
                if (english.contains(top_copy)){//是否合法单词
                    if (top_copy==w2){//找到一条ladder,判断ladder是否更短
                        if (ladder.isEmpty()){ladder = partial_ladder_stack; ladder.push(w2);}
                        if (ladder.size()>(partial_ladder_stack.size()+1)){
                            ladder = partial_ladder_stack; ladder.push(w2);
                        }
                    }
                    if (!(track.contains(top_copy))){//未包含在已有ladder中
                        track.add(top_copy);//储存新找到的单词
                        stack_copy = partial_ladder_stack;//复制当前stack
                        stack_copy.push(top_copy);//添加新找到的单词(节点)
                        queue.enqueue(stack_copy);//加到队列尾部
                    }
                }
            }
        }
    }
    cout<<"A ladder from "<<w2<<" back to "<<w1<<": \n";
    while (!(ladder.isEmpty())){cout<<ladder.pop()<<" ";}
    cout<<endl;
    }
}

/*
 * usage: string filename = promptForFilename(string prompt)
 * ask user to input filename, ask user input again if file not exist.
 * return filename.
*/
//build后字典文件会被复制到当前文件夹
string promptForFilename(string prompt){
    while (true){
        string cur_dir = getCurrentDirectory();
        string filename;
        cout<<prompt;
        getline(cin, filename);
        filename = cur_dir + "\\" + filename;
        if (fileExists(filename)){
            return filename;
        }
        cout<<"Unable to open that file.  Try again."<<endl;;
    }
}

//get word w1,w2.
//为了使用exit函数,要包含<stdlib.h>
void promptUserForWord(string &w1, string &w2, Lexicon &english){
    cout<<endl;
    while (true){
        cout<<"Word #1 (or Enter to quit): ";//不需要\n。getline函数,用户按下回车键的换行会被打印出来。。
        getline(cin,w1);
        //------满足按enter退出,getline会读取到一个空字符---
        if (w1.empty()){cout<<"Have a nice day."; exit(0);}
        cout<<"Word #2 (or Enter to quit): ";
        getline(cin,w2);
        if (w2.empty()){cout<<"Have a nice day."; exit(0);}
        w1 = toLowerCase(w1);
        w2 = toLowerCase(w2);
        //------需要检测用户输入的单词是否合法。不管大小写,要在字典里面-------
        if (w1.length()!=w2.length()){
            cout<<"\nThe two words must be the same length."<<endl;
            continue;
        }
        if (w1==w2){
            cout<<"\nThe two words must be different."<<endl;
            continue;
        }
        if (!(english.contains(w1)&&english.contains(w2))){
            cout<<"\nThe two words must be found in the dictionary."<<endl;
            continue;
        }
        break;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值