[理学]IRM_Chapter_6e_10.doc

上传人:音乐台 文档编号:1986541 上传时间:2019-01-28 格式:DOC 页数:138 大小:189.50KB
返回 下载 相关 举报
[理学]IRM_Chapter_6e_10.doc_第1页
第1页 / 共138页
[理学]IRM_Chapter_6e_10.doc_第2页
第2页 / 共138页
[理学]IRM_Chapter_6e_10.doc_第3页
第3页 / 共138页
亲,该文档总共138页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《[理学]IRM_Chapter_6e_10.doc》由会员分享,可在线阅读,更多相关《[理学]IRM_Chapter_6e_10.doc(138页珍藏版)》请在三一文库上搜索。

1、SavitchInstructors Resource GuideProblem Solving w/ C+, 6eChapter 10Chapter 10DEFINING CLASSES AND ABSTRACT DATA TYPES1. Solutions for and Remarks on Selected Programming ProblemsThis chapter can be done after Chapter 7, Arrays. However, I have not used anything from that chapter in these solutions.

2、 Several of these solutions could be simplified in good measure if arrays were used instead of the extensive switch and nested if else statements.1. Class grading programI have put statements of programming strategy and of the problem in the program comments./ch10Prg1.cpp#include using namespace std

3、;const int CLASS_SIZE = 5;/ The problem says this is for a class, rather than one student. One/ programming stratagem is to deal with a single student, then extend/ to treat an array of N students. /Grading Program/Policies:/ Two quizzes, 10 points each/ midterm and final exam, 100 points each/ Of g

4、rade, final counts 50%, midterm 25%, quizes25%/ Letter Grade is assigned:/ 90 or more A/ 80 or more B/ 70 or more C/ 60 or more D/ less than 60, F/ Read a students scores, / output record: scores + numeric average + assigned letter grade/ Use a struct to contain student record.struct StudentRecord i

5、nt studentNumber; double quiz1; double quiz2; double midterm; double final; double average; char grade;void input(StudentRecord& student);/prompts for input for one student, sets the/structure variable members.void computeGrade(StudentRecord& student);/calculates the numeric average and letter grade

6、.void output(const StudentRecord student);/outputs the student record.int main() StudentRecord studentCLASS_SIZE; for(int i = 0; i CLASS_SIZE; i+) input(studenti); / Enclosing block fixes VC+ for loop control defined outside loop for(int i = 0; i CLASS_SIZE; i+) computeGrade(studenti); output(studen

7、ti); cout endl; return 0;void input(StudentRecord &student) cout student.studentNumber; cout student.studentNumber endl; cout enter two 10 point quizes student.quiz1 student.quiz2; cout student.quiz1 student.quiz2 endl; cout enter the midterm and final exam grades. student.midterm student.final; cou

8、t student.midterm student.final endl endl;void computeGrade(StudentRecord& student)/ Of grade, final counts 50%, midterm 25%, quizes25% double quizAvg= (student.quiz1 + student.quiz2)/2.0; double quizAvgNormalized = quizAvg * 10; student.average = student.final * 0.5 + student.midterm * 0.25 + quizA

9、vgNormalized * 0.25; char letterGrade= FFFFFFDCBAA; int index = static_cast(student.average/10); if(index 0 | 10 = index) cout Bad numeric grade encountered: student.average endl Aborting.n; abort(); student.grade = letterGradeindex;void output(const StudentRecord student) cout The record for studen

10、t number: student.studentNumber endl The quiz grades are: student.quiz1 student.quiz2 endl The midterm and exam grades are: student.midterm student.final endl The numeric average is: student.average endl and the letter grade assigned is student.grade endl;Data for the test run:1 7 10 90 952 9 8 90 8

11、03 7 8 70 804 5 8 50 705 4 0 40 35Command line command to execute the text run:ch10prg1 dataOutput:enter the student number: 1enter two 10 point quizes7 10enter the midterm and final exam grades. These are 100 point tests90 95enter the student number: 2enter two 10 point quizes9 8enter the midterm a

