版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.,1,2、C+ programming,A simple C+ program Types, variables, expressions,.,2,How to write a program,Find the whole steps in the real model Use some graph or nature language to describe Realize with computer language,.,3,workflow,.,4,Pseudo Code,A first idea: int main() variables while (condition) anal
2、yze the expression evaluate the expression print the result ,4,.,5,main Function,int main(void) return 0; ,int x=5; int y=8; int z=x+y; coutx“+”y“=“zendl;,5,.,6,/2.1.cpp,#include using namespace std; /* * A simple program for demonstrating the basics of a C+ project. * * It does a good job of demons
3、trating C+ fundamentals, but a * terrible job with the poetry. */ int main() cout Dont you just feel like a louse; cout endl; cout To learn that your new theorem was proved by Gauss?; cout endl; return 0; ,6,.,7,cout ,cout,7,.,8,C+ Tokens,A token is the smallest element of a C+ program that is meani
4、ngful to the compiler. Kinds of tokens: identifiers, keywords, literals, operators, punctuators, and other separators. Tokens are usually separated by white space. White space can be one or more blanks, horizontal or vertical tabs, new lines, form feeds or comments.,8,.,9,C+ Keywords,auto const doub
5、le float int short struct unsigned unsigned break break continue else for long switch void case sizeof typedef char do if return static union while ,ETC.,9,.,10,Commenting,/* *name of program *information of author *function of program * */ /a sample int main() / /*this is in the comment this is als
6、o in the comment */ . ,10,.,11,Constants,1,2,3 1.2, 4.50 “name”, “your_phonenumber” ture,false 0 x12 1,A,$,xhh,ddd #define PI 3.141592 #define PRICE 100 const int pi= 3.141592;,11,.,12,/2.2.cpp,#include using namespace std; int main() int x; int y; x = 3; y = 4; cout x+y endl; return 0; ,12,.,13,Var
7、iables Types,Built-in types Boolean type bool 1byte Character types char 1byte Integer types int 2-4bytes(2) -3276832767 short (2) true, false Character literals: char c; a, x, 4, n, $ Integer literals: int x; 0, 1, 123, -6, 0 x34, 0 xa3 Floating point literals: double d; float f; 1.2, 13.345, .3, -
8、0.54, 1.2e3, . 3F, .3F String literals: string s; asdf, Howdy, all yall!”,14,.,15,Variables Names,Choose meaningful names confuse mtbf, TLA, myw, nbv Short names can be meaningful x is a local variable i is a loop index Dont use long names Ok: partial_sum, element_count, staple_partition Too long: t
9、he_number_of_elements remaining_free_slots_in_the_symbol_table,15,.,16,Not Variables Names,A name in a C+ program Starts with a letter, contains letters, digits, and underscores (only) x, number_of_elements, Fourier_transform, z2 Not names: 12x, time$to$market, main line Not start names with undersc
10、ores: _foo Not use keywords int if while,16,.,17,Declaration and initialization,int a = 7; int b = 9; char c = a; double x = 1.2; string s1 = Hello, world; string s2 = 1.2;,17,9,a,1.2,13 Hello, world,4 | 1.2,b:,c:,x:,s1:,s2:,7,a:,|,.,18,Constant variables,const int i=5; i=6; /error,18,.,19,Think abo
11、ut:,int a,b,c=2; int x,y,z,10; int m=2; int n=3; long int sum=0,add; long hello; char a=m; char b,c,d; char m=65,n=a+1; float a,b,ccc=3.1415; float sum=0.0; double f1, f2=1.414e12,19,.,20,Assignment and increment,int a = 7; a = 9; a = a+a; a += 2; +a;,20,7,9,18,20,21,a:,.,21,Think about,int a=10, b;
12、 b=a; float x; int k=300; x=k; float x=3.14; int n; n=x+6; float x=3.14; int n; n=3; cout x+n; 3.0/9 or (float)3/9,.,22,/2.3.cpp A program to illustrate integer overflow,#include using namespace std; /* * A program to illustrate what happens when large integers * are multiplied together. */ int main
13、() int million = 1000000; int trillion = million * million; cout According to this computer, million squared is trillion . endl; ,22,.,23,A type-safety violation (“implicit narrowing”),int main() int a = 20000; char c = a; int b = c; if (a != b) cout oops!: a != b n; else cout Wow! We have large cha
14、ractersn; ,23,.,24,C+ character set,0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z _ $ # ( ) % : ; . ? * + / char let = 97; Cycle through the ASCII table with math! char bee = a + 1;,26,.,27,Operators,+, + -, - * / % ,27,.,
15、28,Arithmetic Assignment Operators,28,Think about: int a=12; a += a -= a * a ; cout a; cout ; cin b; cout a % b = a%b endl; return 0; ,30,.,31,Think about:,17/3=5? 5/9=0? int n; n%2+(n+1)%2=? n=2; n+; n=n+1 n=2: n+; n-; +n;-n; r=2; m=-n; p=r+; m=10,n=100; p=(n+n,n*n,n-2); p=n+n,n*n,n-2;,31,.,32,Thin
16、k about:,int i=2; (i+)+(i+)+(i+) ; couti; i=2; (-i)+(-i) ; couti; i=2; i=(i+i+i) ; couti; i=2; i=(i-i) ; cout second; string name = first + + second; cout Hello, name n; ,33,.,34,Think about: the output?,int x,y; x = 10; y = x+; cout y endl; int a,b; a = 10; b = +a; cout b y) a=! k,37,.,38,38,Statem
17、ents,a = b; double d2 = 2.5; if (x = 2) y = 4; while (true) cout“hello”; for(int i=0;i8;i+) cout x; if (x0)x=-x; cout x; coutsqrt(x); ,.,44,/bool leapyear(int y),/*any year divisible by 4 except centenary years not divisible by 400*/ bool leapyear(int y) / any year divisible by 4 except centenary ye
18、ars not divisible by 400 if (y%4) return false; if (y%100=0 ,.,45,Conditions2,if (condition) /do this else /do that ,if (x0) cout-x; else coutb) cout a; else cout =a ,.,47,Conditions3,if (condition) /do this else if (condition) /do that else /do other thing ,void main() int a=3,b=17,c=5; if (ab) if (bc) coutc) coutc) coutc) coutbca; else coutcb0 y=f(x)= 0 x=0 -1 x0) k=1; else if (x=0) k=0; else k=-1; coutx b ? a : b ; return m; ,.,50,if (condition) /do this else if (condition) /do this ,conditions4,.,51,Switches,switch (expression) case i: /do this b
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年东南亚海盗活动新趋势下的船舶安防措施
- 工程施工协议书范文
- 2025湖泊治理(生态修复)合同
- 浙江2026年高级会计师《高级会计实务》历年真题汇编
- 2026年消防有毒气体探测系统施工方案
- 2026年水泥路面施工方案及切缝养护要求
- 体温单绘制规范
- 海南2026年注册会计师CPA《会计》考试题库
- 腹股沟斜疝护理查房
- 2026年国家公务员考试《申论》真题回忆版
- DZ∕T 0305-2017 天然场音频大地电磁法技术规程(正式版)
- 《光伏发电工程可行性研究报告编制规程》(NB/T32043-201)中文版
- 教授的研究生手册
- 儿童珠绣手工课件
- 大连理工大学经济学原理试卷与参考答案
- 咯血临床思维及诊断治疗课件
- 建立模糊专家系统实验报告
- 医院科室人员信息一览表
- 家庭社会工作PPT完整全套教学课件
- 先导式减压阀的设计方案
- 基础生态学-群落的组成与结构
评论
0/150
提交评论