Објектно ориентирано програмирање

Similar documents
Object oriented programming

Објектно ориентирано програмирање

Објектно ориентирано програмирање

Напредно програмирање

Напредно програмирање

Објектно ориентирано програмирање

Напредно програмирање

Објектно ориентирано програмирање

Structured programming

Напредно програмирање

Напредно програмирање

Structures. 21 July 2009 Programming and Data Structure 1

Напредно програмирање

Објектно ориентирано програмирање

What is a Structure? related data items. Examples: n Used for handling a group of logically

User Defined Data: Product Constructor

Engineering 12 - Spring, 1998

Programming & Data Structure

МЕТОДОЛОГИЈА НА НАУЧНОИСТРАЖУВАЧКАТА РАБОТА ПРЕБАРУВАЧИ. Марина Стефова Зоран Здравковски

Објектно ориентирано програмирање

Structured programming

ПОДГОТОВКА ЗА НАСТАВЕН ЧАС Година: I Паралелки: 1,2,5,6,7,8 Предметен наставник: Костадинов Ванчо Датум: Наставна тема: Хардвер

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Data types, variables, constants

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Structured programming

Structured programming. Exercises 3

Structured programming

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Signals, Instruments, and Systems W2. C Programming (continued)

ПОДГОТОВКА ЗА НАСТАВЕН ЧАС Година: I Паралелки: 1,6,7,8 Предметен наставник: Костадинов Ванчо Датум: Наставна тема: Софтвер

typedef int Array[10]; String name; Array ages;

Main differences with Java

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Ценовник за отстапување на движна ствар на трети лица

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

CGS 3460 Summer 07 Midterm Exam

COMP 2001/2401 Test #1 [out of 80 marks]

Fundamental of Programming (C)

!!! "! Backtrackin # g % $ 14

Fundamentals of Programming

Hacking C Code - Local Machine

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Fundamental of Programming (C)

COL 100 Introduction to Programming- MINOR 1 IIT Jammu

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

Introduction to Programming

НАСОКИ ЗА ИЗРАБОТКА НА ТЕЗАТА (ТЕОРЕТСКИ ДЕЛ)

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Fundamentals of Computer Programming Using C

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

AMCAT Automata Coding Sample Questions And Answers

return return else return

Напредно програмирање

Lecture 12 - Structures

Programming in C Lab

UNIT-I Input/ Output functions and other library functions

2. Numbers In, Numbers Out

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

CSC 270 Survey of Programming Languages

Algorithms & Programming. Midterm Examination # 2. Student Name: Student ID Number: Duration of examination:

What we have learned so far

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

C PROGRAMMING Lecture 4. 1st semester

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

NYU SCPS X Section 1 Unix System Calls. Fall 2004 Handout 8. Source code on the Web at mm64/x /src/doubly2.

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

LAB7 : Characters and Strings

Strings and Library Functions

Објава за тендер. ПроКредит Банка АД Скопје ќе врши избор на најповолен понудувач за набавка на следната ИТ опрема: Nr. Requirement Specification Qty

Полиса за приватност. Опсег. Видови податоци и методи на собирање податоци

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Tribhuvan University Institute of Science and Technology 2065

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Programming Language A

СОБРАНИЕ НА РЕПУБЛИКА МАКЕДОНИЈА

Assoc. Prof. Dr. Tansu FİLİK

Slides adopted from T. Ferguson Spring 2016

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

CS1100 Introduction to Programming

2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park

Engineering 12 - Spring, 1999

CS13002 Programming and Data Structures, Spring 2005

Programming and Data Structures

Question Bank (SPA SEM II)

Structures. 5 th March Spring 2012 Programming and Data Structure 1

Introduction to Computing Lecture 01: Introduction to C

ECE 15 Fall 15 Midterm Solutions

ESC 101N: Fundmentals of Computing ( IInd Semester) Mid Sem II Examination PM, Monday 7th March, 2011

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

Transcription:

Аудиториски вежби 1 Верзија 1.0, 16 Февруари, 2017

Содржина 1. Структури.................................................................. 1 1.1. Date.................................................................... 1 1.2. Vector.................................................................. 2 1.3. Комплексни броеви.................................................... 3 1.4. Студенти............................................................... 4 1.5. Држави................................................................ 6 2. Изворен код од примери и задачи.......................................... 8

1. Структури 1.1. Date Да се напише програма која ќе споредува два датуми (ден, месец, година) и ќе ја пресмета разликата во денови од едниот до другиот датум. Пресметките да се реализираат во посебни функции. За датумот да се дефинира посебна структура date. 1. Структури 1

Решение oop_av11.c #include <stdio.h> struct date { int year; int month; int day; ; typedef struct date date; // 1 d1 > d2 // 0 d1 == d2 // -1 d1 < d2 int compare(date d1, date d2) { if (d1.year > d2.year) return 1; else if (d1.year < d2.year) return -1; else { // d1.year == d2.year if (d1.month > d2.month) return 1; else if (d1.month < d2.month) return -1; else { // d1.month == d2.month if (d1.day > d2.day) return 1; else if (d1.day < d2.day) return -1; else return 0; /** * This function is aproximation */ int days(date d1, date d2) { int days = d1.day - d2.day; days += (d1.month - d2.month) * 30; days += (d1.year - d2.year) * 365; return days; void read(date *d) { scanf("%d.%d.%d", &d->day, &d->month, &(*d).year); void print(date *d) { printf("%02d.%02d.%d\n", d->day, d->month, d->year); int main() { date d1; date d2; read(&d1); read(&d2); int res = compare(d1, d2); if (res == 0) { printf("dates are equal\n"); else if (res > 0) { printf("the difference in days is %d days.\n", days(d1, d2)); else { printf("the difference in days is %d days.\n", days(d2, d1)); return 0; 1.2. Vector Да се напише програма која ќе го пресметува векторскиот и скаларниот производ на два вектори. Векторите се претставени со координати во тродимензионален координатен систем. Скаларниот и векторскиот производ 2 1.2. Vector

да се пресметуваат со посебни функции. За вектор да се дефинира посебна структура vector. Решение oop_av12.c #include <stdio.h> struct vector { float x; float y; float z; ; typedef struct vector vector; float scalar_product(vector v1, vector v2) { return v1.x * v2.x + v1.y + v2.y + v1.z * v2.z; vector vector_product(vector v1, vector v2) { vector v; v.x = v1.y * v2.z - v1.z * v2.y; v.y = v1.z * v2.x - v1.x * v2.z; v.z = v1.x * v2.y - v1.y * v2.x; return v; int main () { vector v1 = { 2, 4, 6 ; vector v2 = { 3, 5, 9 ; vector v = vector_product(v1, v2); printf("v1 * v2 = %.2f\n", scalar_product(v1, v2)); printf("v1 x v2 = [%.2f, %.2f, %.2f]\n", v.x, v.y, v. z); return 0; 1.3. Комплексни броеви Да се напише програма во која ќе се дефинира структура за претставување комплексни броеви. Потоа да се напишат функции за собирање, одземање и множење на два комплексни броја. Програмата да се тестира во главна програма во која се вчитуваат два комплексни броја од стандарден влез. 1.3. Комплексни броеви 3

Решение oop_av13.c #include <stdio.h> #include <math.h> typedef struct complex_number { float real; float imag; comp; comp add(comp a, comp b) { comp c = a; c.real += b.real; c.imag += b.imag; return c; comp subtract(comp *pok1, comp *pok2) { comp c = *pok1; c.real -= (*pok2).real; c.imag -= (*pok2).imag; return c; void multiply(comp a, comp b, comp *c) { c->real = a.real * b.real - a.imag * b.imag; c->imag = a.real * b.imag + a.imag * b.real; void print(comp *pok) { printf("%.2f", pok->real); if (pok->imag >= 0) printf("+j%.2f\n", pok->imag); else printf("-j%.2f\n", fabs(pok->imag)); int main() { comp a, b, c; scanf("%f %f", &a.real, &a.imag); scanf("%f %f", &b.real, &b.imag); print(&a); print(&b); printf("a + b\n"); c = add(a, b); print(&c); printf("a - b\n"); c = subtract(&a, &b); print(&c); printf("a * b\n"); multiply(a, b, &c); print(&c); return 0; 1.4. Студенти Од стандарден влез се читаат податоци за непознат број студенти (не повеќе од 100). Податоците се внесуваат така што во секој ред се состои од: името презимето бројот на индекс (формат xxyzzzz) 4 1.4. Студенти

четири броја (поени од секоја задача) со произволен број празни места или табулатори меѓу нив. Да се напише програма која ќе испечати список на студенти, каде во секој ред ќе има: презиме, име, број на индекс, вкупен број на бодови сортиран според бројот на бодови. При тоа имињата и презимињата да се напишат со голема почетна буква. Решение oop_av14.c #include <stdio.h> #include <string.h> struct student { char first_name[15]; char last_name[20]; int number; int points; ; void norm(char *s) { // First letter uppercase, others lowercase *s = toupper(*s); while (*(++s)!= '\0') *s = tolower(*s); void sort(struct student a[], int n) { int i, j; struct student s; for (i = 0; i < n; i++) for (j = 0; j < n - i - 1; j++) if (a[j].points < a[j + 1].points) { s = a[j]; a[j] = a[j + 1]; a[j + 1] = s; int main() { struct student st[50]; int i, n; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%s", &st[i].first_name); scanf("%s", &st[i].last_name); scanf("%d", &st[i].number); int j, zadaca; st[i].points = 0; for(j = 0; j < 4; j++) { scanf("%d", &zadaca); st[i].points += zadaca; norm(st[i].first_name); norm(st[i].last_name); sort(st, n); for (i = 0; i < n; i++) { printf("%d. %s %s\t%d\t%d\n", i + 1, st[i].first_name, st[i].last_name, st[i].number, st[i].points); return 0; 1.4. Студенти 5

1.5. Држави Да се напише програма која од стандарден влез ќе чита податоци за држави и на екран ќе го отпечати името и презимето на претседателот на државата чиj што главен град има најмногу жители. Податоци за држава: име претседател главен град број на жители. Податоци за град: име број на жители. Податоци за претседател: име презиме политичка партија. 6 1.5. Држави

Решение oop_av15.c #include<stdio.h> typedef struct city { char name[30]; long population; city; typedef struct president { char name[20]; char party[20]; pres; typedef struct country { char name[30]; pres president; long population; city capital; country; int main() { country d[20]; int n, i, maxi, max; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%s", &d[i].name); printf("president:\n"); scanf("%s", &d[i].president.name); scanf("%s", &d[i].president.party); scanf("%d", &d[i].population); scanf("%s", &d[i].capital.name); scanf("%d", &d[i].capital.population); maxi = 0; max = d[maxi].capital.population; for (i = 0; i < n; ++i) if (d[i].capital.population > max) { max = d[i].capital.population; maxi = i; printf( "Name of the president of the country with the largest capital is: %s\n", d[maxi].president.name); return 0; 1.5. Држави 7

2. Изворен код од примери и задачи https://github.com/finki-mk/oop/ Source code ZIP 8 2. Изворен код од примери и задачи