天天进步,算法练手吧。
BEGIN===>===>
1.闰年的算法
/******************************************************/
/*
*描述:获取1900年到2050年的闰年
*作者:sanbo(sanbo.xyz@gmail.com)
*/
/******************************************************/
void printbissextile()
{
int i;
for(i=1900;i<2051;i++)
{
if((i%4)==0&&(i%100)!=0||(i%400)==0)
{
printf("%d 是闰年\n",i);
}
}
}
int n;
printf("请输入年份");
scanf("%d",&n);
if(((n%4)==0)&&(n%100)!=0)||(n%400==0))
printf("闰年");
else
printf("不是闰年");
2.打印图形
/******************************************************/
/*
*描述:用#打印等腰三角形
*语言:C语言
*作者:sanbo(sanbo.xyz@gmail.com)
*/
/******************************************************/
void printTriangle(int q)
{
int i, j , k;
for(i = 1; i <= q; i++)
{
for(j = 1;j <= q - i; j++)
printf(" ");
for(k =1; k <= 2*i - 1; k++)
printf("#");
printf("\n");
}
printf("\n");
}
#include <conio.h>
#include <math.h>
/********************************************************/
/*
*描述:用*打印余弦曲线
*语言:C语言
*作者:sanbo(sanbo.xyz@gmail.com)
*
*
*要点:反余弦函数acos(double d);62是一个接近值(2π*10)
*
*/
/********************************************************/
void printAcos()
{
double y;
int x ,m ;
for(y = 1; y >= -1; y-=0.1)
{
m = acos(y) *10;
for(x = 1; x < m; x++)
printf(" ");
printf("*");
for(; x< 62 -m; x++)
printf(" ");
printf("*\n");
}
getch();
}
ps:这俩头文件一定要添加,不然运行不了滴
3.乘法口诀表
/********************************************************/
/*
*描述:打印乘法口诀表(小时候最头疼的事,哈哈)
*语言:C语言
*作者:sanbo(sanbo.xyz@gmail.com)
*
*笑谈:前几天北京地铁还放了个小短片,一小孩哭着说,我以后
*一定不上大学,大学得背乘法口诀表,太难了!!
*/
/********************************************************/
void printChengfa()
{
int i, j;
for(i = 1; i <= 9; i ++)
{
for(j = 1; j <= i; j++)
printf("%d*%d=%d ",i,j,i*j);
printf("\n");
}
}
4.读取文件
#define BUFFERSIZE 8189
/************************************************************************/
/* 作用:读取一个文件保存到一个数组中 */
/************************************************************************/
int readFileToString(const char *filepath)
{
FILE *fp = NULL;
int i = 0;
char buffer[BUFFERSIZE+1] ={0}; //char 数组,存放结果
if ((fp = fopen(filepath, "r")) == NULL)
{
fprintf(stderr,"can't find the %s\n",filepath);
return -1;
}
while (i < BUFFERSIZE) //防止文件长度大于BUFFERSIZE
{
char ch = fgetc(fp);
if (ch != EOF)
{
buffer[i++] = ch;
}
else
{
break;//到达文件的末尾
}
}
buffer[i]='0';
fclose(fp);
fp == NULL;
printf("%s",buffer); //输出结果
return 0;
}
5.写文件
/************************************************************************/
/* save string tp a file */
/************************************************************************/
int save(const char *filepath)
{
FILE *fp;
char target[2369] = {"你在哪里呀"
};;
char buf[2369];
fp = fopen(filepath,"w+");
if (NULL == fp)
{
printf("The file doesn't exist!\n");
return -1;
}
fwrite(target,strlen(target),1,fp);//把字符串内容写入到文件
fseek(fp,0,SEEK_SET);//定位文件指针到文件开始位置
fread(buf,strlen(target),1,fp);//把文件内容读入到缓存
buf[strlen(target)] = '\0';//删除缓存内多余的空间
printf("buf = %s\n",buf);
printf("strlen(buf) = %d\n",strlen(buf));
return 0;
}
6.更为便捷的读取文件
#include <fstream>
#include <string>
using namespace std;
/************************************************************************/
/* read file to a char point */
/************************************************************************/
void readFileToCharPoint(const char *filepath)
{
ifstream ifile(filepath);
string s;
while (ifile) s.push_back(ifile.get());
const char *str = s.c_str();
printf("%s",str);
}
7、读取文件(自动调整长度)
void readFilePoint(const char *filepath)
{
FILE *fp = NULL;
char *pchBuf;
int nLen;
fp = fopen (filepath,"r");
fseek(fp, 0, SEEK_END); //文件指针移到文件尾 //fseek(fp, 2, SEEK_SET); //文件指针移到文件尾
nLen = ftell(fp); //得到当前指针位置, 即是文件的长度
rewind(fp); //文件指针恢复到文件头位置
pchBuf = (char*) malloc(sizeof(char)*nLen+1);//动态申请空间, 为保存字符串结尾标志\0, 多申请一个字符的空间
if(!pchBuf)
{
perror("内存不够!\n");
exit(0);
}
//读取文件内容//读取的长度和源文件长度有可能有出入,这里自动调整 nLen
nLen = fread(pchBuf, sizeof(char), nLen, fp);
fclose(fp);
pchBuf[nLen] = '\0'; //添加字符串结尾标志
printf("从文本文件读取的字符串:%s \n", pchBuf);//把读取的内容输出到屏幕看看
free(pchBuf);
}