コード例 #1
0
ファイル: phonebook.c プロジェクト: njagdale/code-magic
void printStrings(int* num, char* res, int pos)
{
    int i;

    if(pos == LEN)
    {
        printf("%s\n", res);
        return;
    }
    else
    {
        if (num[pos] == 7 || num[pos] == 9)
        {
            for(i=1; i<=4; i++)
            {
                res[pos] = getCharKey(num[pos], i);
                printStrings(num, res, pos+1);

                if(num[pos] == 0 || num[pos] == 1)
                    return;
            }
        }
        else
        {
            for(i=1; i<=3; i++)
            {
                res[pos] = getCharKey(num[pos], i);
                printStrings(num, res, pos+1);

                if(num[pos] == 0 || num[pos] == 1)
                    return;
            }
        }
    }
}
コード例 #2
0
ファイル: main.c プロジェクト: leonwlaw/UnixSystemProgramming
int main(int argc, char **argv) {
	int ignoreExistingEnvironment = 0;

	// Is the user attempting to pass a command line argument?
	if (argc > 1 && (strncmp(argv[1], "-", 1) == 0)) {
		// Check any recognized flags...
		if (strcmp(argv[1], "-i") == 0) {
			ignoreExistingEnvironment = (strcmp(argv[1], "-i") == 0);

		} else if (strcmp(argv[1], "--help") == 0) {
			printStrings(USAGE_STRING);
			exit(0);

		} else {
			// Not recognized.  Print error string and die.
			fprintf(stderr, "env: unrecognized option '%s'\n", argv[1]);
			fprintf(stderr, "Try 'env --help' for more information.\n");
			exit(1);
		}
	}

	if (ignoreExistingEnvironment) {
		env(ignoreExistingEnvironment, argv + 2);
	} else {
		env(ignoreExistingEnvironment, argv + 1);
	}
}
コード例 #3
0
void String_sort() {
	char input[M][N];
	int count = 0;
	int i;
	while(scanf("%s", input[count++]) != EOF) {}
	Straight_insertion_sort(input, 0, count - 1);
	printStrings(input, 0, count - 1);
}
コード例 #4
0
ファイル: main.c プロジェクト: leonwlaw/UnixSystemProgramming
int env(int ignoreExistingEnvironment, char** args) {
	int numOldEnvironmentVariables = getEndOfCStringArray(environ) - environ;
	int numNewEnvironmentVariables = ignoreExistingEnvironment? 0 : numOldEnvironmentVariables;

	numNewEnvironmentVariables += calculateNumNewEnvironmentVariables(environ, args);

	char** newEnvironment = environ;
	int allocateNewEnvironment = numNewEnvironmentVariables != numOldEnvironmentVariables;

	if (allocateNewEnvironment) {
		// Allocate 1 additional slot for NULL to mark the end of the array
		newEnvironment = calloc(numNewEnvironmentVariables + 1, sizeof(char*));
	}

	// If we allocated a new environment, we need to copy over the old
	// environment to the new one.
	if (!ignoreExistingEnvironment)
		overwriteEnvironment(newEnvironment, environ);
	overwriteEnvironment(newEnvironment, args);
	
	environ = newEnvironment;

	// User specified a non-variable argument.
	// This means they intend to run a program with the modified
	// environment.
	newEnvironment[numNewEnvironmentVariables] = NULL;

	char **nonVariableArgument = findFirstNonVariable(args);
	if (*nonVariableArgument != NULL) {
		execvp(nonVariableArgument[0], nonVariableArgument);
		perror("env");
		exit(1);
	}

	// Default behavior, simply print out everything in the
	// environment variable
	printStrings(newEnvironment);

	if (allocateNewEnvironment)
		free(newEnvironment);

	return 0;
}
コード例 #5
0
ファイル: phonebook.c プロジェクト: njagdale/code-magic
void printWords(int* num)
{
    char res[8];
    res[7] = '\0';
    printStrings(num, res, 0);
}
コード例 #6
0
ファイル: main.c プロジェクト: MSaIim/SortedList
int main(int argc, char *argv[])
{
	// Lists
	SortedListPtr integers = SLCreate(compareIntegers, destroyList);
	SortedListPtr doubles = SLCreate(compareDoubles, destroyList);
	SortedListPtr characters = SLCreate(compareStrings, destroyList);
	SortedListPtr strings = SLCreate(compareStrings, destroyList);
	
	// Insertions
	int a = 7, b = 7, c = 8, d = 1;
	double h = 22.3, i = 98.2, j = 1.2;
	char s = 's', w = 'a', v = 'c';
	char word1[10] = "Hello";
	char word2[10] = "World";

	
	// ========================== Case 1 ========================== 
	printf("\nIntegers:\n");
	SLInsert(integers, (void*)&a);
	SLInsert(integers, (void*)&b);
	SLInsert(integers, (void*)&c);
	SLInsert(integers, (void*)&d);

	SortedListIteratorPtr itr1 = SLCreateIterator(integers);
	printIntegers(itr1);
	printf("\n");

	SLRemove(integers, (void*)&c);

	SortedListIteratorPtr itr2 = SLCreateIterator(integers);
	printIntegers(itr2);
	printf("\n");

	// ========================== Case 2 ========================== 
	printf("\nDoubles:\n");
	SLInsert(doubles, (void*)&h);
	SLInsert(doubles, (void*)&i);
	SLInsert(doubles, (void*)&j);

	SortedListIteratorPtr itr3 = SLCreateIterator(doubles);
	printDoubles(itr3);
	printf("\n");
	
	SLRemove(doubles, (void*)&h);

	SortedListIteratorPtr itr4 = SLCreateIterator(doubles);
	printDoubles(itr4);
	printf("\n");

	// ========================== Case 3 ========================== 
	printf("\nCharacters:\n");
	SLInsert(characters, (void*)&s);
	SLInsert(characters, (void*)&w);
	SLInsert(characters, (void*)&v);

	SortedListIteratorPtr itr5 = SLCreateIterator(characters);
	printChars(itr5);
	printf("\n");
	
	SLRemove(characters, (void*)&v);
	SLRemove(characters, (void*)&s);

	SortedListIteratorPtr itr6 = SLCreateIterator(characters);
	printChars(itr6);
	printf("\n");

	// ========================== Case 4 ========================== 
	printf("\nStrings:\n");
	SLInsert(strings, (void*)word1);
	SLInsert(strings, (void*)word2);

	SortedListIteratorPtr itr7 = SLCreateIterator(strings);
	printStrings(itr7);
	printf("\n");
	
	SLRemove(strings, (void*)word1);

	SortedListIteratorPtr itr8 = SLCreateIterator(strings);
	printStrings(itr8);
	printf("\n\n");

	return 0;
}