第2章 C 程序设计基础(一).ppt

上传人:marr****208 文档编号:133739387 上传时间:2020-05-30 格式:PPT 页数:46 大小:989.50KB
返回 下载 相关 举报
第2章 C 程序设计基础(一).ppt_第1页
第1页 / 共46页
第2章 C 程序设计基础(一).ppt_第2页
第2页 / 共46页
第2章 C 程序设计基础(一).ppt_第3页
第3页 / 共46页
第2章 C 程序设计基础(一).ppt_第4页
第4页 / 共46页
第2章 C 程序设计基础(一).ppt_第5页
第5页 / 共46页
点击查看更多>>
资源描述

《第2章 C 程序设计基础(一).ppt》由会员分享,可在线阅读,更多相关《第2章 C 程序设计基础(一).ppt(46页珍藏版)》请在金锄头文库上搜索。

1、云南颠峰软件培训学院课程名称 使用C 开发数据库应用程序 第二章 C 程序设计基础 一 授课讲师 黎吾鑫 课程地位 就业技能结构图 本门课程目标 学完本门课程后 你能够 运用C 编写 NET命令行程序运用C WinForms编写 NET窗口程序运用ADO NET编写数据库应用程序 预习检查 什么是C 举例说明C 能做什么 说说VS中创建C 控制台程序的步骤 C 中变量的命名规则有什么 第2章C 程序设计 C 是一种面向对象的编程语言 C 源程序扩展名为 cs C 语言在类型安全性 版本转换 事件和垃圾回收等方面进行了相当大的改进和创新 第2章C 程序设计 2 1简单的C 程序2 2C 数据类型

2、和运算符2 3C 基本输入输出 2 1简单的C 程序 usingSystem namespaceHelloWorld classProgram staticvoidMain string args System Console WriteLine HelloWorld NET提供许多功能类 以方便应用程序使用 将这些类依照功能分门别类形成命名空间 使用System命名空间 定义命名空间HelloWorld 非必须 程序入口 M要大写 定义一个类 多个类的C 程序 usingSystem namespaceHelloWorld 命名空间classTestClass privateinta 成员变

3、量publicTestClass intx a x 构造函数publicvoidshow Console WriteLine a 成员函数 方法 classClass1 staticvoidMain string args TestClassA newTestClass 100 构造对象 实例化 A show 了解命名空间 usingSystem namespaceHelloWorld classTestClass privateinta publicTestClass intx a x publicvoidshow Console WriteLine a namespaceHelloWorl

4、d2 classClass1 staticvoidMain string args HelloWorld TestClassA newHelloWorld TestClass 100 A show 返回 命名空间 编译器在using指令指定的命名空间中查找没有在当前命名空间中定义 但在代码中引用的类 不同的命名空间 2 2C 数据类型和运算符 2 2 1数据类型和运算符2 2 2用法示例 返回 2 2 1数据类型和运算符 C 两个基本类别值类型int double char 枚举类型 结构体等表示实际数据 只是将值存放在内存中值类型都存储在堆栈中引用类型类 接口 数组 字符串等表示指向数据的指

5、针或引用包含内存堆中对象的地址为null 则表示未引用任何对象 类型说明 Unicode是一种通用字符编码标准 它覆盖了多国语言和符号 兼容ASCII字符 前128个相同 类型说明 C 运算符 返回 用法示例 数值型 staticvoidMain string args boolflag true shorta 19 inti int 3 0 floatf 3 14F stringstr Tom Console WriteLine 布尔值 flag Console WriteLine 短整型值 a Console WriteLine 整型值 i Console WriteLine 浮点值 f

6、Console WriteLine 字符串值 str inti 3 0 floatf 3 14 doublef 3 14 floatf float 3 14 用法示例 数值型 staticvoidMain string args constfloatPI 3 14F constfloatR 980 doubleperiod 0 period PI R R Console WriteLine 圆面积是 period 符号常量 问题 如何设置输出格式 输出结果 圆的面积为254 340008497238 编译器要确切地知道数据的类型 intnum 123 整数 字符串 需要类型转换 编译出错 隐式

7、转换 自动类型转换 float int 规则 对于数值类型 A的取值范围完全包含在B内 A B Console WriteLine 5 2 Console WriteLine 5 0 2 Console WriteLine 5 2 0 Console WriteLine 5 0 2 0 Console WriteLine 5 2 Console WriteLine 5 1 2 Console WriteLine 5 2 用法示例 运算 练习 Console WriteLine 1 5 2 Console WriteLine 2 5 2 用法示例 逻辑运算示例 inti 0 if i 0 int

