ניתוח מערכות מידע תכנות ב C#

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
Air Force Institute of Technology Electrical and Computer Engineering
Lecture 1 Introduction to Java Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Java Syntax Primitive data types Operators Control statements.
Some basic I/O.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chapter 2 Elementary Programming
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
Getting Started Java Fundamentals CSC207 – Software Design Summer 2011 – University of Toronto – Department of Computer Science.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C# - variablesBjørk Busch1 Variables Basic types (also defined as classes with methods) : Integer-types Int, long, byte, sbyte, short, ushort, uint, ulong.
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
Performing Simple Calculations with C# Telerik Corporation
Control statements Mostafa Abdallah
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
1 More about Flow of Control. Topics Debugging Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement.
Chapter 5: Java Language Basics Java Statements –Declarations primitive types classes modifiers –Expressions operators –arithmetic, bitwise, comparison,
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Introduction to C#
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Lecture 2 Data Types Richard Gesick.
Fundamental of Java Programming Basics of Java Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Escape Sequences What if we wanted to print the quote character?
Chapter 6 More Conditionals and Loops
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I UTPA – Fall 2012 This set of slides is revised from lecture slides.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to Java Programming
Variables, Loops, Decision Statements, etc
An Introduction to Java – Part I, language basics
Basics of ‘C’.
Recap Week 2 and 3.
elementary programming
Module 2 Variables, Assignment, and Data Types
Var Name =Console . ReadLine();
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
OOP With Java/ course1 Sundus Abid-Almuttalib
In this class, we will cover:
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

ניתוח מערכות מידע תכנות ב C# נעים להכיר...

תוכנית ראשונה ב C# using System; class FirstProgram { static void Main(string[] args) Console.WriteLine("Let's Learn C#"); }

; בסוף כל פקודה יש ;

משתנים (variables) משתנה הוא שם של מקום בזכרון. משתנה מוגדר ע"י הגדרת השם שלו והטיפוס שלו. טיפוס – מתאר את סוג המידע שהמשתנה יחזיק. int total; int count, temp, result; (data type)טיפוס (variable name) שם ניתן לאתחל משתנים בהצהרה ניתן אף לאתחל מספר משתנים יחד int sum = 0; int base = 32, max = 149;

השמה (assignment) השמה משנה ערך של משתנה אופרטור ההשמה הוא הסימן = total = 25; total מקבל את הערך 25. הערך שהיה בתוךtotal נמחק אפשר לשים רק ערך שמתאים לטיפוס של המשתנה.

דוגמא משתנים והשמה להוסיף... using System; class Program { static void Main(string[] args) int x = 5; int y; y = 2 * x; Console.WriteLine(y); } להוסיף...

פקודת הקלט והערות ReadLine – קורא שורה רישום הערות השורה נקראת כמחרוזת (String) אם רוצים לקרוא מספר יש להמיר (נראה בהמשך) ניתן להשתמש בסוף תוכנית כדי לגרום למסך הריצה להשאר יצא כשנלחץ return רישום הערות // this is a comment /* this is a comment On more than one line*/

דוגמא - טיפול בקלט ופלט using System; class IOProgram { דוגמא - טיפול בקלט ופלט using System; class IOProgram { static void Main(string[] args) Console.WriteLine("Please Type Something:"); String x; x = Console.ReadLine(); Console.WriteLine("You typed {0}" , x ); Console. ReadKey(); }

WriteLine מדפיס למסך את מה שנמצא בין גרשיים "XXX" ניתן לשלב טיפול במשתנים הם ממוספרים לפי סדר הופעתם Console.WriteLine("You typed {0}" , x ); ידפיס את הערך של x Console.WriteLine("You typed {0,1}" , x , y ); ידפיס את הערך של x ואז את הערך של y ניתן לשרשר מחרוזות ע"ישימוש ב + Console.WriteLine("You typed “ + x + y); ידפיס גם כן את הערך של x ואז את הערך של y

קלט של מספרים using System; class numberProgram { static void Main(string[] args) String x; int y; Console.WriteLine("Enter a number:"); x = Console.ReadLine(); y = int.Parse(x); Console.WriteLine("You typed {0}" ,x); Console.WriteLine("Adding 2 makes: {0} or {1}", x+2, y+2 ); Console. ReadKey(); }