12、nd final exam grades. These are 100 point tests90 80enter the student number: 3enter two 10 point quizes7 8enter the midterm and final exam grades. These are 100 point tests70 80enter the student number: 4enter two 10 point quizes5 8enter the midterm and final exam grades. These are 100 point tests5

13、0 70enter the student number: 5enter two 10 point quizes4 0enter the midterm and final exam grades. These are 100 point tests40 35The record for student number: 1The quiz grades are: 7 10The midterm and exam grades are: 90 95The numeric average is: 91.25and the letter grade assigned is AThe record f

14、or student number: 2The quiz grades are: 9 8The midterm and exam grades are: 90 80The numeric average is: 83.75and the letter grade assigned is BThe record for student number: 3The quiz grades are: 7 8The midterm and exam grades are: 70 80The numeric average is: 76.25and the letter grade assigned is

15、 CThe record for student number: 4The quiz grades are: 5 8The midterm and exam grades are: 50 70The numeric average is: 63.75and the letter grade assigned is DThe record for student number: 5The quiz grades are: 4 0The midterm and exam grades are: 40 35The numeric average is: 32.5and the letter grad

16、e assigned is F*/2. Redefine CDAccount from Display 10.1 to be a class rather than struct.Use the same variables, make them private.Add member functions:to return initial balanceto return balance at maturityto return interest rateto return the termdefault constructorconstructor to set specified valu

17、esinput function (istream&);output function (ostream&);Embed in a test programThe code in Display 10.1 makes the behavior of the required functions clear. Note on capitalization schemes: I use a slightly different capitalization scheme than the author. You should make your conventions clear to the s

18、tudent. Any capitalization that produces readable code is acceptable to this author. The instructor, as always, is left free to do as is wished./ File: ch10Prg2.cpp/ Title: CDAccount#include using namespace std;class CDAccount public: CDAccount(); CDAccount(double bal, double intRate, int T ); doubl

19、e InterestRate(); double InitialBalance(); double BalanceAtMaturity(); int Term(); void input(istream&); void output(ostream&);private: double balance; double interestRate; / in PER CENT int term; / months to maturity;int main() double balance; double intRate; int term; CDAccount account = CDAccount

20、( 100.0, 10.0, 6 ); cout CD Account interest rate: account.InterestRate() endl; cout CD Account initial balance: account.InitialBalance() endl; cout CD Account balance at maturity is: account.BalanceAtMaturity() endl; cout CD Account term is: account.Term() months endl; account.output(cout); cout En

21、ter CD initial balance, interest rate, and term: endl; account.input(cin); cout CD Account interest rate: account.InterestRate() endl; cout CD Account initial balance: account.InitialBalance() endl; cout CD Account balance at maturity is: account.BalanceAtMaturity() endl; cout CD Account term is: ac

22、count.Term() months endl; account.output( cout ); cout balance; inStream interestRate; inStream term;void CDAccount:output(ostream& outStream) outStream.setf(ios:fixed); outStream.setf(ios:showpoint); outStream.precision(2); outStream when your CD matures in term months endl it will have a balance o

23、f BalanceAtMaturity() endl;/*A typical run follows:CD Account interest rate: 10CD Account initial balance: 100CD Account balance at maturity is: 105CD Account term is: 6 monthswhen your CD matures in 6 monthsit will have a balance of 105.00Enter CD initial balance, interest rate, and term:2001012CD

24、Account interest rate: 10.00CD Account initial balance: 200.00CD Account balance at maturity is: 220.00CD Account term is: 12 monthswhen your CD matures in 12 monthsit will have a balance of 220.00*/3. CD account, different interfaceRedo the definition of class CDAccount from Project 2 so that the i

25、nterface is the same but the implementation is different. The new implementation is similar to the second implementation of BankAccount in Display 10.7. Here the balance is recorded in two int values, one for dollars, one for cents. The member variable for interest rate stores the interest as a frac

26、tion rather than a percentage. Term is stored as in Project 2.Remark: The changes to be made are in the functions that take balance as argument. The implementation of the members must change:1) to generate the int objects dollars and cents from the external representation of balance (a double)2) to

