Skip to content

Instantly share code, notes, and snippets.

@Nircek
Created August 9, 2018 12:48
Show Gist options
  • Select an option

  • Save Nircek/268f0a69e3c8f88f66cf01532a6e20fa to your computer and use it in GitHub Desktop.

Select an option

Save Nircek/268f0a69e3c8f88f66cf01532a6e20fa to your computer and use it in GitHub Desktop.
program do interpretowania komend (ćwiczenie obsługi stringstream)
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream sss[8], cmd;
string scmd, escmd;
int in=0, out=0;
for(;;){
getline(cin, scmd); // pobranie komendy do scmd
cmd.clear(); // wyzerowanie cmd
cmd << scmd; // pobranie komendy do cmd
while(getline(cmd, escmd, ';')) { // wyjęcie pierwszej komendy z cmd i zapis do escmd, separator komend to ';'
// pętla wykonuje się do momentu kiedy można coś wyjąć
cout << "Executing " << escmd << '\n';
if(escmd.substr(0,2)=="in") {
stringstream t;
t<<escmd.substr(2);
t>>in;
cout<< "Setting in to "<<in<<'\n';
} else if(escmd.substr(0,3)=="out") {
stringstream t;
t<<escmd.substr(3);
t>>out;
cout<< "Setting out to "<<out<<'\n';
} else if(escmd=="print") {
cout << sss[out].str() << '\n';//stringstream.str() przekształca aktualną zawartość strumienia do stringa
} else if(escmd=="add") {
int a,b;
sss[1] >> a;
sss[2] >> b;
cout<< "Setting "<<out<<" to "<<a+b<<'\n';
sss[out].clear(); // czyszczenie tego co aktualnie jest w stringu
sss[out] << a+b;
} else if(escmd=="mul") {
int a,b;
sss[1] >> a;
sss[2] >> b;
cout<< "Setting "<<out<<" to "<<a*b<<'\n';
sss[out].clear();
sss[out] << a*b;
} else if(escmd.substr(0,1)=="\""){
sss[in]<<escmd.substr(1);
cout<< "Setting "<<in<<" to "<<escmd.substr(1)<<'\n';
} else {
cout<< "Setting "<<in<<" to "<<escmd<<'\n';
sss[in].clear();
sss[in] << escmd;
}
}
}
cout << "Hello world!" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment