Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
coding-streams
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
coding_tug
coding-streams
Commits
69196e46
Commit
69196e46
authored
Mar 24, 2021
by
Kerschbaumer, David
Browse files
Options
Downloads
Patches
Plain Diff
adding ku material 0317
parent
670504ef
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
2021-oop1/ku/stream-0317/product.cpp
+190
-0
190 additions, 0 deletions
2021-oop1/ku/stream-0317/product.cpp
2021-oop1/ku/stream-0317/vector.cpp
+41
-0
41 additions, 0 deletions
2021-oop1/ku/stream-0317/vector.cpp
with
231 additions
and
0 deletions
2021-oop1/ku/stream-0317/product.cpp
0 → 100755
+
190
−
0
View file @
69196e46
#include
<cstdio>
// stdio.h
#include
<cstring>
// string.h
#include
<vector>
class
Product
{
private:
char
name_
[
32
];
int
count_
;
int
cost_per_unit_
;
int
total_cost_
;
public:
Product
()
=
default
;
Product
(
const
char
*
name
,
int
count
,
int
cost_per_unit
);
char
*
getName
()
{
return
name_
;
}
int
getCount
()
{
return
count_
;
}
int
getCostPerUnit
()
{
return
cost_per_unit_
;
}
int
getTotalCost
()
{
return
total_cost_
;
}
void
setName
(
const
char
*
name
);
void
setCount
(
int
count
);
void
setCostPerUnit
(
int
cost_per_unit
);
void
increaseCount
(
int
value
);
void
updateTotalCost
();
void
print
();
void
merge
(
Product
&
prod
);
};
Product
::
Product
(
const
char
*
name
,
int
count
,
int
cost_per_unit
)
:
name_
(),
count_
(
count
),
cost_per_unit_
(
cost_per_unit
),
total_cost_
(
-
1
)
{
strncpy
(
name_
,
name
,
31
);
name_
[
31
]
=
0
;
if
(
count_
<
0
)
{
count_
=
0
;
}
if
(
cost_per_unit_
<
0
)
{
cost_per_unit_
=
0
;
}
}
void
Product
::
setName
(
const
char
*
name
)
{
strncpy
(
name_
,
name
,
31
);
name_
[
31
]
=
0
;
}
void
Product
::
setCount
(
int
count
)
{
count_
=
count
;
if
(
count_
<
0
)
{
count_
=
0
;
}
}
void
Product
::
setCostPerUnit
(
int
cost_per_unit
)
{
cost_per_unit_
=
cost_per_unit
;
if
(
cost_per_unit_
<
0
)
{
cost_per_unit_
=
0
;
}
}
void
Product
::
increaseCount
(
int
increment
)
{
if
(
increment
>=
1
)
count_
+=
increment
;
}
void
Product
::
updateTotalCost
()
{
total_cost_
=
cost_per_unit_
*
count_
;
}
void
Product
::
print
()
{
printf
(
"%s
\n
"
,
name_
);
printf
(
"
\t
Count: %d - "
"Cost: %d - "
"Total Cost: %d
\n
"
,
count_
,
cost_per_unit_
,
total_cost_
);
}
void
Product
::
merge
(
Product
&
prod
)
{
if
(
strcmp
(
name_
,
prod
.
name_
)
!=
0
)
return
;
if
(
cost_per_unit_
!=
prod
.
cost_per_unit_
)
return
;
count_
+=
prod
.
count_
;
prod
.
count_
=
0
;
updateTotalCost
();
prod
.
updateTotalCost
();
}
//--------------------------------------------------------------
// Vector added by David BEGIN
void
printShoppingList
(
std
::
vector
<
Product
>
shopping_list
)
{
printf
(
"------------------
\n
"
);
printf
(
"Shopping List:
\n
"
);
for
(
Product
product
:
shopping_list
)
{
product
.
print
();
}
printf
(
"------------------
\n
"
);
}
void
goShopping
()
{
std
::
vector
<
Product
>
shopping_list
;
Product
onion
(
"onion"
,
10
,
2
);
Product
banana
(
"banana"
,
3
,
1
);
Product
beer
(
"beer"
,
20
,
1
);
shopping_list
.
push_back
(
onion
);
shopping_list
.
push_back
(
banana
);
shopping_list
.
push_back
(
beer
);
printShoppingList
(
shopping_list
);
// WARNING: size() - 1 can be a problem if the shopping_list is empty
Product
last_product
=
shopping_list
.
at
(
shopping_list
.
size
()
-
1
);
last_product
.
print
();
shopping_list
.
pop_back
();
shopping_list
.
clear
();
printShoppingList
(
shopping_list
);
}
// Vector added by David END
//--------------------------------------------------------------
int
main
(
void
)
{
Product
prod
;
prod
.
setName
(
"Zwiebel"
);
prod
.
setCount
(
-
2
);
prod
.
setCostPerUnit
(
5
);
printf
(
"%s
\n
"
,
prod
.
getName
());
printf
(
"
\t
Count: %d - "
"Cost: %d - "
"Total Cost: %d
\n
"
,
prod
.
getCount
(),
prod
.
getCostPerUnit
(),
prod
.
getTotalCost
());
printf
(
"----------
\n
"
);
prod
.
increaseCount
(
4
);
prod
.
updateTotalCost
();
prod
.
print
();
printf
(
"----------
\n
"
);
Product
prod2
(
"Zwiebel"
,
2
,
5
);
prod2
.
updateTotalCost
();
prod2
.
print
();
printf
(
"----------
\n
"
);
prod
.
merge
(
prod2
);
prod
.
print
();
prod2
.
print
();
printf
(
"----------
\n
"
);
goShopping
();
return
0
;
}
This diff is collapsed.
Click to expand it.
2021-oop1/ku/stream-0317/vector.cpp
0 → 100755
+
41
−
0
View file @
69196e46
//
// program creates an int-vector and uses methods
// to manipulate the vector
//
#include
<cstdio>
#include
<vector>
void
printVector
(
std
::
vector
<
int
>
int_vector
)
{
printf
(
"(size %lu) "
,
int_vector
.
size
());
for
(
int
element
:
int_vector
)
{
printf
(
"[%d] "
,
element
);
}
printf
(
"
\n
"
);
}
int
main
()
{
std
::
vector
<
int
>
myvector
;
printVector
(
myvector
);
myvector
.
push_back
(
42
);
myvector
.
push_back
(
4
);
printVector
(
myvector
);
if
(
!
myvector
.
empty
()
&&
myvector
.
at
(
0
)
==
42
)
{
printf
(
"life, the universe and everything
\n
"
);
}
myvector
.
pop_back
();
printVector
(
myvector
);
myvector
.
clear
();
printVector
(
myvector
);
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
sign in
to comment