Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
C
cpp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edu
cpp
Commits
88d125f5
Commit
88d125f5
authored
Nov 22, 2020
by
Hans Buchmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
solutions
parent
8358ac25
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
57 deletions
+72
-57
exams/p2014-11-20/area.cc
exams/p2014-11-20/area.cc
+9
-7
exams/p2014-11-20/exp2.cc
exams/p2014-11-20/exp2.cc
+26
-0
exams/p2014-11-20/ld2.cc
exams/p2014-11-20/ld2.cc
+10
-1
exams/p2014-11-20/unsigned.cc
exams/p2014-11-20/unsigned.cc
+11
-30
exams/p2014-11-20/utf8-string.cc
exams/p2014-11-20/utf8-string.cc
+16
-19
No files found.
exams/p2014-11-20/area.cc
View file @
88d125f5
...
...
@@ -6,19 +6,21 @@
double
area
(
double
*
x
,
double
*
y
,
unsigned
n
)
{
double
s
=
0
;
//sum
double
a
=
0
;
for
(
unsigned
i
=
1
;
i
<
n
;
++
i
)
{
s
=
s
+
(
x
[
i
-
1
]
*
y
[
i
]
-
y
[
i
-
1
]
*
x
[
i
]);
a
+=
(
x
[
i
-
1
]
*
y
[
i
]
-
y
[
i
-
1
]
*
x
[
i
]);
}
s
=
s
+
(
x
[
n
-
1
]
*
y
[
0
]
-
y
[
n
-
1
]
*
x
[
0
]);
return
0.5
*
s
;
a
+=
(
x
[
n
-
1
]
*
y
[
0
]
-
y
[
n
-
1
]
*
x
[
0
]);
return
0.5
*
a
;
}
int
main
(
int
argc
,
char
**
args
)
{
double
x
[]
=
{
-
0.0400682349440255
,
0.8609006329573937
,
0.4270168938398355
,
-
0.4739519740615837
};
double
y
[]
=
{
0.3136790342824826
,
0.7475627734100397
,
1.6485316413024589
,
1.2146479021849006
};
std
::
cout
<<
"a="
<<
area
(
x
,
y
,
4
)
<<
"
\n
"
;
double
x
[]
=
{
1
,
2
,
2
,
1
};
double
y
[]
=
{
5
,
5
,
6
,
6
};
double
a
=
area
(
x
,
y
,
4
);
std
::
cout
<<
"a="
<<
a
<<
"
\n
"
;
return
0
;
}
exams/p2014-11-20/exp2.cc
0 → 100644
View file @
88d125f5
//----------------------
//exp2
//(c) H.Buchmann FHNW 2014
//----------------------
#include <iostream>
#include <sstream>
bool
isExp2
(
unsigned
val
)
{
return
(
val
&
(
-
val
))
==
val
;
}
unsigned
toUnsigned
(
char
*
s
)
{
std
::
stringstream
ss
(
s
);
unsigned
v
=
0
;
ss
>>
v
;
return
v
;
}
int
main
(
int
argc
,
char
**
args
)
{
unsigned
v
=
toUnsigned
(
args
[
1
]);
std
::
cerr
<<
"isExp2("
<<
v
<<
")="
<<
isExp2
(
v
)
<<
"
\n
"
;
}
exams/p2014-11-20/ld2.cc
View file @
88d125f5
...
...
@@ -5,9 +5,18 @@
#include <iostream>
#include <iomanip>
ld2
(
unsigned
val
)
unsigned
ld2
(
unsigned
val
)
{
static
const
unsigned
N
=
sizeof
(
val
)
*
8
;
//the number of bits
unsigned
log
=
N
;
unsigned
m
=
(
1
<<
(
N
-
1
));
while
(
m
)
{
--
log
;
if
(
val
&
m
)
return
log
;
m
>>=
1
;
}
return
log
-
1
;
}
...
...
exams/p2014-11-20/unsigned.cc
View file @
88d125f5
...
...
@@ -54,25 +54,6 @@ unsigned String::hexDigit(char ch)
return
10
+
(
ch
-
'a'
);
}
/* the fragments
(--------------------------------------A
--------------------------------------A )
(-------------------------------------- B
-------------------------------------- B)
(-------------------------------------- C
-------------------------------------- C)
(-------------------------------------- D
-------------------------------------- D)
(-------------------------------------- E
-------------------------------------- E)
*/
bool
String
::
to
(
char
*
s
,
unsigned
*
val
)
{
unsigned
i
=
0
;
//index in s
...
...
@@ -83,49 +64,49 @@ bool String::to(char* s,unsigned* val)
char
ch
=
s
[
i
++
];
switch
(
status
)
{
case
0
:
case
0
:
if
(
ch
==
'0'
)
{
v
=
0
;
status
=
2
;
status
=
1
;
break
;
}
if
(
isDecDigit
(
ch
))
{
v
=
decDigit
(
ch
);
status
=
1
;
status
=
2
;
break
;
}
return
false
;
case
1
:
if
(
ch
==
'\0'
)
if
(
ch
==
'\0'
)
{
*
val
=
v
;
return
true
;
}
if
((
ch
==
'x'
)
||
(
ch
==
'X'
))
{
status
=
3
;
break
;
}
if
(
isDecDigit
(
ch
))
{
v
=
10
*
v
+
decDigit
(
ch
);
status
=
2
;
break
;
}
return
false
;
case
2
:
if
(
ch
==
'\0'
)
if
(
ch
==
'\0'
)
{
*
val
=
v
;
return
true
;
}
if
((
ch
==
'x'
)
||
(
ch
==
'X'
))
{
status
=
3
;
break
;
}
if
(
isDecDigit
(
ch
))
{
v
=
10
*
v
+
decDigit
(
ch
);
status
=
1
;
break
;
}
return
false
;
...
...
exams/p2014-11-20/utf8-string.cc
View file @
88d125f5
...
...
@@ -13,38 +13,35 @@ class UTF8String
//assuming correct utf-8 string
unsigned
UTF8String
::
len
(
char
*
s
)
{
unsigned
l
=
0
;
//the char count
unsigned
i
=
0
;
//index in s
unsigned
l
=
0
;
//length l<=i
while
(
true
)
{
char
ch
=
s
[
i
];
if
(
ch
==
'\0'
)
break
;
l
++
;
if
((
ch
&
0b10000000
)
==
0
)
char
ch
=
s
[
i
];
//current char
if
(
ch
==
'\0'
)
return
l
;
if
((
ch
&
(
1
<<
7
))
==
0
)
{
i
++
;
continue
;
// next step in while loop
++
i
;
++
l
;
continue
;
}
//0x11x
x xxxx
if
((
ch
&
0b00100000
)
==
0
)
// 0x11z
x xxxx
if
((
ch
&
(
1
<<
5
)
)
==
0
)
{
i
+=
2
;
++
l
;
continue
;
}
//0x111x xxxx
if
((
ch
&
0b00010000
)
==
0
)
// 0x111z xxxx
if
((
ch
&
(
1
<<
4
)
)
==
0
)
{
i
+=
3
;
++
l
;
continue
;
}
//0x1111 xxxx
if
((
ch
&
0b00001000
)
==
0
)
{
i
+=
4
;
continue
;
}
}
return
l
;
i
+=
4
;
++
l
;
}
}
int
main
(
int
argc
,
char
**
args
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment