利用预处理使C看起来像Algol-68

利用预处理使C看起来像Algol-68,C专家编程上说是为了便于调试,其实这样的代码在往往难以维护,可读性差。

#include <stdio.h>
#define IF if(
#define THEN ){
#define ELSE }else(
#define FI ;}
#define WHILE while(
#define DO ){
#define OD ;}
#define INT int
#define STRING char*
#define	BEGIN {
#define END }

INT compare(STRING s1, STRING s2)
/* K&R参数定义形式
INT compare(s1, s2)
STRING s1;
STRING s2;
*/
BEGIN
	WHILE *s1++ == *s2
	DO IF *s2++ == 0
		THEN return (0);
		FI
	OD
		return (*--s1 - *s2);
END

/* K&R参数定义形式
int compare2(s1, s2)
char * s1;
char * s2;
*/
int compare2(char * s1, char * s2)
{
	while (*s1++ == *s2){
		if (*s2++ == 0)	return 0;
	}
	return (*--s1 - *s2);
}

int main(void)
{
	printf("%dn", compare("aca", "acA"));
	printf("%dn", compare2("aca", "acA"));
	return 0;
}

One thought on “利用预处理使C看起来像Algol-68

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.