Wczytywanie liczb z ciągu znaków

0

Dzień Dobry mam za zadanie napisać program który po wpisaniu słowa Enqueue i cyfy x ,wprowadzi tę cyfrę X do kolejki.I właśnie w tym tkwi mój problem ponieważ o ile mozna sprawdzić czy uzytkownik podał słowo Enqueue to nie jest problemem ale jak sprawdzić czy po tym słowie podał cyfrę i jak tę cyfrę umieścić w kolejce ? oto mój teraźniejszy kod:

Enqueue x - komenda nakazująca umieszczenie liczby całkowitej x w kolejce

string str;
cin>>str;
if(str=="Enqueue")
cin>>wlozyc;
enqueue(wlozyc);
0
string str;
cin>>str;
if(str=="Enqueue")
if(cin>>wlozyc)
    enqueue(wlozyc);

Możesz też pobawić się w regexy jak chcesz większe rozwiązanie.

0

kq dzięki za szybką odpowiedź lecz po zastosowaniu poniższego kodu, i wpisaniu w komendy Enqueue15 lub Enqueue 15 kod wyskakuje komunikat: Aby kontynuować, naciśnij dowolny klawisz.

int wlozyc;
string str;
cin>>str;

if(str=="Enqueue")
if(cin>>wlozyc)
enqueue(wlozyc)
0

Bardziej mnie ciekawi czy jest jakiś sposób aby sprawdzić czy użytkownik podaje ciąg znaków , po ciągu znak biały , po znaku białym liczbę . Jeśli tak to tę właśnie liczbę wstawiamy do kolejki.

0

Jaki Masz system i kompilator; na linuxie z gcc (kompilacja: g++ <nazwa.cpp>) kod podany przez @kq działa tak jak powinien, gdy podam Enqueue 123 lub Enqueue enter 123. Nie ma znaczenia ile jest spacji między Enqueue a 123.

1

Np. z użyciem stringstream i tuple

using Command = tuple<string,int>;

Command parseInput(const string& input)
{
  string cmd;
  int val;
  stringstream ss{input};
  if (ss >> cmd >> val) {
    return make_tuple(cmd, val);
  }
  return {"", -1};
}

void func()
{
  string userInput;
  getline(cin, userInput);
  auto [token, value] = parseInput(userInput);
  if (token == "Enqueue") {
    cout << "Value -> " << value << "\n";
  }
}
0

Wszystko działa tylko spoj wywala ze przekroczono limit czasu co jest grane ?

#include<iostream>
using namespace std;
#include <string>
const int SIZE=10;

int A[SIZE];
int front =-1;
int rear =-1;


bool isempty()
{
	if(front ==-1 && rear==-1)
	return true;
	else
	return false;
}

void enqueue(int value)
{
if(rear==SIZE-1)
	cout<<"QUEQUE is full \n";
else
{
	if(front==-1)
		front=0;
	rear++;
	A[rear]=value;
	cout<<"--->";


	
}

}

void dequeue()
{
	if(isempty())
		cout<<"QUEQUE is empty\n";
	else
		if(front==rear)
			front=rear=-1;
	
		else front++;
		cout<<A[front-1];
		
		
}



void displayQueue()
{
if(isempty())
	cout<<"QUEUE is empty\n";
else
{
	for(int i=front;i<=rear;i++)
		cout<<A[i]<<"\n";
}
}


int main(){

int ile;
cin>>ile;


for(int i=0;i<ile;i++){
string str;
cin>>str;
int wlozyc;

if(str=="Enqueue")
if(cin>>wlozyc)
enqueue(wlozyc);


if(str=="Dequeue")
dequeue();



if(str=="Print")
displayQueue();

}

return 0;
}
0

system("pause"); - po co to?

0

bez systemu tez przekroczony limit

Na spoju jest polecenie aby to zadanie zrealizować przy pomocy 10 elmentowej tablicy , więc listy odpadają . Macie może pomysł dlaczego wypluwa mi przekroczony limit czasu ?

