c语言学习100例(1-5)

汉语版:

虽然说是100例,但是估计没那么多时间一一都写上去。所以能够发多少个实例,自己也不是很清楚。但是毫无疑问,自己会微笑的坚持下去的。

这些实例均是我自己写的,其中借鉴了一些算法,同时每个实例最后是一些补充的小内容。关于直接粘贴复制会出错的原因:这些代码均在本人win7*64位机上编译通过并且运行的,如果你直接粘贴出错,那么你可以自己重新写一下。


E-version:

So sorry that I may not have enough time towrite 100 instances, but one is true that I will renew the contents with smile.

All those codes were original and some comefrom algorithms. Usually, at the end of each program codes, there would be someextra contents. Most important, all the codes can be compiled under theOS-win7*64, if you copy those codes and find some wrong in your OS or Machine,please rewrite them again.


1.进制转换

#include <STDIO.H>
#include <MALLOC.H>
#include <STRING.H>
#include <MATH.H>

int main()
{
	
	//c语言实现数进制之间的转换
	/*
		十进制、八进制和十六进制之间比较容易转换
		只需要在输入的过程中输出不同的格式。
		输出十进制printf("%d",number);
		输出八进制printf("%o",number);
		输出十六进制小写:(是ling-x,不是o-x)
		printf("%0x",number);
		输出十六进制大写:(是ling-x,不是o-x)
		printf("%0X",number);
	*/
	
	//
	//代码如下:十->八->十六

	
	int temp=10;
	printf("%o\n",temp);
	printf("%0X\n",temp);
	


	/
	//二进制转换成十进制
	
	/*
	由于c语言的数据类型并没有二进制的表示方法
	所以我们只能讲二进制定义为字符类型。
	然后根据数值转换的一般公式进行转换即可
	当转换成十进制,自然也就容易转换成二进制的
	*/
	

	int numD=0;//D十进制的
	char *str=(char *)malloc(sizeof(char));
	scanf("%s",str);
	
	for (int i=strlen(str)-1;i>=0;i--)
	{
		if (str[i]=='1')
		{
			numD+=(int)pow(2,i);
		}
		else if (str[i]=='0')
		{
		}
		else
		{
			printf("非法字符,运算终止!");
			break;
		}
	}

	printf("%d",numD);



	/
	//十进制转换成二进制
	
	/*
	十进制转换成二进制,可以按照与之相对应的
	程序来进行操作,利用整除法。
	*/

	int numDs,twoStage=0,numsCopy;
	scanf("%d",&numDs);
	numsCopy=numDs;
	while(true)
	{
		numDs=numDs/2;
		twoStage++;
		if (numDs==1|| numDs==0)
		{
			break;
		}
	}
	char str[1000]="";
	char *strB=str;

	for (int i=twoStage;i>=0;i--)
	{
		if (numsCopy%2==0)
		{
			*(strB+i)='0';
		}else
		{
			*(strB+i)='1';
		}
		numsCopy/=2;
	}

	printf("%s",strB);

	return 0;
}



2.数值排列
#include <STDIO.H>

/*
功能实现:数值全排列
如用户输入4,则排列1-9的数值
每四个一组。这里实现首位可以为0
*/

void prinfList(int);
int main()
{
	printf("请输入要排列的几个数:");
	int needToArray;
	scanf("%d",&needToArray);

	prinfList(needToArray);

	//
	//其他功能的实现:每次输出的值不一样
	for (int i=0;i<9;i++)
	{
		for (int j=0;j<9;j++)
		{
			for (int m=0;m<9;m++)
			{
				if (i!=j && j!=m && i!=m)
				{
					printf("%d,%d,%d\t",i,j,m);
				}
			}
		}
	}

	
	return 0;
}

void prinfList(int a)
{
	for (int i=0;i<=9;i++)
	{
		printf("\t第%d,位是%d\n>><<\n,",a,i);
		if (a>1)
		{
			prinfList(a-1);
		}
	}
}



3.分支选择


#include <STDIO.H>

/*
功能实现:分支选择
如用户输入某个工资,落在不同的区间
将对应缴纳不同的税额
*/

double getTaxRate(int);
void getLevelTax(int);
int main()
{
	printf("请输入工资水平:");
	int slary;
	scanf("%d",&slary);

	printf("应纳税额是%.2f\n",slary*getTaxRate(slary));

	//
	//实现级差缴纳税额
	/*
		0~5000,零税率;5000~10000,超过的征收0.05;以此类推;
		但是最高税率不得超过0.45;
		写法可以是先计算,再带入;计算的税率表如下:
		金额			税率		应纳税额
		0~5000			.00			0.00
		5000~10000		.05			超过部分税额+0;
		10000~25000		.15			超过部分税额+250;
		25000~50000		.25			超过部分税额+2500;
		以此类推
	*/
	return 0;
}

double getTaxRate(int slary)
{
	if (slary>100000)
	{
		return 0.45;
	}else if (slary>50000)
	{
		return 0.35;
	}else if (slary>25000)
	{
		return 0.25;
	}else if (slary>10000)
	{
		return 0.15;
	}else if (slary>5000)
	{
		return 0.05;
	}else
	{
	}

	return 0;
}

void getLevelTax(int slary)
{
	int taxSum=0;
	if (slary>10000)
	{
		taxSum=(slary-10000)*0.05+250;
	}

	/*以下判断省略*/

	printf("%d",taxSum);

}



