版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、a,1,2、C+ programming,A simple C+ program Types, variables, expressions,a,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,a,3,workflow,a,4,Pseudo Code,A first idea: int main() variables while (condition) anal
2、yze the expression evaluate the expression print the result ,4,a,5,main Function,int main(void) return 0; ,int x=5; int y=8; int z=x+y; coutx“+”y“=“zendl;,5,a,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,a,7,cout ,cout,7,a,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,a,9,C+ Keywords,auto const doub
5、le float int short struct unsigned unsigned break break continue elsefor long switch void case sizeof typedef char do if return static union while ,ETC.,9,a,10,Commenting,/* *name of program *information of author *function of program * */ /a sample int main() / /*this is in the comment this is also
6、 in the comment */ . ,10,a,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,a,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,a,13,Vari
7、ables 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, -0
8、.54, 1.2e3, . 3F, .3F String literals: string s; asdf, Howdy, all yall!”,14,a,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: th
9、e_number_of_elementsremaining_free_slots_in_the_symbol_table,15,a,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 underscor
10、es: _foo Not use keywords int if while,16,a,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:,|,a,18,Constant variables,const int i=5; i=6; /error,18,a,19,Think about
11、:,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,a,20,Assignment and increment,int a = 7; a = 9; a = a+a; a += 2; +a;,20,7,9,18,20,21,a:,a,21,Think about,int a=10, b; b
12、=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,a,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,a,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 charac
14、tersn; ,23,a,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,a,27,Operators,+, + -, - * / % ,27,a,28,
15、Arithmetic Assignment Operators,28,Think about: int a=12; a += a -= a * a ; cout a;,a,29,29,Expressions,Boolean type: bool (true and false) Equality operators:= = (equal), != (not equal) Logical operators: / A program to investigate the behavior of the mod (%) operation. int main() int a, b; cout ;
16、cin a; cout ; cin b; cout a % b = a%b endl; return 0; ,30,a,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,a,32,Think about:,int i=2; (i+)+(i+)+(i+) ; couti; i=2; (-i)+(-i) ; couti; i=2; i=(i+i+i)
17、 ; couti; i=2; i=(i-i) ; couti;,a,33,/2.6.cpp String input,#include #include using namespace std; int main() cout first second; string name = first + + second; cout Hello, name n; ,33,a,34,Think about: the output?,int x,y; x = 10; y = x+; cout y endl; int a,b; a = 10; b = +a; cout b endl;,34,a,35,Bo
18、olean expressions,a b a a=! k,37,a,38,38,Statements,a = b; double d2 = 2.5; if (x = 2) y = 4; while (true) cout“hello”; for(int i=0;i8;i+) cout i; int average = (length+width)/2; return x;,a,39,Floor,int floor(float x) return (int) x; ,39,a,40,Ceiling,int ceiling(float x) if (x (int) x 0.0) return (
19、int)(x + 1.0); else return (int) x; ,40,a,41,To be continued,a,42,3、C+ programming,Control statements,a,43,Conditions1,if (condition) /do this ,void main() int x; cin x; if (x0)x=-x; cout x; coutsqrt(x); ,a,44,/bool leapyear(int y),/*any year divisible by 4 except centenary years not divisible by 40
20、0*/ bool leapyear(int y) / any year divisible by 4 except centenary years not divisible by 400 if (y%4) return false; if (y%100=0 ,a,45,Conditions2,if (condition) /do this else /do that ,if (x0) cout-x; else coutx;,void main() int a=5,b=8; if (ab) cout a; else cout b; ,a,46,Example,void main() char
21、a; couta; if(a=a ,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 coutcba; ,a,48,1 x0 y=f(x)= 0 x=0 -1 x0) k=1; else if (x=0) k=0; else k=-1; coutx k; ,Example,a,49,(xy)? x:y;,Get the max one of a and b int max(int a, int b) int m = a b ? a : b ; return m; ,a,50,if (condition) /do this els
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 蚌埠市五河县城区相关学校选调教师考试真题2025
- 甘肃平凉天泰医院招聘考试真题2025
- 2026四川自贡市社会福利和康复治疗中心第一次编外人员招聘17人考试模拟试题及答案解析
- 黑龙江省齐齐哈尔市克东县2026届中考考前最后一卷英语试卷含答案
- 2026年长春市高三语文二模试卷附答案解析
- 2026年4月广东 深圳市曙光中学选聘教师8人(编制)考试模拟试题及答案解析
- 2026西北工业大学自动化学院赵天云团队招聘3人(陕西)考试模拟试题及答案解析
- 2026广东省梅州市教育系统公办学校专项招聘部属公费师范生26人考试备考题库及答案解析
- 2026届四川省宜宾市高县达标名校初中语文毕业考试模拟冲刺卷含解析
- 护理职业素养与道德
- 宁夏滩羊介绍
- 团委书工作面试题集
- 2026年资料员之资料员基础知识考试题库300道含答案(培优a卷)
- 全国园林绿化养护概算定额(2018版)
- 珠江三角洲地区-2021-2022学年七年级地理下册同步导练案
- 企业能源管理培训教程
- 2025年上海市中考综合测试(物理、化学)试卷真题(含答案解析)
- 神经内科疾病急救处理流程培训
- 思政课有趣的汇报课件
- 2025年河北省事业单位联考真题试卷 公共基础知识及答案详解(全优)
- 2023年文山州辅警协警招聘考试真题及答案详解(必刷)
评论
0/150
提交评论