首页 > 其他分享 >ENG2002 Computer Programming Homework

ENG2002 Computer Programming Homework

时间:2024-10-12 11:26:18浏览次数:5  
标签:function userMenu Programming should will Computer user input Homework

THE HONG KONG POLYTECHNIC UNIVERSITY

ENG2002 Computer Programming Homework

  1. Instructions
  2. At the beginning of each Python file, add a comment to show your student ID and name.
  1. This assignment comprises 3 parts. You may use a new file for each part or a single Jupyter Notebook file.
  1. Your programs need to be well commented. You may refer to your course notes (ch4.pdf) on how comments aremade. Lacking comments will result in down-grading of your submission.
  1. Your programs need to have a good structure. Never write the whole program in a single function. You shouldwrite supporting functions.
  1. Do NOT share your source program with your classmates. Plagiarism will make you and your friends all get zermark.The deadline of submission is 13 October 2024 and you need to submit your file to Teams as an attachment.
  1. The Programming TaskBackground

A simple resistor–capacitor (RC) circuit is an electric circuit composed of one resistor and one capacitor as shown below

The capacitor C could be charged to and then be discharged by the resistor R. The formula for exponential decay ofis the capacitor voltage at time t = 0. The time required for the voltage to fall to 36.8% of the initial value is

called the time constant (?) and is given by:? = ??(2)Q1. Create a new file in Jupyter Notebook with the name "part1". On starting, the application should call a function

userMenu() that shows the following main menu:

[a] Find time constant

[b] Find discharge voltage

[c] Find discharge time

[d] Find best curve from experimental data

[x] Exit

Key in your choice (a, b, c, d or x to exit): • The user is expected to enter a character of 'a', 'b', 'c', 'd' or 'x' only. When any other character is entered,userMenu() will display "Wrong input, enter again!" and the above main menu is shown again. Forthis part, if the user enters 'b' or 'c' or 'd', the above menu will be shown again and your program does not needto do anything.

  • If the user enters 'a', userMenu() will display "Please enter resistance (R): ". The user should input

a value of type float. It is assumed that the user always enters a positive float value for R and your programdoes not need to check this. Then, userMenu() will display "Please enter capacitance (C): ". Theuser should input 代 写ENG2002 Computer Programming Homework a positive value for C. If the user enters 0 or a negative value, userMenu() will prompt theuser for re-entering until a positive value has been entered (you may assume that the user always enters a valueof type float for this and your program does NOT need to check this.) Then, the function timeConst() iscalled, which uses the user-input values of R and C as input parameter values, and returns the value of the timeconstant, which is of type float. Note that this returned value should be printed by userMenu() and thefunction timeConst() must NOT print anything on screen.

  • If the user enters 'x', userMenu() will print "Goodbye!" on the screen. The whole program then ends (withoutshowing the main menu.)
  • The following Python code MUST be used.

#your Name

#student number

from math import exp, log #use math functions in part 2

#Implement the required functions below:

#def timeConst(R, C) #Find time constant of a simple RC circuit

#def userMenu() #shows the user menu and process user input

userMenu() #call function to display user menu and process user input

  • The sample outputs given below should be realised by your program on executing it inside Jupyter Notebook.

Notice that the character(s) following a ‘:’ is/are entered by the user.

-------------------------------------- PASS line ----------------------------------Q2. Create a NEW file with the name "part2". Copy the content of the source file in Q1 to this file. Modify the

program developed for Q1 to perform the following tasks:

  • Rewrite the function userMenu()such that if the user enters 'b', it will display "Enter initial voltage(V0): " on the screen. The user should input a positive value of type float and you should create a functionnput_p()to handle this. Then, userMenu()will display "Enter resistance (R): " on the screen. Theser should input a positive value of type float and the function input_p()should be used. Afterward,userMenu()will display "Enter capacitance (C): " on the screen. The user should input a positive valueof type float and the function input_p() should also be called. Then, userMenu()should call a functiondisVoltage() which uses the values of V0, R, C as input parameters. disVoltage()will display "Enterdischarge time (t): " on the screen. The user should input a positive value of type float and the functionnput_p()should be used. disVoltage()will then display the discharge voltage using equation (1). You mayuse exp()function in the math module. The function disVoltage() should have no returned value.
  • Rewrite the function userMenu()such that if the user enters 'c', it will display "Enter initial voltage

(V0): " on the screen. The user should input a positive value of type float and the function input_p()should be used. Then, userMenu()will print "Enter resistance (R): " onthe screen. The user shouldinput a positive value of type float and the function input_p() should be used. Afterward, userMenu()willdisplay "Enter capacitance (C): " on the screen. The user should input a positive value of type floatnd the function input_p() should also be called. Then, userMenu()should call a function disTime() whichuses the values of V0, R, C as input parameters. disTime()will display "Enter discharge voltage (Vr):" on the screen. The user should input a positive value of type float and the function input_p()should beused. disTime()will then display the discharge time by solving equation (1). You may use log()function inthe math module. The function disTime() should have no returned value.

  • The sample outputs given below should be realised by your program on executing it inside Jupyter Notebook.Notice that the character(s) following a ‘:’ is/are entered by the user.

