Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PPC
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Vojtěch Novotný
PPC
Commits
5981aa50
Commit
5981aa50
authored
3 weeks ago
by
Vojtěch Novotný
Browse files
Options
Downloads
Patches
Plain Diff
funkcni prvni cast domaciho ukolu (3b)
parent
3b4415f8
No related branches found
Branches containing commit
No related tags found
1 merge request
!4
stabilni verze semestralni prace, 3 cviceni, domaci ukol 1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ukoly/du_01/template/main.cpp
+118
-13
118 additions, 13 deletions
ukoly/du_01/template/main.cpp
with
118 additions
and
13 deletions
ukoly/du_01/template/main.cpp
+
118
−
13
View file @
5981aa50
...
...
@@ -8,7 +8,7 @@
// Výchozí konfigurace
int
config_min
=
-
99
;
int
config_max
=
1
5
0
;
int
config_max
=
1
0
0
;
int
config_width
=
3
;
std
::
string
config_align
=
"left"
;
...
...
@@ -20,16 +20,59 @@ void printConfig()
std
::
cout
<<
"config.align="
<<
config_align
<<
"
\n\n
"
;
}
bool
isValidConfig
()
{
if
(
config_min
>
config_max
||
config_width
<=
0
)
{
std
::
cerr
<<
"Invalid configuration
\n
"
;
return
false
;
}
return
true
;
}
bool
isCellWidthTooShort
(
const
std
::
vector
<
std
::
vector
<
std
::
string
>>
&
values
)
{
for
(
const
auto
&
row
:
values
)
{
for
(
const
auto
&
cell
:
row
)
{
try
{
int
value
=
std
::
stoi
(
cell
);
if
(
std
::
to_string
(
value
).
length
()
>
static_cast
<
size_t
>
(
config_width
))
{
std
::
cerr
<<
"Cell is too short
\n
"
;
return
true
;
}
}
catch
(
const
std
::
invalid_argument
&
)
{
// Ignore non-integer cells
}
catch
(
const
std
::
out_of_range
&
)
{
// Ignore non-integer cells
}
}
}
return
false
;
}
int
computeSum
(
const
std
::
vector
<
std
::
string
>
&
row
,
const
std
::
string
&
sumExpr
)
{
std
::
regex
sumRegex
(
R"(SUM\(([A-Z]):([A-Z])\))"
);
std
::
smatch
match
;
if
(
std
::
regex_match
(
sumExpr
,
match
,
sumRegex
))
{
int
start
=
match
[
1
].
str
()[
0
]
-
'A'
;
int
end
=
match
[
2
].
str
()[
0
]
-
'A'
;
size_t
start
=
match
[
1
].
str
()[
0
]
-
'A'
;
size_t
end
=
match
[
2
].
str
()[
0
]
-
'A'
;
if
(
end
>=
row
.
size
())
{
std
::
cerr
<<
"Invalid input
\n
"
;
exit
(
101
);
}
int
sum
=
0
;
for
(
in
t
i
=
start
;
i
<=
end
&&
i
<
row
.
size
();
++
i
)
for
(
size_
t
i
=
start
;
i
<=
end
&&
i
<
row
.
size
();
++
i
)
{
try
{
...
...
@@ -37,17 +80,52 @@ int computeSum(const std::vector<std::string> &row, const std::string &sumExpr)
}
catch
(...)
{
return
0
;
std
::
cerr
<<
"Invalid input
\n
"
;
exit
(
101
);
}
}
return
sum
;
}
return
0
;
std
::
cerr
<<
"Invalid input
\n
"
;
exit
(
101
);
}
bool
isOutOfRange
(
const
std
::
vector
<
std
::
vector
<
std
::
string
>>
&
values
)
{
for
(
const
auto
&
row
:
values
)
{
for
(
const
auto
&
cell
:
row
)
{
try
{
int
value
=
std
::
stoi
(
cell
);
if
(
value
<
config_min
||
value
>
config_max
)
{
std
::
cerr
<<
"Out of range
\n
"
;
return
true
;
}
}
catch
(...)
{
if
(
cell
.
find
(
"SUM("
)
==
std
::
string
::
npos
)
{
std
::
cerr
<<
"Invalid input
\n
"
;
exit
(
101
);
}
}
}
}
return
false
;
}
void
printTable
(
std
::
vector
<
std
::
vector
<
std
::
string
>>
&
values
)
{
int
cols
=
0
;
if
(
isOutOfRange
(
values
))
{
return
;
}
printConfig
();
size_t
cols
=
0
;
for
(
const
auto
&
row
:
values
)
{
if
(
row
.
size
()
>
cols
)
...
...
@@ -63,15 +141,22 @@ void printTable(std::vector<std::vector<std::string>> &values)
}
std
::
string
separator
=
"+"
;
for
(
in
t
i
=
0
;
i
<=
cols
;
i
++
)
for
(
size_
t
i
=
0
;
i
<=
cols
;
i
++
)
{
separator
+=
std
::
string
(
config_width
+
2
,
'-'
)
+
"+"
;
}
std
::
cout
<<
separator
<<
"
\n
| |"
;
for
(
char
c
=
'A'
;
c
<
'A'
+
cols
;
c
++
)
for
(
size_t
i
=
0
;
i
<
cols
;
i
++
)
{
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
left
<<
c
<<
" |"
;
if
(
config_align
==
"right"
)
{
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
right
<<
static_cast
<
char
>
(
'A'
+
i
)
<<
" |"
;
}
else
{
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
left
<<
static_cast
<
char
>
(
'A'
+
i
)
<<
" |"
;
}
}
std
::
cout
<<
"
\n
"
<<
separator
<<
"
\n
"
;
...
...
@@ -85,7 +170,14 @@ void printTable(std::vector<std::vector<std::string>> &values)
{
cell
=
std
::
to_string
(
computeSum
(
values
[
i
],
cell
));
}
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
left
<<
cell
<<
" |"
;
if
(
config_align
==
"right"
)
{
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
right
<<
cell
<<
" |"
;
}
else
{
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
left
<<
cell
<<
" |"
;
}
}
std
::
cout
<<
"
\n
"
<<
separator
<<
"
\n
"
;
...
...
@@ -111,7 +203,10 @@ int main()
}
}
printConfig
();
if
(
!
isValidConfig
())
{
return
102
;
}
std
::
vector
<
std
::
vector
<
std
::
string
>>
values
;
while
(
std
::
getline
(
std
::
cin
,
line
))
...
...
@@ -131,6 +226,16 @@ int main()
values
.
push_back
(
row
);
}
if
(
isOutOfRange
(
values
))
{
return
100
;
}
if
(
isCellWidthTooShort
(
values
))
{
return
103
;
}
printTable
(
values
);
return
0
;
}
}
\ No newline at end of file
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