C语言笔记:两字符串比较

自写函数,实现字符串比较.若字符串中字符不同,则输出两者的ASCII码差值

#include 
int strcmp(char *p1,char *p2)
{
int i,h,j,k;
for (i=0;p1[i]!='\0' || p2[i]!='\0';i++)
{
	if (*(p1+i)!=*(p2+i))
	{
		h=(int)*(p1+i);
		j=(int)*(p2+i);
		k=h-j;
		return k;
	}
}
printf("The same word\n");
return 0;
}

int main()
{
	char s1[100],s2[100];
	int result;
	printf("Please input the first word:\n");
	gets(s1);
    printf("Please input the second word:\n");
	gets(s2);
	result=strcmp(s1,s2);
	printf("%d",result);
	return 0;
}

C语言笔记:定义结构体完成分数四则运算

#include 

typedef struct{				//定义“分数”结构体,num代表分子,denom分母
	int num;
	int denom;
}fraction;

fraction makeRat(int num, int denom);			//创建一个分数,num代表分子,denom代表分母
void Display(fraction r);						//显示一个分数

fraction add(fraction first, fraction second);	//分数的加
fraction sub(fraction first, fraction second);	//分数的减
fraction mul(fraction first, fraction second);	//分数的乘
fraction div(fraction first, fraction second);	//分数的除

fraction neg(fraction rat);						//求一个分数的相反数
fraction inv(fraction rat);						//求一个分数的倒数

int gcd(int a, int b);					//求两个数a和b最高公因数
int main(){
	fraction a,b,c,d;
	a = makeRat(1,2);
	b = makeRat(11,23);
	c = makeRat(5,6);
	d = makeRat(5,26);
	
	fraction result = div(add(a,b),sub(c,d));	
	Display(result);
	return 0;
}

fraction makeRat(int num, int denom){
	fraction rat;
	int i = gcd(num, denom);
	rat.num = num / i;
	rat.denom = denom / i;
	return rat;
}

void Display(fraction r){
	printf("%d / %d\n", r.num, r.denom);
}

fraction add(fraction first, fraction second){
	int num, denom;
	num = first.num * second.denom + first.denom * second.num;
	denom = first.denom * second.denom;
	return makeRat(num, denom);
}

fraction neg(fraction rat){
	return makeRat(-rat.num, rat.denom);
}

fraction sub(fraction first, fraction second){
	return add(first, neg(second));
}

fraction mul(fraction first, fraction second){
	int num, denom;
	num = first.num * second.num;
    denom = first.denom * second.denom;
    return makeRat(num, denom);
}

fraction inv(fraction rat){
	return makeRat(rat.denom, rat.num);
}

fraction div(fraction first, fraction second){
	return mul(first, inv(second));
}

int gcd(int a, int b){
	while(b != 0){
		int tmp = a % b;
		a = b;
		b = tmp;
	}
	return a;
}

C语言笔记:有关字符串替换

自写函数,使用数组,形式上使用指针.实现输入一个含有n个字符的字符串str1,再输入另一个含有m个字符的字符串str2,从str1中的某位开始替换为str2.

#include 
#include 
void copyx(char *x,char *y,int i);
int main()
{
	char str1[100],str2[100];         //[]里的数字足够大即可
	int l,i;
	printf("Please input the first word:\n");
	scanf("%s",str1);
	printf("Please input the second word:\n");
	scanf("%s",str2);
	l=strlen(str1);
	printf("Where do you want to insert the second word (From 1 to %d)\n",l);
	scanf("%d",&i);
	if (i<=0 || i>l)
	{
		printf("Wrong Number!\n");
		return 0;
	}
	copyx(str1,str2,i);
	printf("%s",str1);
	return 0;
}

void copyx(char *x,char *y,int n)
{
    int j;
    for (j=0;y[j]!='\0';j++)
	{
	  *(x+n+j)=*(y+j);
        }
    *(x+n+j)='\0';
}

C语言笔记:输出数组中最大和最小值

使用指针,采用自写函数法输出所给数组中20个数的最大和最小值。

#include 
void findnum (int *n);
int main()
{
	int i,a[20];
	printf("Please input 20 numbers:\n");
	for (i=0;i<=19;i++)
	{
		scanf("%d",&a[i]);
	}
	findnum(a);
	return 0;
}

void findnum (int *a)
{
	int biggest=a[0],smallest=a[0],j;
	for (j=0;j<=19;j++)
	{
		if (*(a+j)>biggest) biggest=*(a+j);
		if (*(a+j)
				

C语言笔记:递归法累加

自写函数,递归

#include 
int leijia(int num)
{
	int f;
	if (num==1) f=1;
	else f=leijia(num-1)+num;
	return (f);
}
int main()
{
	int num,result;
	printf("Please input a number:\n");
	scanf("%d",&num);
	result=leijia(num);
	printf("The result is:\n");
	printf("%d\n",result);
	return 0;
}