#include <iostream>
using namespace std;
#include <string>
const int SIZE = 10;

int A[SIZE];
int front = -1;
int rear = -1;

bool isempty() {
  if (front == -1 && rear == -1)
    return true;
  else
    return false;
}

void enqueue(int value) {
  if (rear == SIZE - 1)
    cout << "QUEQUE is full \n";
  else {
    if (front == -1)
      front = 0;
    rear++;
    A[rear] = value;
    cout << "--->";
  }
}

void dequeue() {
  if (isempty())
    cout << "QUEQUE is empty\n";
  else if (front == rear)
    front = rear = -1;

  else
    front++;
  cout << A[front - 1];
}

void displayQueue() {
  if (isempty())
    cout << "QUEUE is empty\n";
  else {
    for (int i = front; i <= rear; i++)
      cout << A[i] << "\n";
  }
}

int main() {
  int ile;
  cin >> ile;

  for (int i = 0; i < ile; i++) {
    string str;
    cin >> str;
    int wlozyc;

    if (str == "Enqueue")
      if (cin >> wlozyc)
        enqueue(wlozyc);

    if (str == "Dequeue")
      dequeue();

    if (str == "Print")
      displayQueue();
  }

  return 0;
}
0

Na spoju prawie zawsze polecenie obejmuje wyłącznie wejście i wyjście, a nie detale implementacyjne. Co to za zadanie?

0

Procedury drukowały nie to co miał na myśli autor programu, Porównaj tę wersję z poprzednią. Masz buga w funkcji dequeue, uruchomienie i :

➜  tests ./a.out       
2
Enqueue 1
--->
Dequeue
0 

Jak widać w kolejce miało być jeden a nie zero. Kod (dequeue nie zdebugowana):

#include <iostream>
using namespace std;
#include <string>
const int SIZE = 10;
 
int A[SIZE];
int front = -1;
int rear = -1;
 
bool isempty() {
  if (front == -1 && rear == -1)
    return true;
  else
    return false;
}
 
void enqueue(int value) {
  if (rear == SIZE - 1)
    cout << "Error: queue is full \n";
  else {
    if (front == -1)
      front = 0;
    rear++;
    A[rear] = value;
    cout << "--->\n";
  }
}
 
void dequeue() {
  if (isempty())
    cout << "Error: queue is empty\n";
  else if (front == rear)
    front = rear = -1;
    
 
  else
    front++;
  cout << A[front - 1] << "\n";
}
 
