Programming in C of FC tutorial 3

###Full Circle C 5 ####Callback Use Call Back function for implementing a calculator:

#include <stdio.h>

int minus(int a, int b)
{
	return a-b;
}

int add(int a, int b)
{
	return a+b;
}

int multiply(int a, int b)
{
	return a*b;
}

int divide(int a, int b)
{
	return a/b;
}

typedef int (*mathFun)(int, int);

struct operator
{
	char c;
	mathFun f;
};

int main()
{
	struct operator functs[4];
	functs[0].c = '-'; functs[0].f=&minus;
	functs[1].c = '+'; functs[1].f=&add;
	functs[2].c = '*'; functs[2].f=&multiply;
	functs[3].c = '/'; functs[3].f=&divide;
	while(1)
	{
		int a,b,i;
		char c;
		printf("Enter a:\n");
		scanf("%d", &a);
		printf("Enter b:\n");
		scanf("%d", &b);
		printf("Enter the operator:\n");
		scanf("%c", &c);
		scanf("%c", &c);
		i = 0;
		while(i<4)
		{
			if(functs[i].c==c)
			{
				printf("Result:%d\n", functs[i].f(a,b));
				break;
			}
			i++;
		}
		if(i == 4)
		{
			printf("Unkown operator:%c\n", c);
		}
	}
	return 0;
}

####Exercise 1 Modify the application to operate on floating point numbers instead of integers.

#include <stdio.h>

float  minus(float a, float b)
{
	return a-b;
}

float add(float a, float b)
{
	return a+b;
}

float multiply(float a, float b)
{
	return a*b;
}

float divide(float a, float b)
{
	return a/b;
}

typedef float (*mathFun)(float, float);

struct operator
{
	char c;
	mathFun f;
};

int main()
{
	struct operator functs[4];
	functs[0].c = '-'; functs[0].f=&minus;
	functs[1].c = '+'; functs[1].f=&add;
	functs[2].c = '*'; functs[2].f=&multiply;
	functs[3].c = '/'; functs[3].f=&divide;
	while(1)
	{
		int i;
		float a,b; 
		char c;
		printf("Enter a:\n");
		scanf("%f", &a);
		printf("Enter b:\n");
		scanf("%f", &b);
		printf("Enter the operator:\n");
		scanf("%c", &c);
		scanf("%c", &c);
		i = 0;
		while(i<4)
		{
			if(functs[i].c==c)
			{
				printf("Result:%f\n", functs[i].f(a,b));
				break;
			}
			i++;
		}
		if(i == 4)
		{
			printf("Unkown operator:%c\n", c);
		}
	}
	return 0;
}

####Exercise 2 Extend the calculator with the possibility for the user to enter ‘q’ for quit

#include <stdio.h>

int minus(int a, int b)
{
	return a-b;
}

int add(int a, int b)
{
	return a+b;
}

int multiply(int a, int b)
{
	return a*b;
}

int divide(int a, int b)
{
	return a/b;
}

typedef int (*mathFun)(int, int);

struct operator
{
	char c;
	mathFun f;
};

int main()
{
	struct operator functs[4];
	functs[0].c = '-'; functs[0].f=&minus;
	functs[1].c = '+'; functs[1].f=&add;
	functs[2].c = '*'; functs[2].f=&multiply;
	functs[3].c = '/'; functs[3].f=&divide;
	char q;
	do
	{
		int a,b,i;
		char c;
		printf("Enter a:\n");
		scanf("%d", &a);
		printf("Enter b:\n");
		scanf("%d", &b);
		printf("Enter the operator:\n");
		scanf("%c", &c);
		scanf("%c", &c);
		i = 0;
		while(i<4)
		{
			if(functs[i].c==c)
			{
				printf("Result:%d\n", functs[i].f(a,b));
				break;
			}
			i++;
		}
		if(i == 4)
		{
			printf("Unkown operator:%c\n", c);
		}
		printf("Enter q for quit! Others for continue\n");
		scanf("%c",&q);
		scanf("%c",&q);
	}
	while(q != 'q');
	return 0;
}

####Exercise 4 Modify the application that, instead of entering characters, the user is able to enter “5 plus 6” or “6 minus 5”. In order to do this, you will need to adapt the structure to hold a string as the operator, and, instead of reading a character, you will need to read a string. Extra credit if you manage to do this without buffer overrun issues (see man getline) and memory leaks.

#include <stdio.h>
#include <string.h>

int minus(int a, int b)
{
	return a-b;
}

int add(int a, int b)
{
	return a+b;
}

int multiply(int a, int b)
{
	return a*b;
}

int divide(int a, int b)
{
	return a/b;
}

typedef int (*mathFun)(int, int);

struct operator
{	char c[20];
	mathFun f;
};

