Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [C++] Problema con l' uso dello switch

    Ho un main in cui devo valutare delle operazioni da eseguire con l' istruzione switch ma non funziona:

    codice:
    #include <cstdlib>
    #include "Stack.hpp"
    #include <stdio.h>
    #include <iostream> 
    
    
    using namespace std;
    
    /*
     * 
     */
    int main(int argc, char** argv) {
        
        int op;   //č un numero che indica l' operazione da svolgere
        Stack *myStack=new Stack();
        
        cout << "Che operazione vuoi eseguire?\n" << endl;
        cout << "1)push: 2)pop 3)top: 4)Fornisci dimensione:\n";
        cin >> op;
        bool finito=false;
        
        while(!finito)
        {
        
             switch(op)
             {
                 case "1":
                     cout << "Che numero vuoi inserire?\n" << endl;
                      cin >> op;
                     myStack->push(op);
                 break;
                 
                 case "2":
                    myStack->pop();
                 break;
                
                 case "3":
                    myStack->Top();
                 break;
                    
                 case "4":
                 myStack->getsize();
                 break;
                
                 default:
                   cout << "Errore...il programma verrą terminato...\n" <<endl;
                        finito=true;
                 break;
                 
                  cout << "Vuoi continuare? 1)si 2)no\n" << endl;
                      cin >> op;
                      
                 if(op==2)
                     finito=true;
                      
                 else if(op!=1 || op!= 2)
                     cout<<"Errore!!! Il programma verrą terminato\n"<<endl;
             }
        }
    }
    Ora....cosģ non compila proprio...se perņ scrivo i vari case 1, case 2..eccetera senza virgolette all' esecuzione non funziona...cosa non va?

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,472
    Intanto se la variabile č numerica intera, non puoi usare "1" o "2" nei case (non potresti comunque ma lasciamo perdere ...)

    Quindi deve essere case 1 case 2 ...

    Ma che vuol dire "non funziona"?

    E come mai ci sta del codice dopo il break del default all'interno dello switch??
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Ho fatto alcune correzioni, probabilmente quelle istruzioni dopo il break causavano il problema, praticamente quando inserivo il numero dell' istruzione andava a capo senza fare nulla....ora funziona. Una cosa sola, questo č una parte del vecchio esercizio sullo stack che avevo postato, non so se ricordi, non so come usare i distruttori, ho provato a crearli nelle varie classi, potresti dirmi come li faresti tu o se sono scorretti?

    Classi linkedlist cpp e hpp:

    codice:
    class LinkedList
    {
            protected:
                Node* head;
              //  Node* tail;
                int size;
         
                
            public:
                LinkedList();
               ~LinkedList();
               
               
               void setHead(Node* n);
               Node* getHead();
               //void setTail(Node* n);
              // Node* getTail();
               int getSize();
               void addNode(int element);
               void deleteNode();
              
               
    };
    
    #endif	/* LINKEDLIST_HPP */

    codice:
    #include "LinkedList.hpp"
    
    LinkedList:: LinkedList()
    {
        head=NULL;
        size=0;
        
    }
    
    LinkedList::~LinkedList(){
        delete head;
    }
    
       void LinkedList::setHead(Node* n){head=n;}
       Node* LinkedList::getHead(){return head;}
      // void LinkedList:: setTail(Node* n){tail=n;}
      // Node*  LinkedList::getTail(){return tail;}
       int LinkedList::getSize(){return size;}
       
       void LinkedList::addNode(int element){
           
           Node* nuovo=new Node(element);
           if(size==0)
           {
               head=nuovo;
           }
           
           else
           {
               nuovo->setNext(head);
               head=nuovo;
           }
           size++;
       
          }
       
       void LinkedList::deleteNode(){
          if(size>0) 
          {
            Node* tmp=getHead();
            Node* succ=tmp->getNext();
            head=succ;
            delete tmp;
            //delete succ;
            
            size--;
          }
          
       }

    Classi nodo:

    codice:
    #ifndef NODE_HPP
    #define	NODE_HPP
    
    #include <stdio.h>
    
    class Node
    {
            protected:
                int element;
                Node* next;
                
            public:
                Node(int element);
                ~Node();
        
        
               int getElement();
               void setElement(int element);
               void setNext(Node * next);
               Node* getNext();
    };
    
    #endif	/* NODE_HPP */
    codice:
    #include "Node.hpp"
    
    Node:: Node(int number)
    {
        element=number;
        next=NULL;
    }
    
    Node:: ~Node(){ 
    delete next;
    }
    
    int Node:: getElement(){return element;}
    void Node::setElement(int number){element=number;}
    void Node::setNext(Node* succ){next=succ;}
    Node * Node:: getNext(){return next;}

    Classe stack:

    codice:
    #ifndef STACK_HPP
    #define	STACK_HPP
    #include "LinkedList.hpp"
    #include "Node.hpp"
    #include <string>
    
    
    using namespace std;
    
    class Stack
    {
            protected:
                LinkedList* lista;
                int size;
                Node* top;
                
            public:
                Stack();
                ~Stack();
                
                string Top();
                int getsize();
                void push(int nuovo);
                void pop(); 
                string toString();
    };
    
    #endif	/* STACK_HPP */
    codice:
    #include "Stack.hpp"
    #include <string.h>
    #include <sstream>
    //#include "LinkedList.hpp"
    //#include "Node.hpp"
    
    LinkedList* stack;
        
    Stack::Stack()
    {
        stack=new LinkedList();
        top=stack->getHead();
    }
    
    Stack::~Stack(){
        delete stack;
    }
    
    string Stack::Top() 
    {  
        string conv;
        stringstream out;
        
        string stampa;
        if(stack->getSize()>0)
        {
            int number=stack->getHead()->getElement();
            out << number;
            conv=out.str();
            stampa+=conv;   
        }
        
        else
        {
            stampa="Error!!! Stack is Empty!";
        }
        
        return stampa;
    }
    
    int Stack::getsize(){return stack->getSize();}
    
    void Stack::pop()
    {
        if(stack->getSize()>0)
             stack->deleteNode();
        else
            printf("Error!!! Stack is empty");
    }
        
    void Stack::push(int nuovo){
        stack->addNode(nuovo);
        top=stack->getHead();
    }
    
    string Stack::toString()
         {
           
             string str="[";
             int num;
             
             if(stack->getSize()==0) return str+"]";
             
             
                     Node*tmp=stack->getHead();
             
                      for(; tmp->getNext()!=NULL; tmp=tmp->getNext())
                     {
                              string conv;
                              stringstream out;
                 
                              num=tmp->getElement();
                              out << num;
                              conv=out.str();
                 
                              str+=conv+", ";
                     }
             
                string conv;
                stringstream out;
             
               num=tmp->getElement();
               out << num;
               conv=out.str();
               str+=conv+"]";
        
             
             return str;
         }
    codice:
    #include <cstdlib>
    #include "Stack.hpp"
    #include <stdio.h>
    #include <iostream> 
    
    
    using namespace std;
    
    /*
     * 
     */
    int main(int argc, char** argv) {
        
        int op;
        Stack *myStack=new Stack();
        bool finito=false;
        
        while(!finito)
        {
            int el;
             printf ("Che operazione vuoi eseguire?\n");
              cout << "1)push: 2)pop 3)top: 4)Fornisci dimensione: 5)Stampa struttura:\n";
               cin >> op;
        
             switch(op)
             {
                 case 1:
                     cout << "Che numero vuoi inserire?\n" << endl;
                      cin >> op;
                     myStack->push(op);
                 break;
                 
                 case 2:
                    myStack->pop();
                 break;
                
                 case 3:
                    cout <<myStack->Top()<< endl;
                 break;
                    
                 case 4:
                     el=myStack->getsize();
                     printf("Il numero di elementi e' %d.\n", el);
                     break;
                 
                 case 5:
                     cout <<myStack->toString() << endl;
                 break;
                
                 default:
                   cout << "Errore...il programma verra' terminato...\n" <<endl;
                        finito=true;
                 break;
             }
             
             if(finito==false)
             {
                cout << "Vuoi continuare? 1)si 2)no\n" << endl;
                      cin >> op;
                      
                 if(op==2)
                     finito=true;
                      
                 else if(op!=1 && op!= 2)
                     cout<<"Errore!!! Il programma verra' terminato\n"<<endl;
             }
        }
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.