27、take dollars and cents (int objects) from the internal representation and generate the external information./File: ch10Prg3.cpp/Title: CDAccount - modification of Program1, but with /different implementation but SAME interface./The new implementation should be similar to Display 10.7/record balance

28、as two int values: One for dollars, one for/cents. interest rate is a double (decimal) fraction rather /than a percent (0.043, not 4.3%). term is stored the same / way as Program 1.#include using namespace std;class CDAccountpublic: CDAccount(); CDAccount(double bal, double intRate, int T ); double

29、InterestRate(); double InitialBalance(); double BalanceAtMaturity(); int Term(); void input(istream&); void output(ostream&);private: / double balance; / double interestRate; / in PER CENT int dollars; int cents; double interestRate; / decimal fraction: 0.043 rather than 4.3% int term; / months to m

30、aturity;CDAccount:CDAccount() / do nothingCDAccount:CDAccount(double bal, double intRate, int T ) dollars = int(bal); cents = int(bal*100); interestRate = intRate/100; term = T;double CDAccount:InterestRate() return interestRate*100; / internal decimal frac, /external, percentdouble CDAccount:Initia

31、lBalance() return dollars + cents/100.00;double CDAccount:BalanceAtMaturity() return (dollars + cents/100.0)* (1+ (interestRate)*(term/12.0);int CDAccount:Term() return term;void CDAccount:input(istream& inStream) double dBal; inStream dBal; dollars = int(dBal); cents = int(dBal - dollars)*100); inS

32、tream interestRate/100; inStream term;void CDAccount:output(ostream& outStream) outStream.setf(ios:fixed); outStream.setf(ios:showpoint); outStream.precision(2); outStream when your CD matures in term months endl it will have a balance of BalanceAtMaturity() endl;int main() double balance; double in

33、tRate; int term; CDAccount account = CDAccount( 100.0, 10.0, 6 ); cout CD Account interest rate: account.InterestRate() endl; cout CD Account initial balance: account.InitialBalance() endl; cout CD Account balance at maturity is: account.BalanceAtMaturity() endl; cout CD Account term is: account.Ter

34、m() months endl; account.output( cout ); cout Enter CD initial balance, interest rate, and term: endl; account.input(cin); cout CD Account interest rate: account.InterestRate() endl; cout CD Account initial balance: account.InitialBalance() endl; cout CD Account balance at maturity is: account.Balan

35、ceAtMaturity() endl; cout CD Account term is: account.Term() months endl; account.output( cout ); cout endl;/*A typical run follows:CD Account interest rate: 10CD Account initial balance: 200CD Account balance at maturity is: 210CD Account term is: 6 monthswhen your CD matures in 6 monthsit will hav

36、e a balance of 210.00Enter CD initial balance, interest rate, and term:2001012CD Account interest rate: 10.00CD Account initial balance: 200.00CD Account balance at maturity is: 220.00CD Account term is: 12 monthswhen your CD matures in 12 monthsit will have a balance of 220.00*/4. No Answer Provide

37、d5. No Answer Provided6. Class MonthHere we create an abstract data type to represent month.The class month has the following member functions:a constructor to set month based on the first 3 letters of the name(uses 3 char args)a constructor to set month base on month number, 1 = January etc.a defau

38、lt constructor (what does it do?)an input function to set the month based on the month numberan input function to set the month based on a three-character inputan output function that outputs the month as an integer,an output function that outputs the month as the letters.a function that returns the

39、 next month as a Month objectNB each input and output function have a single formal parameter forthe streamData store is an int object.This problem doesnt say anything about error checking. It is easy and (I hope) obvious how to do error checking. I will require my students put it in, and I use it h

40、ere. The careful reader will note that testing is not thorough. It is an excellent exercise to provide test data that makes coverage complete. (Complete coverage is to test all possible paths through the program.)With these comments, here is the code a the solution to the problem:/file: ch10prb5.cpp/Title: Month/To create and test a month ADT/ Begin month.cpp for Problem 7 HERE.#include / for file and iostream stuff#include / for exit()#include / for tolower()class Monthpublic: Month(char c1, c

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1