C语言学习
0.斐波那契数列(未完成,要求是能够输入一个未知大小的数组)
#include<stdio.h>
int main(void)
{
int i,j,k;
int res=0;
int l=2;
i=0;
j=1;
scanf("%d",&k);
printf(“您输入的数值是:%d\n”,k);
if(k0)
printf(“第%d个数是:%d\n”,k,res);
else if (k1)
printf(“第%d个数是:1\n”,k);
else
for(l=2;l<k;l++)
{
}
}
- 函数定义
#include<stdio.h>
void printmessage()// 子函数的使用,如果放在main()函数之前,就不用声明,否则在定义的时候要进行声明才可以使用。
{
printf(“This is about function!”);
}
int main(void)
{
printmessage();
printf("\n");
return 0;
}
- 二维数组,数据大小比较
include<stdio.h>
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int main()
{
int array[10]={1,4,5,6,7,34,67,3,45,67};
int i;
printf(“输出比较之前的数组:\n”);
for(i=0;i<10;i++)
{
printf("%d “,array[i]);
}
printf(”\n");
void sort(int a[],int n);
sort (array,10);//传的数组的地址
printf("输出比较之后的数组:\n");
for(i=0;i<10;i++)
{
printf("%d ",array[i]);
}
printf("\n");
return 0;
}
3. 递归函数的使用
include<stdio.h>
int main()
{
unsigned int j;
unsigned long int fac(unsigned int n);
for(j=0;j<50;j++)
printf("%2u!=%lu\n",j,fac(j));
return 0;
}
unsigned long int fac(
unsigned int n)
{
unsigned long int result;
if(n==0)
result=1;
else
result =n*fac(n-1);
return result;
}
- 结构体使用
include<stdio.h>
struct date
{
int month;
int year;
int day;
}; // ;一定不能漏掉
struct date today; // 声明变量,通过stuct实现
int main()
{
today.day=10;
printf("%d\n",today.day);
return 0;
}
- 指针和链表的使用(例程)
include<stdio.h>
int main()
{
struct entry
{
int value;
struct entry *next;
};
struct entry n1,n2,n3;
int i;
n1.value =100;
n2.value =200;
n3.value=300;
n1.next=&n2;
n2.next=&n3;
i=n1.next->value;
printf("%d\n",i);
printf("%d\n",n2.next->value);
return 0;
}
2019.1.8 C语言学习
1.用fopen打开文件,用fclose 关闭文件,用getc获取文件中单个字符,通过循环即可得到所有字符(文件中),用putc函数可以将获取的字符放在相应的文件中。注意putc有两个参数
有关文件读取地址问题
在编程中,有绝对地址和相对地址。绝对地址是文件的全名称目录,就是所有的,例如C:\Users\Administrator\Desktop\copyme.txt,在磁盘哪个位置等。
而相对路径是自己新建的程序,就是编程目录下面的,自己文件保存的地方,只用把需要用到的文件放在自己的程序目录下就可以了,例如,同样是刚刚的文件,我可以使用相对路径进行读取:C:\Users\Administrator\Desktop\C语言学习文档\C语言练习1.6程序\copyme.txt
注意:为了让自己编写的程序具有通用性,最好使用相对路径,这样程序移植性比较好。
#include<stdio.h>
int main(void)
{
char inName[64],outName[64]; // 文件名字不能超过64位
FILE *in,*out; // 指针类型,进行文件接收
int c;
// 从使用者得到文件名
printf(“Enter name of file to be copied:”);
scanf("%63s",inName); // 读取的文件名字
printf(“Enter name of output file:”);
scanf("%63s",outName); // 输出文件的名字,如果系统中存在这样一个文件,那么将会被覆盖掉
// 打开文件
if((in = fopen(inName,“r”))==NULL) // 文件进行判断,看是否是空
{
printf(“Can’t open %s this file!\n”,inName);
return 1;
}
if((out=fopen(outName,“w”))==NULL)
{
printf(“Can’t open %s for writing!\n”,outName);
return 2;
}
while((c=getc(in))!=EOF) // 进行循环读取文件,从文件中获得每一个字符,直到结束
putc(c,out);
fclose(in); // 注意使用结束以后一定要关闭文档,这样才不至于损坏文件
fclose(out);
printf("File has been copied.\n");
return 0;
}
EOF使用简介
在C语言中,或更精确地说成C标准函数库中表示文件结束符(end of file)。在while循环中以EOF作为文件结束标志,这种以EOF作为文件结束标志的文件,必须是文本文件。在文本文件中,数据都是以字符的ASCII代码值的形式存放。我们知道,ASCII代码值的范围是0~127,不可能出现-1,因此可以用EOF作为文件结束标志。
#include<stdio.h>
int main(void)
{
char inName[64],outName[64]; // 文件名字不能超过64位
FILE *in,*out; // 指针类型,进行文件接收
int c;
// 从使用者得到文件名
printf(“Enter name of file to be copied:”);
scanf("%63s",inName); // 读取的文件名字
printf(“Enter name of output file:”);
scanf("%63s",outName); // 输出文件的名字,如果系统中存在这样一个文件,那么将会被覆盖掉
// 打开文件
if((in = fopen(inName,“r”))==NULL) // 文件进行判断,看是否是空
{
printf(“Can’t open %s this file!\n”,inName);
return 1;
}
if((out=fopen(outName,“w”))==NULL)
{
printf(“Can’t open %s for writing!\n”,outName);
return 2;
}
while((c=getc(in))!=EOF) // 进行循环读取文件,从文件中获得每一个字符,直到结束
{
{if(c>=97&&c<=122) // 进行大小字符转换,将小写字母转换为大写字母
c=c-32;
//putc(c,out);
}
putc(c,out);
}
fclose(in); // 注意使用结束以后一定要关闭文档,这样才不至于损坏文件
fclose(out);
printf("File has been copied.\n");
return 0;
}