ch6.1-datatypes
ch6.1-datatypes
Example
85.125
85 = 1010101
0.125 = 001
85.125 = 1010101.001
=1.010101001 x 2^6
sign = 0
Example Python
cmp=complex(7,3)
a=1+2j
b=3+4j (4+6j)
print('\n',a+b) (-2-2j)
print('\n',a-b) (-5+10j)
print('\n',a*b) (0.44+0.08j)
print('\n',a/b) (7+3j)
print(cmp)
• Simplest of all
Example C++
int main()
{
int a=-5;
if(a)
std::cout << "a is true"; a is true
else
std::cout << "a is false
}
• Typical operations:
– Assignment and copying
– Comparison (=, >, etc.)
– Catenation
– Substring reference
– Pattern matching
#include <stdio.h>
#include <string.h>
int main () {
return 0;
}
Copyright © 2015 Pearson. All rights reserved. 1-18
Character String Type: Example C++
#include <iostream>
#include <cstring>
int main () {
#include <iostream>
#include <string>
int main () {
• Aid to writability
Compile-time Run-time
descriptor for descriptor for
static strings limited dynamic
strings
• Design issues
– Is an enumeration constant allowed to appear in
more than one type definition, and if so, how is
the type of an occurrence of that constant
checked?
int main () {
int main () {
• Index Syntax
– Fortran and Ada use parentheses
• Ada explicitly uses parentheses to show uniformity between
array references and function calls because both are
mappings
– Most other languages use brackets
In the first three, once the subscript ranges are bound and
the storage is allocated, they remain fixed for the lifetime of
the variable.
a=[]
for i in range(10):
a.append(i) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a)
a.remove(9) [0, 1, 2, 3, 4, 5, 6, 7, 8]
print(a)
0
for i in range(9):
1
print(a[i])
2
3
4
5
6
7
8
a=[[1,2,3,4,5,6],['a','b','c']]
b=[[1,2,3,4,5,7],['a','b','d']]
print(a+b) [[1, 2, 3, 4, 5, 6], ['a', 'b', 'c'], [1, 2, 3, 4, 5, 7], ['a', 'b', 'd']]
print(a==b) False
for x in a: [1, 2, 3, 4, 5, 6]
print(x) ['a', 'b', 'c']
• Python
vector = [2, 4, 6, 8, 10, 12, 14, 16]
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
System.out.println();
}
}
}
347
625 3, 4, 7, 6, 2, 5, 1, 3, 8
138
• Design issues:
- What is the form of references to elements?
- Is the size static or dynamic?
dict={}
dict["apple"]=1
dict["banana"]=2
dict["orange"]=3
dict["apricote"]=4
{'apple': 1, 'banana': 2, 'orange': 3, 'apricote': 4}
print(dict)
• Design issues:
– What is the syntactic form of references to the field?
– Are elliptical references allowed
• ML
val myTuple = (3, 5.8, ′apple′);
- Access as follows:
#1(myTuple) is the first element
• F#
let tup = (3, 5, 7)
let a, b, c = tup This assigns a tuple to a tuple
pattern (a, b, c)
– The Scheme CAR and CDR functions are named hd and tl,
respectively
hd [5, 7 ,9 ] => returns 5
tl [5, 7 ,9 ] => returns [7,9]
• Design issue:
– Should type checking be required?
#include <iostream>
union flextype{
int intEl;
float floatEl;
char charEl;
};
int main() {
union flextype el1;
float x;
el1.intEl=5;
x=el1.intEl;
std::cout << "x="<<x<<std::endl; x=5
el1.charEl='C';
x=el1.charEl;
x=67
std::cout << "x="<<x<<std::endl;
x=el1.floatEl;
x=9.3887e-44
std::cout << "x="<<x;
return 0;
}
int main() {
union flextype el1;
float x;
...
el1.intEl = 27;
x = el1.floatEl;
}