void displayQueue() {
  if (isempty())
    cout << "Print: queue is empty\n";
  else {
	  cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
  cout << " \n";
}
 
int main() {
  int ile;
  cin >> ile;
 
  for (int i = 0; i < ile; i++) {
    string str;
    cin >> str;
    int wlozyc;
 
    if (str == "Enqueue")
      if (cin >> wlozyc)
        enqueue(wlozyc);
 
    if (str == "Dequeue")
      dequeue();
 
    if (str == "Print")
      displayQueue();
  }
 
  return 0;
}
0

Porównuje tę wersję z poprzednią i ten bug jest nadal . Problem tkwi zapewne tutaj tak ? ** cout << A[front - 1] << "\n";**

void dequeue() {
  if (isempty())
    cout << "Error: queue is empty\n";
  else if (front == rear)
    front = rear = -1;
 
  else
    front++;
  cout << A[front - 1] << "\n";
}
0

Wydaje się, że ta dequeue jest poprawna:

void dequeue() {
  if (isempty()){
    cout << "Error: queue is empty\n";
	}
  else if (front == rear){
	 cout << A[front] << "\n";
    front = -1;
    rear =  -1;
    }
  else{
    front++;
     cout << A[front - 1] << "\n";
  }
}
0

Tak ten kod pokazuje prawidłowe wyniki , tylko na spoju wypluwa informację o przekroczonym czasie

#include <iostream>
using namespace std;
#include <string>
const int SIZE = 10;
 
int A[SIZE];
int front = -1;
int rear = -1;
 
bool isempty() {
  if (front == -1 && rear == -1)
    return true;
  else
    return false;
}
 
void enqueue(int value) {
  if (rear == SIZE - 1)
    cout << "Error: queue is full \n";
  else {
    if (front == -1)
      front = 0;
    rear++;
    A[rear] = value;
    cout << "--->\n";
  }
}
 
void dequeue() {
  if (isempty()){
    cout << "Error: queue is empty\n";
    }
  else if (front == rear){
     cout << A[front] << "\n";
    front = -1;
    rear =  -1;
    }
  else{
    front++;
     cout << A[front - 1] << "\n";
  }
}
 
void displayQueue() {
  if (isempty())
    cout << "Print: queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
  cout << " \n";
}
 
int main() {
  int ile;
  cin >> ile;
 
  for (int i = 0; i < ile; i++) {
    string str;
    cin >> str;
    int wlozyc;
 
    if (str == "Enqueue")
      if (cin >> wlozyc)
        enqueue(wlozyc);
 
    if (str == "Dequeue")
      dequeue();
 
    if (str == "Print")
      displayQueue();
  }
 
  return 0;
}
0

Gdzie w treści zadania masz napisane, że na początku masz liczbę rozkazów kolejki?
Nie masz!
Więc nic się nie wczytuje, w zmiennej pozostaje śmieć, który może być dużą liczbą i twój program się nie kończy w wyznaczonym czasie.

Popraw tak:

int main()
{
    string str;
    while (cin >> str) {
        int wlozyc;

        if (str == "Enqueue") {
            if (cin >> wlozyc) {
                enqueue(wlozyc);
            }
        } else if (str == "Dequeue") {
            dequeue();
        } else  if (str == "Print") {
            displayQueue();
        }
    }

    return 0;
}
0

Witam po zastosowaniu tego kody Spoj wyrzuca Błędną odpowiedz , czy ktoś wie jaki jest tego powód ?

#include <iostream>
using namespace std;
#include <string>
const int SIZE = 10;
 
int A[SIZE];
int front = -1;
int rear = -1;
 
bool isempty() {
  if (front == -1 && rear == -1)
    return true;
  else
    return false;
}
 
void enqueue(int value) {
  if (rear == SIZE - 1)
    cout << "Error: queue is full \n";
  else {
    if (front == -1)
      front = 0;
    rear++;
    A[rear] = value;
    cout << "--->\n";
  }
}
 
void dequeue() {
  if (isempty()){
    cout << "Error: queue is empty\n";
    }
  else if (front == rear){
     cout << A[front] << "\n";
    front = -1;
    rear =  -1;
    }
  else{
    front++;
     cout << A[front - 1] << "\n";
  }
}
 
void displayQueue() {
  if (isempty())
    cout << "Print: queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
  cout << " \n";
}
 
int main()
{
    string str;
    while (cin >> str) {
        int wlozyc;
 
        if (str == "Enqueue") {
            if (cin >> wlozyc) {
                enqueue(wlozyc);
            }
        } else if (str == "Dequeue") {
            dequeue();
        } else  if (str == "Print") {
            displayQueue();
        }
    }
 
    return 0;
}
0

Nie jestem pewien co do poprawności samego kodu, ale to nie jest zgodne z przykładem:

  if (isempty())
    cout << "Print: queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
  cout << " \n";

Dla pustej wypiszesz Print: queue is empty\n \n

0

A jakie masz zastrzeżenia do kodu ?

0

Mój kod po waszych radach wygląda następująco , lecz problem błędnej odpowiedzi jest nadal

#include <iostream>
using namespace std;
#include <string>
const int SIZE = 10;
 
int A[SIZE];
int front = -1;
int rear = -1;
 
bool isempty() {
  if (front == -1 && rear == -1)
    return true;
  else
    return false;
}
 
void enqueue(int value) {
  if (rear == SIZE - 1)
    cout << "Error: queue is full \n";
  else {
    if (front == -1)
      front = 0;
    rear++;
    A[rear] = value;
    cout << "--->\n";
  }
}
 
void dequeue() {
  if (isempty()){
    cout << "Error: queue is empty\n";
    }
  else if (front == rear){
     cout << A[front] << "\n";
    front = -1;
    rear =  -1;
    }
  else{
    front++;
     cout << A[front - 1] << "\n";
  }
}
 
void displayQueue() {
  if (isempty())
    cout << "Print: queue is empty\n";
  else {
      
    for (int i = front; i <= rear; i++)
      cout << A[i]<<"\n";
  }
  
}
 
int main()
{
    string str;
    while (cin >> str) {
        int wlozyc;
 
        if (str == "Enqueue") {
            if (cin >> wlozyc) {
                enqueue(wlozyc);
            }
        } else if (str == "Dequeue") {
            dequeue();
        } else  if (str == "Print") {
            displayQueue();
        }
    }
 
    return 0;
}
0

Teraz nie wypisujesz poprawnie Print: 1 2 3 4 5\n

1

Co ja pacze, nie zauważyłem tego buga wczoraj i straszy:)

