Browse Source
* 'master' of github.com:charlesreid1/hello-world: updating http.awk to be http_get.awk removing java widget (moving to hello-oop repo) updating list of programs in master readme updating bash http GET script and factorial. corresponding updates to readme. moving C http get from http to http_get rearranging bash hello world script as a function improve message printing for Awk Simpsons Rule updating Awk http server script add simpsons rule integral calculator in Awkmaster
11 changed files with 207 additions and 62 deletions
@ -1,14 +1,26 @@
@@ -1,14 +1,26 @@
|
||||
# This program runs an HTTP server on port 8080. |
||||
# It will listen for a single connection, send an HTTP response, then die. |
||||
BEGIN { |
||||
RS = ORS = "\r\n" |
||||
HttpService = "/inet/tcp/8080/0/0" |
||||
Hello = "<HTML><HEAD>" \ |
||||
"<TITLE>A Famous Greeting</TITLE></HEAD>" \ |
||||
"<BODY><H1>Hello, world</H1></BODY></HTML>" |
||||
Len = length(Hello) + length(ORS) |
||||
print "HTTP/1.0 200 OK" |& HttpService |
||||
print "Content-Length: " Len ORS |& HttpService |
||||
print Hello |& HttpService |
||||
while ((HttpService |& getline) > 0) |
||||
continue; |
||||
close(HttpService) |
||||
RS = ORS = "\r\n" |
||||
|
||||
# Reach into the bowels of awk |
||||
HttpService = "/inet/tcp/8080/0/0" |
||||
|
||||
# Prepare the HTML response |
||||
Hello = "<HTML><HEAD>" \ |
||||
"<TITLE>A Famous Greeting</TITLE></HEAD>" \ |
||||
"<BODY><H1>Hello, world</H1></BODY></HTML>" |
||||
|
||||
# Return the response to the HTTP service |
||||
Len = length(Hello) + length(ORS) |
||||
print "HTTP/1.0 200 OK" |& HttpService |
||||
print "Content-Length: " Len ORS |& HttpService |
||||
print Hello |& HttpService |
||||
|
||||
# Now wait for a response |
||||
while ((HttpService |& getline) > 0) |
||||
continue; |
||||
|
||||
# Close it out |
||||
close(HttpService) |
||||
} |
||||
|
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
# Define a way for us to assert something is true |
||||
function assert(condition, string) |
||||
{ |
||||
if (! condition) { |
||||
printf("%s:%d: assertion failed: %s\n", |
||||
FILENAME, FNR, string) > "/dev/stderr" |
||||
_assert_exit = 1 |
||||
exit 1 |
||||
} |
||||
} |
||||
|
||||
# The function we are integrating - sin(x) |
||||
function f(x) { |
||||
return sin(x) |
||||
} |
||||
|
||||
# Simpson's rule: |
||||
# Integrates f(x) from a to b, using n intervals |
||||
function simpsons_rule(a,b,n) { |
||||
# a = lower bound of integral |
||||
# b = upper bound of integral |
||||
# n = number of partitions on the interval |
||||
assert(n%2==0,"n%2==0") |
||||
s1 = 0.0 |
||||
s2 = 0.0 |
||||
sum = 0.0 |
||||
h = (b - a) / n |
||||
for(i=1; i<=n; i++) { |
||||
x_i = a * i*h |
||||
if(i%2==0) { |
||||
s1 += f(x_i) |
||||
} else { |
||||
s2 += f(x_i) |
||||
} |
||||
} |
||||
sum = (h/3.0)*(f(a) + f(b) + 2*s1 + 4*s2) |
||||
return sum |
||||
} |
||||
|
||||
BEGIN { |
||||
printf("Simpson's Rule Integral Calculation:") |
||||
printf("Integrating sin(x) from 0 to pi/2\n") |
||||
pi = atan2(0, -1) |
||||
a = 0.0 |
||||
b = pi/2 |
||||
for(n = 10; n<=100000; n*=10) { |
||||
result = simpsons_rule(a,b,n) |
||||
printf("%d segments: int(sin(x)) = %0.8f \n",n,result) |
||||
} |
||||
} |
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash |
||||
|
||||
function factorial() { |
||||
n=${1} |
||||
echo "$n" |
||||
if [ "$n" -eq "1" ] |
||||
then |
||||
return 1 |
||||
else |
||||
factorial $((n-1)) |
||||
fact=$? |
||||
# Do n times fact |
||||
echo "${fact}" |
||||
fi |
||||
} |
||||
|
||||
factorial 5 |
@ -1,3 +1,12 @@
@@ -1,3 +1,12 @@
|
||||
#!/bin/bash |
||||
|
||||
echo "Hello world!" |
||||
function hello_world { |
||||
echo "Hello world!" |
||||
} |
||||
|
||||
function hello_name { |
||||
echo "Hello world! This is ${1}" |
||||
} |
||||
|
||||
hello_world |
||||
hello_name "Robot Gerald Ford" |
||||
|
Loading…
Reference in new issue