~hamblingreen/practical-c

e4a8790b954145f57bc0a906cf0cb9c7206693c8 — Bobby Hamblin 11 months ago e46087b
add new projects, remove readmes
15 files changed, 296 insertions(+), 82 deletions(-)

M README
D cconvert/README
A checkerboard/Makefile
A checkerboard/checkerboard.c
D dateline/README
D posneg/README
D posneg/posneg
D posneg/posneg.o
D primeornot/README
A resistors/Makefile
A resistors/resistors.c
D serialCalc/README
D taxcalc/README
A vowel_or_consonant/Makefile
A vowel_or_consonant/vowel_or_consonant.c
M README => README +6 -5
@@ 1,8 1,9 @@
Simple C Programs
=================
By Hamblingreen
By Bobby Hamblin

This is a collection of short and sweet programs written in C. I got the prompts
from Steve Oualline's book *Practical C Programming*. The docs are included in each folders README and in the C files themeslves. The comment boxes on the C files are
designed to fit on the Pinephone's screen. These programs are licensed under the
MIT License.
This is a collection of short and sweet programs written in C. I got
the prompts from Steve Oualline's book *Practical C Programming*. The
docs are included in each C file. The comment boxes on the C files are
designed to fit on the Pinephone's screen. These programs are licensed
under the MIT License.

D cconvert/README => cconvert/README +0 -20
@@ 1,20 0,0 @@
cConvert -- convert imperial to metric units in your terminal By hamblingreen [hamblingreen@hotmail.com](mailto:hamblingreen@hotmail.com)

Purpose: Convert units of measurement from imperial to metric through the command line. To be small, fast, well-documented, and demonstrate the life cycle of a simple C program.

Usage: Type in the number of units you need to convert, followed by the unit character according to the following chart:

## Unit | Meaning

i | Inches f | Feet y | Yards m | Miles

Reference: Steve Oualline

Restrictions: Only works for the units listed above. Corresponding metric unit not listed in output.

Roadmap:

* make decimal places configurable
* use full names of units
* include name of metric unit in output
* add exit key/command like 'q' or 'quit'
\ No newline at end of file

A checkerboard/Makefile => checkerboard/Makefile +7 -0
@@ 0,0 1,7 @@
#
# Makefile for the program taxCalc
#
# Turn on debugging
CFLAGS=-g
checkerboard:checkerboard.o
	$(CC) $(CFLAGS) -o checkerboard checkerboard.o

A checkerboard/checkerboard.c => checkerboard/checkerboard.c +39 -0
@@ 0,0 1,39 @@
/****************************************
* checkerboard -- print checkerboard	*
* Author: hamblingreen			*
*   <hamblingreen@hotmail.com>		*
* Purpose: demonstrate for loops	*
* Reference: Steve Oualline		*
****************************************/

#include <stdio.h>

int counter;
int counter2;
int counter3;
int width = 7; /* width of checkerboard - 1 */
int height = 7; /* width of checkerboard - 1 */

int main() {
	for (counter2 = 0; counter2 <= height; counter2++) {
		/* Print top row */
		for (counter = 0; counter <= width; counter++) {
			printf("+-----");
		}
		printf("+\n");
		/* Print middle */
		for (counter3 = 0; counter3 <= 3; counter3++) {
			for (counter = 0; counter <= width; counter++) {
				printf("|     ");
			}
			printf("|\n");
			}
		}
		/* Print bottom row */
		for (counter = 0; counter <= width; counter++) {
			printf("+-----");
		}
		printf("+\n");

	return (0);
}

D dateline/README => dateline/README +0 -9
@@ 1,9 0,0 @@
Dateline -- Find the time between 2 different dates

By hamblingreen <hamblingreen@hotmail.com> 

Purpose: Find the time between 2 dates, and display that information in a command-line-interface. To be small, fast, well-documented, and demonstrate the life cycle of a simple C program.

Usage: Type in the first date, followed by the second in the following format: 12 4 22 4 2 23. Enter a blank line to exit.

Reference: Steve Oualline

D posneg/README => posneg/README +0 -12
@@ 1,12 0,0 @@
posneg -- Calculate the number of positive and negative numbers in a
data set

By hamblingreen <hamblingreen@hotmail.com> 

Purpose: Calculate the number of positive and negative numbers in a data
set. To be small, fast, well-documented, and demonstrate the life cycle
of a simple C program.

Usage: Type in numbers seperated by spaces

Reference: Steve Oualline

D posneg/posneg => posneg/posneg +0 -0
D posneg/posneg.o => posneg/posneg.o +0 -0
D primeornot/README => primeornot/README +0 -18
@@ 1,18 0,0 @@
PrimeOrNot -- Find out whether a number is prime or not through a series of tests

By hamblingreen <hamblingreen@hotmail.com> 

Purpose: Find out whether a number is prime or not and display that information in a command-line interface. To be small, fast, well-documented, and demonstrate the life cycle of a simple C program.

Usage: Type in number. Enter a blank line to exit.

Reference: Steve Oualline

code structure

while loop
prompt user input
check for exit value
last digit check
sum divisible by 3 check


A resistors/Makefile => resistors/Makefile +7 -0
@@ 0,0 1,7 @@
#
# Makefile for the program taxCalc
#
# Turn on debugging
CFLAGS=-g
resistors:resistors.o
	$(CC) $(CFLAGS) -o resistors resistors.o

A resistors/resistors.c => resistors/resistors.c +42 -0
@@ 0,0 1,42 @@
/****************************************
* resistors -- compute resistance	*
* Purpose: compute the total resistance	*
*   for a number of parallel resistors	*
* Usage: input resistance in ohms at	*
*   prompt, type 'q' to exit		*
* Author: hamblingreen			*
*   <hamblingreen@hotmail.com>		*
* Purpose: demonstrate for loops	*
* Reference: Steve Oualline		*
****************************************/

