题目1
【问题描述】
利息存款问题
存款类型和利率分别如下:
活期 0.35% 一年 3.25% 两年3.75% 三年4.25% 五年4.75%
编写程序账户类,包括编号,姓名 ,本金,存储类型,以及利率等信息,能够计算账户存款到期的利息值并显示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <string> #include <iostream> using namespace std;class Account { public : Account (); private : string mID,mName; int mMoneyCapital; int mDepositType; float mLife; static float InterestRate[5 ]; float mInerest; }; int main () { Account a1 ("1001" ,"zhang3" ,10000 ,1 ,0.8 ) ; Account a2 ("1002" ,"li4" ,2000 ,2 ,1 ) ; cout<<a1.getInterst (); cout<<a2.getInterst (); }
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <string> #include <iostream> using namespace std;class Account { public : Account (); Account (string ID, string Name, int MoneyCapital, int DepositType, double Life) { this ->mID = ID; this ->mName = Name; this ->mMoneyCapital = MoneyCapital; this ->mDepositType = DepositType; this ->mLife = Life; } double getInterst () { return mMoneyCapital * InterestRate[mDepositType] * mLife; } private : string mID,mName; int mMoneyCapital; int mDepositType; float mLife; static float InterestRate[5 ]; float mInerest; }; float Account::InterestRate[5 ]= {0 ,0.0035 ,0.0325 ,0.0375 ,0.0425 }; Account::Account () {}; int main () { Account a1 ("1001" ,"zhang3" ,10000 ,1 ,0.8 ) ; Account a2 ("1002" ,"li4" ,2000 ,2 ,1 ) ; cout<<a1.getInterst (); cout<<a2.getInterst (); }
题目2
【问题描述】
编写链表实现插入和删除等操作
【样例输入输出】
请输入学生姓名:
zhang
wang
li
zhao
sun
qian
学生信息为:
2018001 li
2018002 wang
2018003 zhang
2018004 zhao
2018005 sun
2018006 qian
请输入要删除的学生学号:
2018004
学生信息为:
2018001 li
2018002 wang
2018003 zhang
2018005 sun
2018006 qian
请输入要插入的学生学号和姓名:
2018004 han
学生信息为:
2018001 li
2018002 wang
2018003 zhang
2018004 han
2018005 sun
2018006 qian
【评分标准】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #include <iostream> #include <string> using namespace std;class Node { public : Node (int id, string name); void showMessage () ; protected : int mId; string mName; Node *next; }; Node::Node (int id, string name) { this ->mId = id; this ->mName = name; this ->next = NULL ; } void Node::showMessage () { cout << this ->mId << " " << this ->mName << endl; } class LinkList { public : protected : Node *head; }; int main () { int ID; LinkList list1; string name; cout << "请输入学生姓名:" << endl; for (int i = 2018003 ; i >= 2018001 ; i--) { cin >> name; list1.addNodeAtHead (i,name); } for (int i = 2018004 ; i <= 2018006 ; i++) { cin >> name; list1.addNodeAtTail (i,name); } cout <<"学生信息为:" << endl; list1.print (); cout << "请输入要删除的学生学号:" <<endl; cin >> ID; cout << "学生信息为:" << endl; list1.deleteNode (ID); list1.print (); cout << "请输入要插入的学生学号和姓名:" <<endl; cin >> ID >>name; cout << "学生信息为:" << endl; list1.insertNode (ID, name); list1.print (); return 0 ; }
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 #include <iostream> #include <string> using namespace std;class Node { public : Node (int id, string name); void showMessage () ; friend class LinkList ; protected : int mId; string mName; Node *next; }; Node::Node (int id, string name) { this ->mId = id; this ->mName = name; this ->next = NULL ; } void Node::showMessage () { cout << this ->mId << " " << this ->mName << endl; } class LinkList { public : LinkList (); ~LinkList (); void addNodeAtHead (int i, string name) ; void addNodeAtTail (int i, string name) ; void print () ; void deleteNode (int ID) ; void insertNode (int ID, string name) ; int getLenght () ; protected : Node *head; }; LinkList::LinkList () { head = new Node (0 , "" ); head->next = NULL ; } LinkList::~LinkList () { delete head; } void LinkList::addNodeAtHead (int i, string name) { Node* List = new Node (i, name); if (head->next == NULL ) { head->next = List; } else { List->next = head->next; head->next = List; } } void LinkList::addNodeAtTail (int i, string name) { Node* List = new Node (i, name); if (head->next == NULL ) { head->next = List; } else { Node* p = head->next; while (p->next != NULL ) { p = p->next; } p->next = List; } } void LinkList::print () { Node* p = head->next; while (p != NULL ) { p->showMessage (); p = p->next; } delete p; } void LinkList::deleteNode (int ID) { Node* temp = head; while (temp->next != NULL ) { if (temp->next->mId == ID) { temp->next = temp->next->next; break ; } temp = temp->next; } } int LinkList::getLenght () { Node* p = head->next; int count = 0 ; while (p != NULL ) { count++; p = p->next; } return count; } void LinkList::insertNode (int ID, string name) { int n; Node* p1 = head->next; while (p1->next != NULL ) { if (p1->mId == ID) return ; p1 = p1->next; } if (p1->mId == ID) return ; if (head->next == NULL ) { addNodeAtHead (ID, name); } else { n = ID - head->next->mId; Node* p = head; if (n > getLenght ()) { addNodeAtTail (ID, name); } else if (n < 0 ) { addNodeAtHead (ID, name); } else { while (n > 0 ) { p = p->next; n--; } Node* node = new Node (ID, name); node->next = p->next; p->next = node; } } } int main () { int ID; LinkList list1; string name; cout << "请输入学生姓名:" << endl; for (int i = 2018003 ; i >= 2018001 ; i--) { cin >> name; list1.addNodeAtHead (i,name); } for (int i = 2018004 ; i <= 2018006 ; i++) { cin >> name; list1.addNodeAtTail (i,name); } cout <<"学生信息为:" << endl; list1.print (); cout << "请输入要删除的学生学号:" <<endl; cin >> ID; cout << "学生信息为:" << endl; list1.deleteNode (ID); list1.print (); cout << "请输入要插入的学生学号和姓名:" <<endl; cin >> ID >>name; cout << "学生信息为:" << endl; list1.insertNode (ID, name); list1.print (); return 0 ; }
题目3
【问题描述】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <string> #include <iostream> using namespace std;class Goods { public : Goods (string id,string name,float inprice,float outprice,int num); void buy (int num) { this ->mNum+=num; } string GetId () { return mId; } void SetId (string val) { mId = val; } string GetName () { return mName; } void SetName (string val) { mName = val; } static float Getsum () { return sum; } static float getProfit () {return profit;} private : string mId; string mName; float mInPrice; float mOutPrice; int mNum; }; int main () { Goods gd[100 ]; gd[0 ]=Goods ("1001" ,"白菜" ,0.8 ,1.5 ,500 ); gd[1 ]=Goods ("1002" ,"萝卜" ,1.5 ,2.1 ,500 ); gd[0 ].sell (10 ); gd[1 ].sell (20 ); gd[0 ].sell (5 ); cout<<gd[0 ].Getsum ()<<endl; cout<<gd[1 ].Getsum ()<<endl; cout<<gd[0 ].getProfit ()<<endl; return 0 ; }
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 #include <string> #include <iostream> using namespace std;class Goods { public : Goods (string id,string name,float inprice,float outprice,int num); Goods (); void sell (int sales) ; float getInPrice () { return this ->mInPrice; } float getOutPrice () { return this ->mOutPrice; } void buy (int num) { this ->mNum+=num; } string GetId () { return mId; } void SetId (string val) { mId = val; } string GetName () { return mName; } void SetName (string val) { mName = val; } static float Getsum () { return sum; } static float getProfit () {return profit;} private : string mId; string mName; float mInPrice; float mOutPrice; int mNum; static float profit; static float sum;; }; float Goods::profit = 0 ;float Goods::sum = 0 ;Goods::Goods (string id, string name, float inprice, float outprice, int num) { this ->mId = id; this ->mName = name; this ->mInPrice = inprice; this ->mOutPrice = outprice; this ->mNum = num; } Goods::Goods () { } void Goods::sell (int sales) { this ->sum += this ->getOutPrice () * sales; this ->profit += (this ->getOutPrice () - this ->getInPrice ()) * sales; } int main () { Goods gd[100 ]; gd[0 ]=Goods ("1001" ,"鐧借彍" ,0.8 ,1.5 ,500 ); gd[1 ]=Goods ("1002" ,"钀濆崪" ,1.5 ,2.1 ,500 ); gd[0 ].sell (10 ); gd[1 ].sell (20 ); gd[0 ].sell (5 ); cout<<gd[0 ].Getsum ()<<endl; cout<<gd[1 ].Getsum ()<<endl; cout<<gd[0 ].getProfit ()<<endl; return 0 ; }
代码可能略有不足,请大佬多多指教