טיפוסים integers - מספרים שלמים byte, short, int, long… floating point - מספרים בעלי נקודה עשרונית float, double… strings - מחרוזות String כל דבר בין " " אוטומטית יוגדר מחרוזת characters - תוים בודדים char, ‘a’ ‘\0’ ‘\n’ … boolean - בוליאני Boolean(מקבל ערך true אוfalse )

טבלת תוים מיוחדים לשימוש במחרוזות משמעות התו Single Quote \’ Double Quote \” Backslash \\ Null, not the same as the C# null value \0 Bell \a Backspace \b form Feed \f Newline \n Carriage Return \r Horizontal Tab \t Vertical Tab \v

ביטויים מתמטים פעולות מתמטיות מוגדרות על טיפוסים מסוג integer ו- floating point + (חיבור) - (חיסור) * (כפל) / (חילוק) (כמו \ ב VB) % (שארית, נקרא גם מודולוס) (כמו Mod ב VB)

פקודת if פקודת if נכתבת באופן הבא: הביטוי (condition) חייב להיות ביטוי בוליאני – כלומר ערכו אמת - true או שקר - false if מילה שמורה if ( condition ) statement; אם הביטוי (condition) אמת (true) מבוצעת ההצהרה (statement) אם הביטוי (condition) שקר (false) מדלגים על ההצהרה (statement)

פקודת if-else ניתן להוסיף לפקודה if פקודת else ולייצר פקודת if-else. if ( condition ) statement1; else statement2; אם ערך הביטוי conditionהואtrue בצע את statement1 אם ערך הביטוי conditionהואfalse בצע את statement2 יבוצע או statement1 או statement2 אבל לא שניהם

דוגמא - if using System; class ifProgram { static void Main(string[] args) string x; x = Console.ReadLine(); if (x == "a" || x == "A") Console.WriteLine("A is for apple"); else if (x == "b" || x == "B") Console.WriteLine("B is for Boy"); else Console.WriteLine("Not defined"); Console.ReadKey(); }

ביטויים בוליאנים אופרטורים של השוואה המחזירים ערך בוליאני == equal to (שווה) != not equal to (לא שווה) < less than (קטו מ) > greater than (גדול מ) <= less than or equal to (קטן או שווה ל) >= greater than or equal to (גדול או שווה ל) שימו לב להבדל בין האופרטור = לאופרטור ==

אופרטורים לוגיים בביטויים בוליאנים אפשר להשתמש גם באופרטורים לוגיים. ! Logical NOT && Logical AND || Logical OR האופרטורים הלוגיים מקבלים אופרנדים בוליאנים ומחזירים תוצאה בוליאנית האופרטור NOT הוא אופרטור אונרי -פועל רק על אופרנד אחד האופרטורים AND, OR הם אופרטורים בינרים כלומר פועלים על שני אופרנדים

הבדלים בין אופרטורים של VB ל C# VB.NET Purpose / \ Integer division % Mod Modulus (division returning only the remainder) n/a ^ Exponentiation /= \= Integer division Assignment += &= NEW Concatenate %= Modulus &= Bitwise-AND ^= Bitwise-exclusive-OR |= Bitwise-inclusive-OR == = Equal != <> Not equal Is Compare two object reference variables x is Class1 TypeOf x Is Class1 Compare object reference type + & Concatenate strings && AndAlso Shortcircuited Boolean AND || OrElse Shortcircuited Boolean OR . and base . Scope resolution

הבדלים בין אופרטורים של VB ל C# המשך... VB.NET Purpose [ ] () Array element (type) Cint, CDbl, ..., CType Type cast ++ n/a Postfix increment -- Postfix decrement ! Not Logical-NOT ~ One's complement Prefix increment Prefix decrement sizeof Size of type & And Bitwise-AND ^ Xor Bitwise-exclusive-OR | Or Bitwise-inclusive-OR && Logical-AND || Logical-OR ?: If Function () Conditional . (Unsafe mode only) Pointer to member

קבוצת הצהרות ניתן לקבץ יחד מספר הצהרות שיבוצעו אם הביטוי בפקודת ה if הוא אמת. נשתמש בסוגריים { } על מנת לתחום את ההצהרות. ניתן גם בפקודות אחרות ב C# לקבץ מספר הצהרות באותו אופן. if (total > MAX) { Console.WriteLine ("Error!!"); errorCount++; }

