[使用getline获取整个字符串时程序崩溃

问题描述 投票:0回答:1

使C ++程序使用散列实现字典,但是在试图将含义作为整个句子输入时,程序崩溃了>]

#include <iostream>
using namespace std;

const int MAX=10;
class dictionary;
class node
{
      string key,value;
      node *next;
public:
      friend class dictionary;
      node()
      {
            next=NULL;
      }
      node(string key,string value)
      {
            this->key=key;
            this->value=value;
            next=NULL;
      }
};

class dictionary
{
      node *head[MAX];
public:
      dictionary()
{
            for(int i=0;i<MAX;i++)
                  head[i]=NULL;
}
      int hashf(string word);
      void insert(string,string);
      void find(string word);
      bool deleteWord(string word);
      void display();
};

哈希函数

int dictionary::hashf(string word)
{
      int asciiSum=0;
      for(int i=0;i<word.length();i++)
      {
            asciiSum=asciiSum+word[i];
      }
      return (asciiSum%10);
}

在词典功能中查找单词

void dictionary::find(string word)
{
      int index=hashf(word);
      int flag=0;
      node *start=head[index];
      while(start!=NULL)
      {

            if(start->key==word)
            {
                  flag=1;
                  break;
            }
            start=start->next;
      }
      if(flag==1)
            cout<<"Word Is  present.";
      else
            cout<<"Word Is not present.";
}

插入字典功能

void dictionary::insert(string word,string meaning)
{
      int index=hashf(word);
      node *p=new node(word,meaning);

      if(head[index]==NULL)
      {
            head[index]=p;
      }
      else
      {
            node *start=head[index];
            while(start->next!=NULL)
                  start=start->next;

            start->next=p;
      }

      cout<<endl<<word<<" inserted into dictionary at index"<<index;
}

从字典功能中删除

bool dictionary::deleteWord(string word)
{
      int index=hashf(word);
      node *tmp=head[index];
      node *par=head[index];
      if(tmp==NULL) //if no word is present at that index
      {
            return false;
      }
      if(tmp->key==word && tmp->next==NULL)//only one word is present
      {
            head[index]=NULL;
            delete tmp;
            return true;
      }
      //tmp=tmp->next;
      while(tmp->key!=word && tmp->next!=NULL)
      {
            par=tmp;
            tmp=tmp->next;
      }
      if(tmp->key==word&&tmp->next!=NULL)
      {
          if(par->key==tmp->key)
          {
              head[index]=tmp->next;
          }
          else
          {
            par->next=tmp->next;
            tmp->next=NULL;
          }
            delete tmp;
            return true;
      }
      else //delete at end
      {
            par->next=NULL;
            tmp->next=NULL;
            delete tmp;
            return true;
      }
      return false;
}

显示整个字典功能

void dictionary:: display()
{
      cout<<"\nIndex\t Key\t Value";
      for(int i=0;i<10;i++)
      {
            node *start=head[i];
            if(start==NULL)
                  cout<<"\n";
            while(start!=NULL)
            {
                  cout<<"\n:"<<i<<"\t"<<start->key <<"\t "<<start->value;
                  start=start->next;
            }
      }
}

主要电话

int main() {
      dictionary oxford;
      int choice;
      string word;
      string meaning;
      char ch='y';
      while(ch=='y')
      {
            cout<<"\n**** OXFORD DICTIONARY ****\n"
                        <<"1.Insert Word\n"
                        <<"2.Find Word\n"
                        <<"3.Delete Word\n"
                        <<"4.Display\n"
                        <<"Enter Your Choice :";
            cin>>choice;
            switch(choice)
            {
            case 1:
                  cout<<"Enter Word: ";
                  cin>>word;
                  cout<<"Enter Meaning: ";
                  getline(cin,meaning);
                  oxford.insert(word,meaning);

                  break;
            case 2:
                  cout<<"Enter Word to Search: ";
                  cin>>word;
                  oxford.find(word);

                  break;
            case 3:
                  cout<<"Enter Word to Delete: ";
                  cin>>word;
                  if(oxford.deleteWord(word))
                        cout<<" Word is deleted.";
                  else
                  {
                        cout<<"\nFailed to delete "<<word;
                  }
                  break;

            case 4:
                  cout<<"***Oxford Dictionary***";
                  oxford.display();
                  break;
            default:
                  cout<<"\nWrong Choice.";
            }
            cout<<"\nDo you want to continue(y/n)";
            cin>>ch;
            if(ch=='y')
            {
                continue;
            }
            else if(ch=='n')
            {
                cout<<"\nThank you for using our dictionary";
                break;
            }

      }

      return 0;
}

输出

**** OXFORD DICTIONARY ****
1.Insert Word
2.Find Word
3.Delete Word
4.Display
Enter Your Choice :1
Enter Word: john
Enter Meaning: 
john inserted into dictionary at index1
Do you want to continue(y/n)

使c ++程序使用散列实现字典,但是在试图将含义作为整个句子输入时,该程序使#include 使用命名空间std崩溃; ...

c++ string function hash
1个回答
0
投票

好消息:

它不会崩溃。它确实执行您对其进行编程的操作

© www.soinside.com 2019 - 2024. All rights reserved.