|
@@ -0,0 +1,759 @@
|
|
|
+\documentclass[10pt]{beamer}
|
|
|
+\setbeamercovered{transparent}
|
|
|
+\usetheme{Antibes}
|
|
|
+\usecolortheme{beaver}
|
|
|
+\usepackage[utf8]{inputenc}
|
|
|
+\usepackage[svgpath=images/]{svg}
|
|
|
+\usepackage{graphicx}
|
|
|
+\graphicspath{ {images/} }
|
|
|
+\usepackage[hyphenbreaks]{breakurl}
|
|
|
+\usepackage{hyperref}
|
|
|
+\hypersetup{breaklinks=true}
|
|
|
+\usepackage{subcaption}
|
|
|
+\usepackage{makecell}
|
|
|
+\usepackage{dirtytalk}
|
|
|
+\usepackage{amsmath}
|
|
|
+\usepackage{mathtools}
|
|
|
+\usepackage{listingsutf8}
|
|
|
+\usepackage[italian]{babel}
|
|
|
+
|
|
|
+% Todo red marker
|
|
|
+\usepackage{xcolor}
|
|
|
+\newcommand{\todo}[1]{}
|
|
|
+\renewcommand{\todo}[1]{{\color{red} TODO: {#1}}}
|
|
|
+
|
|
|
+% Stuff for presentation
|
|
|
+\usepackage{pgfpages}
|
|
|
+\setbeameroption{hide notes} % Only slides
|
|
|
+%\setbeameroption{show only notes} % Only notes
|
|
|
+%\setbeameroption{show notes}
|
|
|
+%\setbeameroption{show notes on second screen=right} % Both
|
|
|
+
|
|
|
+% Give a slight yellow tint to the notes page
|
|
|
+%\setbeamertemplate{note page}{\pagecolor{yellow!5}\insertnote}\usepackage{palatino}
|
|
|
+
|
|
|
+% Remove beamer navigation stuff
|
|
|
+%\setbeamertemplate{navigation symbols}{}
|
|
|
+
|
|
|
+\beamertemplatenavigationsymbolsempty
|
|
|
+
|
|
|
+%\setbeamerfont{page number in head/foot}{size=\small}
|
|
|
+%\setbeamertemplate{page number in head/foot}[framenumber]
|
|
|
+%\setbeamertemplate{page number in head/foot}[totalframenumber]
|
|
|
+\setbeamertemplate{footline}[frame number]
|
|
|
+
|
|
|
+% Listings packages
|
|
|
+\usepackage{verbatim}
|
|
|
+\usepackage{inconsolata}
|
|
|
+\usepackage{listings}
|
|
|
+\usepackage{multicol}
|
|
|
+\usepackage{lscape}
|
|
|
+\newcommand{\optcolor}[1]{\color{#1}}
|
|
|
+\lstset{
|
|
|
+ basicstyle={\ttfamily\footnotesize},
|
|
|
+ keywordstyle=\optcolor{blue},
|
|
|
+ stringstyle=\optcolor{red},
|
|
|
+ %commentstyle=\optcolor{green},
|
|
|
+ morecomment=[l][\optcolor{magenta}]{\#},
|
|
|
+ escapechar={|},
|
|
|
+}
|
|
|
+
|
|
|
+\definecolor{alt}{gray}{0.9}
|
|
|
+\definecolor{darkgreen}{rgb}{0,0.65,0}
|
|
|
+\lstdefinelanguage{llvm}{
|
|
|
+ basicstyle={\ttfamily\footnotesize},
|
|
|
+ morecomment = [l]{;},
|
|
|
+ commentstyle=\optcolor{darkgreen},
|
|
|
+ morestring=[b]",
|
|
|
+ sensitive = true,
|
|
|
+ classoffset=0,
|
|
|
+ alsoletter=:\%.,
|
|
|
+ keywordstyle=\optcolor{teal},
|
|
|
+ keywordsprefix={\%},
|
|
|
+ morekeywords={\@a},
|
|
|
+ %keywordstyle=\optcolor{darkgray},
|
|
|
+ classoffset=1,
|
|
|
+ keywordstyle=\optcolor{blue},
|
|
|
+ morekeywords={
|
|
|
+ define, declare, global, constant,
|
|
|
+ internal, external, private,
|
|
|
+ linkonce, linkonce_odr, weak, weak_odr, appending,
|
|
|
+ common, extern_weak,
|
|
|
+ thread_local, dllimport, dllexport,
|
|
|
+ hidden, protected, default,
|
|
|
+ except, deplibs,
|
|
|
+ volatile, fastcc, coldcc, cc, ccc,
|
|
|
+ x86_stdcallcc, x86_fastcallcc,
|
|
|
+ ptx_kernel, ptx_device,
|
|
|
+ signext, zeroext, inreg, sret, nounwind, noreturn,
|
|
|
+ nocapture, byval, nest, readnone, readonly, noalias, uwtable,
|
|
|
+ inlinehint, noinline, alwaysinline, optsize, ssp, sspreq,
|
|
|
+ noredzone, noimplicitfloat, naked, alignstack,
|
|
|
+ module, asm, align, tail, to,
|
|
|
+ addrspace, section, alias, sideeffect, c, gc,
|
|
|
+ target, datalayout, triple,
|
|
|
+ blockaddress
|
|
|
+ },
|
|
|
+ classoffset=2,keywordstyle=\optcolor{blue},
|
|
|
+ morekeywords={
|
|
|
+ fadd, sub, fsub, mul, fmul, add,
|
|
|
+ sdiv, udiv, fdiv, srem, urem, frem,
|
|
|
+ and, or, xor,
|
|
|
+ icmp, fcmp,
|
|
|
+ eq, ne, ugt, uge, ult, ule, sgt, sge, slt, sle,
|
|
|
+ oeq, ogt, oge, olt, ole, ord, ueq, ugt, uge,
|
|
|
+ ult, ule, une, uno,
|
|
|
+ nuw, nsw, exact, inbounds,
|
|
|
+ phi, call, select, shl, lshr, ashr, va_arg,
|
|
|
+ trunc, zext, sext,
|
|
|
+ fptrunc, fpext, fptoui, fptosi, uitofp, sitofp,
|
|
|
+ ptrtoint, inttoptr, bitcast,
|
|
|
+ ret, br, indirectbr, switch, invoke, unwind, unreachable,
|
|
|
+ malloc, alloca, free, load, store, getelementptr,
|
|
|
+ extractelement, insertelement, shufflevector,
|
|
|
+ extractvalue, insertvalue,
|
|
|
+ },
|
|
|
+ classoffset=3,
|
|
|
+ keywordstyle=\optcolor{purple},
|
|
|
+ morekeywords={
|
|
|
+ @v0, @a0, @t0, @ra, @pc, @r1, @ZF, @rax,@rsp
|
|
|
+ @main,@function,@root,@function_call
|
|
|
+ },
|
|
|
+ classoffset=4,keywordstyle=\optcolor{blue},
|
|
|
+ morekeywords={
|
|
|
+i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16, i17, i18,
|
|
|
+i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, i34,
|
|
|
+i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, i45, i46, i47, i48, i49, i50,
|
|
|
+i51, i52, i53, i54, i55, i56, i57, i58, i59, i60, i61, i62, i63, i64, i80, i512,
|
|
|
+void, half, float, double, fp128, x86_fp80, ppc_fp128, x86_mmx, label, metadata
|
|
|
+ },
|
|
|
+ classoffset=5,keywordstyle=\optcolor{teal},
|
|
|
+ morekeywords={
|
|
|
+ entry:,true:,epilogue:,bb.main:,bb._init.0x8,
|
|
|
+ dispatcher:,bb.one:,bb.two:,fallthrough:,call:,BB1:,BB2:,BB3:
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+% Packages for graphs
|
|
|
+\usepackage{tikz}
|
|
|
+\usetikzlibrary{arrows,automata}
|
|
|
+\usetikzlibrary{shapes.geometric}
|
|
|
+\usetikzlibrary{positioning}
|
|
|
+\usetikzlibrary{shapes.multipart}
|
|
|
+\usetikzlibrary{positioning}
|
|
|
+\usetikzlibrary{decorations.pathreplacing}
|
|
|
+\usetikzlibrary{fit}
|
|
|
+\usetikzlibrary{calc}
|
|
|
+\usetikzlibrary{matrix}
|
|
|
+\usetikzlibrary{patterns}
|
|
|
+\usetikzlibrary{quotes}
|
|
|
+\usetikzlibrary{shapes.misc}
|
|
|
+\tikzset{
|
|
|
+ diagonal fill/.style 2 args={fill=#2, path picture={
|
|
|
+ \fill[#1, sharp corners] (path picture bounding box.south west) -|
|
|
|
+ (path picture bounding box.north east) -- cycle;}},
|
|
|
+ reversed diagonal fill/.style 2 args={fill=#2, path picture={
|
|
|
+ \fill[#1, sharp corners] (path picture bounding box.north west) |-
|
|
|
+ (path picture bounding box.south east) -- cycle;}}
|
|
|
+ }
|
|
|
+\tikzset{
|
|
|
+ fadered/.style={
|
|
|
+ very thick,
|
|
|
+ draw=red!50!black!50,
|
|
|
+ top color=white,
|
|
|
+ rounded corners=.5mm,
|
|
|
+ bottom color=red!50!black!20, % and something else at the bottom
|
|
|
+ font=\sf,
|
|
|
+ text=black
|
|
|
+ },
|
|
|
+ faderedprime/.style={
|
|
|
+ very thick,
|
|
|
+ draw=blue!50!black!50,
|
|
|
+ top color=white,
|
|
|
+ rounded corners=.5mm,
|
|
|
+ bottom color=blue!50!black!20, % and something else at the bottom
|
|
|
+ font=\sf,
|
|
|
+ text=black
|
|
|
+ },
|
|
|
+ nonterminal/.style={
|
|
|
+ fadegray,
|
|
|
+ rectangle,
|
|
|
+ minimum size=6mm,
|
|
|
+ minimum width=20mm,
|
|
|
+ minimum height=7mm,
|
|
|
+ rounded corners=3mm
|
|
|
+ },
|
|
|
+ fadegray/.style={
|
|
|
+ text=black,
|
|
|
+ very thick,draw=black!50,
|
|
|
+ top color=white,
|
|
|
+ bottom color=black!20,
|
|
|
+ font=\sf
|
|
|
+ },
|
|
|
+ terminal/.style={
|
|
|
+ rectangle,
|
|
|
+ fadered,
|
|
|
+ minimum height=7mm,
|
|
|
+ minimum width=20mm,
|
|
|
+ rounded corners=3mm,
|
|
|
+ },
|
|
|
+ terminalprime/.style={
|
|
|
+ rectangle,
|
|
|
+ faderedprime,
|
|
|
+ minimum height=7mm,
|
|
|
+ minimum width=20mm,
|
|
|
+ rounded corners=3mm,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+% Visibility on slides
|
|
|
+%\usetikzlibrary{backgrounds}
|
|
|
+%\usetikzlibrary{arrows.meta,calc,shapes.callouts,shadows.blur,positioning}
|
|
|
+%% Conditional elements in TikZ drawings
|
|
|
+\tikzset{
|
|
|
+ invisible/.style={opacity=0,text opacity=0},
|
|
|
+ visible on/.style={alt=#1{}{invisible}},
|
|
|
+ alt/.code args={<#1>#2#3}{%
|
|
|
+ \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
|
|
|
+ },
|
|
|
+}
|
|
|
+
|
|
|
+\def\UrlBreaks{\do\/\do-}
|
|
|
+
|
|
|
+% Pgfplots imports
|
|
|
+\usepackage{pgfplots}
|
|
|
+\usepackage{pgfplotstable}
|
|
|
+\pgfplotsset{compat=1.13}
|
|
|
+
|
|
|
+% use acm colors
|
|
|
+\definecolor[named]{ACMBlue}{cmyk}{1,0.1,0,0.1}
|
|
|
+\definecolor[named]{ACMYellow}{cmyk}{0,0.16,1,0}
|
|
|
+\definecolor[named]{ACMOrange}{cmyk}{0,0.42,1,0.01}
|
|
|
+\definecolor[named]{ACMRed}{cmyk}{0,0.90,0.86,0}
|
|
|
+\definecolor[named]{ACMLightBlue}{cmyk}{0.49,0.01,0,0}
|
|
|
+\definecolor[named]{ACMGreen}{cmyk}{0.20,0,1,0.19}
|
|
|
+\definecolor[named]{ACMPurple}{cmyk}{0.55,1,0,0.15}
|
|
|
+\definecolor[named]{ACMDarkBlue}{cmyk}{1,0.58,0,0.21}
|
|
|
+
|
|
|
+% metropolis colors
|
|
|
+\definecolor{mDarkBrown}{HTML}{604c38}
|
|
|
+\definecolor{mDarkTeal}{HTML}{23373b}
|
|
|
+\definecolor{mLightBrown}{HTML}{EB811B}
|
|
|
+\definecolor{mLightGreen}{HTML}{14B03D}
|
|
|
+
|
|
|
+% Custom commands
|
|
|
+\newcommand{\ccode}[1]{\texttt{#1}}
|
|
|
+\usepackage{xspace}
|
|
|
+\newcommand{\newcommandx}[2]{\newcommand{#1}{#2\xspace}}
|
|
|
+\newcommandx{\revng}{\texttt{rev.ng}}
|
|
|
+\newcommandx{\llvm}{\textsc{llvm}}
|
|
|
+\newcommandx{\llvmcapi}{\textsc{llvm-c API}}
|
|
|
+\newcommandx{\llvmir}{\textsc{llvm ir}}
|
|
|
+\newcommandx{\qemuir}{\textsc{qemu ir}}
|
|
|
+\newcommandx{\qemu}{\textsc{qemu}}
|
|
|
+\newcommandx{\gcc}{\texttt{gcc}}
|
|
|
+\newcommand{\revamb}{\texttt{revamb}}
|
|
|
+\newcommand{\llvmcpy}{\texttt{llvmcpy}}
|
|
|
+
|
|
|
+\usepackage[noframe]{showframe}
|
|
|
+\usepackage{biblatex}[
|
|
|
+ backend=biber,
|
|
|
+ style=numeric,
|
|
|
+]
|
|
|
+
|
|
|
+% Python stuff
|
|
|
+% Default fixed font does not support bold face
|
|
|
+\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{10} % for bold
|
|
|
+\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{10} % for normal
|
|
|
+
|
|
|
+% Custom colors
|
|
|
+\usepackage{color}
|
|
|
+\definecolor{deepblue}{rgb}{0,0,0.5}
|
|
|
+\definecolor{deepred}{rgb}{0.6,0,0}
|
|
|
+\definecolor{deepgreen}{rgb}{0,0.5,0}
|
|
|
+
|
|
|
+\usepackage{listings}
|
|
|
+
|
|
|
+% Python style for highlighting
|
|
|
+\newcommand\pythonstyle{\lstset{
|
|
|
+ language=Python,
|
|
|
+ basicstyle=\ttm,
|
|
|
+ otherkeywords={self}, % Add keywords here
|
|
|
+ keywordstyle=\ttb\color{deepblue},
|
|
|
+ emph={MyClass,__init__}, % Custom highlighting
|
|
|
+ emphstyle=\ttb\color{deepred}, % Custom highlighting style
|
|
|
+ stringstyle=\color{deepgreen},
|
|
|
+ frame=tb, % Any extra options here
|
|
|
+ showstringspaces=false %
|
|
|
+}}
|
|
|
+
|
|
|
+% Python environment
|
|
|
+\lstnewenvironment{python}[1][]
|
|
|
+{
|
|
|
+ \pythonstyle
|
|
|
+ \lstset{#1}
|
|
|
+}
|
|
|
+{}
|
|
|
+
|
|
|
+% Python for external files
|
|
|
+\newcommand\pythonexternal[2][]{{
|
|
|
+ \pythonstyle
|
|
|
+\lstinputlisting[#1]{#2}}}
|
|
|
+\lstset{inputencoding=utf8/latin1}
|
|
|
+
|
|
|
+% Python for inline
|
|
|
+\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}
|
|
|
+
|
|
|
+% C environment
|
|
|
+
|
|
|
+\definecolor{mGreen}{rgb}{0,0.6,0}
|
|
|
+\definecolor{mGray}{rgb}{0.5,0.5,0.5}
|
|
|
+\definecolor{mPurple}{rgb}{0.58,0,0.82}
|
|
|
+\definecolor{backgroundColour}{rgb}{0.95,0.95,0.92}
|
|
|
+
|
|
|
+\lstdefinestyle{CStyle}{
|
|
|
+ backgroundcolor=\color{backgroundColour},
|
|
|
+ commentstyle=\color{mGreen},
|
|
|
+ keywordstyle=\color{magenta},
|
|
|
+ numberstyle=\tiny\color{mGray},
|
|
|
+ stringstyle=\color{mPurple},
|
|
|
+ basicstyle=\footnotesize,
|
|
|
+ breakatwhitespace=false,
|
|
|
+ breaklines=true,
|
|
|
+ captionpos=b,
|
|
|
+ keepspaces=true,
|
|
|
+ numbers=left,
|
|
|
+ numbersep=5pt,
|
|
|
+ showspaces=false,
|
|
|
+ showstringspaces=false,
|
|
|
+ showtabs=false,
|
|
|
+ tabsize=2,
|
|
|
+ language=C
|
|
|
+}
|
|
|
+\lstnewenvironment{C}[1][]
|
|
|
+{
|
|
|
+ \lstset{style=CStyle}
|
|
|
+}
|
|
|
+{}
|
|
|
+
|
|
|
+%\addbibresource{bibliography.bib}
|
|
|
+
|
|
|
+%\setbeamersize{text margin left=15pt,text margin right=15pt}
|
|
|
+
|
|
|
+% Information to be included in the title page:
|
|
|
+\title[]{Fondamenti di Informatica}
|
|
|
+%\subtitle{Presentation relative to ACACES 2019}
|
|
|
+\author{Andrea Gussoni\\andrea1.gussoni at polimi.it}
|
|
|
+\institute{Politecnico di Milano}
|
|
|
+\date{March 30, 2021}
|
|
|
+%\titlegraphic{\includegraphics[width=0.60\textwidth]{logo-polimi}}
|
|
|
+
|
|
|
+% Create section header slide
|
|
|
+\AtBeginSection{\frame{\sectionpage}}
|
|
|
+\begin{document}
|
|
|
+
|
|
|
+\frame{\titlepage
|
|
|
+ \note[item]{My name is ...}
|
|
|
+ \note[item]{Thank the audience.}}
|
|
|
+
|
|
|
+\begin{frame}
|
|
|
+ \frametitle{Table of Contents}
|
|
|
+ \tableofcontents
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\section{Ripasso Esercitazione Precedente}
|
|
|
+\begin{frame}{Ripasso}
|
|
|
+ \begin{alertblock}{}
|
|
|
+ Problemi con esercizi della precedente esercitazione?
|
|
|
+ \begin{itemize}
|
|
|
+ \item Codifica\pause
|
|
|
+ \item Dimensionamento memoria\pause
|
|
|
+ \item Diagrammi di flusso
|
|
|
+ \end{itemize}
|
|
|
+ \end{alertblock}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\section{Ripasso Costrutti di Controllo}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Variabili}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Una variabile è una etichetta che usiamo per riferirci ad contenitore di dati nella memoria centrale del calcolatore, durante l'esecuzione del nostro programma
|
|
|
+ \item Vengono principalmente usate per eseguire i calcoli implementati nel nostro algoritmo, e il loro valore varia.
|
|
|
+ \item Si usa l'operatore di assegnamento \ccode{=} per assegnare ad una variabile un valore: \ccode{a = 42}
|
|
|
+ \item Fare attenzione alla differenza con l'operatore matematico di uguaglianza (che in C diventa l'operatore \ccode{==})
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Variabili}
|
|
|
+ \begin{itemize}
|
|
|
+ \item In C ogni variabile ha associato un tipo e una dimensione. Il tipo è immutabile nell'esecuzione del programma.
|
|
|
+ \item Dovreste aver visto i vari modi di dichiarare e inizializzare una variabile:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{int var;}
|
|
|
+ \item \ccode{var = 42;}
|
|
|
+ \item \ccode{int var = 42;}
|
|
|
+ \end{itemize}
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Tipi Base}
|
|
|
+ Alcuni tipi di dato disponibili in C:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{int}, $>= 2 byte$
|
|
|
+ \item \ccode{short}, $>= 2 byte$
|
|
|
+ \item \ccode{long}, $>= 4 byte$
|
|
|
+ \item \ccode{float}, $4 byte$
|
|
|
+ \item \ccode{double}, $8 byte$
|
|
|
+ \item \ccode{char}, $>= 1 byte$
|
|
|
+ \end{itemize}
|
|
|
+
|
|
|
+ Avete visto anche alcuni modificatori che possono essere impiegati per aggiungere attributi al tipo di dato:
|
|
|
+ \begin{itemize}
|
|
|
+ \begin{comment}
|
|
|
+ \item \ccode{unsigned}
|
|
|
+ \item \ccode{static}
|
|
|
+ \end{comment}
|
|
|
+ \item \ccode{const}
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Tipi Aggregati e Dimensione}
|
|
|
+ \begin{itemize}
|
|
|
+ \item In C sono anche disponibili i cosiddetti tipi di dato aggregato, \ccode{struct, union}... (si vedrà prossimamente a lezione).
|
|
|
+
|
|
|
+ \item Inoltre, per sapere a runtime la dimensione di un tipo di dato, si può utilizzare l'operatore \ccode{sizeof()}.
|
|
|
+ \item Questo operatore restituisce la dimensione in byte. Si può utilizzare sia sui tipi che su una variabile.
|
|
|
+ \begin{center}
|
|
|
+ \begin{C}
|
|
|
+ printf("%d\n", sizeof(int));
|
|
|
+ short numero = 3;
|
|
|
+ printf("%d\n", sizeof(numero));
|
|
|
+ \end{C}
|
|
|
+ \end{center}
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Costrutto di Controllo if}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Il costrutto di controllo \ccode{if}, è il principale costrutto di controllo che ci permette di prendere decisione all'interno del nostro codice.
|
|
|
+ \item La valutazione di una \emph{condizione}, ci permette quindi di eseguire alternativamente il ramo \ccode{then} o il ramo \ccode{else} dell' \ccode{if}.
|
|
|
+ \begin{center}
|
|
|
+ \begin{C}
|
|
|
+ if (condizione) {
|
|
|
+ condizione vera;
|
|
|
+ } else {
|
|
|
+ condizione falsa;
|
|
|
+ }
|
|
|
+ \end{C}
|
|
|
+ \end{center}
|
|
|
+ \item Quiz, come posso fare a ottenere qualcosa che esegua:
|
|
|
+ \begin{C}
|
|
|
+ if (condizione) {
|
|
|
+ condizione falsa;
|
|
|
+ } else {
|
|
|
+ condizione vera;
|
|
|
+ }
|
|
|
+ \end{C}
|
|
|
+ senza scambiare il corpo dei branch?
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Operatori Aritmetici}
|
|
|
+ All'interno del nostro codice possiamo utilizzare gli operatori aritmetici binari per combinare i valori di diverse variabili/costanti:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{A + B} Addizione.
|
|
|
+ \item \ccode{A - B} Sottrazione.
|
|
|
+ \item \ccode{A * B} Moltiplicazione.
|
|
|
+ \item \ccode{A / B} Divisione.
|
|
|
+ \item \ccode{A \% B} Modulo.
|
|
|
+ \end{itemize}
|
|
|
+
|
|
|
+ Ci sono anche gli operatori aritmetici unari:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{-A} Inversione di segno.
|
|
|
+ \item \ccode{++A e A++} Pre/Post-incremento.
|
|
|
+ \item \ccode{--A e A--} Pre/Post-decremento.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Operatori Logici}
|
|
|
+ Ugualmente, esistono degli operatori logici binari:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{A == B} Uguaglianza.
|
|
|
+ \item \ccode{A != B} Disuguaglianza.
|
|
|
+ \item \ccode{A >= B, A > B} Maggiore o uguale, Maggiore.
|
|
|
+ \item \ccode{A <= B, A < B} Minore o uguale, Minore.
|
|
|
+ \item \ccode{A \&\& B} and.
|
|
|
+ \item \ccode{A || B} or.
|
|
|
+ \end{itemize}
|
|
|
+
|
|
|
+ E anche un operatore logico unario:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{!A} not.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\begin{frame}[fragile]{Operatori bitwise}
|
|
|
+ Esistono anche gli operatori bitwise binari.
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{A \& B} and.
|
|
|
+ \item \ccode{A | B} or.
|
|
|
+ \item \ccode{A \char`\^ B} xor.
|
|
|
+ \item \ccode{A << B} Shit a sinistra.
|
|
|
+ \item \ccode{A >> B} Shit a destra.
|
|
|
+ \end{itemize}
|
|
|
+
|
|
|
+ E anche un operatore bitwise unario:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{\char`\~A} not.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Operatori di assegnamento}
|
|
|
+ Conoscete già il principale operatore di assegnamento in C.
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{A = B} assegna ad A il valore di B.
|
|
|
+ \end{itemize}
|
|
|
+
|
|
|
+ Ma ci sono anche operatori che fanno una operazione ed un assegnamento, condesati in una notazione unica:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \ccode{+=}, \ccode{-=}, \ccode{*=}, \ccode{/=}, \ccode{\%=}, \ccode{\&=}, \ccode{|=}, \ccode{\char`\^=}.
|
|
|
+ \end{itemize}
|
|
|
+
|
|
|
+ Ad esempio:
|
|
|
+ \begin{center}
|
|
|
+ \begin{C}
|
|
|
+ numero -= 42;
|
|
|
+ \end{C}
|
|
|
+ \end{center}
|
|
|
+ è equivalente a:
|
|
|
+ \begin{center}
|
|
|
+ \begin{C}
|
|
|
+ numero = numero - 42;
|
|
|
+ \end{C}
|
|
|
+ \end{center}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Precedenza degli operatori}
|
|
|
+ Come abbiamo già accennato durante le esercitazioni riguardanti gli operatori logici, vi è una precedenza degli operatori da conoscere. In particolare, per quanto riguarda il C, bisogna rifarsi alla reference:
|
|
|
+ \begin{itemize}
|
|
|
+ \item \url{https://en.cppreference.com/w/c/language/operator_precedence}.
|
|
|
+ \item Sempre meglio però usare le parentesi, anche a costo di essere ridondanti.
|
|
|
+ \item Come side effect, noterete che l'uso delle parentesi migliora anche la leggibilità del codice altrui.
|
|
|
+ \item E soprattutto previene l'inserimento di bug accidentali dovuti a modifiche successive.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Costrutto di controllo while}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Il costrutto di controllo \ccode{while}, permette l'esecuzione del \emph{corpo} di esso fino a quando una condizione rimane vera (potenzialmente, infinitamente, come lo fareste?).
|
|
|
+ \begin{center}
|
|
|
+ \begin{C}
|
|
|
+ while (condizione) {
|
|
|
+ corpo_del_ciclo;
|
|
|
+ }
|
|
|
+ \end{C}
|
|
|
+ \end{center}
|
|
|
+ \item Esempio:
|
|
|
+ \begin{C}
|
|
|
+ a = 10;
|
|
|
+ while (a > 5) {
|
|
|
+ b = b + c;
|
|
|
+ a = a - b;
|
|
|
+ }
|
|
|
+ c = c + 1;
|
|
|
+ \end{C}
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\begin{frame}[fragile]{Costrutto di controllo do while}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Il costrutto di controllo \ccode{do while}, permette l'esecuzione del \emph{corpo} di esso fino a quando una condizione rimane vera, ma garantendo l'esecuzione di almeno la prima iterazione (la condizione è controllata \emph{dopo} il corpo del ciclo .
|
|
|
+ \begin{center}
|
|
|
+ \begin{C}
|
|
|
+ do {
|
|
|
+ corpo_del_ciclo;
|
|
|
+ } while (condizione);
|
|
|
+ \end{C}
|
|
|
+ \end{center}
|
|
|
+ \item Esempio:
|
|
|
+ \begin{C}
|
|
|
+ a = 10;
|
|
|
+ do {
|
|
|
+ b = b + c;
|
|
|
+ a = a - b;
|
|
|
+ } while (a > 5)
|
|
|
+ c = c + 1;
|
|
|
+ \end{C}
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Costrutto di controllo for}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Il costrutto di controllo \ccode{for} serve per costruire un ciclo, ma con delle comode aggiunte per gestire il numero di iterazioni.
|
|
|
+ \begin{center}
|
|
|
+ \begin{C}
|
|
|
+ for (inizializzazione; condizione; incremento) {
|
|
|
+ corpo_ciclo;
|
|
|
+ }
|
|
|
+ \end{C}
|
|
|
+ \end{center}
|
|
|
+ \item Esempio:
|
|
|
+ \begin{C}
|
|
|
+ a = 5;
|
|
|
+ for (i = 0; i < 10; i++) {
|
|
|
+ b = b + a;
|
|
|
+ }
|
|
|
+ b = b;
|
|
|
+ \end{C}
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{break e continue}
|
|
|
+ Sono inoltre disponibili due parole chiave che possono aiutare ulteriormente nella gestione delle iterazioni di un ciclo:
|
|
|
+ \begin{description}
|
|
|
+ \item [\ccode{break}]: interrompe immediatamente l'esecuzione del ciclo, ignorando la condizione e eventuali statement successivi del ciclo.
|
|
|
+ \item [\ccode{continue}]: passa alla successiva iterazione del ciclo, ignorando eventuali statement successivi nel corpo del ciclo. La condizione dell'iterazione successa viene comunque valutata, e in caso di ciclo \ccode{for} l'operazione di incremento viene comunque effettuata.
|
|
|
+ \end{description}
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\begin{frame}[fragile]{Costrutto di controllo switch}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Il costrutto di controllo \ccode{switch} permette di eseguire differenti porzione di codice in base al valore di una variabile. Spesso può venire usato per ristrutturare catene di \ccode{if else if} in una composizione più elegante.
|
|
|
+ \begin{center}
|
|
|
+ \begin{C}
|
|
|
+ switch (variabile) {
|
|
|
+ case 1:
|
|
|
+ codice_1;
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ codice_2;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ codice_default;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ \end{C}
|
|
|
+ \end{center}
|
|
|
+ \item la keyword \ccode{break} consente di non fare \emph{fallthrough}, che significa continuare con l'esecuzione del codice senza saltare alla fine dello scope dello \ccode{switch}.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Costrutto di controllo goto}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Non esiste nessun costrutto con nome \ccode{goto} in C.\
|
|
|
+ \pause
|
|
|
+ \item Jokes aside, lo statement \ccode{goto label}, può venire usato per reindirizzare l'esecuzione del codice ad una etichetta arbitraria posizionata nel codice. Si usa in specifici contesti, per quanto ci riguarda, bisogna assolutamente preferire e utilizzare codice strutturato (codice che contiene \ccode{goto} è anche chiamato codice non strutturato).
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\begin{frame}[fragile]{Lettura del codice}
|
|
|
+ \todo{Ideare semplice esercizio di lettura code}
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\section{Traduzione da Flowchart}
|
|
|
+\begin{frame}[fragile]{Traduzione da Flowchart}
|
|
|
+ Per i primi esercizi di programmazione che affronteremo:
|
|
|
+ \begin{itemize}
|
|
|
+ \item Cominceremo con la stesura del diagramma di flusso.
|
|
|
+ \item Solo successivamente ci sposteremo ad implementare il programma in C.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\begin{frame}[fragile]{Media Aritmetica}
|
|
|
+ Scrivere un programma che calcoli la media aritmetica di una serie di valori in input. Quando viene ricevuto il valore $0$, l'immissione dei valori termina e la media viene calcolata.
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Media Aritmetica}
|
|
|
+ \begin{center}
|
|
|
+ \includegraphics[height=6.5cm]{media}
|
|
|
+ \end{center}
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Numeri Primi}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Scrivere un programma che dato in input un numero intero $X > 0$, verifica se $X$ è primo.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Numeri Primi}
|
|
|
+ \begin{center}
|
|
|
+ \includegraphics[height=6.5cm]{primo}
|
|
|
+ \end{center}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Numeri di Fobonacci}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Scrivere un programma che dato in input un numero intero $N > 0$, stampi i primi $N$ numeri di Fibonacci.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Numeri di Fibonacci}
|
|
|
+ \begin{center}
|
|
|
+ \includegraphics[height=8.5cm]{fibo}
|
|
|
+ \end{center}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\section{Altri Esercizi}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Quadrato}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Implementare un programma in linguaggio C che legge un numero intero positivo, e ne stampa il quadrato usando solo operazioni di somma.
|
|
|
+
|
|
|
+ \item Osservazione: Il quadrato di un numero N, è dato dalla somma dei primi N numeri interi dispari.
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{frame}[fragile]{Conversione in binario}
|
|
|
+ \begin{itemize}
|
|
|
+ \item Implementare un programma in linguaggio C che legge un numero intero positivo, e ne stampa la rappresentazione in binario, usando il sistema delle divisioni successive. Stampare anche il numero minimo di bit necessari a rappresentare il numero.
|
|
|
+
|
|
|
+ \item Osservazione: e se volessimo stampare le cifre in ordine?
|
|
|
+ \end{itemize}
|
|
|
+\end{frame}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\begin{frame}[fragile]{Sequenze pari}
|
|
|
+ Implementare un programma in linguaggio C che legge una sequenza (di lunghezza arbitraria) di interi positivi terminata dal valore 0, e al termine della sequenza, stampa quante terne di numeri consecutivi diversi tra loro e pari sono contenute nella sequenza.
|
|
|
+
|
|
|
+ Osservazione: Notare la differenza tra i cicli precedenti, con un orientamente all'esecuzione di iterazioni fissati, a cicli a \emph{sentinella}
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\section{Extra}
|
|
|
+\begin{frame}[fragile]{Monte Carlo}
|
|
|
+ Scrivere un programma che calcoli il valore di $\pi$, utilizzando il metodo di Monte Carlo.
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\begin{frame}[t,allowframebreaks]
|
|
|
+ \frametitle{References}
|
|
|
+ \nocite{*}
|
|
|
+ \printbibliography
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\begin{comment}
|
|
|
+\begin{frame}
|
|
|
+ \frametitle{License}
|
|
|
+ \begin{center}
|
|
|
+ \vfill
|
|
|
+ \includesvg[height=1.5cm]{by-sa}\\
|
|
|
+ {\footnotesize These slides are published under a Creative Commons Attribution-ShareAlike 4.0 license\footnote{\url{https://creativecommons.org/licenses/by-sa/4.0/}}.}
|
|
|
+ \end{center}
|
|
|
+\end{frame}
|
|
|
+\end{comment}
|
|
|
+
|
|
|
+\end{document}
|