ADA程序实例(字符串类型及其简单处理)
来源:互联网 发布:南山区软件产业基地 编辑:程序博客网 时间:2024/06/07 05:38
ADA语言内建的字符串类型是定长类型,基本接近相当于C的静态字符数组。对ADA而言,String也完全是通过字符数组的严格定义派生出来的(可参见wikibooks关于ADA类型系统的条目;关于ADA的复杂的类型系统需要另行撰文)。定长字符类型对应的操作包在Ada.Strings.Fixed中。另外String类型也有很多这个类型的Attribute。这类字符串通常在编译期决定长度(由其界限参数指定或所附值决定);但对ADA而言,这种长度确定也可以宽松一点:字符串和所有数据变量实例一样都定义在变量定义区,它可以不限定长度,而由一个函数返回的字符串确定;另一方面,对于一个子程序变量定义区的字符串,它可以用一个数值变量为其在运行时配置长度,这有点像扩展后C语言中的栈空间分配一样(其实背后运行机理也是这样,而这个特性也适用于ADA的其他如数组Array上)。而在字符串作为参数传给子程序的时候,这个形参的类型必须定义成string,而不含长度节点,这个也很容易理解。至此,对于一般的字符串处理,充分利用这种栈空间分配的特性,合理创建字符变量,这种定长字符串类型已经足够了。
另外ADA的类库也提供了字符串类型,相当于是一种字符串对象了,主要有两种,一种是Bounded(有界)类型,另一种是Unbounded(无界)类型。Bounded类型的使用需要实例化定义它的一个泛型包,而这个泛型包的参数只是一个数值,用于指定这个包提供的Bounded字符串的最大长度,这样估计包里的功能根据这个长度进行一些存储准备或策略配置等,而这个包产生每个字符串实例的长度也就不能超过这个界限。而无界类型是不需要做这个指定而字符串也能无限增长。
ADA字符串操作符除了已经见到的乘法用于复制串以外,对字符串连接则用&,而不是一般语言中用的+(当然用户可以去改,但这不符合ADA的通常实践)。需要注意的是,如果连接两个定长字符类型变量,如果赋值给一个定长类型变量,这个变量必须正正好好和这两个连接起来的字符串的长度相等,这个比较麻烦,但这是没办法的;除了连接外,ADA还提供了一些字符函数的插入替换操作等(Replace_Slice, Overwrite, Insert等),也是比较方便的。ADA的定长类型求子字符串只需要在数组下标中指定即可;但如果在字符串对象上进行的话,由于ADA不支持数组下标的运算符重载,所以必须使用函数如Slice/Replace_Slice来提取设置子串或Element/Replace_Element来提取设置单个字符。而string,Bounded_String和Unbounded_String之间转换目前似乎只提供了string和后两者之间的互转,对于这些ADA库可能还有待更新扩展。
with Ada.Text_IO;use Ada.Text_IO;with Ada.Strings.Fixed;use Ada.Strings.Fixed;with Ada.Strings.Bounded;with Ada.Strings.Unbounded;with Ada.Command_Line;procedure adastrings is package CL renames Ada.Command_Line; -- shorten the package name package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length(Max => 128); use Bounded; use Ada.Strings.Unbounded; fixed_str_without_sizespec : string := "size determined by designated value"; fixed_str_from_cmd_line : string := CL.Argument(1); fixed_str : string(1..32) := 32 * "0"; -- has to be 32 fixed_str2 : string(1..32) := 32 * "1"; fixed_str3 : string(1..64); bounded_str : Bounded_String; unbounded_str : UnBounded_String; -- create a string 'dynamically' by duplicating a string given times function duplicate_string(str: string; occurs : integer) return string is result : string := occurs * str; begin return result; end duplicate_string; -- create a string 'dynmically' by returning a string of any length -- specified at run time function produce_blank_string(length : integer) return string is result : string(1..length) := length * ' '; begin return result; end produce_blank_string; procedure print_string (str : string) is begin put_line(str); end print_string; procedure print_string (bounded_str : bounded_string) is begin put_line(To_String(bounded_str)); end print_string; procedure print_string (unbounded_str : unbounded_string) is begin put_line(To_String(unbounded_str)); end print_string;begin print_string("demo program for ada string types"); print_string(fixed_str); fixed_str3 := fixed_str2 & fixed_str; print_string(fixed_str3); print_string(fixed_str2(1..16)); fixed_str := Overwrite(fixed_str, 17, fixed_str2(1..16)); print_string(fixed_str); print_string(fixed_str_without_sizespec); print_string(fixed_str_from_cmd_line); bounded_str := bounded_str & fixed_str; print_string(bounded_str); unbounded_str := To_Unbounded_String(To_String(bounded_str)); unbounded_str := unbounded_str & "123"; print_string(unbounded_str); Replace_Slice(unbounded_str, 3, 10, "replaced"); print_string(unbounded_str); print_string(duplicate_string("Sample", 5)); print_string(produce_blank_string(12)); end adastrings;执行结果:
> adastrings.exe argument1demo program for ada string types000000000000000000000000000000001111111111111111111111111111111100000000000000000000000000000000111111111111111100000000000000001111111111111111size determined by designated valueargument1000000000000000011111111111111110000000000000000111111111111111112300replaced0000001111111111111111123SampleSampleSampleSampleSample
- ADA程序实例(字符串类型及其简单处理)
- ADA程序实例(类型、控制流、转换、简单数学运算等)
- ADA程序实例(一个简单的智能指针实现)
- ADA程序实例(运算符重载)
- ADA程序实例(面向对象特性之多态)
- 程序错误类型及其处理
- ADA类型系统(类型兼容性和派生)
- 稳定婚姻的求解程序——Ada应用实例之一
- Ada
- ada
- ada
- 字符串类型处理实战
- POJ 1002(不用排序的方法)(简单的字符串处理程序)
- JS字符串处理实例
- Java Applet简单介绍及其使用实例(键人岐)
- php学习笔记(五)字符串及其相关处理函数
- Pixel density(简单的字符串处理)
- UVA 401(palindorme)简单字符串处理
- zoj2740-简单并查集
- 各大公司android开源地址
- linux的自旋锁struct spinlock_t的使用
- 图灵机和NP难度问题
- linux下iconv()函数的用法
- ADA程序实例(字符串类型及其简单处理)
- 大小字节序
- android 解析json数据格式(整合)
- insmod lsmod rmmod
- 使用PreparedStatement 的excute返回值问题
- Linux内核空间和用户空间
- “节能之星”EFM32 应用方案(一)—— 无磁热表应用案例
- VS2010 C++ 操作Excel表格的编程实现
- 从0开始学模拟挂(一)--找内存基址,包含原理