[C++JTask Create a program that calculate the IC in the tube. Each commands li:
ID: 3881937 • Letter: #
Question
[C++JTask Create a program that calculate the IC in the tube. Each commands li: input IC in the left side of tube ri : input IC in the right side of tube Ir : Move IC from left side to right side rr Move IC from right side to left side Input First line : Integer M (1SMS 1,000) for how much of the commands. Next line : In each line must begin with "li" or "ri" or "Ir" or "rr". If begin with li or ri must follow by the number of IC. Example Input 8 ri 1 ri 2 ri 3 li 20 ri 100 Ir ri 1000 Output 1000 123 100 20Explanation / Answer
#include<iostream>
#include<list>
#include<iterator>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
int cmdCount, tempVar;
list <int> mylist;
char *cmdstring;
string cmd;
cout<<"Enter how many commands:";
cin>>cmdCount;
cin>>ws; /*Clearing the input buffer */
while(cmdCount--)
{
getline(cin, cmd); /* Get string input along with line */
cmdstring = (char*)cmd.c_str();
if(0 == strncmp("li", cmdstring, 2))
{
tempVar = atoi(&cmdstring[3]); /*Convert input to integer and add to front(left of the list) */
mylist.push_front(tempVar);
}
else if(0 == strncmp("ri", cmdstring, 2))
{
tempVar = atoi(&cmdstring[3]); /*Convert input to integer and add to front(right of the list) */
mylist.push_back(tempVar);
}
else if(0 == strncmp("lr", cmdstring, 2))
{
if(mylist.empty() == false) /* If list is not empty, get element from front(left) and delete it from front. Then add it in the back(right) */
{
tempVar = mylist.front();
mylist.pop_front();
mylist.push_back(tempVar);
}
}
else if(0 == strncmp("rr", cmdstring, 2))
{
if(mylist.empty() == false) /* If list is not empty, get element from back(right) and delete it from back. Then add it in the front(left) */
{
tempVar = mylist.back();
mylist.pop_back();
mylist.push_front(tempVar);
}
}
}
cout<<endl;
for(list <int> :: iterator it = mylist.begin(); it != mylist.end(); ++it)
cout<< *it <<endl;
return 0;
}
/* Note :- If the ouput is not your desired output, then you can explain the output once. I have done the program as your requirement stated. Thanks. */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.