void displayQueue() {
  if (isempty())
    cout << "Print: queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
        cout <<" "<< A[i];
    cout << "\n";
  }
}
0

lion137 oto mój kod z tym poprawionym bugiem i nadal jest blędna odpowiedź :

#include <iostream>
using namespace std;
#include <string>
const int SIZE = 10;
 
int A[SIZE];
int front = -1;
int rear = -1;
 
bool isempty() {
  if (front == -1 && rear == -1)
    return true;
  else
    return false;
}
 
void enqueue(int value) {
  if (rear == SIZE - 1)
    cout << "Error: queue is full\n";
  else {
    if (front == -1)
      front = 0;
    rear++;
    A[rear] = value;
    cout << "--->\n";
  }
}
 
void dequeue() {
  if (isempty()){
    cout << "Error: queue is empty\n";
    }
  else if (front == rear){
     cout << A[front] << "\n";
    front = -1;
    rear =  -1;
    }
  else{
    front++;
     cout << A[front - 1] << "\n";
  }
}
 
void displayQueue() {
  if (isempty())
    cout << "Print: Queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
  cout << " \n";
}
 
int main()
{
    string str;
    while (cin >> str) {
        int wlozyc;
 
        if (str == "Enqueue") {
            if (cin >> wlozyc) {
                enqueue(wlozyc);
            }
        } else if (str == "Dequeue") {
            dequeue();
        } else  if (str == "Print") {
            displayQueue();
        }
    }
 
    return 0;
}
0

lion137 poprawione:

void displayQueue() {
  if (isempty())
    cout << "Print: Queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
  cout << " \n";
}
0

To nie jest poprawione, to jest dokładnie to samo na co zwróciłem uwagę kilka postów temu

0

Wiem że problem jest zapewne trywialny ale powiedz mi co z tym jest nie tak

void displayQueue() {
  if (isempty())
    cout << "Print: Queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
  
  }
0

Nie drukujesz znaku końca linii dla wariantu niepustego

0

Czyli tak będze

void displayQueue() {
  if (isempty())
    cout << "Print: Queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i]<<"\n";
  }
  
  }
1

Programujesz metodą Monte Carlo? Wstawiasz losowe rzeczy w losowe miejsca i liczysz na poprawny wynik?

Może po prostu zobacz odpowiedź @lion137 https://4programmers.net/Forum/1543515

0
Cisi204 napisał(a):

Wiem że problem jest zapewne trywialny ale powiedz mi co z tym jest nie tak

Popatrz dokładnie w zadanie i przykład oraz przemyśl algorytm.
Zwróć uwagę że w przykładzie wpisano w sumie 11 liczb do kolejki. Zwykłe zwiększanie zmiennych front/rear nie zadziała przy 10-elementowej tablicy.

1 użytkowników online, w tym zalogowanych: 0, gości: 1