8、i 0 if i 0 仅在必要时才计算第二个操作数 两个操作数都要计算 用法示例 bool型 基本用法 boolb1 true 不是Trueboolb2 x 1示例 if b2 else intx 0 y if x y 0 else y 1 思考结果如何 注意 intx 0 if x 可用if x 0 进行显式比较或者用转换函数if System Convert ToBoolean x 非0为true 无法将类型int隐式转换为bool 用法示例 字符型 charch A charch 101 用8进制数表示ASCII字符 最多3位charch x41 用2位16进制数表示ASCII字符cha

9、rch x0041 用低2位16进制数表示ASCII字符charch u0041 Unicode字码 必须4位16进制数charMyChar 65 integraltypecannotfitincharcharMyChar char 65 Castfromintegraltypeinta MyChar implicitconversiontoint 用法 char 方法名 参数 程序练习 usingSystem publicclassBoolTest publicstaticvoidMain Console Write Enteracharacter charc Console Read 读入

10、一个字符 返回值为int型 if if Console WriteLine 小写字母 elseConsole WriteLine 大写字母 elseConsole WriteLine 不是字母 参考答案 usingSystem publicclassBoolTest publicstaticvoidMain Console Write Enteracharacter charc char Console Read 读入一个字符 返回值为int型 if char IsLetter c if char IsLower c Console WriteLine 小写字母 elseConsole Wri

11、teLine 大写字母 elseConsole WriteLine 不是字母 用法示例 string型 stringa hello stringb h b ello 是连接字符串 b hello stringc good morning Console WriteLine a b 输出结果是FalseConsole WriteLine object a b 结果是False 类型不一致了 strings1 tom strings2 jerry Console WriteLine s1 s2 思考结果如何 比较两个字符串的最简单方法是使用 和 运算符 执行区分大小写的比较 可以用索引运算符 访问

12、字符串中的各个字符 charx test 2 x s 序号从0开始 索引示例 strings Printingbackwards 汉字能行吗 for inti 0 i s Length i System Console Write s s Length i 1 用法示例 string型 字符串的长度属性 编程练习 输入一行字符串 统计字母 数字 空格和其他字符个数 字符串转换为其它型的方法 Parse 方法 很重要System Convert 方法 1 Parse 方法 int Parse string long Parse string float Parse string double P

13、arse string bool Parse string char Parse string DateTime Parse string strings1 123 inta int Parse s1 strings2 123 45 doublef double Parse s2 strings3 2008 03 15 DateTimedt DateTime Parse s3 Console WriteLine dt Year 2 Sytem Convert 方法 示例 strings1 123 inta System Convert ToInt32 s1 a 123strings2 123

14、45 doublef Convert ToDouble s2 f 123 45strings true 不区分大小写boolb Convert ToBoolean s strings 2008 03 15 一种日期格式DateTimedt Convert ToDateTime s Console WriteLine dt Year usingSystem 补充 将数字转换为字符串时 需要使用ToString 方法 或者使用Convert ToString 方法 例如 inti 123 textBox1 Text Convert ToString i textBox2 Text i ToStri

15、ng 返回 2 3C 基本输入输出 输出语句 System Console Write System Console WriteLine 有换行输入语句 System Console ReadLine 读取一行字符System Console Read 读取一个字符 返回值为int型 返回 Console ReadLine 示例 staticvoidMain string args Console Write 请输入姓名 stringa Console ReadLine Console WriteLine 欢迎 a 光临 连接符 staticvoidMain string args Conso

16、le Write 请输入a stringa Console ReadLine Console Write 请输入b stringb Console ReadLine Console WriteLine a b a b 字符串的连接 不用括号呢 staticvoidMain string args Console Write 请输入a stringa Console ReadLine Console Write 请输入b stringb Console ReadLine Console WriteLine a b int Parse a int Parse b 不用括号呢 问题 如何输出多个数据staticvoidMain string args Console Write 请输入a stringa Console ReadLine Console Write 请输入b stringb Console ReadLine intsum int Parse a int Parse b Console WriteLine 0 1 2 a b sum 占位符 数据用逗号分隔 关于输出格式化 in

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号