Commit 88d125f5 authored by Hans Buchmann's avatar Hans Buchmann

solutions

parent 8358ac25
......@@ -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;
}
//----------------------
//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";
}
......@@ -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;
}
......
......@@ -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;
......
......@@ -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;
}
//0x11xx 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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment