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
77e6ccfd
Commit
77e6ccfd
authored
6 years ago
by
Simon Ebbers
Browse files
Options
Downloads
Patches
Plain Diff
codestyle
parent
f8f63d21
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
+20
-38
20 additions, 38 deletions
src/main.cpp
with
20 additions
and
38 deletions
src/main.cpp
+
20
−
38
View file @
77e6ccfd
...
...
@@ -9,73 +9,55 @@
/*
* Adds two integers
*/
int
add
(
int
a
,
int
b
)
{
int
res
;
bool
overflow
=
__builtin_sadd_overflow
(
a
,
b
,
&
res
);
if
(
overflow
)
{
throw
std
::
invalid_argument
(
"Malformed input. Integer overflow (add)."
);
}
return
res
;
int
add
(
const
int
a
,
const
int
b
)
{
return
a
+
b
;
}
/*
*
Multiplies two
integer
s
*
Subtracts integer b from
integer
a
*/
int
multiply
(
int
a
,
int
b
)
{
int
res
;
bool
overflow
=
__builtin_smul_overflow
(
a
,
b
,
&
res
);
if
(
overflow
)
{
throw
std
::
invalid_argument
(
"Malformed input. Multiplication error."
);
}
return
res
;
int
subtract
(
const
int
a
,
const
int
b
)
{
return
b
-
a
;
}
/*
*
Subtracts integer b from
integer
a
*
Multiplies two
integer
s
*/
int
subtract
(
int
a
,
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
;
int
multiply
(
const
int
a
,
const
int
b
)
{
return
a
*
b
;
}
/*
* Divides integer b from integer a, throwing an exception when b is zero.
* Throws an std::invalid_argument exceptions if invalid parameters are provided.
*/
int
divide
(
int
a
,
int
b
)
{
int
divide
(
const
int
a
,
const
int
b
)
{
if
(
b
==
0
)
{
throw
std
::
invalid_argument
(
"
Malformed input. Division by zero
."
);
throw
std
::
invalid_argument
(
"
Tried to divide by 0
."
);
}
return
a
/
b
;
}
using
binary_operator
=
decltype
(
&
add
);
std
::
unordered_map
<
char
,
binary_operator
>
map
{
{
'+'
,
add
},
{
'-'
,
subtract
},
{
'*'
,
multiply
},
{
'/'
,
divide
}
};
int
evaluate
(
const
std
::
string
&
s
)
{
int
a
,
b
;
decltype
(
map
)
::
iterator
iter
;
std
::
stack
<
int
>
stack
;
static
const
std
::
unordered_map
<
char
,
binary_operator
>
map
{
{
'+'
,
add
},
{
'-'
,
subtract
},
{
'*'
,
multiply
},
{
'/'
,
divide
}
};
decltype
(
map
)
::
iterator
iter
;
for
(
char
c
:
s
)
{
if
(
std
::
isdigit
(
c
))
{
stack
.
push
(
c
-
'0'
);
}
else
if
(
stack
.
size
()
>=
2
)
{
iter
=
map
.
find
(
c
);
if
(
iter
!=
map
.
end
())
{
a
=
stack
.
top
();
const
auto
a
=
stack
.
top
();
stack
.
pop
();
b
=
stack
.
top
();
const
auto
b
=
stack
.
top
();
stack
.
pop
();
stack
.
push
(
iter
->
second
(
b
,
a
));
}
else
{
...
...
@@ -109,8 +91,8 @@ 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