---------------------------------- CREDIT line ------------------------------------

Q3. Create a NEW file with the name "part3". Copy the content of the source file in Q2 to this file. Modify theprogram developed for Q2 to perform the following tasks:

  • Rewrite the function userMenu()such that if the user enters 'd', it will call the function bestCurve() and thenthe function will call input_p() to get the input data from the user. It will display the result on the screen

nside the function. Get back to the main menu after the result is shown.• To find the best curve, you need to transform equation (1) into straight line, y = m x + b andthen use the followingormulae to find the best straight line:

 

where

  • The sample outputs given below should be realised by your program on executing it inside Jupyter Notebook.

Notice that the character(s) following ‘:’ is/are entered by the user.

---------------------------------- DISTINCTION line -------------------------------

标签:function,userMenu,Programming,should,will,Computer,user,input,Homework
From: https://www.cnblogs.com/comp9313/p/18459730

相关文章

  • 2023 Benelux Algorithm Programming Contest (BAPC 23)
    A-\texttt题意\(n\)个软件包,已知大小,可以同时下载\(k\)个,已经下载好了\(m\)个,选\(k\)个下载使得下载完后进度最大,输出已完成进度所占百分比。思路选最大的\(m+k\)个。代码点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoid......
  • The 2022 ICPC Asia Hangzhou Regional Programming Contest K. Master of Both
    题目链接题意:给定n个字符串,然后给定q种字典序规则,在这q种字典序规则下求出这n个字符串的逆序对数量。思路:我们发现q很大,对于每一种排序规则都遍历一遍n个字符串去求逆序对的数量复杂度太高,所以考虑预处理。我们知道要判断两个字符串字典序的大小关系,只需要知道它们第......
  • 《Programming from the Ground Up》读后感
    之所以看这本书,是想了解一些跟汇编相关的知识,打开这本书后就被作者的观点——“Ifyoudon'tunderstandsomethingthefirsttime,rereadit.Ifyoustilldon'tunderstandit,itissometimesbesttotakeitbyfaithandcomebacktoitlater(第一遍看不懂,那就看第二......
  • 《Programming from the Ground Up》阅读笔记:p217-p238
    《ProgrammingfromtheGroundUp》学习第11天,p217-p238总结,总计22页。一、技术总结1.Ccompilingp216,Ccompilingissplitintotwostages-thepreprocessorandthemaincompiler。注:感觉这个写法不好,因为preprocessor和compiler都是对象,这里应该指动作。应该是:Cco......
  • 《Programming from the Ground Up》阅读笔记:p181-p216
    《ProgrammingfromtheGroundUp》学习第10天,p181-p216总结,总计34页。一、技术总结第10章主要讲计算机是如何计算的,如十进制、二进制、八进制、十六进制以及浮点数和负数的表示。属于比较基础的内容,如果有一定基础,本章可跳过。1.exponent&mantissa示例:p197,12345.2isst......
  • Computer Science 320SC
    ComputerScience320SC–(2024)ProgrammingAssignment5Due:Oct132024(11:59pm)AcademicIntegrityBeforeattemptingtosolvetheassignment,pleasereadthemessagebelowverycarefully.Asdescribedonhttps://academicintegrity.cs.auckland.ac.nz/,yo......
  • INFS3208 – Cloud Computing Programming
    SchoolofInformationTechnologyandElectricalEngineeringINFS3208–CloudComputingProgrammingAssignmentTaskIII(10Marks)Taskdescription:Inthisassignment,youareaskedtowriteapieceofSparkcodetocountoccurrencesofverbsintheUN......
  • COMP612 Computer Graphics Programming
    COMP612ComputerGraphicsProgrammingSemester2,2024Project:HelicopterSceneThisisanindividualassignment.Allworkyousubmitmustbeentirelyyourown.Theassignmentisworth70%andwillbemarkedoutof100.Youmustworkfromtheprovided......
  • 国庆homework
    1最长递增序列简单来说就是从一串数字李找出连续的最长递增序列,暴力的思路就是通过两次循环,第一层是便利每个元素,第二层便利第一层之前的元素,如果当前元素大于前一个元素,并且以j结尾的递增子序列长度加1大于dp[i],则更新普通点击查看代码intn;cin>>n;intma......
  • The 2020 ICPC Asia Shenyang Regional Programming Contest Northeastern University
    The2020ICPCAsiaShenyangRegionalProgrammingContestNortheasternUniversity(SMU2024ICPC网络赛选拔赛2)D.JourneytoUn'Goro思路队友写得,没看。代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongintll;#defineintlonglong#defineP......