[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Need help with my c++ coding assignment.

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 17
Thread images: 3

File: help me.png (8KB, 588x228px) Image search: [Google]
help me.png
8KB, 588x228px
So my task is to implement a method on building binary expression tree (which I have already done).

I only have 1 more task to do, and it's the one included in the picture.The goal of this function is to take an arithmetic expression and break it into a vector.

So for example, 3+4*5 would be [3, +, 4, *, 5].
Also, 34+45*56 would be [34, +, 45, *, 56] so I would not be able to iterate through a string and just call it a day.

There are two helper functions which can be used to help determine whether a string is an int or operator. There is none for parenthesis but it can be done easily I reckon.

I need to pass the following test cases to achieve full marks (since I already have finished the rest of the assignment).
std::string expr1 = "1 + 2";
std::string expr2 = "1 * 2";
std::string expr3 = "1 - 2";
std::string expr4 = "1 / 2";
std::string expr5 = "(1 + 2)";
std::string expr6 = "1 + 2 * 3";

std::stringstream stream;
stream << std::rand() % 10000;
std::string expr7 = stream.str();

The last one is simply tests a random number to see if it goes through the test.

So overall, I'm guessing I can split by white space? I'm struggling like crazy and it's in 7 hours. ;_;
>>
Are you allowed to use regular expressions?
>>
>>60336161
Nope only arithmetic expressions can be taken in. anything else would still pass through the code but it will not be tested. Only the test cases need to be achieved. I will be marked on design so I can allow it to handle broken input but honesty it's not worth it.
>>
Iterate through the string and put all the data into a string vector until you hit an operator. When you hit the operator put that in another sting vector and continue on until you see a number again and repeat?
>>
I'd help more but I'm not at a computer right now. Basically if you have 34+55, iterate through the string and append each number to the previous
So 34 would be another string and put that in the vector. 3 would initialize the string then you append the 4 onto it or however many numbers until you reach an operator
>>
>>60336220
So let's say I finish iterating a string and splitting it up.

So for 34+45*56, it would be in 3, 4, +, 4, 5, *, 5, 6 format, but you use string concatenation to join the numbers 3 and 4 to make 34?

Also, what about parenthesis? Is it the same concept?
>>
>>60336288
No, join them together as you iterate through
Iterate through one string while building another to put into the vector
Or even build right into the vector

Parentheses are just another operator right?
>>
>>60336314
Parenthesis are not in my if_operator() function but I can make a separate if statement for the iterating through string for that case.
>>
http://ysonggit.github.io/coding/2014/12/16/split-a-string-using-c.html
Anyways, I found this website. The first method tells to use stringstream but that will require a separate function to be written (which is okay with the assignment specs). Since this solution doesn't skip white space, is there a way to get around that?
>>
Just ignore white space ?
>>
File: chrome_2017-05-12_11-45-43.png (59KB, 1205x569px) Image search: [Google]
chrome_2017-05-12_11-45-43.png
59KB, 1205x569px
>>60336130
hello data structures goy

what class timeslot are you in
>>
File: 1493817354195.jpg (23KB, 552x535px) Image search: [Google]
1493817354195.jpg
23KB, 552x535px
Use lex and yacc
>>
>>60336130
I don't know shit about c++, so i'm probably missing some part of the problem.

1. Instantiate your result vector
2. Iterate through the input string
3. If the character is an int, continue until you hit an operator/space, then add that substring to your result vector
4. Add the previous operator to the result vector
5. Repeat from 3


Isn't that all it is?
>>
>>60337729
lol I don't go tutorials but I go lectures. It's stupid but i have this indian woman that doesn't know jackshit as my tutor.

Anyways I think I have figured out the code but I won't be able to test it until I'm at uni since MinGW is broken on my pc.
>>
>>60337943
Try repl.it
>>
>>60337943


then we have the same tutor, shes a tiny brown indian and she tells us to google shit that we don't know

i dont go to tutorials either but lectures are even worse
>>
>>60337770
>>60336130
Better yet, implement your own regex engine
https://swtch.com/~rsc/regexp/regexp1.html
Thread posts: 17
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.