#include <stdio.h>

int main() {
	char line[10];
	int resistor_count = 0; /* number of resistors in the network */
	int current_resistor = 0; /* resistance of currently input resistor */
	float total_resistance = 0; /* total resistance of all resistors in network */

	while (1) {
		printf("Input resistance in ohms (e.x. 200): ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%d", &current_resistor);

		if (current_resistor > total_resistance)
			total_resistance = current_resistor;

		if (current_resistor == 0) {
			resistor_count++;
			break;
		} else {
			resistor_count++;
		}
	}

	total_resistance /= resistor_count;

	printf("%g\n", total_resistance);

	return (0);
}

D serialCalc/README => serialCalc/README +0 -9
@@ 1,9 0,0 @@
serialCalc -- Serial data transfer speed calculator

By hamblingreen <hamblingreen@hotmail.com> 

Purpose: Calculate how long data transfer through a serial cable would take. To be small, fast, well-documented, and demonstrate the life cycle of a simple C program.

Usage: Type in the data size in Megabytes and, if you wish, the baud rate of your serial port (seperated by a space). If the baud rate is not specified, the Pinephone's default of 115200 will be used.

Reference: Steve Oualline

D taxcalc/README => taxcalc/README +0 -9
@@ 1,9 0,0 @@
TaxCalc -- Sales tax calculator

By hamblingreen <hamblingreen@hotmail.com> 

Purpose: Find the added cost of sales tax to a product of a particular price, and display this information in a command-line interface. To be small, fast, well-documented, and demonstrate the life cycle of a simple C program.

Usage: Type in price and (optionally) desired sales tax percentage. The default sales tax percentage is 8%. Enter a blank line to exit.

Reference: Steve Oualline

A vowel_or_consonant/Makefile => vowel_or_consonant/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for the program vowel_or_consonant

# Turn on debugging
CFLAGS=-g
vowel_or_consonant:vowel_or_consonant.o
	$(CC) $(CFLAGS) -o vowel_or_consonant vowel_or_consonant.o

A vowel_or_consonant/vowel_or_consonant.c => vowel_or_consonant/vowel_or_consonant.c +189 -0
@@ 0,0 1,189 @@
/****************************************
* vowel_or_consonant - check whether	*
*   character is a consonant or vowel	*
* Usage: Type in character at prompt	*
****************************************/
#include <stdio.h>

int main(void) {
	char line[2] = "0"; /* Input line */
	char input = '0'; /* Character input */
	int vowel = '0'; /* Vowel or not */

	/* Receive user input */
	printf("Input character (a-z, A-Z): ");
	fgets(line, sizeof(line), stdin);
	sscanf(line, "%c", &input);

	/* Check for vowel, consonant, or non-letter */
	switch (input) {
		case 'a':
			vowel = 1;
			break;
		case 'A':
			vowel = 1;
			break;
		case 'b':
			vowel = 0;
			break;
		case 'B':
			vowel = 0;
			break;
		case 'c':
			vowel = 0;
			break;
		case 'C':
			vowel = 0;
			break;
		case 'd':
			vowel = 0;
			break;
		case 'D':
			vowel = 0;
			break;
		case 'e':
			vowel = 1;
			break;
		case 'E':
			vowel = 1;
			break;
		case 'f':
			vowel = 0;
			break;
		case 'F':
			vowel = 0;
			break;
		case 'g':
			vowel = 0;
			break;
		case 'G':
			vowel = 0;
			break;
		case 'h':
			vowel = 0;
			break;
		case 'H':
			vowel = 0;
			break;
		case 'i':
			vowel = 1;
			break;
		case 'I':
			vowel = 1;
			break;
		case 'j':
			vowel = 0;
			break;
		case 'J':
			vowel = 0;
			break;
		case 'k':
			vowel = 0;
			break;
		case 'K':
			vowel = 0;
			break;
		case 'l':
			vowel = 0;
			break;
		case 'L':
			vowel = 0;
			break;
		case 'm':
			vowel = 0;
			break;
		case 'M':
			vowel = 0;
			break;
		case 'n':
			vowel = 0;
			break;
		case 'N':
			vowel = 0;
			break;
		case 'o':
			vowel = 1;
			break;
		case 'O':
			vowel = 1;
			break;
		case 'p':
			vowel = 0;
			break;
		case 'P':
			vowel = 0;
			break;
		case 'q':
			vowel = 0;
			break;
		case 'Q':
			vowel = 0;
			break;
		case 'r':
			vowel = 0;
			break;
		case 'R':
			vowel = 0;
			break;
		case 's':
			vowel = 0;
			break;
		case 'S':
			vowel = 0;
			break;
		case 't':
			vowel = 0;
			break;
		case 'T':
			vowel = 0;
			break;
		case 'u':
			vowel = 1;
			break;
		case 'U':
			vowel = 1;
			break;
		case 'v':
			vowel = 0;
			break;
		case 'V':
			vowel = 0;
			break;
		case 'w':
			vowel = 0;
			break;
		case 'W':
			vowel = 0;
			break;
		case 'x':
			vowel = 0;
			break;
		case 'X':
			vowel = 0;
			break;
		case 'y':
			printf("Y is sometimes a vowel");
			break;
		case 'Y':
			printf("Y is sometimes a vowel");
			break;
		case 'z':
			vowel = 0;
			break;
		case 'Z':
			vowel = 0;
			break;
		default:
			printf("The character input was not a letter. Try again!\n");
			break;
	}

	/* Print result */
	if (vowel == 0)
		printf("Character '%c' is not a vowel", input);
	if (vowel == 1)
		printf("Character '%c' is a vowel", input);

	return 0;
}