C语言最经典的程序设计,18个典型程序,最基本的内容printf("array b: n"):forli=0: i<4: 1++)i for(j=0: j<4: j++)printf("%o6d", b[il[jD);printf("n");6、拌编程打印直角杨辉三角形maine[int i, j, a[6][6];for(i=0<=5;i++)fail[il=l; a[il[o]=1;for(1=2;i<=5;i++)for(j=l: j<=i-1: j++)a[][]=ali-10]+a[i-1[j-1];for(i=0;i<=5;i++)for(j=0j<=ij+十)printf("904d, a[i]Lj1);printf("n"): 17、通过键盘输入3名学生4门课程的成绩,分别求每个学生的平均成绩和每门课程的平均成绩。要求所有成绩均放入一个4行5列的数组中,输入时同一人数据间用空格,不同人用回车其中最后一列和最后一行分别放每个学生的平均成绩、每门课程的平均成绩及班级总平均分。数#include #include mainoI float a(41[5], suml, sum2Int 1.for(i=0;i<3;i++)forGe=0; j<4: j ++)scanf("%of, &a[[jD:for(i=0;i<3:i++)i sumI=0;for(j=0; j<4: j++)suml+=ailljl:a[4=sum 1/4for(j=0; j<5: j++)sum2=0for(i=0;i<3;i++)sum2+=a[il[jl:a3[=sum2/3forli=0: i<4: 1++)i for(j=0 j<5; j++)printf("96.2f", a[il[jl);printf("In );8、完善程序,实现将输入的字符串反序输出如输入 windows输出 swodniw。#include main(char c[2001, c1;Int 1,Iprintf("Enter a string: )scanf("%os", c)k=strlen(c);for(i=0j=k-1;imain(i char s[801, cprintf("eNter a string: )gets(sprintf("nEnter a character: )=getchar()for(j=k=0; s [j]!=10; j ++if(sli!=c)s[k++]=s{i]s[k}="0;printf("\n%os,S)10、/编写一个 void sort (int*xntn)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p91out中*#includevoid sort(intx, int n)int i,j, k, t;for(i=0: ix)a[i+1-a[il;i-;件将比x大的数往后移动一个位置a[ ++il=x;J++;插入x后元素总个数增加for(i=l; i<=j; i++) printf("%8d", a[iD)printf("n);12、倖编写函数 replace(char*s, char clchar c2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符cI和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p102out中#includereplace(char *, char cl, char c2)i while(s!=\0)if(*s==c1)S十+;mainOI FILE fpchar str[ 1001, a, b:if((fp=fopen("p10_2out", "W))==NULL)i printf("cannot open the fileIn);exit(o)printf("Enter a string: \n")gets( str)printf("Enter a&&b: Inscanf("%c,%c", &a, &b);printf("%osin,str);fprintf(fp, " os\n", str)replace(str, a, b)printf("The new string is---%sIn",str)fprintf(fp, " The new string is----%\n", str);fclose(fp);13、件在一个字串s1中查找一子串s2,若存在则返回子串在主串中的起始位置,不存在则返回-1。*maino[char sl[6="thisis" char s2[5]="is",printf("%\n", search(s l, s2)system(" pause");int search(char s[l, char s2l[int i=0. j, len=strlen(s2);while(sli](for(j=0:: j=len )return 1;else i++:return14、/用指针变量输出结构体数组元素。*struct studentInt numchar * namechar sex;Int agelstu[Sl={{1001," lihua",F,18},{1002" liuxing",M,19},1003," huanghe",F,19},(1004,"tengshou,F, 191, 1005, "Wangming,M, 181:maIn(lint 1;struct student ps;printf("Num \tNamelt\t\tSextAgeItIn");/*用指针变量输出结构体数组元素。*for(ps=stu: psnum, ps->name, ps->sex, ps->age)*用数组下标法输出结构体数组元素学号和年龄。*for(i=0;i<5;i++)printf("%dt%d\t\n", stu[li]. num, stu[i]- age)15、/建立一个有三个结点的简单链表:*井 define null0struct studentnt numchar name.Int agestruct student * nextd mainostruct student a, b, c, head,anum=1001; a name=" lihua";a.age=18:/伴对结点成员进行赋值*b num=1002; b. name="liuxing" b age=19c num=1003; c name="huangke cage=18head=&a建立链表,a为头结点*a next=&b:b next=&c.c. nexteNULL:=head/*输出链表*doprintf("95d,os, %3din"p->num, p->name, p->age)p=p->next;J while(p!=NULL)16、输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*#include #include #includemainoi char s[100]print("输入字符串:Ⅶ");gets(s)n=strlen(s);for(i=0.j=n-l; K=j) printi("是回文串n");printf("不是回文串Ⅶ");17、/冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out#includevoid fun(int all, int n)[int i,j, t;for(i=0;i<=n-1;i++)for(=0; jdouble countpi(double eps/+eps为允许误差*Int m=l;double temp=1. 0.=0while(temp>=eps)S+=temp:temp=temp"m/(2 m+1)return(2*s);mainO[ fpdouble eps=le-5 pi;if(fp=fopen(p7_3out", " W))==NULLi printf("cannot open the filen")exit(O)pi= countpi(eps);printf("pi=golf\",pi);fprintf(fp, "pi=%If\n pi)fclose(tp)