仓酷云

标题: 来一发近100个C言语基本算法案例(低级篇) [打印本页]

作者: 再现理想    时间: 2015-1-16 11:05
标题: 来一发近100个C言语基本算法案例(低级篇)
使用gcc或g++进行编译,使用gdb进行调试;
  上面这些C言语基本算法案例都是经由测试和考证过了的,接待列位利用。
  本文是该系列的第一篇,都是一些绝对低级的算法,很合适刚入手下手学C言语的同砚。
 1、C言语打印一条语句

  源代码:
  1. /*CProgramtoprintasentence.*/#include<stdio.h>intmain(){printf("CProgramming");/*printf()printsthecontentinsidequotation*/return0;}
复制代码
  输入:
  1. CProgramming
复制代码
 2、C言语打印用户输出的一个整数

  源代码:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}
复制代码
  输入:
  1. Enterainteger:25Youentered:25
复制代码
 3、C言语完成两个整数相加

  源代码:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}
复制代码
  输入:
  1. Entertwointegers:1211Sum:23
复制代码
 4、C言语完成两个小数相乘

  源代码:
  1. /*Cprogramtomultiplyanddisplaytheproductoftwofloatingpointnumbersenteredbyuser.*/#include<stdio.h>intmain(){floatnum1,num2,product;printf("Entertwonumbers:");scanf("%f%f",&num1,&num2);/*Storesthetwofloatingpointnumbersenteredbyuserinvariablenum1andnum2respectively*/product=num1*num2;/*Performsmultiplicationandstoresit*/printf("Product:%f",product);return0;}
复制代码
  输入:
  1. Entertwonumbers:2.41.1Product:2.640000
复制代码
 5、C言语查找字符的ASCII值

  源代码:
  1. /*SourcecodetofindASCIIvalueofacharacterenteredbyuser*/#include<stdio.h>intmain(){charc;printf("Enteracharacter:");scanf("%c",&c);/*Takesacharacterfromuser*/printf("ASCIIvalueof%c=%d",c,c);return0;}
复制代码
  输入:
  1. Enteracharacter:GASCIIvalueofG=71
复制代码
 6、C言语依据用户输出的整数做商和余数

  源代码:
  1. CProgramming0
复制代码
  输入:
  1. CProgramming1
复制代码
 7、C言语猎取整型、单精度浮点型、双精度浮点型和字符型的长度

  基础语法是:
  1. CProgramming2
复制代码
  源代码:
  1. CProgramming3
复制代码
  输入:
  1. CProgramming4
复制代码
注:大概会因为体系的分歧出来的了局也不尽不异。
 8、C言语猎取关头字long的长度局限

  源代码:
  1. CProgramming5
复制代码
  输入:
  1. CProgramming6
复制代码
 9、C言语互换数值

  源代码:
  1. CProgramming7
复制代码
  输入:
  1. CProgramming8
复制代码
 10、C言语反省数值是奇数仍是偶数

  源代码:
  1. CProgramming9
复制代码
  输入1:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}0
复制代码
  输入2:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}1
复制代码
  也能够用前提运算符办理:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}2
复制代码
 11、C言语反省是元音仍是子音

  源代码:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}3
复制代码
  输入1:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}4
复制代码
  输入2:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}5
复制代码
  也能够用前提运算符办理
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}6
复制代码
  输入了局和下面的程序不异。
 12、C言语完成从三个数值中查找最年夜值

  完成1:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}7
复制代码
  完成2:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}8
复制代码
  完成3:
  1. #include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}9
复制代码
  输入了局不异:
  1. Enterainteger:25Youentered:250
复制代码
 13、C言语解一元二次方程

  源代码:
  1. Enterainteger:25Youentered:251
复制代码
  输入1:
  1. Enterainteger:25Youentered:252
复制代码
  输入2:
  1. Enterainteger:25Youentered:253
复制代码
 14、C言语反省是不是是闰年

  源代码:
  1. Enterainteger:25Youentered:254
复制代码
  输入1:
  1. Enterainteger:25Youentered:255
复制代码
  输入2:
  1. Enterainteger:25Youentered:256
复制代码
  输入3:
  1. Enterainteger:25Youentered:257
复制代码
 15、C言语反省一个数是负数、正数仍是零

  源代码:
  1. Enterainteger:25Youentered:258
复制代码
  也能够利用if-else语句
  1. Enterainteger:25Youentered:259
复制代码
  输入1:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}0
复制代码
  输入2:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}1
复制代码
  输入3:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}2
复制代码
 16、C言语反省某字符串是否是字母

  源代码:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}3
复制代码
  输入1:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}4
复制代码
  输入2:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}5
复制代码
 17、C言语盘算天然数的和

  源代码:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}6
复制代码
  也能够利用for轮回语句
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}7
复制代码
  输入:
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}8
复制代码
  也能够经由过程if-else语句
  1. /*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}9
复制代码
 18、C言语盘算阶乘

  关于恣意负数n,阶乘指的是:
  1. Entertwointegers:1211Sum:230
复制代码
  假如数值是正数,那末阶乘就不存在。而且我们划定,0的阶乘就是1。
  源代码:
  1. Entertwointegers:1211Sum:231
复制代码
  输入1:
  1. Entertwointegers:1211Sum:232
复制代码
  输入2:
  1. Entertwointegers:1211Sum:233
复制代码
  先先容到这里,下一篇将分享更多的C言语基本算法,敬请等候。
  英文原文:CProgrammingExamplesvia:codeceo
但不会命令而成为高手也是不可能的.这就好比学英语,什么语法都不懂,只捧着单词手册背单词是学不会英语的,但是没有单词词汇量英语水平也提不高的。
作者: 再见西城    时间: 2015-1-24 19:20
之前一直都没有怎么这两个库,特别是vector,一直被难缠的数组死死拉着,而vector正在逐步取代数组的地位,这一点我也深深体会到,在我们项目的封装模块中
作者: 简单生活    时间: 2015-2-2 13:03
最后一部分:算是高级一些的主题,总感觉书的这一部分翻译的不太好,读起来完全没有前面几章顺畅,像多重继承什么的,如果觉得太难理解,看的时候放一放,改天等你用到的时候,再来看看是怎么回事。
作者: 金色的骷髅    时间: 2015-2-7 21:40
虽然还不明确软件技术包含的具体内容,但从C++语言这门课程开始,已发现程序设计的乐趣,在学习C++语言的过程中也学到了许多计算机应用基础知识,对计算机的机体也有了一个大体的了解。
作者: 兰色精灵    时间: 2015-2-23 12:57
当函数模板遇到重载遇到模板特化无疑会让你傻傻分不清楚,做好笔记,慢慢理解,其实不难。
作者: 变相怪杰    时间: 2015-3-7 09:28
通过实际操作,学会 C++语言程序编程的基本步骤、基本方法,开发了自己的逻辑思维能力,培养了分析问题、解决问题的能力。深刻体会到“没有做不到的,
作者: 莫相离    时间: 2015-3-14 22:46
在编程序时,我也学会了敢于尝试,“不试不知道,试试就能行”这是我在实训中领悟的道理之一。
作者: 第二个灵魂    时间: 2015-3-21 17:53
用的vector,而很少使用了数组。当然在我现在的能力范围内,还很难驾驭vector,毕竟数组当年也是我的最爱,相比于更加难缠的指针控制来说。




欢迎光临 仓酷云 (http://ckuyun.com/) Powered by Discuz! X3.2