1 / 26

Trabalho Computacional Mecânica de Fluidos Ambiental

Trabalho Computacional Mecânica de Fluidos Ambiental. Guillaume Riflet. Dúvidas: guillaume.riflet@ist.utl.pt Ext:3430 Página: https:// fenix.ist.utl.pt/homepage/ist146730/mecanica-dos-fluidos-ambiental-2011-2012 ( work in progress ...). Objectivos. Programming 101 MS-excel macros

brand
Download Presentation

Trabalho Computacional Mecânica de Fluidos Ambiental

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. Trabalho Computacional Mecânica de Fluidos Ambiental Guillaume Riflet • Dúvidas: guillaume.riflet@ist.utl.pt Ext:3430 • Página: https://fenix.ist.utl.pt/homepage/ist146730/mecanica-dos-fluidos-ambiental-2011-2012(workinprogress...)

  2. Objectivos • Programming 101 • MS-excel macros • Visual Basic for Applications (VBA) 101 https://fenix.ist.utl.pt/homepage/ist146730/mecanica-dos-fluidos-ambiental-2011-2012 (workinprogress...)

  3. Interesse para engenheiros do ambiente • Utilização mais avançadas de ferramentas de folhas de cálculo. • Em muitas circunstâncias, o excel (ou derivado) é a melhor ferramenta realizar determinadas tarefas. • Qualquer empresa do sector público ou privado usa uma ferramenta de folha de cálculo. Muitas permitem utilizar macros.

  4. Linguagens de programação • Visual Basic • Fortran • C/C++ • C# • Matlab • Java • (pôr linguagem de programação favorita)

  5. Diferenças entre VB e VBA

  6. Exemplo I Sheet1 Dica: Alt+F11

  7. Exemplo I: glossário • Source-code, código-fonte • Compiler/interpreter, compilador/interpretador • Subroutine, Subrotina • String, Sequência de caracteres • Propriedade dum objecto • Excel macro, Macro de excel

  8. Exemplo II Sheet1

  9. Exemplo III Sheet1

  10. Exemplo III: glossário • Argument, Argumento • Argument passing, Passagem de argumento • Argument type, Tipo de argumento

  11. Exemplo IV Sheet1

  12. Exemplo IV: glossário • Function, Função • Function type, Tipo de função

  13. Exemplo V Sheet1

  14. Exemplo V: glossário • Variable, Variável • Variable type, Tipo de variável

  15. Variáveis Declaração Implícita/Explícita Dim NomeAs Tipo Dim Aluno as String Dim Idade as Integer Dim Nota as Single Function SafeSqr(num) TempVal = Abs(num) SafeSqr = Sqr(TempVal) End Function Function SafeSqr(num) TempVal = Abs(num) SafeSqr = Sqr(TemVal) End Function Option Explicit

  16. Data type Range Byte 0 to 255 Boolean True or False Integer -32,768 to 32,767 Long(long integer) -2,147,483,648 to 2,147,483,647 Single(single-precision floating-point) -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values Double(double-precision floating-point) -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values Currency(scaled integer) -922,337,203,685,477.5808 to 922,337,203,685,477.5807 Tipos de variáveis I

  17. Decimal +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; Date January 1, 100 to December 31, 9999 Object Any Object reference String(variable-length) 0 to approximately 2 billion String (fixed-length) 1 to approximately 65,400 Variant(with numbers) Any numeric value up to the range of a Double Variant(with characters) Same range as for variable-length String User-defined(using Type) The range of each element is the same as the range of its data type. Tipos de variáveis II

  18. Constantes • Public Const conMaxPlanets As Integer = 9 • Const conReleaseDate = #1/1/95# • Const conPi = 3.14159265358979 • Constantes são valores que aparecem várias vezes no programa e que ao contrário das variáveis não alteram o seu valor.

  19. Private Nome as Tipo Public Nome as Tipo Projecto Modulo SubRotina Dim Nome as Tipo

  20. Sub-procedures & functions Sub Rotinas Subteste(File As String) Open (File) ReadFirstLine (File) ... End Sub Call teste (A) Funções FunctionHypotenuse (A As Integer, B As _ Integer) As Double Hypotenuse = Sqr(A ^ 2 + B ^ 2) End Function strX = Hypotenuse(Width, Height)

  21. i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 12 0 0 21 0 3 i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 False True False False False False False False True Arrays Dim Conta(6) As Integer Conta(0) = 12 ; Conta(3) = 21; Conta(5) = 3 Dim Teste(3,3) As Boolean Teste(0,1) = True ; Teste (2,0) = True

  22. Exemplo VI Sheet1

  23. Estruturas de loop I Do While ... Loop Do While line < 10 Call ReadLine(line) line = line+1 Loop For Each...Next For Each File In folder.File() Call ReadFile (File)Next File For...Next For i = 0 To 10 For j = 0 to 10 Matriz(i,j) = cos(x) NextNext

  24. Exemplo VII Sheet1

  25. Grupo de exercícios 1 • Criem a função de umargumento de tipo double que retorne 3x*x – 2. Criemumasubrotina que teste a função para x=4 e x=25, escrevendo o resultado numa folha de excel. • Concebamumciclofor numa subrotina que escreva numa folha de excel os resultados de 3x*x*x + 4 para todos os x que pertençamaointervalo [-30 , 50] comincrementos 2 em 2. • Casonãotenhamfeito, repitam o exercícioanteriorusandoumarray. • Repitam o exercícioanterior, mas destavez a função é 3x*x*x + 4 se o resultado for positivo e 0 (zero) casocontrário (usemumif). • Gravem a macro dumgráficocom os resultados do exercícioanterior (com x nasabscissas e os valores da funçãonasordenadas). Vejam o código que resultou da gravação da vossa macro.

  26. Configurações regionais • Atenção no que toca à linguagem nativa do office. SEN (português) ou SIN (inglês). • Atenção às definições de separadores de casas decimais (3.14 ou 3,14) e de formatos de data (13/05/1980 ou 1980-05-13 ou...).

More Related