When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
172 messages ยท Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Wdym by "does not work"?
If I put in the numbers like 99+32=, than nothing happens
I mean first of all if your input is 33 + 22 / 20 = then you don't have 4 numbers and 4 operator like your code asks you to input
If I input e.g. 33 + 22 + 20 + 20 = then I get the outcome 75 which is correct
*incorrect
Oh so its actually a working code?
No, just realized that 33 + 22 + 20 + 20 doesn't add up to 75 ๐
Also apparently 75 is always the output, regardless of what operators and operands you input
why is that?
Idk, but your code is really weird in that you somehow try to account for precedence but then don't really care about it, e.g. if the first operator is a + then you perform that + on the first and second number, regardless of if the second operand is a e.g. a * which would mean you'd need to wait for the 2nd * 3rd operator to add that multiplied value to the first_num rather than what you're doing
In general your code has a lot of duplicate code and is very hard to extend/fix stuff in because you have so many if/else if/else stuffs and so many different variables
Can you help plz.. I have to get this done within a hour and a half ...
The a lot of variables come from the instruction which the teacher gave.. I have to follow it all
I mean I can help you to get onto the correct path but really to do this while staying sane we basically have to scratch your entire code.
Also I'm not writing the code for you and neither is anyone here.
There's a nice saying
Your lack of planning is not my emergency
OH, and I didn't even read the instructions, yet.
Just a quick skim and nevermind, you apparently really do need to do it cursed like that and don't need to scratch your entire code.
Anyways, I'll be out for a joint and then read through the assignment.
this assignment seems a bit too challenging for beginners honestly imho; best way to do it would be recursive descent parsing but by no means that's a "beginner" topic ๐
Does it seem too challenging?
I mean they're given basically the entire algorithm + they're restricted to a fixed amount of numbers and operands
Also we don't even know from what week that homework is.
I'd say it's okay for like week 3
Maybe even week 2
shoot jumped the gun sorry;
guess what I meant more is that parsing stuff in general can get difficult fast esp. in C land ๐
I say that because the concepts required are really simple:
if/else, no loopsinteresting I see
*Oh yeah, also when I say "week 2" I mean week 2 of uni.
If OP is still in school then that'd be more like week 8 or so
I had Java in school for 2.5 years (last semester of 10th, then 11th and 12th grade)
In my mandatory 1st semester uni Java course we surpassed my entire Java knowledge I had from school in week 7 or 8
cool
did you get your code from chatgpt?
cuz it kinda looks like it lmao
wait how did you know is there a ai kind of style in programming too like in writing?
yea at my uni using code directly from chatgpt is bona fide academic misconduct ๐
But it stil doesnt work
cuz chatgpt is free for everyone probably ๐
i would imagine so
idk it just looks a bit broken
not sure what are the implications for chatgpt-assisted code at least for creating the initial drafts that I'd have to check with my professors ://
are y'all in uni?
4th year ohio state correct
a butt ton of my assignments have all been in C ๐
So can you help my code...
so did u take from chat gpt?
depends on definition of "good"; I can't code OOP code for sh1t ๐
but c isn't oop
exactly that's why I'm weak at OOP which according to my dad is an "industry standard"
oop is an industry standard or being weak at it?
the former; my father does industry 4.0 work helping to design digital twins and what not
man we really hijacked this thread ๐
nice
lmao
msg to OP:
do you want us to rewrite your code for you?
and you didn't answer my first question
I downloaded OP's code and gcc'ed it and it's freezing after accepting input for some reason ๐
nah it wasn't gpt
I have the same problem
if I input 4 numbers it sill gives a wrong answer
maybe try using switch statements
I think the scanf is being "misused" as a preliminary hunch...
thanks
also @modern pendant make sure you add an additional percent sign like so:
printf("The numbers must be integers and the operators must be +, -, *, /, or %%!\n")
Helps to get rid of gcc warning ๐
oh that was also the problem thank you
I just looked through it and what I found is that you made a mistake when reading the given real language algorithm.
9.
If operator_one is *, /, or %
a) If operator_one is *
(1) Multiply the first_num and the second_num, and store it in firstSecondOutcome
b) Else if operator_one is /
(1) Divide the first_num and the second_num, and store it in firstSecondOutcome
c) Else
(1) Find the remainder of the first_num and the second_num, and store it in firstSecondOutcome
d) Set firstOperatorFlag with value 2
10. Else if operator_two is * /, or % <-- here you went wrong
a) If operator_two is *
if (operator_one == '*' || operator_one == '/' || operator_one == '%') {
if (operator_one == '*') {
firstSecondOutcome = first_num * second_num;
} else if (operator_one == '/') {
firstSecondOutcome = first_num / second_num;
} else {
firstSecondOutcome = first_num % second_num;
}
firstOperatorFlag = 2;
} else /* <-- here you should've written 'else if (operator_two == '*' || operator_two == '/' || operator_two == '%' { ... */ {
...
}
nope the scanf was just fine I just checked ๐
but 1+2+3+4 is giving me 7 tho
i'll be back within 15 minutes, currently doing it again rn
Welcome to a working Calculator~!
You will be providing four numbers and four operators
The numbers must be integers and the operators must be +, -, *, /, or %!
Remember that the last operator MUST be an equal sign ~
Please enter the expression below:
1+2+3+4=
The outcome for the expression came out to be 3
Thank You
huh?
wait sht sorry wrong patch ๐
do you mean?
// Check for the first operator precedence
if (operator_one == '*' || operator_one == '/' || operator_one == '%') {
if (operator_one == '*') {
firstSecondOutcome = first_num * second_num;
} else if (operator_one == '/') {
firstSecondOutcome = first_num / second_num;
} else {
firstSecondOutcome = first_num % second_num;
}
firstOperatorFlag = 2;
} else if (operator_two == '*' || operator_two == '/' ||
operator_two == '%') {
if (operator_one == '+') {
firstSecondOutcome = first_num + second_num;
} else {
firstSecondOutcome = first_num - second_num;
}
firstOperatorFlag = 2;
}
@modern pendant Has your question been resolved? If so, type !solved :)
๐
tedliosu@victus-ted:~/Downloads/programs/from_discord$ ./rando_prog
Welcome to a working Calculator~!
You will be providing four numbers and four operators
The numbers must be integers and the operators must be +, -, *, /, or %!
Remember that the last operator MUST be an equal sign ~
Please enter the expression below:
33+22/20+0=
The outcome for the expression came out to be 55
Thank You
man this program is cursed and convoluted fr fr
@modern pendant
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
I did it thanks!!
awesome! ๐
please use the !solved command to mark this as solved ๐
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
oh ok it's solved
neato ๐
if anybody is interested i remade this in c++ with switch statements
bored? lol
do you want to see it?
can I download and compile it with g++ without making any changes to it? then sure ๐
Sounds fun, I'll try to craft one together in C++, too
yes
It's just easier to get something running.
Best example is std::vector
Whereas for C you'd have to write your own linked list + insert + remove + whatever you need
ah yse std::vector is nice indeed ๐
actually i'm using visual studio
yea screw that lol
so i dunno if it will work with g++
msvc or "mingw"?
could you show me the headers here? ๐
#include <iostream>
#include <string>
#include <sstream>
#include <iostream>
#include <string>
#include <sstream>
void mainScreen();
int evaluateExpression(int num1, char op1, int num2, char op2, int num3, char op3, int num4);
int main() {
mainScreen();
return 0;
}
void mainScreen() {
std::cout << "Welcome to the Calculator!" << std::endl;
std::cout << "Please enter an expression of up to 4 digits (e.g., 5 + 3 * 2 + 3=): ";
std::string input;
std::getline(std::cin, input);
std::istringstream iss(input);
int num1, num2, num3, num4;
char op1, op2, op3, equals;
iss >> num1 >> op1 >> num2 >> op2 >> num3 >> op3 >> num4 >> equals;
int result = evaluateExpression(num1, op1, num2, op2, num3, op3, num4);
std::cout << "Result: " << result << std::endl;
}
int evaluateExpression(int num1, char op1, int num2, char op2, int num3, char op3, int num4) {
int result;
switch (op2) {
case '*':
result = (op1 == '+') ? num1 + num2 * num3 : num1 - num2 * num3;
break;
case '/':
result = (op1 == '+') ? num1 + num2 / num3 : num1 - num2 / num3;
break;
case '%':
result = (op1 == '+') ? num1 + num2 % num3 : num1 - num2 % num3;
break;
case '+':
result = (op1 == '+') ? num1 + num2 + num3 : num1 - num2 - num3;
break;
case '-':
result = (op1 == '+') ? num1 + num2 - num3 : num1 - num2 + num3;
break;
}
switch (op3) {
case '*':
result = (op2 == '+') ? result + num4 : result - num4;
break;
case '/':
result = (op2 == '+') ? result + num4 : result - num4;
break;
case '%':
result = (op2 == '+') ? result + num4 : result - num4;
break;
case '+':
result += num4;
break;
case '-':
result -= num4;
break;
}
return result;
}
ok that might work lemme crack at it real quick
is this complete? ๐
wry guys i need to go i'll be back in a bit
yes
This doesn't look like it's working properly.
Specifically this part:
case '+':
result = (op1 == '+') ? num1 + num2 + num3 : num1 - num2 - num3;
break;
case '-':
result = (op1 == '+') ? num1 + num2 - num3 : num1 - num2 + num3;
break;
Also it doesn't seem to look like as if you respected the case for when op1 is also a *, / or %
is this what you mean?
Welcome to the Calculator!
Please enter an expression of up to 4 digits (e.g., 5 + 3 * 2 + 3=): 4-3+2+1=
Result: 0
No, I meant something like 4 * 3 * 2 * 1
Damn:
Welcome to the Calculator!
Please enter an expression of up to 4 digits (e.g., 5 + 3 * 2 + 3=): 4*3*2*1=
Result: -3
Even that one wasn't correct
but -3 is wild
at this point I'd just use a normal calculator ๐
lmao looks like it's not working
LOL
told ya parsing was hard innit? ๐
imma try and write some unit tests for this in bash actually ๐
or something idk I'm just trying to practice testing as per advice from father ๐
do you mind if I write tests and you do the actual logic? sorry I'm terrible with parsing stuff lol
and I'm also terrible with C++
My gosh, haha.
I'm in my 4th data structure now.
First I tried with a vector, then a stack, then a queue and now I'm with the deque (I could've just used a vector but I wanted to pick the correct data structure... kill me)
how's this thus far (without the asserts?)
tedliosu@victus-ted:~/Downloads/programs/from_discord$ ./unit_tests.sh
Testing 1+2+3+4=
Welcome to the Calculator!
Please enter an expression of up to 4 digits (e.g., 5 + 3 * 2 + 3=): Result: 10
Testing 4*3*2*1=
Welcome to the Calculator!
Please enter an expression of up to 4 digits (e.g., 5 + 3 * 2 + 3=): Result: -3
Testing 5*8-9-0=
Welcome to the Calculator!
Please enter an expression of up to 4 digits (e.g., 5 + 3 * 2 + 3=): Result: 6
Testing 4-3+2+1=
Welcome to the Calculator!
Please enter an expression of up to 4 digits (e.g., 5 + 3 * 2 + 3=): Result: 0
First result is correct, all others are wrong
Btw, I'm finished
;compile | 1 + 2 * 4 / 3 - 1 - 1 - 1 - 1 =
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <deque>
int eval_expr(std::deque<int> nums, std::deque<char> ops) {
static int i{};
while (ops.front() != '=') {
int n1 = nums.front();
nums.pop_front();
char op = ops.front();
ops.pop_front();
int n2 = nums.front();
nums.pop_front();
switch (op) {
case '*':
nums.push_front(n1 * n2);
break;
case '/':
nums.push_front(n1 / n2);
break;
case '%':
nums.push_front(n1 % n2);
break;
case '+':
nums.push_front(n2);
return n1 + eval_expr(nums, ops);
case '-':
nums.push_front(-n2);
return n1 + eval_expr(nums, ops);
}
}
return nums.front();
}
int main() {
std::cout << "Welcome to the Calculator!" << std::endl;
std::cout << "Please enter an expression ending with '='";
std::string input;
std::getline(std::cin, input);
/* Code from https://stackoverflow.com/a/83481 . Removes all spaces (wasn't required) */
/*
std::string::iterator end_pos = std::remove(input.begin(), input.end(), ' ');
input.erase(end_pos, input.end());
*/
std::istringstream iss(input);
std::deque<int> nums;
std::deque<char> ops;
for (;;) {
int n;
char c;
iss >> n >> c;
nums.push_back(n);
ops.push_back(c);
if (c == '=') break;
}
std::cout << std::endl << input << " " << eval_expr(nums, ops) << std::endl;
}
Welcome to the Calculator!
Please enter an expression ending with '='
1 + 2 * 4 / 3 - 1 - 1 - 1 - 1 = -1
hold on lemme finish building the testing framework ๐
Arbitrary amount of inputs even
Python would be the easiest by far
;compile | 1 + 2 * 4 / 3 - 1 =
equation = input("Please enter an expression ending with '=': ")
print(f"{equation[:-2]} = {eval(equation[:equation.find('=')])}")
Please enter an expression ending with '=': 1 + 2 * 4 / 3 - 1 = 2.6666666666666665
LMAO
could even enforce integer division with a simple replace
;compile | 1 + 2 * 4 / 3 - 1 =
equation = input("Please enter an expression ending with '=': ")
print(f"{equation[:-2]} = {eval(equation.replace('/', '//')[:equation.find('=')])}")
Please enter an expression ending with '=': 1 + 2 * 4 / 3 - 1 = 2
How's this now?
tedliosu@victus-ted:~/Downloads/programs/from_discord$ ./unit_tests.sh
Testing 1+2+3+4=
10 was expected; PASS
Testing 4*3*2*1=
24 was expected, but -3 was actual; FAIL
Testing 5*8-9-0=
31 was expected, but 6 was actual; FAIL
Testing 4-3+2+1=
4 was expected, but 0 was actual; FAIL
How's what?
the output looking for a testing framework lol?
hey guys
For an actual testing framwork? Bad, because you need to write your own shell script.
For this very case it's okay, however I would probably let it randomly choose operands and operators, evaluate these and then check that the result of your program matches your evaluation
my "unit_tests.sh" is ready to go for your program; want me to upload the contents lol?
sure why not
And I would make it like a few thousand tests rather than just 4
sh1t ๐
#!/bin/bash
# Declare our array of test expressions
test_exps=(
"1+2+3+4"
"4*3*2*1"
"5*8-9-0"
"4-3+2+1"
)
name_of_prog="rando_prog_itex"
name_of_exten=".cpp"
result_field_num="19"
# DON'T ALTER ANYTHING BELOW THIS LINE; UNLESS IF YOU KNOW
# EXACTLY WHAT YOU'RE DOING.
# First argument is expected, second argument is actual :D
assert_expected () {
if [[ "$1" -eq "$2" ]]; then
echo "$2 was expected; PASS"
else
echo "$1 was expected, but $2 was actual; FAIL"
fi
}
g++ "${name_of_prog}${name_of_exten}" -o "$name_of_prog"
for expressn in "${test_exps[@]}"; do
echo "Testing $expressn="
actual_ans=$(echo "${expressn}=" | "./$name_of_prog" | cut -d" " -f"$result_field_num" | tr -d "\n")
expected_ans="$(echo "${expressn}" | bc)"
assert_expected "$expected_ans" "$actual_ans"
done
# Cleanup
rm -rf "$name_of_prog"
i don't know anything about bash really, but i guess this will test my program
unfortunately I made it so that you have to edit the contents of "test_exps" in order to have additional test expressions, and this shell script can only run with bash ๐
you're on windows right? so that means that this will somehow have to be converted to powershell i think ://
yep on windows
nvm my program was broken anyway
i have a new version now
I can test that new version if you'd like ๐
it even throws an error when there is a modulo or adivision by zero
ok sounds good tysm for chipping in ๐
wait
i found an error
i need to take into account PEMDAS
should i make it so it uses parenthesis?
no that'll be too complicated :/
LGTM:
tedliosu@victus-ted:~/Downloads/programs/from_discord$ ./unit_tests.sh
Testing 1+2+3+4=
10 was expected; PASS
Testing 4*3*2*1=
24 was expected; PASS
Testing 5*8-9-0=
31 was expected; PASS
Testing 4-3+2+1=
4 was expected; PASS
Testing 3*4/9+9=
10 was expected; PASS
Testing 4*7+2+64=
94 was expected; PASS
Testing 34*6*1+3=
207 was expected; PASS
Testing 2-5+43-4=
36 was expected; PASS
Testing 3-54*43*43=
-99843 was expected; PASS
Testing 4-2*65/4=
-28 was expected; PASS
Testing 54-3-47-2=
2 was expected; PASS
Tired of writing code for now imma take a break lol