I am trying to put tokens into a queue. However, when I try to put in a number that has more than one digit (i.e., 10, 123 ) it reads them as separate digits. Can someone tell me what I'm doing wrong? I've tried string insert and append and none of them seem to work
queue <string> getTokens(string token){
int a = (int) token.length();
string temp;
queue <string> numbers;
char t;
for (int i =0; i <a; i++){
t = token[i];
while (!isdigit(t)){
if (t=='+' || t=='-' || t=='/' || t=='*' || t=='^'){
string temp1;
temp1 += t;
numbers.push(temp1);
temp1.clear();
}
else if (t=='(' || t==')'){
string temp1;
temp1 += t;
numbers.push(temp1);
temp1.clear();
}
else if (!isalpha(token[i-1]) && t == 'e' && !isalpha(token[i+1])){
string e = "2.718";
numbers.push(e);
}
else if (!isalpha(token[i-1]) && t == 'p' && token[i+1]== 'i' && !isalpha(token[i+2])){
string pi = "3.14169";
numbers.push(pi);
}
break;
}
//if it is one single number
if (!isdigit(token[i-1]) && isdigit(t) && !isdigit(token[i+1])){
string tt;
tt += t;
numbers.push(tt);
}
//if there is more than one number
else if ((isdigit(t) && isdigit(token[i+1])) || (isdigit(token[i-1]) && isdigit(t))){ //if it is a number
string temp2;
string temp3="k";
string temp4;
//cout << t;
//int j=1;
if( isdigit(token[i])){
temp2 += t;
cout<<"temp2 : "<<temp2<<endl;
cout <<"temp3 :" << temp3<<endl;
//temp2.clear();
temp3 +=temp2;
}
temp4.append(temp3);
temp4 +=temp3;
//cout<<"hi"<<endl;
cout << "This is temp4: " << temp4 <<endl;
//cout << "this is temp3: " << temp3<< endl;
//temp2.clear();
//cout<<temp2 << "yo";
//temp3.assign(temp2);
//cout << "temp3 is : "<< temp3;
}
else
continue;
}
return numbers;
}
int main (){
string expression;
getline(cin,expression);
cout << expression;
queue <string> ts;
ts= getTokens(expression);
}
At every Iteration temp2 temp3 temp4 variable's value will get reset
so it does not hold previous iteration's value.
I think this is where you might be facing problem.
try to declare it outside for loop and clear it after you obtain desirable output, for next use
//if there is more than one number
else if ((isdigit(t) && isdigit(token[i+1])) || (isdigit(token[i-1]) && isdigit(t))){
//if it is a number
string temp2;
string temp3="k";
string temp4;
I hope this might help
Related
I'm quite new to C++, so I apologize if I am not sounding technical. I am having a little trouble getting a multiple input and output with my code, I'm thinking I should have a loop to get the data but i'm not sure how i'd go about that in my code, I've thought about using getline() but that doesn't seem to want to work with Char.
I've tried getline but I'm not sure how to implement it with the Char input, I believe I may need a separate loop as well but again not too sure. I'm thinking it could be done to EoF as well.
Here's my code:
int main()
{
char inpval;
int outval = 0;
cout << "Enter a Roman Number to convert: " << endl;
while (cin.get(inpval))
{
inpval = toupper(inpval);
if (inpval == 'M')
outval = outval + 1000;
else if (inpval == 'D') {
inpval = cin.peek();
inpval = toupper(inpval);
if (inpval == 'M') {
outval = outval - 500;
continue;
} else {
outval = outval + 500;
continue;
}
}
//etc
cout << "The Equivalent Arabic value is:" << endl;
cout << outval << "\n";
return 0;
}
My expected output is:
(All on newline)
Input:
I
II
IV
V
VI
Output:
1
2
4
5
6
Actual output is:
Input:
I
Output:
1
P.S: The program converts Roman Numeral chars to their respected number.
Any help is appreciated!
You can take input multiple items from cin, using below syntax.
cin >> a;
cin >> b;
cin >> c;
Another way is also there
cin >> a >> b >> c;
This technique is called "operator chaining" which is similar to the above.
Do you have any problem doing it like this?
cout << "Enter a Roman Numeral" << endl;
string inpval;
cin >> inpval;
while (inpval != "exit")
{
int outval = 0;
if (inpval == "I")
outval = 1;
else if (inpval == "II")
outval = 2;
else if (inpval == "III")
outval = 3;
else if (inpval == "IV")
outval = 4;
// ect
cout << "The Equivalent Arabic value is: " << outval << endl << endl;
cout << "Enter next numeral: (type exit to exit) " << endl;
cin >> inpval;
}
Method 1: Use getchar(), Calculate/Convert Roman to integer until you encounter a space ' ',when you get a space ' ' output the integer and do the same next roman number until you get another space ' ' or newline '\n' and stop the program once you encounter newline '\n'.
Method 2:Use type std::string and take input with getline. Then iterate through the string and calculate until you find space ' ' output the number, do the same till you find next space ' ' or end when string ends.
If you know # of Roman numbers you want to convert you can put it in a loop.
Hope this helps.
Example(Method 2)
#include <bits/stdc++.h>
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
int main()
{
int out=0;
std::string s;
std::string::iterator i; //string iterator
//for more info go to https://www.geeksforgeeks.org/stdstring-class-in-c/
getline(std::cin,s);
for (i = s.begin(); i!= s.end() ; ++i)
{
if(*i != ' ')//Encounter a space, output result and
{ //go to next roman numeral
int s1 = value(*i);
if (*(i+1) != ' ' || *(i+1) != '\0')
{
// Getting value of i+1 nth Element
int s2 = value(*(i+1));
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equal to the next symbol
out = out + s1;
}
else
{
out = out + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
out = out + s1;
i++;
}
}
else
{
std::cout<<out<<" ";
out = 0;
}
}
std::cout<<out<<" ";
std::cout<<std::endl;
return 0;
}
Input:
I II MM MCMIV
Output:
1 2 2000 1904
I'm aware that there's already stuff here regarding this but I'm just posting to check if my logic lines up with my code (if I'm thinking about this whole infix Postfix thing the way I should be).
The code I've seen regarding this topic on this site look a little different from mine. I'm kind of a novice at C++. Anyway, my code so far looks like it is just fine and should work just fine but it's not working the way it should. I've included my thought process as comments in the code below. Please let me know if what I'm thinking is okay.
Here's the code:
#include <iostream>
#include <stack>
#include <sstream>
#include <string>
using namespace std;
int priority (string item)
{
int prior = 0;
if (item == "(")
{
prior = 1;
}
if ((item == "+") || (item == "-"))
{
prior = 2;
}
else if ((item == "*") || (item == "/"))
{
prior = 3;
}
else
{
prior = 4;
}
return prior;
}
int main()
{
stack<string> st;
string output;
cout << "Welcome. This program will calculate any arithmetic expression you enter" << endl;
cout << "Please enter an arithmetic expression below: " << endl;
char line[256];
cin.getline(line, 256);
string exp;
exp = line;
cout << "This is the expression you entered: ";
cout << exp << endl;
string item;
istringstream iss(exp);
iss >> item;
// While there are still items in the expression...
while ( iss )
{
// If the item is a number
if (isdigit('item') == true)
{
output = output + " " + item;
}
// If the stack is empty
else if (st.size() == 0)
{
st.push(item);
}
// If the item is not a number
else if (isdigit('item') == false)
{
// convert that item in the expression to a string
atoi(item.c_str());
int prior1 = 0;
int prior2 = 0;
// If that string is a LEFT PARENTHESIS
if (item == "(")
{
st.push(item);
}
// If that string is a RIGHT PARENTHESIS
else if (item == ")")
{
while ((st.empty() == false) && (st.top() != "("))
{
output = st.top() + " ";
st.pop();
}
st.pop();
}
else
{
// store what is returned from "priority" in "prior1"
prior1 = priority(item);
// pass item on top of the stack through "priority"
// store that in "prior2;
prior2 = priority(st.top());
// while loop here, while item has a higher priority...
// store numbers in a variable
while (prior1 > prior2)
{
output = output + " " + st.top();
st.pop();
prior2 = priority(st.top());
}
}
}
}
while (st.empty() == false)
{
output = st.top() + " ";
st.pop();
}
cout << "Here's the postfix: " << output << endl;
}
This is for evaluating postfix to an actual answer:
// EVALUATE POSTFIX
stack<int> st2;
string output2;
istringstream iss2(exp);
iss2 >> item;
while (iss)
{
// If the item is a number
if (isdigit('item') == true)
{
st.push(item);
}
if (isdigit('item') == false)
{
// store item in a variable
// pop another item off the stack
// store that in a variable.
// apply operator to both.
// push answer to the top of the stack.
int m = 0;
int n = 0;
int total = 0;
m = st2.top();
st2.pop();
n = st2.top();
st2.pop();
if (item == "+")
{
total = m + n;
st2.push(total);
}
else if (item == "-")
{
total = m - n;
st2.push(total);
// add the thing you popped of the stack with the first thing on the stack
}
else if (item == "*")
{
total = m * n;
st2.push(total);
}
else if (item == "/")
{
total = m / n;
st2.push(total);
}
}
}
cout << "Here's your answer: " << st2.top() << endl;
}
Am I thinking about this whole infix-postfix thing the way I should be? Am I on track to solving this problem? My apologies if this question seems simple. I'm kind of a novice at this. Thank you very much.
Why am I getting stuck in my validation loop after hitting Y to continue? I have to use cin.get and can not use strings
This program collects input from a user and displays them by using a pointer with an array, I have to validate for negative numbers, letters and newline characters with the appropriate message
#include <iostream>
#include <iomanip>
using namespace std;
void validateUserInput(char *userInputCharArray, int &strLength);
int const ARRAY_SIZE = 100;
int main()
{
char *userInputCharArray = nullptr;
char yes = NULL;
int lengthOfInput;
//creating a dynamic array size 100
userInputCharArray = new char[ARRAY_SIZE];
//loop
do
{
int count = 0;
//user prompt and accept input
cout << "Please enter an integer >= 0 and press <ENTER>: " << endl;
cin.get(userInputCharArray, ARRAY_SIZE);
while(userInputCharArray[count] != ' ' && userInputCharArray[count] != '\0')
count++;
{
if(userInputCharArray[count] == ' ')
{
userInputCharArray[count] = '\0';
}
}
lengthOfInput = count;
validateUserInput(userInputCharArray, lengthOfInput);
cout << "Your number is: " << endl;
for(int i = 0; i < lengthOfInput; i++)
{
cout << userInputCharArray[i] - '0' << " ";
}
cout << endl;
cout << "Press y to continue or any other button to exit: " <<endl;
cin >> yes;
delete [] userInputCharArray;
userInputCharArray = nullptr;
userInputCharArray = new char[ARRAY_SIZE];
}while(yes == 'y' || yes == 'Y');
cout << "Thank you Good-Bye";
cout << endl;
delete [] userInputCharArray;
userInputCharArray = nullptr;
system("pause");
return 0;
}
Im getting stuck in my functions while loop
void validateUserInput(char *userInputCharArray, int &strLength)
{
int counter = 0;
while(*userInputCharArray < '0' || (*userInputCharArray >= 'A' && *userInputCharArray <= 'Z')
|| (*userInputCharArray >= 'a' && *userInputCharArray <= 'z')
|| *userInputCharArray == 0)
{
cout << "Please enter a positive integer and press <ENTER>: " <<endl;
cin.get(userInputCharArray, ARRAY_SIZE);
}
while(userInputCharArray[counter] != ' ' && userInputCharArray[counter] != '\0')
counter++;
if(userInputCharArray[counter] == ' ')
{
userInputCharArray[counter] = '\0';
}
strLength = counter;
}
According to the while loop you have here, you will keep on calling in.get so long as the first character that you read in is a digit or an alphabetic character. So, if you start your input with one of those characters, you will loop forever.
I'd suggest that you get input a line at a time and then parse what you get.
cin.get(*userInputCharArray);
Will extract one character only, therefore the terminator character '\0' will not be part of userInputCharArray.
Change it to read a line at a time and parse the input. If you're still having issues, you can flush your input buffer before each read like so:
cin.ignore(10, "\n");
cin.get(userInputCharArray, ARRAY_SIZE);
When I run my program, I have to type how many rows do I want in my output. I have a limit from 1 to 100 rows. Each row is a task with a name of the task followed by increasing number, example: Task1:, Task2, .... When I type something into input, it must convert input string /see the code below - except the code in main();/.
My problem is that when I type first input, it should go to next task/next row/ but it doesnt. I type for example 10 strings but they dont go each to next task but they stay in one task..hope you understand now.
#include<iostream>
#include<string>
#include <ctype.h>
using namespace std;
void Convert(string input){
string output = "";
string flag = "";
bool underscore = false;
bool uppercase = false;
if ( islower(input[0]) == false){
cout << "Error!" <<endl;
return;
}
for (int i=0; i < input.size(); i++){
if ( (isalpha( input[i] ) || (input[i]) == '_') == false){
cout << "Error!" <<endl;
return;
}
if (islower(input[i])){
if (underscore){
underscore = false;
output += toupper(input[i]);
}
else
output += input[i];
}
else if (isupper(input[i])){
if (flag == "C" || uppercase){
cout << "Error!"<<endl;
return;
}
flag = "Java";
output += '_';
output += tolower(input[i]);
}
else if (input[i] == '_'){
if (flag == "Java" || underscore){
cout << "Error!" <<endl;
return;
}
flag = "C";
underscore = true;
}
}
cout << output <<endl;
}
int main(){
const int max = 100;
string input;
int pocet_r;
cout << "Zadaj pocet uloh:" << endl;
cin >> pocet_r;
if(pocet_r >= 1 && pocet_r <=100)
{
for (int i = 0; i <pocet_r; i++)
{
cout << "Uloha " << i+1 << ":" << endl;
while (cin >> input)
Convert (input);
while(input.size() > max)
cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
while(input.size() > max)
cin >> input;
while (cin >> input)
Convert(input);
}
}else{
cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}
Your first if in Convert will always fail on a non-underscore and return. I don't think that's what's intended. Agree with other answer on the while cin loop. The next two whiles should be if's apparently. Step thru this code with a debugger and watch it line by line and see where it fails. You've got multiple issues here, and I'm not entirely sure what the intent is.
Edit - I didn't parse the extra parenthesis correctly. The first if in convert is actually okay.
So, I'm having issues with this program. Can anyone tell me what I'm doing wrong here? The program is supposed to take a simple infix notation mathematical expression (e.g. "5 - 2 + 1") and then convert it to (e.g. "5 2 - 1 +") and then solve it which would be 4. It converts just fine but as soon as it gets into the evaluating part, it doesn't show anything on the command prompt. Can I get some help? Thanks!
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int priority(string item) //prioritizing the operators
{
if(item == "(" || item == ")")
{
return 0;
}
else if(item == "+" || item == "-")
{
return 1;
}
else //if(item == "/" || item == "*") <-- guess didnt need to define this one
{
return 2;
}
}
void welcome()//welcome text
{
cout << "Welcome to this program!" << endl;
cout << "Please enter your equation" << endl;
}
int main()
{
welcome(); // welcome text
stack <string> myStack; // initializing the stack.
char line[256];
cin.getline( line, 256); // this and the proceeding line get the input.
string exp = line;
string item;
string postFix;
istringstream iss(exp);
iss >> item;
while(iss)
{
if(item != "+" && item != "-" && item != "/" && item != "*" && item != "(" && item != ")") //If the char is a number
{
cout << item;
postFix = postFix + item;
}
else if(myStack.size() == 0) // If the stack is empty
{
myStack.push(item);
}
else if( item == "+" || item == "-" || item == "/" || item == "*") //If the char is an operator
{
if(priority(myStack.top()) < priority(item)) // the item on the stack is greater priority than the array item
{
myStack.push(item);
}
else
{
while(myStack.size() > 0 && priority(myStack.top()) >= priority(item)) //while the stack contains something, and the item on
{
cout << myStack.top();
postFix = postFix + item;
myStack.pop();
}
myStack.push(item);
}
}
else if(item == "(") // left peren
{
myStack.push(item);
}
else if(item == ")") // right peren
{
while(myStack.top() != "(")
{
cout << myStack.top();
postFix = postFix + item;
myStack.pop();
}
myStack.pop();
}
iss >> item;
}
while (myStack.size() > 0 ) //When nothing is left to evaluate
{
cout << myStack.top();
postFix = postFix + myStack.top();
myStack.pop();
}
cout << endl;
//PART 2
int x1;
int x2;
int x3;
stack<int> thirdStack;
string exp2 = postFix;
string item2;
istringstream iss2(exp2);
iss2 >> item2;
while(iss2)
if(item2 != "+" && item2 != "-" && item2 != "/" && item2 != "*") //if its a number
{
int n;
n = atoi(item2.c_str());
thirdStack.push(n);
}
else if( item2 == "+" || item2 == "-" || item2 == "/" || item2 == "*") //if its an operator
{
x1 = thirdStack.top();
thirdStack.pop();
x2 = thirdStack.top();
thirdStack.pop();
if(item2 == "+")
{
x3 = x1 + x2;
thirdStack.push(x3);
}
else if(item2 == "-")
{
x3 = x1 - x2;
thirdStack.push(x3);
}
else if(item2 == "/")
{
x3 = x1 * x2;
thirdStack.push(x3);
}
else if(item2 == "*")
{
x3 = x1 / x2;
thirdStack.push(x3);
}
}
}
cout << "The conversion into infix notation is" + thirdStack.top() << endl;
}
There are a number of problems with this code.
In part 1, while your code appears to write out the correct postfix conversion, the postFix string it builds up isn't the same thing.
For example, in some places you have code like this:
cout << myStack.top();
postFix = postFix + item;
You're writing out myStack.top() but adding item to your postFix result. It should be this:
cout << myStack.top();
postFix = postFix + myStack.top();
In addition, you ought to be including spaces between each item that you add to the postFix string. So your result should be 5 2 - 1 + rather than 52-1+. Otherwise when trying to interpret that expression, the first item is going to be interpreted as 52.
Then in part 2, you're missing a call to iss2 >> item2; at the end of the while loop. You're basically just interpreting the first item over and over again, so the code will end up in an infinite loop.
And in your calculations, you have the operand order incorrect. This doesn't matter for addition and multiplication, but it does for subtraction and division. For example, the substraction calculation should be x3 = x2 - x1; rather than x3 = x1 - x2;.
Finally, when streaming out the result you've got a plus when you should have a stream operator. It should look like this:
cout << "The conversion into infix notation is" << thirdStack.top() << endl;