Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rpn_calculator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anna Gillert
rpn_calculator
Commits
43c7442b
Commit
43c7442b
authored
6 years ago
by
Simon Ebbers
Browse files
Options
Downloads
Patches
Plain Diff
Update main.cpp
parent
ad6abbac
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main.cpp
+25
-10
25 additions, 10 deletions
src/main.cpp
with
25 additions
and
10 deletions
src/main.cpp
+
25
−
10
View file @
43c7442b
...
...
@@ -10,21 +10,36 @@
* Adds two integers
*/
int
add
(
const
int
a
,
const
int
b
)
{
return
a
+
b
;
int
res
;
bool
overflow
=
__builtin_sadd_overflow
(
a
,
b
,
&
res
);
if
(
overflow
)
{
throw
std
::
invalid_argument
(
"Malformed input. Integer overflow (add)."
);
}
return
res
;
}
/*
*
Subtracts integer b from
integer
a
*
Multiplies two
integer
s
*/
int
subtract
(
const
int
a
,
const
int
b
)
{
return
b
-
a
;
int
multiply
(
const
int
a
,
const
int
b
)
{
int
res
;
bool
overflow
=
__builtin_smul_overflow
(
a
,
b
,
&
res
);
if
(
overflow
)
{
throw
std
::
invalid_argument
(
"Malformed input. Multiplication error."
);
}
return
res
;
}
/*
*
Multiplies two
integer
s
*
Subtracts integer b from
integer
a
*/
int
multiply
(
const
int
a
,
const
int
b
)
{
return
a
*
b
;
int
subtract
(
const
int
a
,
const
int
b
)
{
int
res
;
bool
overflow
=
__builtin_ssub_overflow
(
b
,
a
,
&
res
);
if
(
overflow
)
{
throw
std
::
invalid_argument
(
"Malformed input. Integer overflow (subtract)."
);
}
return
res
;
}
/*
...
...
@@ -33,7 +48,7 @@ int multiply(const int a, const int b) {
*/
int
divide
(
const
int
a
,
const
int
b
)
{
if
(
b
==
0
)
{
throw
std
::
invalid_argument
(
"
Tried to divide by 0
."
);
throw
std
::
invalid_argument
(
"
Malformed input. Division by zero
."
);
}
return
a
/
b
;
}
...
...
@@ -41,7 +56,6 @@ int divide(const int a, const int b) {
using
binary_operator
=
decltype
(
&
add
);
int
evaluate
(
const
std
::
string
&
s
)
{
std
::
stack
<
int
>
stack
;
static
const
std
::
unordered_map
<
char
,
binary_operator
>
map
{
{
'+'
,
add
},
{
'-'
,
subtract
},
...
...
@@ -49,6 +63,8 @@ int evaluate(const std::string &s) {
{
'/'
,
divide
}
};
decltype
(
map
)
::
iterator
iter
;
std
::
stack
<
int
>
stack
;
for
(
char
c
:
s
)
{
if
(
std
::
isdigit
(
c
))
{
stack
.
push
(
c
-
'0'
);
...
...
@@ -91,7 +107,6 @@ int main() {
}
catch
(
const
std
::
exception
&
e
)
{
std
::
cout
<<
e
.
what
();
}
if
(
result
!=
INT_MAX
)
{
std
::
cout
<<
"Result is: "
<<
result
<<
'\n'
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment