簡體   English   中英

C++ 如何使 function 在用戶輸入時停止

[英]C++ How to make a function stop upon user input

我正在為 class 創建一個圖靈機模擬器。 它接收轉換函數的文件,將這些轉換函數加載到向量中,然后接收一個單詞並檢查該單詞是否被接受或拒絕。 該程序在處理計算的操作 function 中使用 while 循環。 我們需要實現一個我們選擇的按鈕,該按鈕將在計算期間結束循環。 我將如何 go 關於這個?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <thread>
using namespace std;

bool able_to_stop = true;

class turing_sim{
     public:
          void fill_vecs(int & num, string & word){
               switch(num){
                    case 0: curr_states.push_back(word);
                            break;
                    case 1: curr_tape_sym.push_back(word);
                            break;
                    case 2: new_state.push_back(word);
                            break;
                    case 3: new_tape_sym.push_back(word);
                            break;
                    case 4: direction.push_back(word);
                            break;
               }
          }

          void fill_tape(string & n){
               vector<string> newtape;
               tape = newtape;
               for(int i = 0; i < n.length(); i++){
                    auto it = tape.begin();
                    tape.insert(it + i, n.substr(i, 1));
               }
               tape_ptr = 0;
               current_state = "0";
          }

          string operate(){
               bool rejected = false;
               while(current_state != "f" && !rejected && !stop_operate){
                    int sub = 0;
                    for(int i = 0; i < curr_states.size(); i++){
                         if(curr_states[i] == current_state){
                              if(curr_tape_sym[i] == tape[tape_ptr]){
                                   cout<<current_state<<" ";
                                   for(int j = tape_ptr; j < tape.size(); j++){
                                        cout<<tape[j]<<" ";
                                   }
                                   cout<<endl;
                                   current_state = new_state[i];
                                   tape[tape_ptr] = new_tape_sym[i];
                                   if(direction[i] == "R"){
                                        tape_ptr++;
                                        if(tape_ptr > (tape.size() - 1)){
                                             tape.push_back("B");
                                        }
                                   }
                                   else{
                                        tape_ptr--;
                                        if(tape_ptr < 0){
                                             auto it = tape.begin();
                                             tape.insert(it, "B");
                                             tape_ptr = 0;
                                        }
                                   }
                                   sub = 2;
                              }
                         }
                    }
                    if(sub == 0){
                         rejected = true;
                         cout<<current_state<<" ";
                         for(int i = tape_ptr; i < tape.size(); i++){
                              cout<<tape[i]<<" ";
                         }
                         cout<<endl;
                    }
               }
               if(current_state == "f") return "Accepted!";
               else return "Rejected...";
          }

     private:
          vector<string> curr_states;
          vector<string> curr_tape_sym;
          vector<string> new_state;
          vector<string> new_tape_sym;
          vector<string> direction;

          string current_state = "0";

          int tape_ptr = 0;

          vector<string> tape;

          bool stop_operate = false;
};

void Decompose_sentence(string& sentence, turing_sim & sim){//separates each line
     string wrd = "";                                       //into individual elements
     int count = 0;                                         //and stores them in vectors
     for(int i = 0; i < sentence.size(); i++){
          if(sentence.substr(i, 1) != " "){
               wrd += sentence.substr(i, 1);
          }
          else if(wrd != ""){
               sim.fill_vecs(count, wrd);
               wrd = "";
               count++;
          }
          if(sentence.substr(i + 1, 1) == "/") i = sentence.size() + 1;
     }
     if(!wrd.empty()){
          sim.fill_vecs(count, wrd);
     }
}

void read_file(string & file, turing_sim & sim){//loads important lines to decompose
     string sentence;
     ifstream infile;
     infile.open(file);
     while (getline(infile, sentence)) {
          while(sentence.back() == '\r' || sentence.back() == '\n'
               || sentence.back() == '.' || sentence.back() == char(34)){
                    sentence.erase(sentence.size() - 1);
          }
          if(sentence[0] != '/' && !sentence.empty()){
               Decompose_sentence(sentence, sim);
          }
     }
     infile.close();
}

int main(){
     turing_sim machine;
     string file, word;
     cout<<"Enter file name: ";
     cin>>file;
     read_file(file, machine);
     bool x = true;
     while(x){
          cout<<"Enter word to test: ";
          cin>>word;
          machine.fill_tape(word);
          cout<<machine.operate()<<endl;
     }
     return 0;
}

SetConsoleHandler()將為 windows 解決問題。

#ifdef _WIN32
BOOL WINAPI ControlHandler(DWORD dwCtrlType)
{
    switch (dwCtrlType) {
    case CTRL_C_EVENT:
        std::cout << "Going to terminate now\n";
        exit(0);

        /*case CTRL_BREAK_EVENT:
    case CTRL_CLOSE_EVENT:
    case CTRL_LOGOFF_EVENT:
    case CTRL_SHUTDOWN_EVENT:*/

    }
    return TRUE;
}

int main()
{
    SetConsoleCtrlHandler(ControlHandler, TRUE);
    //your code;
}
#endif

找到了一種跨平台的 c++ 方式:

#include "signal.h"    
void signal_handler(int signal)
{
  if(signal == 2) //2 here refer to ctrl+c event. you can replace it with integer of your choose
      exit(signal);
}

int main()
{
  // Install a signal handler
  signal(SIGINT, signal_handler);
  //YOUR code
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM