Browse Source

Merge branch 'master' of github.com:charlesreid1/hello-world

* '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 Awk
master
Charles Reid 8 years ago
parent
commit
e69cdc44de
  1. 32
      README.md
  2. 78
      awk/README.md
  3. 0
      awk/http_get.awk
  4. 36
      awk/http_server.awk
  5. 50
      awk/simpsons_rule.awk
  6. 34
      bash/README.md
  7. 17
      bash/factorial.sh
  8. 11
      bash/hello_world.sh
  9. 0
      bash/http_get.sh
  10. 2
      c/http_get.c
  11. 9
      java/Widget.java

32
README.md

@ -47,25 +47,38 @@ What time is it: @@ -47,25 +47,38 @@ What time is it:
Factorial:
* Implements the factorial function: `4! = 4*3*2*1`
* http://rosettacode.org/wiki/Factorial
### List of Programs In Progress
* Uses iterative and recursive versions
HTTP Get:
* Implements accessing an HTTP resource using get
* http://rosettacode.org/wiki/HTTP
* [Link](http://rosettacode.org/wiki/HTTP)
HTTP Server:
* Implements running an http server
* https://rosettacode.org/wiki/Hello_world/Web_server
* [Link](https://rosettacode.org/wiki/Hello_world/Web_server)
Calculus: Simpson's Rule for Integration
* Implements composite Simpson's Rule to integrate a function over an interval `[a,b]` using `n` approximating points
### Programs In Progress
HTTP Post:
* Access an API using POST request
Algebra: Log and Trig Tables
* Print out logarithm and trigonometric tables
* Exercises trigonometric functions, formatted print functionality, loops
### List of Programs To Implement
Calculus: Taylor Series Approximation of Sine Function
* Implements a Taylor Series to approximate a sine function
Pi/Sqrt/Logs:
* Print out a log table, square root table, pi, etc.
Calculus: Newton's Method
* Implements Newton's Method to compute the roots of an arbitrary function f(x)
### Programs To Implement
Arrays:
* Use a native built-in data type to store a 1D array of data
@ -86,4 +99,3 @@ Calculus: @@ -86,4 +99,3 @@ Calculus:
* Compute the numerical integral of a function
* Integrate a differential equation

78
awk/README.md

@ -10,6 +10,14 @@ $ awk -f hello_world.awk @@ -10,6 +10,14 @@ $ awk -f hello_world.awk
Hello world!
```
## What Time Is It?
Prints the current date and time using built-in awk functions (strftime).
```
$ awk -f what_time_is_it.awk
```
## 99 Bottles
Prints the lyrics to the 99 Bottles of Beer on the Wall song:
@ -52,6 +60,54 @@ Take one down, pass it around. @@ -52,6 +60,54 @@ Take one down, pass it around.
[...]
```
## HTTP Get
This file will get a webpage using an HTTP GET request. Run the script like this:
```
$ awk -f http_get.awk
HTTP/1.1 200 OK
Server: nginx/1.9.3 (Ubuntu)
Date: Thu, 09 Mar 2017 19:45:26 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Content-Type-Options: nosniff
Content-language: en
X-UA-Compatible: IE=Edge
Vary: Accept-Encoding,Cookie
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: private, must-revalidate, max-age=0
Last-Modified: Sun, 19 Feb 2017 21:59:32 GMT
<!DOCTYPE html>
<html lang="en" dir="ltr" class="client-nojs">
<head>
<meta charset="UTF-8" />
<title>charlesreid1</title>
```
## HTTP Post
This will send a POST request to a server. Specifically, this will call a URL and pass POST data with the request.
```
$ awk -f http_post.awk
```
## HTTP Server
Yes, you can abuse awk, a text manipulation scripting language, to run a web server. This utilizes networking capabilities deep within the bowels of awk.
The following script listens for a single request on port 8080. Once the user has visited localhost:8080, awk responds with an HTTP 200 OK code, and a "Hello World" page.
Once the page has been returned, the script will die; the HTTP server is not persistent.
```
awk -f http_server.awk
```
## Factorial
Print the factorial of a number (12 by default). Edit to change it to your own number. To run:
@ -62,27 +118,12 @@ $ awk -f factorial.awk @@ -62,27 +118,12 @@ $ awk -f factorial.awk
12! = 479001600
```
## HTTP Get
## Simpson's Rule
This file will get a webpage using an HTTP Get request.
This integrates the function `sin(x)` from 0 to pi/2 (the result should be exactly 1) using Simpson's composite rule and a user-specified number of sample points.
```
$ awk -f http.awk HTTP/1.1 301 TLS Redirect
Server: Varnish
Location: https://en.wikipedia.org/wiki/Apollo_11
Content-Length: 0
Accept-Ranges: bytes
Date: Thu, 28 Jul 2016 06:40:42 GMT
X-Varnish: 1997612095
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: cp1053 int
Set-Cookie: WMF-Last-Access=28-Jul-2016;Path=/;HttpOnly;secure;Expires=Mon, 29 Aug 2016 00:00:00 GMT
X-Client-IP: 209.95.50.128
Cache-Control: private, s-maxage=0, max-age=0, must-revalidate
Set-Cookie: GeoIP=US:NY:New_York:40.74:-74.00:v4; Path=/; secure; Domain=.wikipedia.org
awk -f simpsons_rule.awk
```
@ -90,3 +131,4 @@ Set-Cookie: GeoIP=US:NY:New_York:40.74:-74.00:v4; Path=/; secure; Domain=.wikipe @@ -90,3 +131,4 @@ Set-Cookie: GeoIP=US:NY:New_York:40.74:-74.00:v4; Path=/; secure; Domain=.wikipe

0
awk/http.awk → awk/http_get.awk

36
awk/http_server.awk

@ -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)
}

50
awk/simpsons_rule.awk

@ -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)
}
}

34
bash/README.md

@ -12,7 +12,12 @@ Prints a message to the screen. Uses the echo function: @@ -12,7 +12,12 @@ Prints a message to the screen. Uses the echo function:
## 99 Bottles
Shows off how we concatenate strings, increment numbers, convert types, and call functions.
Utilizes functions to print out various pieces of a message. This prints "99 Bottles of Beer" with correct punctuation.
```
./99bottles.sh
```
## What Time Is It
@ -22,25 +27,32 @@ Prints the current date and time. Uses the date utility: @@ -22,25 +27,32 @@ Prints the current date and time. Uses the date utility:
./what_time_is_it.sh
```
### Http Server
Runs a web server at port 8080. Uses netcat `nc`:
## HTTP Get
Get a URL using curl.
```
./http_server.sh
./http_get.sh
```
Warning: this process can't be killed with Control+C.
Use Control-Z, then look for the process (`ps aux | grep nc`),
then kill the process (`kill -9 <PID>`).
## HTTP Get
## HTTP Post
Get a URL using curl.
Send POST data to a URL using curl.
```
./http.sh
./http_post.sh
```
### HTTP Server
Runs a web server at port 8080. Uses netcat `nc`:
```
./http_server.sh
```
Not sure if I should be saving to a file........
### Factorial

17
bash/factorial.sh

@ -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

11
bash/hello_world.sh

@ -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"

0
bash/http.sh → bash/http_get.sh

2
c/http.c → c/http_get.c

@ -14,7 +14,7 @@ main(void) @@ -14,7 +14,7 @@ main(void)
char buffer[CURL_ERROR_SIZE];
// use curl library
// following libcurl.org documentation/example
// following example provided in libcurl.org documentation
if ((curl = curl_easy_init()) != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.wikipedia.org/en/Apollo_11");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

9
java/Widget.java

@ -1,9 +0,0 @@ @@ -1,9 +0,0 @@
public class Widget {
private String message;
public Widget() {
this.message = "Hello object-world!";
}
public void printMessage() {
System.out.println( this.message );
}
}
Loading…
Cancel
Save