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
c0b71138
Commit
c0b71138
authored
3 weeks ago
by
Vojtěch Novotný
Browse files
Options
Downloads
Patches
Plain Diff
domaci ukol 1, gitignore
parent
5981aa50
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
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+1
-1
1 addition, 1 deletion
.gitignore
ukoly/du_01/template/main.cpp
+119
-22
119 additions, 22 deletions
ukoly/du_01/template/main.cpp
with
120 additions
and
23 deletions
.gitignore
+
1
−
1
View file @
c0b71138
*.exe
tasks.json
\ No newline at end of file
*.json
\ No newline at end of file
This diff is collapsed.
Click to expand it.
ukoly/du_01/template/main.cpp
+
119
−
22
View file @
c0b71138
...
...
@@ -11,18 +11,25 @@ int config_min = -99;
int
config_max
=
100
;
int
config_width
=
3
;
std
::
string
config_align
=
"left"
;
int
config_stretch
=
-
1
;
// -1 indicates not set
int
config_header
=
-
1
;
// -1 indicates not set
void
printConfig
()
void
printConfig
(
int
width
)
{
std
::
cout
<<
"config.min="
<<
config_min
<<
"
\n
"
;
std
::
cout
<<
"config.max="
<<
config_max
<<
"
\n
"
;
std
::
cout
<<
"config.width="
<<
config_width
<<
"
\n
"
;
std
::
cout
<<
"config.align="
<<
config_align
<<
"
\n\n
"
;
std
::
cout
<<
"config.width="
<<
width
<<
"
\n
"
;
std
::
cout
<<
"config.align="
<<
config_align
<<
"
\n
"
;
if
(
config_stretch
!=
-
1
)
std
::
cout
<<
"config.stretch="
<<
config_stretch
<<
"
\n
"
;
if
(
config_header
!=
-
1
)
std
::
cout
<<
"config.header="
<<
config_header
<<
"
\n
"
;
std
::
cout
<<
"
\n
"
;
}
bool
isValidConfig
()
{
if
(
config_min
>
config_max
||
config_width
<=
0
)
if
(
config_min
>
config_max
||
config_width
<=
0
||
(
config_stretch
!=
-
1
&&
config_stretch
!=
0
&&
config_stretch
!=
1
)
||
(
config_header
!=
-
1
&&
config_header
!=
0
&&
config_header
!=
1
)
)
{
std
::
cerr
<<
"Invalid configuration
\n
"
;
return
false
;
...
...
@@ -41,8 +48,11 @@ bool isCellWidthTooShort(const std::vector<std::vector<std::string>> &values)
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
;
if
(
config_stretch
==
-
1
)
{
std
::
cerr
<<
"Cell is too short
\n
"
;
return
true
;
}
}
}
catch
(
const
std
::
invalid_argument
&
)
...
...
@@ -118,13 +128,44 @@ bool isOutOfRange(const std::vector<std::vector<std::string>> &values)
return
false
;
}
// Funkce pro zjištění maximální šířky buňky potřebné pro čísla
int
calculateMaxCellWidth
(
const
std
::
vector
<
std
::
vector
<
std
::
string
>>
&
values
)
{
int
maxWidth
=
config_width
;
if
(
config_stretch
==
1
)
{
for
(
const
auto
&
row
:
values
)
{
for
(
const
auto
&
cell
:
row
)
{
try
{
int
value
=
std
::
stoi
(
cell
);
int
length
=
std
::
to_string
(
value
).
length
();
if
(
length
>
maxWidth
)
{
maxWidth
=
length
;
}
}
catch
(...)
{
// Ignoruje buňky, které nejsou čísla
}
}
}
}
return
maxWidth
;
}
void
printTable
(
std
::
vector
<
std
::
vector
<
std
::
string
>>
&
values
)
{
if
(
isOutOfRange
(
values
))
{
return
;
}
printConfig
();
int
actualWidth
=
calculateMaxCellWidth
(
values
);
printConfig
(
actualWidth
);
size_t
cols
=
0
;
for
(
const
auto
&
row
:
values
)
{
...
...
@@ -140,43 +181,95 @@ void printTable(std::vector<std::vector<std::string>> &values)
}
}
// Určení skutečné šířky buňky (s ohledem na stretch)
std
::
string
separator
=
"+"
;
for
(
size_t
i
=
0
;
i
<=
cols
;
i
++
)
if
(
config_header
==
0
)
{
separator
+=
std
::
string
(
config_width
+
2
,
'-'
)
+
"+"
;
for
(
size_t
i
=
0
;
i
<=
cols
-
1
;
i
++
)
{
separator
+=
std
::
string
(
actualWidth
+
2
,
'-'
)
+
"+"
;
}
}
std
::
cout
<<
separator
<<
"
\n
| |"
;
for
(
size_t
i
=
0
;
i
<
cols
;
i
++
)
else
{
if
(
config_align
==
"right"
)
for
(
size_t
i
=
0
;
i
<=
cols
;
i
++
)
{
s
td
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
right
<<
static_cast
<
char
>
(
'A'
+
i
)
<<
" |
"
;
s
eparator
+=
std
::
string
(
actualWidth
+
2
,
'-'
)
+
"+
"
;
}
else
}
// Vykreslení hlavičky pouze pokud config_header != 0
if
(
config_header
!=
0
)
{
std
::
cout
<<
separator
<<
"
\n
| "
<<
std
::
setw
(
actualWidth
)
<<
" |"
;
for
(
size_t
i
=
0
;
i
<
cols
;
i
++
)
{
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
left
<<
static_cast
<
char
>
(
'A'
+
i
)
<<
" |"
;
if
(
config_align
==
"right"
)
{
std
::
cout
<<
" "
<<
std
::
setw
(
actualWidth
)
<<
std
::
right
<<
static_cast
<
char
>
(
'A'
+
i
)
<<
" |"
;
}
else
{
std
::
cout
<<
" "
<<
std
::
setw
(
actualWidth
)
<<
std
::
left
<<
static_cast
<
char
>
(
'A'
+
i
)
<<
" |"
;
}
}
std
::
cout
<<
"
\n
"
<<
separator
<<
"
\n
"
;
}
else
{
std
::
cout
<<
separator
<<
"
\n
"
;
}
std
::
cout
<<
"
\n
"
<<
separator
<<
"
\n
"
;
for
(
size_t
i
=
0
;
i
<
values
.
size
();
i
++
)
{
std
::
cout
<<
"| "
<<
std
::
setw
(
3
)
<<
i
+
1
<<
" |"
;
// Vykreslení čísla řádku pouze pokud config_header != 0
if
(
config_header
!=
0
)
{
std
::
cout
<<
"| "
<<
std
::
setw
(
actualWidth
)
<<
i
+
1
<<
" |"
;
}
else
{
std
::
cout
<<
"|"
;
}
for
(
auto
&
cell
:
values
[
i
])
{
if
(
cell
.
find
(
"SUM("
)
!=
std
::
string
::
npos
)
{
cell
=
std
::
to_string
(
computeSum
(
values
[
i
],
cell
));
}
if
(
config_align
==
"right"
)
bool
isTooLong
=
false
;
try
{
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
right
<<
cell
<<
" |"
;
int
value
=
std
::
stoi
(
cell
);
if
(
std
::
to_string
(
value
).
length
()
>
static_cast
<
size_t
>
(
config_width
)
&&
config_stretch
==
0
)
{
isTooLong
=
true
;
}
}
catch
(...)
{
// Není číslo, neřešíme
}
if
(
isTooLong
)
{
// Vyplníme buňku znaky #
std
::
cout
<<
" "
<<
std
::
string
(
actualWidth
,
'#'
)
<<
" |"
;
}
else
{
std
::
cout
<<
" "
<<
std
::
setw
(
config_width
)
<<
std
::
left
<<
cell
<<
" |"
;
if
(
config_align
==
"right"
)
{
std
::
cout
<<
" "
<<
std
::
setw
(
actualWidth
)
<<
std
::
right
<<
cell
<<
" |"
;
}
else
{
std
::
cout
<<
" "
<<
std
::
setw
(
actualWidth
)
<<
std
::
left
<<
cell
<<
" |"
;
}
}
}
std
::
cout
<<
"
\n
"
...
...
@@ -200,6 +293,10 @@ int main()
config_width
=
std
::
stoi
(
config
.
value
);
else
if
(
config
.
type
==
"align"
)
config_align
=
config
.
value
;
else
if
(
config
.
type
==
"stretch"
)
config_stretch
=
std
::
stoi
(
config
.
value
);
else
if
(
config
.
type
==
"header"
)
config_header
=
std
::
stoi
(
config
.
value
);
}
}
...
...
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