c语言必背18个程序,C++必背18个经典程序(最新整理)

《C++必背18个经典程序(最新整理)》由会员分享,可在线阅读,更多相关《C++必背18个经典程序(最新整理)(14页珍藏版)》请在人人文库网上搜索。

1、c 语言入门经典必背 18 个程序1 、 /* 输出 9*9 口诀。共 9 行 9 列, i 控制行, j 控制列。 */#include stdio.h main()int i,j,result; for (i=1;i #include main() float a45,sum1,sum2; int i,j;for(i=0;imain() char c200,c1; int i,j,k;printf(Enter a string: ); scanf(%s,c);k=strlen(c);for (i=0,j=k-1;i main() char s80,c; int j,k;printf(nEn。

2、ter a string: ); gets(s);printf(nEnter a character: ); c=getchar( );for(j=k=0;sj!= 0;j+) if(sj!=c)sk+=sj;sk= 0;printf(n%s,s);10 、 /* 编写一个 void sort(int *x,int n) 实现将 x 数组中的 n 个数据从大到小排序。 n 及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件 p9_1.out 中 */ #includevoid sort(int *x,int n)int i,j,k,t; for(i=0;ixk) k=j; if(k!=i。

3、)t=xi; xi=xk;xk=t;void main()FILE *fp;int *p,i,a10; fp=fopen(p9_1.out,w); p=a;printf(Input 10 numbers:); for(i=0;ix) ai+1=ai; i-; /* 将比 x 大的数往后移动一个位置 */a+i=x;j+; /* 插入 x 后元素总个数增加 */ for(i=1;ireplace(char *s,char c1,char c2) while(*s!=0) if (*s=c1)*s=c2; s+;main() FILE *fp;char str100,a,b; if(fp=fope。

4、n(p10_2.out,w)=NULL) printf(cannot open the filen); exit(0); printf(Enter a string:n); gets(str);printf(Enter a&b:n); scanf(%c,%c,&a,&b); printf(%sn,str);fprintf(fp,%sn,str); replace(str,a,b);printf(The new string is%sn,str);fprintf(fp,The new string is%sn,str);fclose(fp);13 、 /* 在一个字串 s1 中查找一子串 s2 。

5、,若存在则返回子串在主串中的起始位置,不存在则返回 -1 。 */ main()char s16=thisis;char s25=is;printf(%dn,search(s1,s2); system(pause);int search(char s1,char s2)int i=0,j,len=strlen(s2); while(s1i) for(j=0;j=len)return i;else i+;return -1;14 、 /* 用指针变量输出结构体数组元素。 */struct studentint num; char *name; char sex; int age;stu5=100。

6、1,lihua,F,18,1002,liuxing,M,19,1003,huangke,F,19,1004,fengshou,F,19,1005,Wangming,M, 18;main()int i;struct student *ps;printf(Num tNametttSextAgetn);/* 用指针变量输出结构体数组元素。 */ for(ps=stu;psnum,ps-name,ps-sex,ps-age);/* 用数组下标法输出结构体数组元素学号和年龄。 */ for(i=0;inum,p-name,p-age);p=p-next;while(p!=NULL);16 、 /* 输入。

7、一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。 */#include #include #include main() char s100;int i,j,n;printf( 输入字符串: n);gets(s); n=strlen(s);for(i=0,j=n-1;i=j) printf( 是回文串 n); else printf( 不是回文串 n);17 、 /* 冒泡排序,从小到大,排序后结果输出到屏幕及文件 myf2 .out*/#includevoid fun(int a,int n)int i,j,t;for(i= 0 ;iaj+1) t=aj;。

8、aj=aj+1;aj+1=t;main()int a10=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!n); fun(a,10);for(i=0;idouble 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 。

9、*fp;double eps=1e-5,pi; if(fp=fopen(p7_3.out,w)=NULL) printf(cannot open the filen); exit(0);pi= countpi(eps); printf(pi=%lfn,pi); fprintf(fp,pi=%lfn,pi); fclose(fp);“”“”At the end, Xiao Bian gives you a passage. Minand once said, people who learn to learn are very happy people. In every wonderful l。

10、ife, learning is an eternal theme. As a professional clerical and teaching position, I understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. Only by constantly learning and mastering the latest relevant knowledge, can employees from all walks of life keep up with the pace of enterprise development and innovate to meet the needs of the market. This document is also edited by my studio professionals, there may be errors in the document, if there are errors, please correct, thank you。

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言作为一门广泛应用于计算机编程的语言,在学习过程中需要掌握一些经典程序。下面是18经典C语言程序: 1. Hello World:最经典C语言程序,用于最简单的输出"Hello World"。 2. 求两个整数之和:输入两个整数,计算它们的和并输出结果。 3. 斐波那契数列:生成并输出斐波那契数列。 4. 判断是否为质数:判断一个数是否为质数,并输出结果。 5. 字符串长度:计算字符串的长度,并输出结果。 6. 字符串逆序:将字符串倒序输出。 7. 判断回文数:判断一个数是否为回文数。 8. 大小写转换:将输入的字符串中的大写字母转换为小写字母。 9. 求最大公约数:计算两个数的最大公约数。 10. 求最小公倍数:计算两个数的最小公倍数。 11. 排序算法(如冒泡排序):实现一个简单的排序算法。 12. 矩阵转置:将一个给定的矩阵进行转置操作。 13. 数组倒序:将一个数组中的元素倒序排列。 14. 字符串连接:将两个字符串进行连接。 15. 斐波那契堆建立:利用斐波那契堆的特性进行堆的建立。 16. 简单链表操作:实现链表的插入、删除、查找等操作。 17. 十进制转二进制:将一个十进制数转换为二进制表示。 18. 二叉树的遍历(如先序、中序、后序遍历):实现二叉树的不同遍历方式。 以上程序不仅展现了C语言的基础语法和常用操作,还涵盖了常见的算法和数据结构的应用。通过学习这些经典C语言程序,可以增强对编程思想和技巧的理解和掌握,为进一步的编程学习打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值