[C语言基础]一些C语言小程序(一)

</pre><p><strong>1,请把从1到1000的数打印出来,不能使用任何的循环语句或是条件语句</strong><span style="WHITE-SPACE: pre"></span></p><pre code_snippet_id="198047" snippet_file_name="blog_20140221_1_1416869" class="cpp" name="code">#include <stdio.h>
void func(int i);
void func(int i)
{
	printf("%d",i);
	int a = i/i-1;
	func(i--);
}
int main()
{
	func(1000);
	return 0;
}


 

2,向一个有序的单链表中插入一个新的节点:

#include <stdio.h>
#include <stdlib.h>

typedef struct NODE{
	struct NODE *link;
	int value;
} Node;

int sll_insert(Node **rootp,int new_value)
{
	Node *current;
	Node *previous;
	Node *new;
	
	current = *rootp;
	previous = NULL;
	
	while(current != NULL && current->value < new_value)
	{
		previous = current;
		current = current->link;
	}
	new = (Node*)malloc(sizeof(Node))
	if(new == NULL)
		return 0;
	new->value = new_value;
	
	new->link = current;
	if (previous == NULL)
		*rootp = new;
	else
		previous->link=new;
	return 1;
}


 3,malloc和relloc函数的学习

代码如下:

#define PERSIZE 100
int *malloctest();
int *malloctest()
{
	int *dst;
	int value;
	int count = PERSIZE;
	int size = 0;
	int i = 0;
	dst = (int*)malloc(count*sizeof(int));
	while(scanf("%d",&value)== 1)
	{
		i++;
		if(i == (count+PERSIZE))
		{
			count+=PERSIZE;
			dst = realloc(dst,(count+1)*sizeof(int));
			if (dst == NULL)
			{
				printf("realloc error;");
				return NULL;
			}
		}
		dst[i] = value;	
	}
	//dst[i]= value;
	size = i;
	dst = realloc(dst,(size+1)*sizeof(int));
	if(dst == NULL)
	{
		printf("realloc error;");
		return NULL;
	}
	dst[0] = size;
	return dst;
}


int main()
{
	int *array;
	array = malloctest();
	char str[20];
	scanf("%[^r]",str);
	printf("%s\n",str);
	free(array);
	return 0;
}

 

4,通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉,比如字符串“abacacde”过滤结果为“abcde”。

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

void stringFilter(const char*pInputStr,long lInputLen,char *pOutputStr);

void stringFilter(const char*pInputStr,long lInputLen,char *pOutputStr)
{
	int i=0,j=0;
	const char *str=pInputStr;
	while (i<lInputLen)
	{
		int k=i+1;
	
		while(*(str+i) == *(str+k))
		{
			k+=1;
		}
		*(pOutputStr+j)=*(str+i);
		j+=1;
		i=k;
	}
}

int main()
{
	char *str1="abbbdeeef";
	long len=strlen(str1);
	char *str2;
	str2=(char*)malloc(len*sizeof(char));
	stringFilter(str1,len,str2);
	printf("str2 is %s:\n",str2);
	free(str2);
	return 0;
}


5,自己实现函数将一个整数转换为字符串:

#include <stdio.h>
#include <stdlib.h>

void itoa_my(int num,char *str)
{
	int sign = num;
	int i = 0,j=0;
	char temp[20];
	if (sign < 0)
	{
		num = -num;
	}
	do{
		temp[i] = num %10 + '0';
		num /= 10;
		i++;
	}while(num > 0);
	if(sign < 0)
	{
		temp[i++]= '-';
	}
	temp[i]='\0';
	i--;
	while(i >= 0)
	{
		str[j]=temp[i];
		j++;
		i--;
	}
	str[j]='\0';
}


int main()
{
	int n;
	scanf("%d",&n);
	char dst[20];
	itoa_my(n,dst);
	printf("result is %s\n",dst);
	return 0;
}

 

6,递归和迭代:

计算n的阶乘
递归形式:

long factorial (int n)
{
	if (n <= 0)
		return 1;
	else
		return n*factorial(n-1);
	
}


迭代形式:

long factorial(int n)
{
	int result =1;
	while(n >= 1)
	{
		result=result*n;
		n-=1;
	}
	return result;
}


计算斐波拉契数列:
递归形式:

long fibonacci(int n)
{
	if (n <= 2)
		return 1;
	else
		return fibonacci(n-1)+fibonacci(n-2);
}


迭代形式:

long fibonacci(int n)
{
	int result=1;
	int pre_result=1;
	int next_old_result;
	while (n>2)
	{
<span style="white-space:pre">		</span>n=n-1;
		next_old_result=pre_result;
		pre_result=result;
		result=next_old_result+pre_result;
	}
	return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值