210 likes | 347 Views
TIPOS DE DATOS. DECLARACIONES. Nuestro primer programa. // File: pgm1-4ex6.cpp // Description: 1.4 Exercise 6 // Programmer: N. Ashton // Date: 1/15/2006 #include <iostream> using namespace std; int main() {
E N D
TIPOS DE DATOS DECLARACIONES
Nuestro primer programa // File: pgm1-4ex6.cpp // Description: 1.4 Exercise 6 // Programmer: N. Ashton // Date: 1/15/2006 #include <iostream> using namespace std; int main() { cout << "N. Ashton"; cout << "\n123 Main Street"; cout << "\nBoston, MA 02210"; return 0; }
Estilo en la programacion #include <iostream> using namespace std; int main ( ) {cout<< "Reading a program\n"; cout << "is much easier\n" ;cout << " if a standard form for main is used\n" ;cout << "and each statement is written\n" ; cout << "on a line by itself\n" ; system("PAUSE");return 0;}
Temas • Ejemplo de algoritmo • Ejemplo de codificación
Ejemplo:Escriba programa para sumar dos números Algoritmo. • obtener valores a sumar(num1 , num2) • .Obtener la sumaresultado = num1 + num2 • .Mostrar el resultado (en el monitor)
Programa(codificacion // Este programa calcula la suma de dos enteros #include<iostream> int main() { int num1, num2, resultado; num1 = 3; num2 = 4;
//Se calcula la suma resultado = num1 + num2; • cout <<"La suma de "; • cout << num1; • cout << " + "; • cout << num2;cout << " = "; • cout<< resultado<<endl;
Built-in Data Types • Two categories of data types • Built-in (primitive or atomic) such as int or float • Class (user or programmer-defined)
Integer Data Types • C++ provides nine built-in integer data types • Variations related to storage capacity • Most commonly used: int, bool, and char • Unsigned data types enforce sign convention
Figure 2-2 C++ Integer Data Types
Integer Data Types (continued) • The int data type • Common storage allocation: 4 bytes • Range of values: -2,147,483,648 to 2,147,483,647 • The char data type • Stores individual characters (letters, numbers, symbols) • ASCII: standard accommodates 256 codes • Unicode: accommodates 65,536 characters
Table 2-2 The ASCII Uppercase Letter Codes
Integer Data Types (continued) • The escape character • Escape sequence • A backslash followed by one or more characters • Example: newline character ‘\n’ • Tells compiler to escape from normal interpretation routine • The bool data type • Represents Boolean (logical) • Range of values restricted to 1 (True) or 0 (False)
Table 2-3 Escape Sequences
Integer Data Types (continued) • Determining storage size • sizeof ( ) operator: evaluates capacity in bytes • Data type argument inserted between ( ): sizeof(int) • Signed and unsigned data types • Signed data type permits zero, positive and negative values • Unsigned data type restricted to zero, positive values • char, bool, and other integers
#include <iostream> using namespace std; int main() { cout << "\nData Type Bytes" << "\n--------- -----" << "\nint " << sizeof(int) << "\nchar " << sizeof(char) << "\nbool " << sizeof(bool) << '\n'; return 0; }
Table 2-4 Integer Data Type Storage
Floating-Point Types • Floating-point numbers are real numbers • Include decimal point in explicit references; e.g., 3.0 • C++ supports 3 types: float, double, long double • Exponential Notation • Alternative system for representing floating-point data • Similar to scientific notation • Example: 5.67e2 • Assume base 10. Letter ‘e’ = exponent, 2 = power
Table 2-5 Floating-Point Data Types