C语言必背18个经典程序(转)

1、/*输出9*9口诀。共9行9列,i控制行,j控制列。*/

#include "stdio.h"
main()
{int i,j,result;
 for (i=1;i<10;i++)
   { for(j=1;j<10;j++)
       {
          result=i*j;
          printf("%d*%d=%-3d",i,j,result);/*-3d表示左对齐,占3位*/
       }
      printf("
");/*每一行后换行*/
   }
system("pause");
}

2、/*古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

兔子的规律为数列1,1,2,3,5,8,13,21....*/

#include "stdio.h"
main()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)
  { printf("%12ld %12ld",f1,f2);
     if(i%2==0) printf("
");/*控制输出,每行四个*/
     f1=f1+f2; /*前两个月加起来赋值给第三个月*/
     f2=f1+f2; /*前两个月加起来赋值给第三个月*/
   }
	system("pause");
}
 

3、/*判断101-200之间有多少个素数,并输出所有素数及素数的个数。

程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,

     则表明此数不是素数,反之是素数。*/

#include "math.h"
main()
{
  int m,i,k,h=0,leap=1;
  printf("
");
  for(m=101;m<=200;m++)
   { k=sqrt(m);
     for(i=2;i<=k;i++)
       if(m%i==0)
          {leap=0;break;}
       if(leap)    /*内循环结束后,leap依然为1,则m是素数*/ 
		   {printf("%-4d",m);h++;
           if(h%10==0)
               printf("
");
          }
     leap=1;
   }
  printf("
The total is %d",h);
  system("pause"); 
}

4、 /*一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如 6=1+2+3.编程
找出 1000 以内的所有完数。 */

int main()
{
	static int k[10];
	int i,j,n,s;
	for(j=2;j<1000;j++)
		{
		n=-1;
		s=j;
		for(i=1;i<j;i++)
			{if((j%i)==0)
				{ 	n++;
					s=s-i;
					k[n]=i;
				}
			}
		if(s==0)
		{
			printf("%d is a wanshu: ",j);
			for(i=0;i<n;i++)
			printf("%d,",k[i]);
			printf("%d
",k[n]);
		}
	}
	system("pause");
}

5、 /*下面程序的功能是将一个 4×4 的数组进行逆时针旋转 90 度后输出,要求原始数组的
数据随机输入,新数组以 4 行 4 列的方式输出,
请在空白处完善程序。 */

int main()
{ 
	int a[4][4],b[4][4],i,j; /*a 存放原始数组数据, b 存放旋转后数组数据*/
	printf("input 16 numbers: ");
	/*输入一组数据存放到数组 a 中,然后旋转存放到 b 数组中*/
	for(i=0;i<4;i++)
	for(j=0;j<4;j++)
	{ 
		scanf("%d",&a[i][j]);
		b[3-j][i]=a[i][j];
	}
	printf("array b:
");
	for(i=0;i<4;i++)
	{ 
		for(j=0;j<4;j++)
		printf("%6d",b[i][j]);
		printf("
");
	}
	system("pause");
}

6、 /*编程打印直角杨辉三角形*/

int main()
{
	int i,j,a[6][6];
	for(i=0;i<=5;i++)
	{
		a[i][i]=1;a[i][0]=1;
	}
	for(i=2;i<=5;i++)
	for(j=1;j<=i-1;j++)
	a[i][j]=a[i-1][j]+a[i-1][j-1];
	for(i=0;i<=5;i++)
	{
		for(j=0;j<=i;j++)
		printf("%4d",a[i][j]);
		printf("
");
	}
	system("pause");
}

7、 /*通过键盘输入 3 名学生 4 门课程的成绩,
分别求每个学生的平均成绩和每门课程的平均成绩。
要求所有成绩均放入一个 4 行 5 列的数组中,输入时同一人数据间用空格,不同人用回车
其中最后一列和最后一行分别放每个学生的平均成绩、每门课程的平均成绩及班级总平均
分。 */

#include <stdio.h>
#include <stdlib.h>
 
int main()
{ 
	float a[4][5],sum1,sum2;
	int i,j;
	for(i=0;i<3;i++)
	for(j=0;j<4;j++)
	scanf("%f",&a[i][j]);
	for(i=0;i<3;i++)
	{ 
		sum1=0;
		for(j=0;j<4;j++)
		sum1+=a[i][j];
		a[i][4]=sum1/4;
	}
	for(j=0;j<5;j++)
	{ 
		sum2=0;
		for(i=0;i<3;i++)
		sum2+=a[i][j];
		a[3][j]=sum2/3;
	}
	for(i=0;i<4;i++)
	{ 
		for(j=0;j<5;j++)
		printf("%6.2f",a[i][j]);
		printf("
");
	}
	system("pause");
}

8、 /*完善程序,实现将输入的字符串反序输出,
如输入 windows 输出 swodniw。 */

#include <string.h>
int main()
{ 
	char c[200],c1;
	int i,j,k;
	printf("Enter a string: ");
	scanf("%s",c);
	k=strlen(c);
	for (i=0,j=k-1;i<k/2;i++,j--)
	{ 
		c1=c[i];c[i]=c[j];c[j]=c1; 
	}
	printf("%s
",c);
	system("pause");
}

 

指针法:

#include <stdio.h>
 
void invert(char *s)
{
	int i,j,k;
	char t;
	k=strlen(s);
	for(i=0,j=k-1;i<k/2;i++,j--)
	{ 
		t=*(s+i); *(s+i)=*(s+j); *(s+j)=t; 
	}
}
main()
{ 
	FILE *fp;
	char str[200],*p,i,j;
	if((fp=fopen("p9_2.out","w"))==NULL)
	{ 
		printf("cannot open the file
");
		exit(0);
	}
	printf("input str:
");
	gets(str);
	printf("
%s",str);
	fprintf(fp,"%s",str);
	invert(str);
	printf("
%s",str);
	fprintf(fp,"
%s",str);
	fclose(fp);
	system("pause");
}

9、 /*下面程序的功能是从字符数组 s 中删除存放在 c 中的字符。 */

#include <stdio.h>
int main()
{ 
	char s[80],c;
	int j,k;
	printf("
Enter a string: ");
	gets(s);
	printf("
Enter a character: ");
	c=getchar( );
	for(j=k=0;s[j]!= '';j++)
	if(s[j]!=c)
	s[k++]=s[j];
	s[k]= '';
	printf("
%s",s);
	system("pause");
}

 

10、 /*编写一个 void sort(int *x,int n)实现将 x 数组中的 n 个数据从大到小
排序。 n 及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件 p9_1.out 中*/

#include<stdio.h>
 
void sort(int *x,int n)
{
	int i,j,k,t;
	for(i=0;i<n-1;i++)
	{
		k=i;
		for(j=i+1;j<n;j++)
		if(x[j]>x[k]) k=j;
		if(k!=i)
		{
			t=x[i];
			x[i]=x[k];
			x[k]=t;
		}
	} 
}
void main()
{
	FILE *fp;
	int *p,i,a[10];
	fp=fopen("p9_1.out","w");
	p=a;
	printf("Input 10 numbers:");
	for(i=0;i<10;i++)
	scanf("%d",p++);
	p=a;
	sort(p,10);
	for(;p<a+10;p++)
	{ 
		printf("%d ",*p);
		fprintf(fp,"%d ",*p); 
	}
	system("pause");
	fclose(fp);
}

11、已知数组 a 中的元素已按由小到大顺序排列,以下程序的功能是将输入的一个数插入数
组 a 中,插入后,数组 a 中的元素仍然由小到大顺序排列*/

#include <stdio.h>
 
int main()
{ 
	int a[10]={0,12,17,20,25,28,30}; /*a[0]为工作单元,从 a[1]开始存放数据*/
	int x , i, j=6; /*j 为元素个数*/
	printf("Enter a number: ");
	scanf("%d",&x);
	a[0]=x;
	i=j; /*从最后一个单元开始*/
	while(a[i]>x)
	{ 
		a[i+1]=a[i]; i--; 
	} /*将比 x 大的数往后移动一个位置*/
	
	a[++i]=x;
	j++; /*插入 x 后元素总个数增加*/
	for(i=1;i<=j;i++) printf("%8d",a[i]);
	printf("
");
	system("pause");
}

 

12、 /*编写函数 replace(char *s,char c1,char c2)实现将 s 所指向的字符串中所有字符 c1
用 c2 替换,字符串、字符 c1 和 c2 均在主函数中输入,将原始字符串和替换后的字符串显
示在屏幕上, 并输出到文件 p10_2.out 中*/

#include<stdio.h>
 
replace(char *s,char c1,char c2)
{ 
	while(*s!='')
		{ if (*s==c1)
			*s=c2;
			s++;
		}
}
 
main()
{ 
	FILE *fp;
	char str[100],a,b;
	if((fp=fopen("p10_2.out","w"))==NULL)
	{ 
		printf("cannot open the file
");
		exit(0); 
	}
	printf("Enter a string:
");
	gets(str);
	printf("Enter a&&b:
");
	scanf("%c,%c",&a,&b);
	printf("%s
",str);
	fprintf(fp,"%s
",str);
	replace(str,a,b);
	printf("The new string is----%s
",str);
	fprintf(fp,"The new string is----%s
",str);
	fclose(fp);
	system("pause");
}

13、 /*在一个字串 s1 中查找一子串 s2,若存在则返回子串在主串中的起始位置
,不存在则返回-1。 */

main()
{
	char s1[6]="thisis";char s2[5]="is";
	printf("%d
",search(s1,s2));
	system("pause");
}
 
int search(char s1[],char s2[])
{
	int i=0,j,len=strlen(s2);
	while(s1[i])
	{
		for(j=0;j<len;j++)
		if(s1[i+j]!=s2[j]) break;
		if(j>=len)return i;
		else i++;
	}
	return -1;
}

14、 /*用指针变量输出结构体数组元素。 */

struct student
{
	int num;
	char *name;
	char sex;
	int age;
}
stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"feng
shou",'F',19},{1005,"Wangming",'M',18}};
 
main()
{
	int i;
	struct student *ps;
	printf("Num 	Name			Sex	Age	
");
	/*用指针变量输出结构体数组元素。 */
	for(ps=stu;ps<stu+5;ps++)
	printf("%d	%-10s		%c	%d	
",ps->num,ps->name,ps->sex,ps->age);
	/*用数组下标法输出结构体数组元素学号和年龄。 */
	for(i=0;i<5;i++)
	printf("%d	%d	
",stu[i].num,stu[i].age);
	system("pause");
}

15、 /*建立一个有三个结点的简单链表: */

#define NULL 0
struct student
{
	int num;
	char *name;
	int age ;
	struct student *next;
};
 
void main()
{
	struct student a,b,c,*head,*p;
	a.num=1001; a.name="lihua"; a.age=18; /* 对结点成员进行赋值 */
	b.num=1002; b.name="liuxing"; b.age=19;
	c.num=1003; c.name="huangke"; c.age=18;
	head=&a; /* 建立链表, a 为头结点 */
	a.next=&b;
	b.next=&c;
	c.next=NULL;
	p=head; /* 输出链表 */
	
	do{
		printf("%5d,%s,%3d
",p->num,p->name,p->age);
		p=p->next;
	}while(p!=NULL);
	
	system("pause");
}

16、 /*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读
完全相同的字符串。 */

#include <stdio.h>
#include <string.h>
#include<string.h>
 
main()
{ 
	char s[100];
	int i,j,n;
	printf("输入字符串: 
");
	gets(s);
	n=strlen(s);
	for(i=0,j=n-1;i<j;i++,j--)
	if(s[i]!=s[j]) 
		break;
	if(i>=j) 
		printf("是回文串
");
	else 
		printf("不是回文串
");
	system("pause");
}

17、 /*冒泡排序,从小到大,排序后结果输出到屏幕及文件 myf2.o

#include<stdio.h>
void fun(int a[],int n)
{
	int i,j,t;
	for(i=0;i<=n-1;i++)
	for(j=0;j<i;j++)
	if(a[j]>a[j+1]) 
	{
		t=a[j];a[j]=a[j+1];a[j+1]=t;
		}
}
 
main()
{
	int a[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;
	FILE *f;
	if((f=fopen("myf2.out","w"))==NULL)
	printf("open file myf2.out failed!
");
	fun(a,10);
	for(i=0;i<10;i++)
	{
		printf("%4d",a[i]);
		fprintf(f,"%4d",a[i]);
	}
	fclose(f);
	system("pause");
}

#include<stdio.h>
double countpi(double eps) /*eps 为允许误差*/
{
	int m=1;
	double temp=1.0,s=0;
	while(temp>=eps)
	{ 
		s+=temp;
		temp=temp*m/(2*m+1);
		m++;
	}
	
	return(2*s);
}
 
main()
{
	FILE *fp;
	double eps=1e-5,pi;
	if((fp=fopen("p7_3.out","w"))==NULL)
	{ 
		printf("cannot open the file
");
		exit(0);
	}
	pi= countpi(eps);
	printf("pi=%lf
",pi);
	fprintf(fp,"pi=%lf
",pi);
	fclose(fp);
	system("pause");
}
原文地址:https://www.cnblogs.com/linxw-blog/p/12664939.html