Ad

Naive solution.

I think this would be more interesting if the challenge was to generate as few characters of b**infuck as possible.

maybe add a limit to *ra*nfuck characters in the test cases? might be fun.

Code
Diff
  • #include<stddef.h>
    #include<string.h>
    #include<stdlib.h>
    
    char buf[4096] = {0};
    char*
    s2bf(char *arr) {
    	char cv = 0;
    	char *r;
    	
    	while(*arr){
    		while(cv != *arr){
    			strcat(buf, "+");
    			cv++;
    		}
    		strcat(buf, ".");
    		arr++;
    	}
    	
    	r = malloc(4096);
    	strcpy(r, buf);
    	return r;
    }
    • #include<stddef.h>
    • #include<string.h>
    • #include<stdlib.h>
    • char s[4096] = "";
    • char buf[4096] = {0};
    • char*
    • s2bf(char *argv) {
    • register int i = 0,j;
    • register int k,m;
    • register int l,last = 0;
    • while (argv[++i]!=0)
    • continue;
    • for (j=0; j<i; j++) {
    • register int t;
    • t = argv[j];
    • l = k = t-last;
    • last = t;
    • #ifdef DEBUG
    • printf("\n%d, %d, %d, %d\n",last,k,j,i);
    • #endif
    • if (k >= 0) {
    • k >>= 3; // k /= 8
    • l &= 7; //在更多的机器工作,求余
    • if (k == 1)
    • strcat(s,">++++++++");
    • else {
    • if (k != 0) {
    • strcat(s,"++++++++[>");
    • for (m=0; m<k; m++)
    • strcat(s,"+");
    • strcat(s,"<-]>");
    • } else {
    • strcat(s,">");
    • }
    • }
    • for (m=0; m<l; m++)
    • strcat(s,"+");
    • strcat(s,".");
    • } else {
    • k = -k >> 3; // k /= 8
    • l = -l & 7; //在更多的机器工作,求余
    • if (k == 1)
    • strcat(s,">--------");
    • else {
    • strcat(s,"++++++++[>");
    • for (m=0; m<k; m++)
    • strcat(s,"-");
    • strcat(s,"<-]>");
    • }
    • for (m=0; m<l; m++)
    • strcat(s,"-");
    • strcat(s,".");
    • s2bf(char *arr) {
    • char cv = 0;
    • char *r;
    • while(*arr){
    • while(cv != *arr){
    • strcat(buf, "+");
    • cv++;
    • }
    • #ifdef DEBUG
    • printf("\n%d, %d, %d\n",m,k,l);
    • #endif
    • strcat(s,"<");
    • strcat(buf, ".");
    • arr++;
    • }
    • char* q = malloc(4096);
    • strcpy(q, s);
    • return q;
    • r = malloc(4096);
    • strcpy(r, buf);
    • return r;
    • }

added a proper test system.

Code
Diff
  • #include<stddef.h>
    #include<string.h>
    #include<stdlib.h>
    
    char s[4096] = "";
    char*
    s2bf(char *argv) {
    	register int i = 0,j;
    	register int k,m;
    	register int l,last = 0;
    	while (argv[++i]!=0)
    		continue;
    	for (j=0; j<i; j++) {
    		register int t;
    		t = argv[j];
    		l = k = t-last;
    		last = t;
    #ifdef DEBUG
    		printf("\n%d, %d, %d, %d\n",last,k,j,i);
    #endif
    
    		if (k >= 0) {
    			k >>= 3; // k /= 8
    			l &= 7; //在更多的机器工作,求余
    			if (k == 1)
    				strcat(s,">++++++++");
    			else {
    				if (k != 0) {
    					strcat(s,"++++++++[>");
    					for (m=0; m<k; m++)
    						strcat(s,"+");
    					strcat(s,"<-]>");
    				} else {
    					strcat(s,">");
    				}
    			}
    			for (m=0; m<l; m++)
    				strcat(s,"+");
    			strcat(s,".");
    		} else {
    			k = -k >> 3; // k /= 8
    			l = -l & 7; //在更多的机器工作,求余
    			if (k == 1)
    				strcat(s,">--------");
    			else {
    				strcat(s,"++++++++[>");
    				for (m=0; m<k; m++)
    					strcat(s,"-");
    				strcat(s,"<-]>");
    			}
    			for (m=0; m<l; m++)
    				strcat(s,"-");
    			strcat(s,".");
    		}
    #ifdef DEBUG
    		printf("\n%d, %d, %d\n",m,k,l);
    #endif
    
    		strcat(s,"<");
    	}
    	
    	char* q = malloc(4096);
    	strcpy(q, s);
      return q;
    }
    • #include <stdio.h>
    • #include <string.h>
    • // #define DEBUG
    • #include<stddef.h>
    • #include<string.h>
    • #include<stdlib.h>
    • char s[4096] = "";
    • int s2b(int argc, char *argv[]) {
    • char*
    • s2bf(char *argv) {
    • register int i = 0,j;
    • register int k,m;
    • register int l,last = 0;
    • if (argc != 2) {
    • printf("Usage: %s [string]",*argv);
    • return 1;
    • }
    • while (argv[1][++i]!=0)
    • while (argv[++i]!=0)
    • continue;
    • for (j=0; j<i; j++) {
    • register int t;
    • t = argv[1][j];
    • t = argv[j];
    • l = k = t-last;
    • last = t;
    • #ifdef DEBUG
    • printf("\n%d, %d, %d, %d\n",last,k,j,i);
    • #endif
    • if (k >= 0) {
    • k >>= 3; // k /= 8
    • l &= 7; //在更多的机器工作,求余
    • if (k == 1)
    • strcat(s,">++++++++");
    • else {
    • if (k != 0) {
    • strcat(s,"++++++++[>");
    • for (m=0; m<k; m++)
    • strcat(s,"+");
    • strcat(s,"<-]>");
    • } else {
    • strcat(s,">");
    • }
    • }
    • for (m=0; m<l; m++)
    • strcat(s,"+");
    • strcat(s,".");
    • } else {
    • k = -k >> 3; // k /= 8
    • l = -l & 7; //在更多的机器工作,求余
    • if (k == 1)
    • strcat(s,">--------");
    • else {
    • strcat(s,"++++++++[>");
    • for (m=0; m<k; m++)
    • strcat(s,"-");
    • strcat(s,"<-]>");
    • }
    • for (m=0; m<l; m++)
    • strcat(s,"-");
    • strcat(s,".");
    • }
    • #ifdef DEBUG
    • printf("\n%d, %d, %d\n",m,k,l);
    • #endif
    • strcat(s,"<");
    • }
    • return 0;
    • char* q = malloc(4096);
    • strcpy(q, s);
    • return q;
    • }