#include "stdio.h"
#include "stdafx.h"
int getfish(int intn,int n)
{
int s=intn;
while(n)
{
s=5*s+1;
n--;
}
return s;
}
int main()
{
printf("Fish which were gotten by fishers at least are %d",getfish(6,4));
return 0;
}
2:编程基本功----计算字符的ASCII码
在终端输入一个字符,输出它的ASCII码
二:分析
一个字符在内存中的存储方式是:以它的ASCII码形式存放的,大小为8位。例如:空格符的ASCII码是32,它在内存中32对应的8位二进制数为100000就代表一个空格。
所以只要变换一种输出的形式就可以显示出该字符的ASCII码。
int main()
{
char c;
printf("please input a character\n");
scanf("%c",&c);
getchar();
printf("The ASCII of %c is %d\n",c,c);
return 0;
}
嵌套IF-ELSE语句的妙用
一:问题
对输入的成绩进行等级划分
二:分析
使用if-else嵌套语句时必须注意else与if的配对正确
#include <stdio.h>
int main()
{
int nScore;
printf("please input the score\n");
scanf("%d", &nScore);
if (nScore < 80)
{
if (nScore < 70)
{
if (nScore < 60)
{
printf("E\n");
}
else
{
printf("D\n");
}
}
else
{
printf("C\n");
}
}
else
{
if (nScore < 90)
{
printf("B\n");
}
else
{
printf("A\n");
}
}
getchar();
return 0;
}
3:判断闰年
一:分析
闰年特点:该年份能被4整除,但不能被100整除
该年份能被4整除,又能被400整除
int main()
{
int nyear;
printf("please input a year:\n");
scanf("%d",&nyear);
if((nyear%4==0 && nyear%100!=0 )||nyear%400==0)
{
printf("%d is a leap year!\n",nyear);
}
else
{
printf("%d is not a leap year!\n",nyear);
}
return 0;
}
指针变量做参数
要想通过被调用函数修改主函数中的数组值,只能将数组的首地址(实际上在被调用函数中看作指针)作为参数进行传递,被调用函数通过该指针对数组值进行修改。注意!并不是把整个数组的内容传到了被调用函数中!!!
void inputarray(int *parray,int nlen)
{
int i;
for(i=0;i<nlen;++i)
{
scanf("%d",parray+i);
}
}
int main()
{
int i,array[10];
printf("please input data for array\n");
inputarray(array,10);
for(i=0;i<10;++i)
{
printf("%d",array[i]);
}
printf("\n");
return 0;
}
巧用位运算做整数交换
使用位运算交换整数,可使用按位异或的方法,省掉临时变量,减少空间复杂度
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("a=%d,b=%d\n",a,b);
a=a^b;
b=a^b;
a=a^b;
printf("a=%d,b=%d\n",a,b);
return 0;
}
C标准文件的读写操作示例
#include "string.h"
int main()
{
FILE *fp;
char pathname[20],txt1[20]={'\0'},txt2[20]={'\0'};
int nFileLen;
printf("please input the path name of the file\n");
scanf("%s",pathname);
fp=fopen(pathname,"w");
printf("please input a string to this file\n");
scanf("%s",txt1);
nFileLen=strlen(txt1);
fwrite(txt1,nFileLen,1,fp);
fclose(fp);
printf("the file has been saved\n");
printf("the content of the file:%s is \n",pathname);
fp=fopen(pathname,"r");
fread(txt2,nFileLen,1,fp);
printf("%s\n",txt2);
fclose(fp);
return 0;
}
计算文件的大小
巧妙利用fseek()重定位流上的文件指针和ftell()获得当前文件指针相对于文件开头处的位置,可以高效率求的文件的大小
记录程序允许的时间
int main()
{
FILE *fp;
long f;
fp=fopen("C:\\test.txt","r");
fseek(fp,0,SEEK_END);
f=ftell(fp);
fclose(fp);
printf("the length of the file is %d bytes\n",f);
return 0;
}
记录程序允许的时间
主要使用<time.h>中的类型和几个函数即可。
clock_t定义了表示时间值的算术类型,一般用于记录存储一个系统时间值。
clock()可以取得从程序运行开始到调用clock()函数所花费的处理器时间,返回类型就是clock_t。
(end - start) / CLK_TCK就可以将时间差转换为以秒为单位
#include "time.h"
#include "windows.h"
#include "dos.h"
int main()
{
clock_t start,end;
start=clock();
Sleep(1000);//S要大写 否则出错
end=clock();
printf("the time was:%f\n",(end-start)/CLK_TCK);
return 0;
}
十进制、二进制转换
除2取余法,先得到的是低位,后得到的是高位。所以可以用栈思想计算
void DeToBi(int a)
{
int i=0,stack[10],r,s;
do
{
r=a/2;
s=a%2;
stack[i]=s;
if(r!=0)
{
i++;
a=r;
}
}while(r);
for (;i>=0;--i)
{
printf("%d",stack[i]);
}
printf("%d\n");
}
int main()
{
int a;
printf("please input a Decimal digit\n");
scanf("%d",&a);
DeToBi(a);
return 0;
}