【问题标题】:Implementing Dictionary interface实现字典接口
【发布时间】:2017-07-04 17:03:36
【问题描述】:

这是 IEnumerable 接口,在其中添加一些单词并在最后两个方法中返回。

这里的想法是你有一个字典,它将每个单词与同义词列表相关联。 AddSynonyms 方法将枚举同义词并从创建唯一词列表开始,以防有任何重复。然后它遍历每个单词并检查它是否已经被添加。如果有,那么它会遍历该单词的所有同义词(单词列表减去单词)并检查它们是否在与该单词关联的列表中。如果不是,则将同义词添加到列表中。如果单词不在字典中,则将其与同义词列表一起添加(同样是单词列表减去单词)。

GetSynonymns 方法然后只返回与给定单词关联的列表。如果词不在同义词库中,则返回一个空枚举(如果需要,您可以更改它以引发异常)。

GetWords 方法只返回字典的所有键,即添加的每个单词。

summary>
/// Represents a thesaurus.
/// </summary>
public interface IThesaurus
{
/// <summary>
/// Adds the given synonyms to the thesaurus
/// </summary>
/// <param name="synonyms">The synonyms to add.</param>
void AddSynonyms(IEnumerable<string> synonyms);

/// <summary>
/// Gets the synonyms for a given word.
/// </summary>
/// <param name="word">The word the synonyms of which to get.</param>
/// <returns>A <see cref="string"/> with all synonyms for the given word.</returns>
IEnumerable<string> GetSynonyms(string word);

/// <summary>
/// Gets all words from the thesaurus.
/// </summary>
/// <returns>An <see cref="IEnumerable<string>"/> containing
/// all the words in the thesaurus.</returns>
IEnumerable<string> GetWords();

} }

这是我已经走了多远,但似乎卡在了这里。该程序将无法正确编译。我无法使用 obj.GetSynonyms(); 调用我的方法。在我的公共词库类的底部 public IEnumerable GetWords() { return lookup.Keys(); } } 不起作用我只是不明白为什么它不起作用。我无法真正发现代码的问题。

   namespace Thesaurus
 {
    public interface IThesaurus
    {
        IEnumerable<string> AddSynonyms();
        IEnumerable<string> GetSynonyms(string word);

        IEnumerable<string> GetWords();
}


public class Thesaurus : IThesaurus
{
    private Dictionary<string, List<string>> lookup =
        new Dictionary<string, List<string>>();

    public IEnumerable<string> AddSynonyms() //public void AddSynonyms(IEnumerable<string> synonyms)
    {
        var words = AddSynonyms.Distinct().ToList();
        foreach (var word in words)
        {
            var currentWordSynonyms = words.Where(s => s == word);
            if (lookup.ContainsKey(word))
            {
                foreach (var synonym in currentWordSynonyms)
                {
                    if (!lookup[word].Contains(synonym))
                        lookup[word].Add(synonym);
                }
            }
            else
            {
                lookup.Add(word.currentWordSynonyms);
            }

        }
    }

    public IEnumerable<string> GetSynonyms(string word)
    {
        if (lookup.ContainsKey(word))
            return lookup[word];
        return Enumerable.Empty<string>();
        // Or throw an exception.
    }

    public IEnumerable<string> GetWords()
    {
        return lookup.Keys();
    }
}

class MainClass
{

    static void Main()
    {
        // Declare an interface instance.
        IThesaurus obj = new Thesaurus();


        // Call the member.
        obj.AddSynonyms();
        obj.GetSynonyms();
        obj.GetWords();
    }
}

}

【问题讨论】:

  • 你卡在哪里了?不明显。
  • @jdweng 程序无法正确编译。我无法使用 obj.GetSynonyms(); 调用我的方法。在我的公共词库类的底部 public IEnumerable GetWords() { return lookup.Keys(); } } 不起作用 我只是不明白为什么它不起作用。我无法真正发现代码的问题。
  • 将评论中的信息添加到问题文本中可能是个好主意。
  • 欢迎来到 SO!请查看以下内容并适当地编辑您的问题:stackoverflow.com/help/how-to-ask
  • @garfbradaz 我已经阅读了指南,有什么特别需要改变的吗?是标题吗?我的问题?我不确定我还能做些什么来改善提出的问题。问候

标签: c# .net interface


【解决方案1】:

得到编译器错误:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Thesaurus
{


    class MainClass
    {

        static void Main(string[] args)
        {
            // Declare an interface instance.
            IThesaurus obj = new Thesaurus();
            List<string> words = new List<string>();

            // Call the member.
            obj.AddSynonyms(words);
            obj.GetSynonyms(words[0]);
            obj.GetWords();
        }
    }
    //<summary>
    /// Represents a thesaurus.
    /// </summary>
    public interface IThesaurus
    {
        /// <summary>
        /// Adds the given synonyms to the thesaurus
        /// </summary>
        /// <param name="synonyms">The synonyms to add.</param>
        void AddSynonyms(IEnumerable<string> synonyms);

        /// <summary>
        /// Gets the synonyms for a given word.
        /// </summary>
        /// <param name="word">The word the synonyms of which to get.</param>
        /// <returns>A <see cref="string"/> with all synonyms for the given word.</returns>
        IEnumerable<string> GetSynonyms(string word);

        /// <summary>
        /// Gets all words from the thesaurus.
        /// </summary>
        /// <returns>An <see cref="IEnumerable<string>"/> containing
        /// all the words in the thesaurus.</returns>
        IEnumerable<string> GetWords();
    }

    public class Thesaurus : IThesaurus
    {
        private Dictionary<string, List<string>> lookup =
            new Dictionary<string, List<string>>();



        public void AddSynonyms(IEnumerable<string> allWords) //public void AddSynonyms(IEnumerable<string> synonyms)
        {
            var words = allWords.Distinct().ToList();
            foreach (var word in words)
            {
                var currentWordSynonyms = words.Where(s => s == word);
                if (lookup.ContainsKey(word))
                {
                    foreach (var synonym in currentWordSynonyms)
                    {
                        if (!lookup[word].Contains(synonym))
                            lookup[word].Add(synonym);
                    }
                }
                else
                {
                    List<string> newSynonyms =  new List<string>();
                    newSynonyms.AddRange(currentWordSynonyms);
                    lookup.Add(word, newSynonyms);
                }

            }
        }

        public IEnumerable<string> GetSynonyms(string word)
        {
            if (lookup.ContainsKey(word))
                return lookup[word];
            return Enumerable.Empty<string>();
            // Or throw an exception.
        }

        public IEnumerable<string> GetWords()
        {
            return lookup.AsEnumerable().Select(x => x.Key).ToList();
        }
    }


}

【讨论】:

  • 当我尝试编译时,当 obj.GetSynonyms(words[0]);被调用。除非它有效。
  • 你有没有:使用 System.Collections。创建新项目时通常不是默认设置。
  • 您是否在列表中添加了单词?
  • 这听起来可能很愚蠢,但是如何将单词添加到列表中?
  • 它是你的项目。你的意见是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
相关资源
最近更新 更多