Bir ödev için basit durum makinesini programlamaya çalışıyorum. Ödev, giriş için bir dize verildiğinde, “Kedi” durumunda başlayıp bilgi tükenene kadar eylemler gerçekleştiren bir kod oluşturmaktır. Yapı türünün bir nesnesini bir if… else deyimine nasıl düzgün bir şekilde yerleştiririm?
İşte yapmak ne çalışıyorum betimlediği bir grafik verilmiştir:Şimdi, neredeyse kodu bitirdim ama bir işlev içinde bir problem var. Hata alıyorum "geçersiz işlenenler ikili ifadeye ('Devlet' ve 'Devlet')" Birisi bana bu sorunun nasıl düzeltileceğine dair bir ipucu verebilir ve neyin yanlış olduğuyla ilgili kısa bir açıklama yapabilir mi? Sorun, bir if..else ifadesindeki yapı türlerini kullanmaktır.
I) (int ana önce bu kod bölümünü vardır:
struct State{
string A, B;
};
State Cat = {"Meow", "Ignore"};
State Noise = {"Boing", "Thud"};
State Food = {"Lemons", "Cinnamon"};
State mystate = Cat;
//my_state.A -> string
Ve burada fonksiyon hatası olduğu:
void change_state(char c) {
// on taking character c, it changes current state
// If state is Cat and I get 1 , change to Food
// If state is Cat and I get 2 , change to Noise
// If state is Food and I get 1 , change to Noise
// If state is Food and I get 2 , change to Cat
// If state is Noise and I get 1 , change to Cat
// If state is Noise and I get 2 , change to Food
if (mystate == Cat){ //error
if (c == '1') {
mystate = Food;
}
else {
mystate = Noise;
}
}
else if (mystate == Food) {
if (c == '1') {
mystate = Noise;
}
else {
mystate = Cat;
}
}
else {
if (c == '1') {
mystate = Cat;
}
else {
mystate = Food;
}
}
}
Herhangi bir yardım takdir edilecektir!
'std :: bağı (left.A, left.B) == std :: bağlantı (right.A, right.B) 'açık olabilir . Eğer hiçbir şey işlenenler –
sebepten dolayı büyürse [KISS] tercih ederim (https://en.wikipedia.org/wiki/KISS_principle). – emlai
Bana daha basit görünüyor. –