לפני תחילת ביצוע הלולאה איטרציה (סיבוב של הלולאה) לולאת for סינטקס של פקודת for: initialization אתחול מבוצע פעם אחת לפני תחילת ביצוע הלולאה statement הצהרה מבוצעת עד שהתנאי condition נהיה שקר for ( initialization ; condition ; increment ) statement; increment הוספה מבוצעת בסוף כל איטרציה (סיבוב של הלולאה)

הגדרת משתנה באתחול ניתן להגדיר משתנה בתוך חלק האתחול. for (int count=1; count <= 5; count++) Console.WriteLine(count); ניתן להגדיר משתנה בתוך חלק האתחול.

דוגמא - for using System; class ifProgram { static void Main(string[] args) for (int i = 0; i < 100; i++) Console.WriteLine("The square root of {0} is {1} ", i, Math.Sqrt(i) ); Console.ReadKey(); }

הוספה (increment) בלולאת for for (int num=0; num < 100; num ++) Console.WriteLine (num); בחלק הוספה ניתן לבצע כל פעולה for (int num=0; num <100; num += 5) Console.WriteLine (num); for (int num=100; num > 0; num -= 5) Console.WriteLine (num);

לולאת while סינטקס של פקודת while: while ( condition ) statement; אח"כ התנאי נבדק שוב אם ערכו עדיין 'אמת' ההצהרה מבוצעת שוב ההצהרה מבוצעת שוב ושוב עד שהתנאי נהיה 'שקר' int count = 1; while (count <= 5) { Console.WriteLine(count); count++; }

דוגמא לשימוש בלולאה – בדיקת קלט הבעיה: רוצים לקלוט מספרים מהמשתמש דרוש לקלוט מספרים בטווח מסויים פתרון: נשתמש בלולאה כל עוד לא קיבלנו קלט מתאים נבקש ונקלוט קלט נוסף

דוגמא - while int won; double ratio; using System; class FirstProgram { static void Main(string[] args) int NUM_GAMES = 12; int won; double ratio; Console.WriteLine("Enter num of games won (0 to {0})", NUM_GAMES); string tmp = Console.ReadLine(); won = int.Parse(tmp); while (won < 0 || won > NUM_GAMES) Console.WriteLine ("Invalid input. Please reenter: "); tmp = Console.ReadLine(); } ratio = (double)won / NUM_GAMES; Console.WriteLine ("Winning percentage is: {0}", ratio);

דוגמא ללולאת do יודפס הגוף של לולאת do מבוצע לפחות פעם אחת! 1 2 3 4 5 int count = 0; do { count++; Console.WriteLine (count); } while (count < 5); יודפס 1 2 3 4 5 הגוף של לולאת do מבוצע לפחות פעם אחת!

פקודת switch switch ( expression ) { case value1 : statement-list1 jump-statement case value2 : statement-list2 case value3 : statement-list3 ... } switch וגם case מילים שמורות אם expression מתאים לvalue2- הבקרה עוברת לכאן

שימוש ב break, default בתוך switch למשל break, continue ניתן ומקובל לייצר מקרה של ברירת מחדל בשם default גם כאן יש להשתמש בפקודת קפיצה switch (option) { case 'A': aCount++; break; case 'B': bCount++; case 'C': cCount++; default: }

int grade, category; Console.WriteLine("Enter a numeric grade (0 to 100): "); string tmp = Console.ReadLine(); grade = int.Parse(tmp); category = grade / 10; switch (category) { case 10: Console.WriteLine("Perfect score. Well done."); break; case 9: Console.WriteLine("Well above average. Excellent."); case 8: Console.WriteLine("Above average. Nice job."); case 7: Console.WriteLine("Average."); default: Console.WriteLine("Study some more."); }

מערכים בC#

מערך (תזכורת?) נניח שרוצים אוסף של משתנים מאותו הסוג למשל: רשימת ציונים, רשימת סטודנטים מתאים להשתמש במערך 79 87 94 82 67 98 87 81 74 91 grades students Id: 333 Liat 2 courses Id: 444 Sivan 0 courses Id:111 Michal Id: 222 Ronit 3 courses

מערך – הצהרה ויצירה ניתן להצהיר על מערך באופן הבא: int[] scores; הצהרה כנ"ל מגדירה רק קיום של מערך ללא המערך עצמו scores מצביע כעת לכתובת null אם רוצים לייצר מופע (instance) של המערך נשתמש ב new: scores = new int[10]; זה יגדיר מערך של 10 int