int main()
{
	struct operator functs[4];
	strcpy(functs[0].c, "minus");
	functs[0].f=&minus;
	strcpy(functs[1].c, "add");
	functs[1].f=&add;
	strcpy(functs[2].c, "mul");
	functs[2].f=&multiply;
	strcpy(functs[3].c, "div");
	functs[3].f=&divide;
	while(1)
	{
		int a,b,i;
		char c[20];
		printf("Enter a:\n");
		scanf("%d", &a);
		printf("Enter b:\n");
		scanf("%d", &b);
		printf("Enter the operator:\n");
		scanf("%s", c);
		i = 0;
		while(i<4)
		{
			int result = strcmp(functs[i].c, c);
			if(result == 0)
			{
			printf("Result is :%d\n", functs[i].f(a,b));
			break;
			}
			i++;
		}
		if(i == 4)
		{
			printf("Unkown operator:%s\n", c);
		}
	}
	return 0;
}

Programming in C of FC tutorial 2

###Full Circle C 3 ####Exercise 1 Collect all the code snippets on this page and turn them into the working program
Pointer:

#include <stdio.h>

int main(void)
{
	int anInt = 5;

	int *anIntPointer = &anInt;

	printf("Address: %p Value: %d \n", &anInt, anInt);

	printf("Address of Pointer: %p Address: %p Value: %d\n", &anIntPointer, anIntPointer, *anIntPointer);

	printf("Size of pointer: %d, size of int: %d\n", sizeof(anIntPointer), sizeof(anInt));

	return 0;
}

array.c

#include <stdio.h>

int main(void)
{
	int i;
	int anIntArray[5] = {10, 20, 30, 40, 50};

	printf("Address of Array: %p \n", &anIntArray);

	printf("Size of Array: %d\n", sizeof(anIntArray));

	for(i = 0; i<sizeof(anIntArray)/sizeof(int); i++)
	{
		printf("Index: %x Address: %p Value: %d, Value: %d\n", i, &anIntArray[i], anIntArray[i], *(anIntArray+i));
	}

	return 0;
}

string:

#include <stdio.h>
#include <string.h>

int main()
{
	int i;
	char aChar = 'c';
	char * aString = "Hello";

	printf("Address: %p value: %c size: %d\n", &aChar, aChar, sizeof(aChar));

	printf("Address of the string: %p\n", &aString);

	printf("Size of String: %d\n", strlen(aString));

	printf("Value: %s\n", aString);

	for(i = 0; i<=strlen(aString); i++)
	{
		printf("Index:%x Address: %p, Value:%c\n", i, &aString[i], aString[i]);
	}
	return 0;
}

Structure:

#include <stdio.h>
struct aStruct_def
{
	int intMember;
	int * intPointer;
	char charMember;
	char ** stringPointer;
};

int main(void)
{
	struct aStruct_def aStruct;
	struct aStruct_def *aStructPointer;
	int anInt = 5; 
	char *aString = "Hello";

	printf("Address: %p Size: %d\n", &aStruct, sizeof(struct aStruct_def));

	printf("%p %p %p %p\n", &aStruct.intMember, &aStruct.intPointer, &aStruct.charMember, &aStruct.stringPointer);

	aStruct.intMember = 6;
	aStruct.intPointer = &anInt;
	aStruct.charMember = 'k';
	aStruct.stringPointer = &aString;

	aStructPointer = &aStruct;

	printf("Member of struct: %d\n", (*aStructPointer).intMember);
	printf("Member of struct: %d\n", *(*aStructPointer).intPointer);
	printf("Member of struct: %d\n", aStructPointer->intMember);
	printf("Member of struct: %d\n", *aStructPointer->intPointer);
	printf("Member of struct: %s\n", *aStructPointer->stringPointer);

	return 0;
}

####Exercise 2 Implement strlen yourself use a while() loop:

#include <stdio.h>

int mystrlen(char *strings)
{
	int i = 0;
	while(strings[i] != '\0')
	{
		i++;
	}
	return i;
}

int main()
{
	char *a = "Hello World";
	printf("%s 's length is %d\n", a, mystrlen(a));
	return 0;
}

###Exercise 3 A C application typically has ‘int main(int argc, char **argv)’ as it’s main prototype, here argc contains the number of strings passed to the application, and argc is an array of argc strings. Write a small application which prints all arguments given to the application. What is stored in argv[0] ?

#include <stdio.h>

int main(int argc, char **argv)
{
	int i = 0; 
	for(i = 0; i<argc; i++)
	{
		printf("arg %d is %s\n", i, argv[i]);
	}
	return 0;
}

Programming In C New Words

