The first question: "Fish treasure"
Title Description
Please implement a "fish treasure" class, the following has given the code template, according to the main function in the operation of the class, supplementary class implementation of partial completion of code.
The "Fish Treasure" category can record account balances, save, withdraw money, and calculate interest. There is a private static member variable in the class Profitrate stores the interest rate of "fish treasure" and can modify the value of the interest rate with a common static member function setprofitrate. The program input is the 1th day to the nth day of the account operation, only one account operation per day, or deposit or take, each day the interest generated is the balance of the former account and the "fish treasure" interest rate of the product, the same day will be credited to the account balance, due to 1th days before the account does not exist, So the 1th day must be a new account and save money, and the day will not have interest deposit balance. After accepting the input of the N-day operation, the program needs to calculate the account balance and output after the nth day operation is completed. The code is as follows:
#include <iostream>using namespacestd; classyuebao{Static Doubleprofitrate; Public: Static voidSetprofitrate (DoubleRate );/*Your Code here!*/}; intMain () {intN; while(Cin >>N) {Doubleprofitrate; CIN>>profitrate; Yuebao::setprofitrate (profitrate);//set the rate of fish treasureYuebao y (0);//New Fish Treasure account, the balance is initialized to 0 intOperation;//accept the input to decide whether to save or take DoubleAmount//Accept input Access amount for(inti =0; I < n; ++i) {y.addprofit ();//interest accrued from the balance of the day before joiningCin >> Operation >>amount; if(Operation = =0) y.deposit (amount);//Deposit Amount ElseY.withdraw (amount);//Withdraw Amount} cout<< y.getbalance () << Endl;//output Final account balance } return 0;}
Enter a description
A total of n+2 lines per test sample
Line 1th enters an integer n, which indicates the next N-day operation
Line 2nd Enter a real number, which is the interest rate of "fish treasure" and the interest rate is unchanged in n days.
Next there are n rows, which represent N-day operations, with 2 numbers per line, 1th or 0 or 1,0 for saving, 1 for withdrawals, and the second real for the amount of access
1 <= N <= 20
Output description
For each test example, the account balance after the nth day operation is completed
Sample input
30.10 100 101 10
Sample output
13.1
#include <iostream>using namespacestd;classyuebao{Static Doubleprofitrate; Doublebalance; Public: //static void Setprofitrate (double rate);Yuebao (Doublenewset) {Balance=Newset; } Static voidSetprofitrate (DoubleRate ) {Profitrate=Rate ; } voidAddprofit () {Balance= Balance * (1+profitrate); } voidDepositDoubleamount) {Balance+=amount; } voidWithdraw (Doubleamount) {Balance-=amount; } DoubleGetBalance () {returnbalance; }};DoubleYuebao::p rofitrate =0;intMain () {intN; while(Cin >>N) {Doubleprofitrate; CIN>>profitrate; Yuebao::setprofitrate (profitrate);//set the rate of fish treasureYuebao y (0);//New Fish Treasure account, the balance is initialized to 0 intOperation;//accept the input to decide whether to save or take DoubleAmount//Accept input Access amount for(inti =0; I < n; ++i) {y.addprofit ();//interest accrued from the balance of the day before joiningCin >> Operation >>amount; if(Operation = =0) y.deposit (amount);//Deposit Amount ElseY.withdraw (amount);//Withdraw Amount} cout<< y.getbalance () << Endl;//output Final account balance } return 0;}
Question number two: count the mice
Title Description
Please implement a mouse class, the following has given the code template, according to the main function in the operation of the class, supplementary class implementation of partial completion of the code.
The class has a public static variable num records all the objects of the class, the main function will output the number of objects after the different statements, only the correct implementation of the class, to ensure that NUM correctly records the number of objects of the class, in order to output the correct results.
#include <iostream>using namespacestd; classmouse{/*Your Code here!*/}; voidfn (Mouse m); intMain () {Mouse::num=0; Mouse A; cout<< Mouse::num <<Endl; Mouse B (a); cout<< Mouse::num <<Endl; for(inti =0; I <Ten; ++i) {Mouse x; cout<< Mouse::num <<Endl; } fn (a); cout<< Mouse::num <<Endl; return 0;} voidfn (Mouse m) {cout<< Mouse::num <<Endl; Mouse N (m); cout<< Mouse::num <<Endl;}
Enter a description
Output description
The output of the main function is already written.
Answer:
#include <iostream>using namespacestd; classmouse{ Public: Static intnum; Mouse () {num++;} Mouse (ConstMouse &m) {num++;}; ~mouse () {num--;}};intmouse::num=0; voidfn (Mouse m); intMain () {Mouse::num=0; Mouse A; cout<< Mouse::num <<Endl; Mouse B (a); cout<< Mouse::num <<Endl; for(inti =0; I <Ten; ++i) {Mouse x; cout<< Mouse::num <<Endl; } fn (a); cout<< Mouse::num <<Endl; return 0;} voidfn (Mouse m) {cout<< Mouse::num <<Endl; Mouse N (m); cout<< Mouse::num <<Endl;}
Academy Online tsinghuax:00740043x C + + language programming Fundamentals Fifth Lab