4.数学运算

#include <STDIO.H>
#include <MATH.H>
#include <MALLOC.H>
/*
功能实现:数学运算
查找某个特殊的数,加上100是平方数,加上168又是平方数
*/

int numberE(int);				//求一个数有几位
int expTen(int);				//指数循环
int main()
{
	
	int circlebase=0;

	while(true)
	{
		int x=(int)sqrt(circlebase+100);
		int y=(int)sqrt(circlebase+168);
		if (x*x==circlebase+100 && y*y==circlebase+168)
		{
			printf("该数字数值是:\n%d!\t",circlebase);
		}
		circlebase++;	
	}

	//
	//实现其他的功能,如水仙花数
	/*
	上面的函数虽然可以遍历很多数值,但是没有终止条件。
	可以加一个判断:
	if(circleBase>10000)
	{
		break;
	}
	有关水仙花是一种数,各自位数值立方相加等于该数值。
	首先应该求出这个数有几个位数,然后将每个位数分离
	出来各自立方,最后相加判断是否相等。
	*/

	int circleBase=0;
	int *a=(int *)malloc(sizeof(int)*100);
	while(true)
	{	
		int sum=0;
	
		for (int i=0;i<numberE(circleBase);i++)
		{
			if (i==0)
			{
				if (numberE(circleBase)==1)
				{
					a[i]=circleBase;
				}else
				{
					a[i]=circleBase/expTen(numberE(circleBase)-i-2);
				}
				
			}else if (i==numberE(circleBase)-1)
			{
				a[i]=circleBase%10;
			}else
			{
				a[i]=circleBase/expTen(numberE(circleBase)-i-2)%expTen(i-2);
			}
		}

		for (int j=0;j<numberE(circleBase);j++)
		{
			a[j]=a[j]*a[j]*a[j];
			sum+=a[j];
		}

		if (sum==circleBase)
		{
			printf("其中一个是:%d\n",circleBase);
		}
				
		circleBase++;

		if (circleBase>1000)
		{
			break;
		}
	}


	return 0;
}

int numberE(int aNumber)
{
	int i=0;
	bool yes=true;
	while(yes)
	{
		if (aNumber<expTen(i))
		{
			yes=false;
		}
		i++;
	}
	return i;
}

int expTen(int number)
{
	int aNumber=10;
	for (int i=0;i<number;i++)
	{
		aNumber*=10;
	}
	return aNumber;
}



5.日期换算

#include <STDIO.H>
#include <MALLOC.H>
/*
功能实现:月份星期计算
给出某个日期,查看该日期属于第几天等。
*/

void getIntSumDay( int year, int month, int day );//数值输入日期计算
void getStrSumDay( char * date );				  //字符串日期计算
bool judgeLeapYear(int year);					  //判断是否为闰年
int getMonthDay(int,int);							  //判断某月天数

int main()
{
	/*
	日期输入:支持字符类型也即大小写
	支持数字分别输入年份,月份,日期
	需要判断是否为闰年等情况
	*/

	int year=0;
	int month=0;
	int day=0;
	int choice=0;
	char *date=(char *)malloc(sizeof(char)*20);

	printf("请选择输入格式:[1->字符],[2->数值]\n");
	scanf("%d",&choice);

	if (choice==1)
	{
		printf("请输入字符串日期:");
		gets(date);

		getStrSumDay(date);
	}else if (choice==2)
	{
		printf("请输入数值年份:");
		scanf("%d",&year);
		printf("请输入数值月份:");
		scanf("%d",&month);
		printf("请输入数值天数:");
		scanf("%d",&day);
		
		getIntSumDay(year,month,day);

	}else
	{
		printf("输入有误!");
	}

	//
	//根据给出的日期,计算这一天星期几
	/*
		主要是应用基姆拉尔森计算公式
		Week= (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;
		其中,本年的一月,二月要换算成上一年的13,14月份
		也就是说year-1;month+12;
	*/

	int yearW=0;
	int monthW=0;
	int dayW=0;
	int week=0;

	printf("请输入数值年份:");
	scanf("%d",&yearW);
	printf("请输入数值月份:");
	scanf("%d",&monthW);
	printf("请输入数值天数:");
	scanf("%d",&dayW);

	if (monthW==1 ||monthW==2)
	{
		yearW--;
		monthW+=12;
	}
	week= (dayW+2*monthW+3*(monthW+1)/5+yearW+yearW/4-yearW/100+yearW/400+1)%7;
	printf("这一天星期:%d\n",week);


	return 0;
}

void getIntSumDay( int year, int month, int day )
{
	int totalDay=0;
	
	for (int i=1;i<month;i++)
	{
		totalDay+=getMonthDay(i,year);
	}

	printf("这天是一年中的%d天!\n",totalDay+day);

}

void getStrSumDay( char * date )
{
	/*
	  只需要将字符串转换成对应的year,month,day就可以了
	  这个在后面的小程序里会有,这里不在写了。
	*/

}

bool judgeLeapYear(int year)
{
	if (year%400==0 ||(year%4==0 && year%100!=0))
	{
		return true;
	}

	return false;
}

int getMonthDay(int month,int year)
{
	switch(month)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:	
		case 10:
		case 12:
			return 31;
			break;
		case 4:		
		case 6:
		case 9:		
		case 11:
			return 30;
			break;
		default:
			if (judgeLeapYear(year))
			{
				return 29;
			}else
			{
				return 28;
			}
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值