资源描述
/*C语言重要程序算法实例整理*/
//递归练习:阶乘
#include"stdio.h"
float fac(int x);
void main()
{
int n;puts("Input the number:");
scanf("%d",&n);
float f;
f=fac(n);
printf("%d!=%2.f",n,f);
}
float fac(int x)
{
int f=x;
if(x<0)
puts("ERROR!");
else
{
if(x==0||x==1)
f=1;//当x=0或x=1时,须返回f的值,考虑满足条件时f值是多少
else
f=x*fac(x-1);//返回函数return必须写在最后,每次调用完函数后都要有一个返回值
}
return f;
}
/*
递归调用的关键:
1.f=x*fac(x-1)的嵌套模式;
2.最后一个被调函数的返回值,比如fac(0);
*/
/*===================================================================*/
//递归练习:求和1
#include"stdio.h"
int sum(int x);
void main()
{
int n,*p=&n,a;
puts("Input the last number:");
scanf("%d",p);
a=sum(n);
printf("1+…+%d=%d\n",n,a);
}
int sum(int x)
{
int s=0;
if(x==0)
s=0;//当x=0时,须返回s的值,考虑满足条件时s值是多少
else
s=x+sum(x-1);
return s;//返回函数return必须写在最后,每次调用完函数后都要有一个返回值
}
/*
递归调用的关键:
1.s=x*sum(x-1)的嵌套模式;
2.最后一个被调函数的返回值,比如sum(0);
*/
/*==================================================================*/
//递归求和2
#include"stdio.h"
int SUM(int n)
{
static int fs=0;
fs+=n;
if((n==1)&&(n>0))
return(fs);
else
SUM(n-1);
}
void main()
{
int x;
int fsum;
printf("please input the number:\n");
scanf("%d",&x);
fsum=SUM(x);
printf("SUM=%d\n",fsum);
}
/*=================================================================*/
//动态分配内存应用:删除字符
#include"stdio.h"
#include"stdlib.h"
void main()
{
int n,i;
printf("Please input n:");
scanf("%d",&n);
printf("Please input the string:");
char *p;
p=(char *)malloc(sizeof(char));
for(i=0;i<n+1;i++)
scanf("%c",p+i);
*(p+i)='\0';
// puts(p);
printf("Please input the k and the m: ");
static int k,m;
scanf("%d,%d",&k,&m);
char *q;
q=(char *)calloc(n,sizeof(char));
int b;
for(i=0;i<m;i++)
{
b=i+k+m;
*(q+i)=*(p+b);
}
*(q+i)='\0';
// puts(q);
*(p+k)='\0';
// puts(p);
for(i=0;i<m;i++,k++)
*(p+k)=*(q+i);
// *(p+k)='\0';
printf("******After Delete******\n");
puts(p);
// free(p);
// free(q);
}
//*============================================================*/
//数据类型计算函数
#include"stdio.h"
void main()
{
int a;
long b;
float f;
double d;
char c;
printf("\nint:%d\nlong:%d\nfloat:%d\ndouble:%d\nchar:%d\n",sizeof(a),sizeof(b),sizeof(f),sizeof(d),sizeof(c));
}
//*=====================================*/
//基础练习:矩阵转置
#include"stdio.h"
void main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9},i,j,b[3][3],t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i!=j)
b[j][i]=a[i][j];
else
b[i][j]=a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d",b[i][j]);
printf("\n");
}
}
//=====================================================
//基础练习:运算符
#include"stdio.h"
void main()
{
int a=12,b=7;
printf("%d,%d",a%b,a/b);//(%)取余数,(/)取商数
}
//=====================================================
//递归法整数转换字符串
#include"stdio.h"
void fun2(int x,int n,char s[])
{
if(x<0)
{
printf("x<0,data error!\n");
fun2;
}
else
if(x<10)
s[n]='0'+x;
else
{
fun2(x/10,n-1,s);
s[n]='0'+x%10;
}
}
//主函数
void main()
{
int a,x,n=0;
char str[20];
scanf("%d",&a);
x=a;
while(a>0)
{
n++;
a=a/10;
str[n]='\0';
fun2(x,n-1,str);
puts(str);
}
}
//=================================================================
//整数转换字符串2
#include"stdio.h"
#include"math.h"
void fun1(int x,char s[])
{
int i,n,t,y;
n=0;
y=x;
while(x>0)
{
n++;
x=x/10;
}
for(i=0;y>0;i++)
{
t=y/(int)pow(10,n-1);
s[i]='0'+t;
y=y%(int)pow(10,n-1);
n--;
}
s[i]='\0';
}
void main()
{
int a;
char str[20];
scanf("%d",&a);
fun1(a,str);
puts(str);
}
//===================================================
//基础练习:自增自减的前后区分
#include"stdio.h"
void main()
{
int i=8;
printf("++i=%d\n",++i);printf("i=%d\n",i);
printf("--i=%d\n",--i);printf("i=%d\n",i);//先进行加1或减1,再输出i的值
printf("i++=%d\n",i++);printf("i=%d\n",i);
printf("i--=%d\n",i--);printf("i=%d\n",i);//先输出i值,再进行加1或减1
printf("-i++=%d\n",-i++);printf("i=%d\n",i);//先输出-i,在加1
printf("-i--=%d\n",-i--);printf("i=%d\n",i);//先输出-i,再减1
}
//=======================================================
//
/*链表建立的算法(p1指向新结点,p2指向表尾)
S1 定义三个结点类型(struct node)的指针变量:head,p1,p2,head=NULL
S2 调用malloc(#include"stdlib.h")函数产生一个新结点,令p1,p2都指向该结点,并输入其数据成员
S3 判断新节点的学号是否为0,若学号不为0,则执行 S4,否则执行 S7
S4 判断头指针(*head)是否为NULL。若head为NULL,则令头指针指向表头的结点,即,head=p1;若head不为NULL,则责令当前表尾结点的指针成员指向新结点,即,p2->next=p1;
S5 令指针变量p2指向新的表尾结点:p2=p1;
S6 调用malloc函数产生一个新结点,令p1指向新结点,并输入数据成员,然后执行S3;
S7 表尾结点的指针成员赋空值,即,p2->next=NULL;
S8 释放p1所指向的结点空间,即,free(p1);
S9 返回链表的头指针即,return head;
(思考:数据存在哪了?被调函数不是会将内存擦去吗)
链表输出的算法
S1 判断链表的头指针是否为NULL,若head为NULL,则输出“链表为空”,然后结束;若链表不为空,则执行S2
S2 另一个指针变量p赋值为head
S3 判断p是否为NULL,若p不为NULL,则执行S4,否则结束循环
S4 输出p数据成员的值,然后使p指向下一个结点,即,p=p->next。在执行S3
链表删除的算法
例如
*/
#include"stdio.h"
#include"stdlib.h"
struct sal_node{
int num;
char nam[20];
float salary;
struct sal_node *next;
};
#define L sizeof(struct sal_node)
struct sal_node *creat()
{
struct sal_node *p,*q,*head=NULL;
printf("Please input the worders' informations:\nnumber\tname\tsalary\n");
p=q=(struct sal_node *)malloc(L);
scanf("%d%s%f",&p->num,&p->nam,&p->salary);
while(p->num!=0)
{
if(head==NULL)
head=p;
else
q->next=p;
q=p;
p=(struct sal_node *)malloc(L);
scanf("%d%s%f",&p->num,&p->nam,&p->salary);
}
q->next=NULL;
free(p);
return head;
}
void list(struct sal_node *head)
{
struct sal_node *r;
if(head==NULL)
printf("Empty!\n");
else
{
printf("Salary informations:\n\nnumber\tname\tsalary\n");
r=head;
while(r!=NULL)
{
printf("%d\t%s\t%.2f\n",r->num,r->nam,r->salary);
r=r->next;
}
}
}
void main()
{
struct sal_node *head;
head=creat();
list(head);
}
//=============================================================
//链表应用:职工工资动态信息
#include"stdio.h"
#include"stdlib.h"
struct salary_node
{
int num;
char nam[20];
float sal;
struct salary_node *next;
};
#define L sizeof(struct salary_node)
struct salary_node *creat()
{
struct salary_node *p,*q,*h=NULL;
p=q=(struct salary_node *)malloc(L);
printf("请输入职工信息:\n编号\t姓名\t工资\n");
scanf("%d%s%f",&p->num,&p->nam,&p->sal);
while(p->num!=0)
{
if(h==NULL)
h=p;
else
q->next=p;
q=p;
p=(struct salary_node *)malloc(L);
scanf("%d%s%f",&p->num,&p->nam,&p->sal);
}
q->next=NULL;
free(p);
return h;
}
void list(struct salary_node *h)
{
struct salary_node *p;
p=h;
if(h==NULL)
printf("无职工工资信息!");
else
printf("\n编号\t姓名\t工资\n");
while(p!=NULL)
{
printf("%d\t%s\t%.2f\n",p->num,p->nam,p->sal);
p=p->next;
}
}
float ave(struct salary_node *h)
{
int n=0;
float s=0,a;
struct salary_node *p;
p=h;
while(p!=NULL)
{
s+=p->sal;
p=p->next;
n++;
}
a=s/n;
return a;
}
void above(float x,struct salary_node *h)
{
struct salary_node *p;
p=h;
while(p!=NULL)
{
if(p->sal>x)
printf("%s\n",p->nam);
p=p->next;
}
}
void main()
{
float Ave;
struct salary_node *h;
h=creat();
list(h);
Ave=ave(h);
printf("\n职工工资平均水平:%.2f\n",Ave);
printf("高于职工工资平均水平的职工姓名是:\n");
above(Ave,h);
}
//==========================================================
//结构体应用:日期计算器
#include"stdio.h"
struct date
{
int y;
int m;
int d;
};
int Cal_day(int k,int m,int d)
{
int i;
int a[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
for(i=0;i<m-1;i++)
d+=a[k][i];
return d;
}
void main()
{
int i,k,d[2],sd;
struct date day[2],*p,*q;
q=p=day;
for(i=0;i<2;i++)
{
printf("Please input the No.%d date:\n",i+1);
scanf("%d%d%d",&p->y,&p->m,&p->d);
k=((p->y%4==0&&p->y%100!=0)||(p->y%400==0));
d[i]=Cal_day(k,p->m,p->d);
p++;
}
p--;
int u=p->y-q->y,v=q->y-p->y;
if(u<0||u==0)
sd=365-d[0]+d[1]+(v-1)*365+v/4;
else
sd=365-d[1]+d[0]+(u-1)*365+u/4;
printf("There are %d days between two dates\n",sd);
}
//=====================================================================
//文件的操作:fputc
#include"stdio.h"
#include"stdlib.h"
void main()
{
FILE *f;
char s[100],c;
printf("Please input the filepath:");
gets(s);
if((f=fopen(s,"a"))==NULL)
{
printf("Haven't Found %s!",s);
exit(0);
}
printf("Please input the string:");
do
{
c=getchar();
fputc(c,f);//输入文件中的字符,文件路径
}while(c!='\n');
fclose(f);
}
//==============================================================
//文件操作:复制文件
#include"stdio.h"
#include"stdlib.h"
#define N 100
void main()
{
FILE *fs,*fd;
char ch,sf[N],df[N];
printf("Please input the Source filepath(name):");
scanf("%s",sf);
printf("Please input the Destanation filepath(name)");
scanf("%s",df);
if((fs=fopen(sf,"r"))==NULL)//path,r(w),""
{
printf("Haven't Found %s",sf);
exit(0);
}
if((fd=fopen(df,"w"))==NULL)
{
printf("Haven't Found %s",df);
exit(0);
}
while(!feof(fs))
{
ch=fgetc(fs);//path
fputc(ch,fd);//character,path
}
fclose(fs);
fclose(fd);
}
/*
w__从文件开头写字符
a+___先读原文件再从文件结尾写字符
r__只读操作
*/
//==============================================
//文件操作:字符(串)的读写
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define N 81
void main()
{
FILE *f;
char p[N];
printf("Please input the filepath:");
scanf("%s",p);getchar();//此处必须要加getchar()函数,否则会对之后吸入字符串有影响,计算机会认为没有写入任何字符而造成产生大量随机值
if((f=fopen(p,"a+"))==NULL)//fopen:打开文件操作,语法是(path,mode),指针f指向fopen
{
printf("Haven't Found the File!");
exit(0);
}
char s[N];
while(strlen(gets(s))>0)//向文件中写入字符串的循环条件是:字符串长度大于0
{
fputs(s,f);//fputs:写入文件字符串,语法fputs(str,f)
fputs("\n",f);//敲击键盘的回车键对于计算机而言是结束输入字符串,而程序员是换行,故须要加换行符
}
rewind(f);//复位函数,避免多次打开文件,语法rewind(f)
char c[N];
while(fgets(c,N,f)!=NULL)//若循环条件是fgets(c,N,f)!=NULL,则循环体中不需要读取字符串,否则文件中的字符串不能全部显示在屏幕上
//循环条件不能是!feof(f),否则,屏幕上会所显示一行字符串
//fgets:读取文件字符串,语法fgets(s,n,f)
printf("%s",c);//另外,fgets(c,N,f)应经将文件中的字符串赋值给c,不需要另赋值
rewind(f);
char q[N];
FILE *g;
puts("input no.2 path");
gets(q);//必须赋值!
if((g=fopen(q,"w+"))==NULL)
{
printf("Haven't Found the File!");
exit(0);
}
char b;
do
{
b=getchar();
fputc(b,g);//fputc(char,g)//你已经打开另一个文件了!
}while(b!='\n');//fputc只能输入一行字符串
char a;
rewind(g);//此处必须加复位函数!!!!
while(!feof(g))//此处是g!
{
a=fgetc(g);
printf("%c",a);
}
fclose(f);
fclose(g);
}
/*
总结:
函数 语法 作用
fopen (p,mode) 打开文件
fclose (f) 关闭文件
fputc (char,f) 向文件中写入一个字符
fgetc (f) 从文件中读取一个字符
fputs (s,f) 向文件中写入一个字符串
fgets (s,n,f) 从文件中读取一个含有n个字符的字符串*/
//=====================================================
//文件操作:输出文本文档的行号
#include"stdio.h"
#include"stdlib.h"
#define N 500
void main()
{
FILE *f;
char name[N],c,str[N];
printf("Please input the filepath(name):");
scanf("%s",name);getchar();
int i=0;
if((f=fopen(name,"a+"))==NULL)
{
printf("Haven't Found %s\n",name);
exit(0);
}
int n=1;printf("%d",n);
do
{
c=fgetc(f);
putchar(c);
if(c=='\n'){n++;printf("%d",n);}
}while(!feof(f));
fclose(f);
}
//=========================================================
//指针练习:二维数组的指针表示
#include"stdio.h"
void main()
{
int a[3][5]={{76,0,21,53,15},{368,0,97,12},{0,86,96,24}};
int i,j;
//下标表示
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%4d",a[i][j]);
printf("\n");//行标增加,换行
}
printf("\n\n");
//行指针表示
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%4d",*(a[i]+j));
printf("\n");
}
printf("\n\n");
//列指针表示
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%4d",(*(a+i))[j]);
printf("\n");
} printf("\n\n");
//二级指针表示
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%4d",*(*(a+i)+j));//*(*(a+i)+j)是二级地址
printf("\n");
} printf("\n\n");
//指针变量表示(双层循环)
int *p;p=a[0];//a[0]<=>&a[0][0]<=>*a (*a是一级地址)
for(i=0;i<3;i++)
{
for(j=0;j<5;j++,p++)//地址也需要增加
printf("%4d",*p);//直接输出*p
printf("\n");
} printf("\n\n");
//指针变量表示(单层循环)
for(p=a[0];p<a[0]+15;p++)//妙,以地址作为循环条件
{
if((p-a[0])%4==0)
printf("\n");
printf("%4",*p);
}
}
//=====================================================
//指针练习:行指针变量
#include"stdio.h"
void main()
{
int a[3][4]={{16,9862},{8073,16,50,0},{6,778,16,9}};//数组的地址是连续的
int (*p)[4];//定义行指针变量
int i,j;
p=a;//p指向二维数组的首地址
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("%5d",*(*(p+i)+j));//将a换成p即可
printf("\n");
}
}
//============================================================
//指针练习:指向函数的指针变量
#include"stdio.h"
int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
void main()
{
int a,b,q;
int (*p)(int x,int y);//必须标明参数类型
p=max;
scanf("%d,%d",&a,&b);
q=(*p)(a,b);
printf("\nmaximum=%d\n",q);
}
//注:在主函数中定义的仍是指针变量,致使该指针变量指向函数的地址,间接访问函数
//===============================================================
//指针练习:指针函数
#include"stdio.h"
int *max(int a,int b)//定义指针函数
{
if(a>b)
return &a;
else
return &b;//返回的是地址
}
void main()
{
int m,n,*p;
printf("Please input the two numbers:");
scanf("%d,%d",&m,&n);
p=max(m,n);
printf("\nmax=%d\n",*p);
}
//=======================================================
//指针练习:指针与一位数组
#include"stdio.h"
int max(int *x);
int *max2(int *x);//指针函数的声明
void main()
{
int a[10],*p,i,maxi,*maxi2;
p=&a[0];
for(i=0;i<10;i++)
scanf("%d",p+i);
maxi=max(p);
printf("Maximum=%d\n",maxi);
maxi2=max2(p);//引用时不用加*
printf("Maximum=%d\n",maxi);
}
int max(int *x)
{
int j,Max;
Max=*x;
for(j=0;j<10;j++)
{
if(*(x+j)>Max)
Max=*(x+j);
}
return Max;
}
int *max2(int *x)//指针函数的定义
{
int j,Max;
Max=*x;
for(j=0;j<10;j++)
{
if(*(x+j)>Max)
Max=*(x+j);
}
return &Max;//返回地址
}
//=======================================================
//指针练习:指针与字符
#include"stdio.h"
void main()
{
char str[]="hello\n",*p=str;
int i;
printf("1:\n");//直接输出
puts(p);//p<=>str
printf("2:\n");//输出字符串
printf("%s",p);
printf("3:\n");//输出字符,i控制循环圈数
for(i=0;*(p+i)!='\0';i++)
printf("%c",*(p+i));
printf("4:\n");//地址也进行自增
for(i=0;*(p+i)!='\0';i++,p++)
printf("%c",*p);
}
//==================================================
//指针练习:输出N个数中的最大值和次最大值
#include"stdio.h"
#define N 5
int max(int *x);
int lessmax(int *x);
void main()
{
int a[N],*p,i,MAX,LESSMAX;
p=&a[0];
printf("Please input %d numbers:",N);
for(i=0;i<N;i++)
scanf("%d",p+i);
MAX=max(p);
printf("Maximum=%d\n",MAX);
LESSMAX=lessmax(p);
printf("Less Maximum=%d\n",LESSMAX);
}
int max(int *x)
{
int i,Max=*x;
for(i=0;i<N;i++)
{
if(*(x+i)>Max)
Max=*(x+i);
}
return (Max);
}
int lessmax(int *x)
{
int i,Max=*x,t;
for(i=0;i<N;i++)
{
if(*(x+i)>Max)
{
t=Max;
Max=*(x+i);
}
}
return (t);
}
//=====================================================
//指针应用:计算围成一圈的相邻三个数的和
#include"stdi
展开阅读全文