דוגמאות להצהרה ויצירה int[] prices = new int[500]; bool[] flags; int length = 20; flags = new bool[length]; char[] codes = new char[1750]; מערך שגודלו נקבע בזמן ריצה: int n = int.Parse(Console.ReadLine()); int[] a = new int[n];

מערכים – גישה לתא בודד 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 scores מתיחסים לתא בודד במערך ע"י [ ] (סוגריים מרובעים) למשל scores[3] מתיחס לתא הרביעי במערך, וערכו 82. התא הראשון הוא scores[0] המערך הזה הוא של int ולכן בתא בודד ניתן להשתמש כמו כל int אחר int a = scores[2]; mean = (scores[0] + scores[1])/2; Console.WriteLine ("Top = " + scores[5]);

אורך מערך Length מייצג את מספר האיברים במערך ידפיס 10 שימו לב: int a = scores.Length; ל a יהיה הערך 10 Console.WriteLine(scores.Length); ידפיס 10 שימו לב: Length מכיל את מספר האיברים ולא האינדקס הכי גבוה! האינדקס הכי גבוה הוא Length-1

רשימת אתחול ניתן ליצור מערך ולמלא בו ערכים ע"י שימוש ברשימת אתחול ניתן ליצור מערך ולמלא בו ערכים ע"י שימוש ברשימת אתחול int[] units = {147, 323, 89, 933, 540, 269, 97, 114} char[] letterGrades = {'A', 'B', 'C', 'D', 'F'}; הערכים מופרדים ע"י פסיק גודל המערך נקבע לפי אורך הרשימה אין צורך להשתמש ב new אפשר להשתמש ברשימת אתחול רק בזמן הגדרת המערך

דוגמא 1 - הדפסת כל האיברים במערך יש לעבור על המערך (נעבור על כל התאים שלו) אורכו arr.Length נדפיס את התוכן של התא int[] arr = {1,2,3,4,5}; for(int i =0; i<arr.Length; i++) { Console.WriteLine(arr[i]); }

דוגמא 2 – שימוש בפונקציה נגדיר מערך נמלא במספרים את המערך נשתמש בפונקציה לחיפוש במערך הפונקציה תחזיר לנו את המיקום של איבר במערך נדפיס את מיקום האיבר שחיפשנו

דוגמא 2 - main שימו לב! נגדיר פונקציה שמחפשת במערך static void Main(string[] args) { int[] arr; //define array pointer arr = new int[10]; // allocate space for array //fill array for (int i = 0; i < 10; i++) arr[i] = i*10; //look for a number in the array, and print its place int result, num1 = 20; result = find(arr, num1); //call the function find if (result != -1) {//check the result returned from the function Console.WriteLine(" the number {0} is in place {1}", num1, result); } שימו לב! נגדיר פונקציה שמחפשת במערך

דוגמא 2 - הפונקציה static int find(int[] arr, int num) { for(int i=0; i<arr.Length; i++) if (arr[i] == num) return i; } return -1;

דוגמא 2 – איך מרכיבים את הכל? class Program { static int find(int[] arr, int num) התוכן של הפונקציה find } static void Main(string[] args) התוכן של הפונקציה Main

פתיחת פרויקט C#

הסברים נוספים לטבלת תווים מיוחדים לשימוש במחרוזות Single Quote – מוסיף גרש במחרוזת \’ Double Quote – מוסיף גרשיים במחזורת \” Backslash – מוסיף קו לכסוני במחרוזת \\  null value – תו ריק באמצע המחרוזת, נראה כמו רווח. \0 Bell – גורם לצלצול \a Backspace – מוחק תו אחד במחרוזת \b form Feed - מעבר לדף הבא – רלוונטי בהדפסה \f - Newline עובר שורה חדשה במחרוזת \n Carriage Return – חוזר לתחילת שורה \r Horizontal Tab – מוסיף רווח של טאב \t Vertical Tab – אמור לרדת למטה – הוראה למדספת \v

ביטויים מתמטיים – הערה על חילוק / (חילוק) (כמו \ ב VB) אם רוצים שהתוצאה לא תתעגל צריך להמיר לטיפוס עשרוני int x = 5; int y =3; int z = 4; double ans; ans= x / y; Console.WriteLine("answer is: " + ans); ans = (double)x / y; Console.WriteLine("casting to double is: " + ans);

הערה על = ו == = פירושו השמה == פירושו השוואה לכן הקומפיילר מודיע על שגיאה אם כותבים if (x=y) נכון לרשום: if (x==y)