###Programming In C intermediate: 中级的
hereby: 特此
scary: 恐慌的,提心吊胆的
de facto (法),事实上的
span: 跨越
interpreted: 可交互的
fanatic: 狂热者
novice: 新手,入门者
concrete: 实际的
prime number: 素数,质数 ternary: 三重的,三元的
intrusive: 侵入的,打扰的
grind: 磨难,磨碎,折磨
confessed: 坦白的
scary: 提心吊胆的,胆小的
apropos: 恰好的
grasp: 掌握
instalment: 分期付款的
gibberish: 乱语,快速而不清楚的话语
condense: 电容器

Programming in C of FC tutorial

###Programming In C GCC’s funcitionality:
gcc command calls a compiler(which transforms a higher level language into assembler), an assembler translates assembler into object files(machine instructions), and a linker, which combines several object files into an executable.

launchpad.net is a unique collaboration and hosting platform for free software. http://www.launchpad.net.

###Programming In C II Example code:

#include <stdio.h>
#define VERSION "1.0"

/* 
 * Runs a prime check on a given integer, return
 * 1 when the integer is a prime number, 0 otherwise
 */
int isPrime(int prime)
{
	int count = 2;

	// Catch two special cases 
	if(prime == 1)
	{
		return 0;
	}
	else if(prime == 2)
	{
		return 1;
	}
	else
	{
		while(prime % count != 0 && count*count<=prime)
		{
			count++;
		}
		return (prime%count==0)?0:1;
	}
}
/*
 * Print version information
 */
void printVersion()
{
	printf("Primality checker version %s\n", VERSION);
	printf("Compiled on %s %s\n", __DATE__, __TIME__);
}

int main()
{
	int i = 1;
	const int max_prime = 2500;

	printVersion();

	for (i = 1; i<max_prime; i++)
	{
		if(isPrime(i))
		{
			printf("%d is prime\n", i);
		}
		else
		{
			printf("%d is not prime\n", i);
		}
	}
	return 0;
}

####Execise 1 Rewrite the for loop in the main() function so it becomes a while loop.

	//for (i = 1; i<max_prime; i++)
	while(i<max_prime)
	{
		if(isPrime(i))
		{
			printf("%d is prime\n", i);
		}
		else
		{
			printf("%d is not prime\n", i);
		}
		i++;
	}

####Execise 2 Rewrite the if…else if…else structure in the isPrime() function to a switch…case structure.

	int count = 2;
	int returnvalue = 0;

	// Catch two special cases 
	switch(prime)
	{
		case 1:
			returnvalue = 0;
			break;
		case 2:
			returnvalue = 1;
			break;
		default:
			while(prime % count != 0 && count*count <= prime)
			{
				count++;
			}
			returnvalue =  (prime % count == 0)?0:1;

	}
	return returnvalue;

####Execise 3 Rewrite the ternary(condition)?value1:value2 to an if..else structure

//return (prime%count==0)?0:1;
if(prime%count == 0)
{
	return 0;
}
else
{
	return 1;
}

####Execise 4 Rewrite the if…else in the main() funciton to make sure the ternary operator

/*
if(isPrime(i))
{
	printf("%d is prime\n", i);
}
else
{
	printf("%d is not prime\n", i);
}
*/
printf("%d %s prime\n", i, isPrime(i)?"is":"is not");

####Execise 5 Replace the isPrime() function by an isOdd() function which return 1 when a given integer is odd.

int isOdd(int odd)
{
	if(odd == 1)
	{
		return 1;
	}
	else
	{
		if(odd%2 == 0)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
}

/* Called via */
if(isOdd(i))
{
	printf("%d is Odd\n", i);
}
else
{
	printf("%d is not Odd\n", i);
}

####Execise 6 Design and write a small application which print out the n Fibonacci sequence, where n should be easily modifiable.

#include <stdio.h>

/* The formula is like
 * a(n+2) = a(n)+a(n+1)
 * while when a1 = 1, a2 =1
 */

int Fib(int fib)
{
	if(fib == 1)
	{
		return 1;
	}
	else if(fib == 2)
	{
		return 1;
	}
	else
	{
		return (Fib(fib-1) + Fib(fib-2));
	}
}

int main(int argc, char **argv)
{
	int i;
	int j = 1;

	printf("Please input an integer number\n");
	scanf("%d", &i);

	printf("The Fibonacci sequence is: \n");
	for( j = 1; j <= i; j++)
	{
		printf("a[%d] is %d.\n", j, Fib(j));
	}

	return 0;
}

Because we use recursion here, when the n is too big, the calculation maybe very slow.

Reading Digest of FC3

###Full Circle 3

  1. Page 6 of cn version: DSL(Damn Small Linux), http://www.damnsmalllinux.org, which could be run on 16MB memory’s 486 computer, try it.
  2. Page