1 / 33

Input & Output Statement

Input & Output Statement. 01204111 Computer & Programming Group 350-352 Dept. of Computer Engineering Kasetsart University. Adopted from Thanomkulabut’s experiences. Brushed up by MikeLab .Net Factory @KU. Version 2012. Review. Data Type Arithmetic Operators Math Class. Data Types.

trish
Télécharger la présentation

Input & Output Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Input & OutputStatement 01204111 Computer & Programming Group 350-352 Dept. of Computer Engineering Kasetsart University Adopted from Thanomkulabut’s experiences. Brushed up by MikeLab.Net Factory @KU. Version 2012

  2. Review • Data Type • Arithmetic Operators • Math Class

  3. Data Types

  4. Priority of Arithmetic Operators high low

  5. Arithmetic Arithmetic Operators Example • x/y*b++ • int a, b, c; • a = 2-10*3/5+(6%4); • b = 5*(15%4+(2-3))/9; • c = 5+9%2*4-14/4%3 Answer a = -2 b = 1 c = 9 2 3 Given x = 3.0, y = 2.0, b =2, what is the output? • x/y*++b • Answer: 4.5 • Answer: 3.0

  6. Arithmetic Arithmetic Operators Example Answer int a=1, b=2, c=3; Console.WriteLine(a++ + ++b); Console.WriteLine(a++ - ++b); Console.WriteLine(++a + b++ + c++); Console.WriteLine(++a + b++ - c++); Console.WriteLine(++a - b++ - c++); 4 -2 7 1 -3 4, -2, 11, 6, -5

  7. Arithmetic Operators Example(2) sum += x; // is equivalent to sum = sum + x prod *= 2.5; // is equivalent to prod =prod * 2.5 y -= 3+a; // is equivalent to y = y – (3+a)

  8. Math Class The Math Class

  9. Today’s Outline • Input Statements • Console.ReadLine() / Console.Read() • Basic Data Conversions • Output Statements • Console.Write() /Console.WriteLine() • Output format • Escape Sequences Characters • Data Conversions • Type of Conversions • Convert class, Parse() methods

  10. Input Statements

  11. Input Statements • Console.ReadLine(); • Read all characters (in form of string) • Use with Convert or Parse commands frequently • Console.Read(); • Read ASCII code of the first character

  12. Console.ReadLine() • Console.ReadLine() • Use to get the input (String) from user • Convert string to other data type • int.Parse() Convert string to integer • double.Parse()  Convert string to double • …. stringst = Console.ReadLine(); Run-time error may be occurred if user’s input is incorrect

  13. Console.ReadLine() Example Ex1: string myname; myname = Console.ReadLine(); Console.WriteLine(“Your name is {0}”, myname); Kun Toto Your name is Kun Toto Ex2: int x; Console.Write(“Input x:”); string temp = Console.ReadLine(); x= int.Parse(temp); x = x*x; Console.WriteLine(“x^2 = {0}”, x); Input x:12 X^2 = 144

  14. Console.Read() • Console.Read returns an ACSII value (Integer) of the code of the first typed character. inti; Console.Write(“Input char:”); i = Console.Read(); Console.WriteLine(i); Input char: A 65

  15. Output Statements

  16. Output Statements • Console.Write(); • Show output • Console.WriteLine(); • Show output and shift to new line

  17. Statement Output Statements • Use the method Write or WriteLine in the Console class (which is in System namespace) • Basic usage: • Advanced usage with formatting Console.WriteLine("Hello"); Console.WriteLine(area); Console.WriteLine(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary);

  18. Basic Usage Console.Write(“Happy”); Console.Write(2009); Output : Happy2009 string Console.WriteLine(“Happy”); Console.WriteLine(2009); Output : integer Happy 2009

  19. Advanced Usage int a=7, b=2; Console.WriteLine(“a={0} and b={1}”,a,b); Console.WriteLine(“{1}+{0}={2}”,b,a,a+b); a=7 and b=2 {0} {1} 7 + 2 = 9 {1} {0} {2}

  20. Advanced Usage (2) 20

  21. Advanced Usage (3) int a = 10; For a complete list of format specifiers, see http://msdn.microsoft.com/library/en-us/csref/html/vclrfFormattingNumericResultsTable.asp

  22. Escape Sequences Characters • What should you do if you would like to write “C:\mydocument” Console.WriteLine(““C:\mydocument””); Console.WriteLine(“\“C:\mydocument\””); Console.WriteLine(“\“C:\\mydocument\””); 

  23. Unicode Characters เกม 0e40 0e01 0e21 Console.Write(“\u0e40\u0e01\u0e21”); เกม Reference: http://unicode.org/charts/PDF/U0E00.pdf

  24. Data Conversions

  25. Data Conversions • In some situation we have to convert data from one type to another. • We may want to convert width and height of rectangle from Console.ReadLine() to int and calculate the rectangle area from the converted data. • Type of conversions • Widening conversions • Convert from a smaller data type to a larger one • Ex; int  double • Narrowing conversions • Convert from a larger data type to smaller one • Ex; double  int Losing some information!!!

  26. Data Conversions (2) • Assignment conversion • Occurs automatically when a value of one type is assigned to a variable of another. • Ex: aFloatVar = anIntVar; • Arithmetic promotion • Occurs automatically • Ex: aFloatVar/anIntVar • Casting Value of anIntVar is converted to float data type automatically Value of aFloatVar/anIntVarisconverted to float (Larger data type)

  27. Casting • Both widening and narrowing conversions can be accomplished by explicitly casting a value. • To cast, the type is put in parentheses in front of the value being converted. • Example • byte b, inti, long l • i = (int)b; l = (long)b + (long)i; int byte int long long

  28. Casting (2) • Example • byte b; int i; long l; double d; • d = (double) i /10; • b = (byte) l +3; • i = (int) d – 10; int double double int 100% OK, but may yield unpredictable result in some case byte long OK, but may yield unpredictable result in some case

  29. Convert.To<type>(); byte boy=100; intimob=100; double dawn=100.0; char alpha=‘d’; string st=“1”; • boy = Convert.ToByte(imob); • dawn = Convert.ToDouble(st); • imob= Convert.ToInt32(alpha); • alpha = Convert.ToChar(boy); • st = Convert.ToString(imob); boy = 100 i = 100 byte int int char dawn = 1 byte char alpha = ‘d’ double string st = 100

  30. <type>.Parse(); • Convert string data to others • Example • d = double.Parse(st); byte b=100; int i=100; double d=100.0; char c=‘d’; string st=“1”; • i = int.Parse (st); • c = char.Parse(st); • b = byte.Parse(st); d = 1 i = 1 double string int string c = 1 b = 1

  31. Test I • Write a program which • Input : Your name • Output : Your name is <your name>.

  32. Test II • Write a program which • Input : 3 numbers • Output : average of the 3 input numbers

  33. Test III • Write a program which • Input : Length of radius of circle • Output : Area of the circle

More Related