C++ Primer Plus: 第10章(2)

第10章編程題:
(1)
Account.h:

#ifndef ACCOUNT_H_
#define ACCOUNT_H_#include <string>class Account
{
private:std::string name ;std::string code ;double money ;
public:Account() ;Account(std::string Name, std::string Code, double Money) ;~Account() ;void ShowAccout() ;void InputMoney(double Money) ;void OutputMoney(double Money) ;
} ;#endif

Account.cpp:

#include "Account.h"
#include <iostream>
#include <string>
using namespace std ;Account::Account()
{}Account::Account(string Name, string Code, double Money)
{name = Name ;code = Code ;money = Money ;
}Account::~Account()
{}void Account::ShowAccout()
{using std::ios_base ;ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield) ;std::streamsize prec = cout.precision(0) ;cout << "This customer, who is called " << name << ", and has $" << money << " in " << code << " Account.\n";cout.setf(orig, ios_base::floatfield) ;cout.precision(prec) ;
}void Account::InputMoney(double Money)
{money += Money ;
}void Account::OutputMoney(double Money)
{money -= Money ;
}

main.cpp:

#include "Account.h"
#include <iostream>const int Num = 8 ;int main()
{Account A[Num] = {Account("Jackie Chen", "098425345", 56943),Account("John Rebort", "*&%$#4586", 12366), Account("Bluse Amy", "3425834", 9032455),Account("Killy Joe", "@#%^&*890", 234568690),Account("Beyonce Jim", "*&^%$#678", 999912366),Account("Alan London", "5555555", 222222222222),Account("Jackie Chen", "098425345", 56943),Account("Floyd Dork", "4324049", 247)} ;std::cout << "Let's see everyone: \n" ;for (int i=0; i<Num; i++){A[i].ShowAccout() ;}std::cout << "\nNow everyone increases $100: \n" ;for (int i=0; i<Num; i++){A[i].InputMoney(100) ;}for (int i=0; i<Num; i++){A[i].ShowAccout() ;}std::cout << "\nNow everyone decreases $100: \n" ;for (int i=0; i<Num; i++){A[i].OutputMoney(100) ;}for (int i=0; i<Num; i++){A[i].ShowAccout() ;}
}

(2)
Person.h:

#ifndef PERSON_H_
#define PERSON_H_#include <string>
using namespace std ;class Person
{
private:static const int LIMIT = 25 ;string lname ;char fname[LIMIT] ;
public:Person() {lname = "" ; fname[0] = '\0' ;} ;Person(const string & ln, const char * fn = "Heyyou") ;void Show() const ;void FormalShow() const ; 
} ;#endif

Person.cpp:

#include "Person.h"
#include <iostream>
#include <cstring>
using namespace std ;Person::Person(const string & ln, const char * fn)
{lname = ln ;strcpy(fname, fn) ;
}void Person::Show() const
{cout << fname << " " << lname ;
}void Person::FormalShow() const 
{cout << lname << ", " << fname ;
}

main.cpp:

#include "Person.h"
#include <iostream>int main()
{Person one ;Person two("Smythecraft") ;Person three("Dimwiddy", "Sam") ;std::cout << "The name of one:\n" ;one.Show() ;std::cout << std::endl ;one.FormalShow() ;std::cout << "\nThe name of two:\n" ;two.Show() ;std::cout << std::endl ;two.FormalShow() ;std::cout << "\nThe name of three:\n" ;three.Show() ;std::cout << std::endl ;three.FormalShow() ;std::cout << std::endl ;
}

(3)
golf.h:

#ifndef GOLF_H_
#define GOLF_H_class golf
{
private:static const int Len = 40 ;char fullname[Len] ;int handicap ;
public:golf(const char * name = "", int hc = 0) ;~golf() ;int setgolf() ;void Handicap() ;void showgolf() const ;
} ;#endif

golf.cpp:

#include "golf.h"
#include <cstring>
#include <iostream>golf::golf(const char * name, int hc)
{strncpy(fullname, name, Len) ;handicap = hc ;
}golf::~golf()
{std::cout << "delete!\n" ;
}int golf::setgolf()
{using namespace std ;char name[Len] = {0} ;int hc ;cout << "Fullname: " ;if (cin.get(name, Len).get() == -1){cin.clear() ;        // 務必對輸入隊列進行重置cout << "Enter Fail!\n" ;return 0 ;}cout << "Handicap: " ;while(!(cin >> hc)){cin.clear() ;while(cin.get() != '\n') ;cout << "Please enter a number: " ;}cin.get() ;golf G(name, hc) ;*this = G ;return 1 ;
}void golf::Handicap()
{int hc ;std::cin >> hc ;(*this).handicap = hc ;
}void golf::showgolf() const
{std::cout << "The " << handicap << " player is "<< fullname << std::endl ;
}

main.cpp:

#include "golf.h"
#include <iostream>const int Num = 5 ;int main()
{using namespace std ;golf G[Num] ;int num = 0 ;cout << "Please input the information of all of players:\n" ;for (int i=0; i<Num; i++){cout << "#" << i+1 << ": \n" ;if(!G[i].setgolf()) break ;else num ++ ;}cout << "\nNow change everyone's handicap:\n" ;for (int i=0; i<num; i++){cout << "#" << i+1 << ": " ;G[i].Handicap() ; }cout << "\nAnd then display all players:\n" ;for (int i=0; i<num; i++){G[i].showgolf() ;}
}

(4)
Sales.h:

#ifndef SALES_H_
#define SALES_H_namespace SALES
{class Sales{private:static const int QUARTERS = 4 ;double sales[QUARTERS] ;double average ;double max ;double min ;public:Sales() ;Sales(const double ar[], int n) ;~Sales() ;void setSales() ;void ShowSales() const ;} ;
} #endif

Sales.cpp:

#include "Sales.h"
#include <iostream>
using namespace SALES ;Sales::Sales()
{min = max = average = 0 ;for (int i=0; i<QUARTERS; i++)sales[i] = 0 ;
}Sales::Sales(const double ar[], int n)
{int i ;for (i=0; i<n && i<QUARTERS; i++)sales[i] = ar[i] ;while (i<QUARTERS){sales[i] = 0 ;i ++ ;} min = max = sales[0] ;double total = 0 ;for (i=0; i<QUARTERS; i++){total += sales[i] ;if (min > sales[i]) min = sales[i] ;if (max < sales[i]) max = sales[i] ;}average = total / QUARTERS ;
}Sales::~Sales()
{std::cout << "delete!\n" ;
}void Sales::setSales()
{using namespace std ;int n ;cout << "Enter the number of double you want to input: " ;cin >> n ;double *ar = new double[n] ;cout << "Enter these numbers you want to input:\n" ;for (int i=0; i<n; i++){cin >> ar[i] ;}Sales S(ar, n) ;*this = S ;delete [] ar ;
}void Sales::ShowSales() const
{std::cout << "The members of Sales:\n" ;int i ;for (i=0; i<QUARTERS; i++){std::cout << sales[i] << " " ;}std::cout << std::endl ;std::cout << "max = " << max << std::endl ;std::cout << "min = " << min << std::endl ;std::cout << "average = " << average << std::endl ;
}

main.cpp:

#include "Sales.h"
#include <iostream>
using namespace SALES ;int main()
{using namespace std ;double ar[7] = {7, 6, 5, 4, 3, 2, 1} ;Sales S1(ar, 7) ;cout << "The content of S1:\n" ;S1.ShowSales() ;Sales S2 ;S2.setSales() ;cout << "The content of S2:\n" ;S2.ShowSales() ;
}

(5)
Stack.h:

#ifndef STACK_H_
#define STACK_H_struct customer {char fullname[35] ;double payment ;
} ;typedef customer Item ;class Stack
{
private:static const int MAX = 10 ;Item items[MAX] ;int top ;double total_payment ;
public:Stack() ;~Stack() ;bool isempty() const ;bool isfull() const ;bool push(const Item & e) ;bool pop(Item & e) ;
} ;#endif

Stack.cpp:

#include "Stack.h"
#include <cstring>
#include <iostream>Stack::Stack()
{top = -1 ;total_payment = 0 ;
}Stack::~Stack()
{std::cout << "Delete!\n" ;
}bool Stack::isempty() const
{if (top == -1) return true ;else return false ;
}bool Stack::isfull() const
{if (top == (MAX-1)) return true ;else return false ;
}bool Stack::push(const Item & e)
{if (top == (MAX-1)) return false ;top++ ;strcpy(items[top].fullname, e.fullname) ;items[top].payment = e.payment ;return true ;
}bool Stack::pop(Item & e)
{if (top == -1) return true ;strcpy(e.fullname, items[top].fullname) ;e.payment = items[top].payment ;top -- ;total_payment += e.payment ;std::cout << "total_payment = " << total_payment << std::endl ;return true ;
}

main.cpp:

#include "Stack.h"
#include <iostream>
#include <cctype>int main()
{using namespace std ;Stack St ;char ch ;Item item ;cout << "Please enter A to add a customer,\n" << "P to process a PO, or Q to quit.\n" ;while (cin>>ch && toupper(ch) != 'Q'){while (cin.get() != '\n')continue ;if (!isalpha(ch)){cout << '\a' ;continue ;}switch(ch){case 'A':case 'a': cout << "Enter a customer:\n" ;cout << "Fullname: " ;cin.get(item.fullname, 35).get() ;cout << "Payment: " ;cin >> item.payment ;if (!St.push(item))cout << "The Stack is full!\n" ;break ;case 'P':case 'p': cout << "Delete a customer:\n" ;if (!St.pop(item))cout << "The Stack is empty!\n" ;cout << "Fullname: " ;cout << item.fullname << endl ;cout << "Payment: " ;cout << item.payment << endl ;break ;}cout << "Please enter A to add a customer,\n"<< "P to process a PO, or Q to quit.\n" ;}cout << "Bye!\n" ;return 0 ;
}

(6)
Move.h:

#ifndef MOVE_H_
#define MOVE_H_class Move
{
private:double x ;double y ;
public:Move(double a = 0, double b = 0) ;~Move() ;void showmove() const ;Move add(const Move & m) const ;void reset(double a = 0, double b = 0) ;
} ;#endif

Move.cpp:

#include "Move.h"
#include <iostream>Move::Move(double a, double b)
{x = a ;y = b ;
}Move::~Move()
{std::cout << "Delete!\n" ;
}void Move::showmove() const
{std::cout << "In this object, x = " << x << ", "<< "y = " << y << std::endl ;
}Move Move::add(const Move & m) const
{Move M(m.x, m.y);return M ;
}void Move::reset(double a, double b) 
{x = a ;y = b ;
}

main.cpp:

#include "Move.h"int main()
{Move Begining ;Move Next(9, 8) ;Begining.showmove() ;Next.showmove() ;Move Destination = Begining.add(Next) ;Destination.showmove() ;
}

(7)
Plorg.h:

#ifndef PLORG_H_
#define PLORG_H_class Plorg
{
private:static const int MAX = 19 ;char fullname[MAX] ;int CI ;
public:Plorg(const char * name = "Plorga") ;~Plorg() ;void ResetCI(int ci) ;void ShowPlorg() const ;
} ;#endif

Plorg.cpp:

#include "Plorg.h"
#include <iostream>
#include <cstring>Plorg::Plorg(const char * name){strncpy(fullname, name, MAX) ;CI = 50 ;
}Plorg::~Plorg(){std::cout << "Delete!\n" ;
}void Plorg::ResetCI(int ci){CI = ci ;
}void Plorg::ShowPlorg() const
{std::cout << "The fullname: " << fullname << ", CI = " << CI << std::endl ;
}

main.cpp:

#include "Plorg.h"int main()
{Plorg P1("Jim Killy") ;Plorg P2("Jackie London") ;Plorg P3("Trump Jim") ;P1.ShowPlorg() ;P2.ShowPlorg() ;P3.ShowPlorg() ;P1.ResetCI(90) ;P2.ResetCI(80) ;P3.ResetCI(70) ;P1.ShowPlorg() ;P2.ShowPlorg() ;P3.ShowPlorg() ;
}

(8)
List.h:

#ifndef LIST_H_
#define LIST_H_typedef int Item ;
struct LinkList {Item ElemType ;LinkList * next ;
} ;class List
{
private:static const int MAX = 10 ;LinkList * head ;LinkList * rail ;LinkList * p ;int num ;
public:List() ;~List() ;bool Add(const Item & i) ;bool isEmpty() ;bool isFull() ;void visit(void (*pf)(Item & i)) ;
} ;#endif

List.cpp:

#include "List.h"
#include <iostream>List::List() {head = new LinkList ;head->next = NULL ;rail = p = head ;num = 0 ;
}List::~List() {while (head != rail) {p = head ;head = head -> next ;delete p ;}delete head ;head = rail = p = NULL ;num = 0 ;std::cout << "This List has been destroyed!\n" ;
}bool List::Add(const Item & i) {if (num == MAX) return false ;p = rail ;p->next = new LinkList ;p->next->ElemType = i ;p->next->next = NULL ;rail = p->next ;num++ ;return true ;
}bool List::isEmpty() {if (num == 0) return true ;else return false ;
}bool List::isFull() {if (num == MAX) return true ;else return false ;
}void List::visit(void (*pf)(Item & i)) {for (p = head->next; p; p=p->next)(*pf)(p->ElemType) ;
}

main.cpp:

#include "List.h"
#include <iostream>void Watch(Item & i) ;
void AddOne(Item & i) ;int main()
{using namespace std ;List L ;cout << "Let's input some numbers:\n" ;Item i ;while (cin>>i) {if (L.isFull()) {cout << "This List is filled!\n" ;break ;}else L.Add(i) ;}cout << "Let's see all of members from this List:\n" ;L.visit(Watch) ;cout << endl ;cout << "Let's add one to every member from this List:\n" ;L.visit(AddOne) ;L.visit(Watch) ;cout << endl ;
}void Watch(Item & i) {std::cout << i << " " ;
}void AddOne(Item & i) {i += 1 ;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/41976.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/41976.shtml
英文地址,請注明出處:http://en.pswp.cn/news/41976.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Vue history和hash模式

目錄 一、簡介 一、簡介 ~~~~~~~~ 在Vue.js中&#xff0c;路由模式是用來管理應用程序中不同頁面之間的導航的機制。Vue Router支持兩種常見的路由模式&#xff1a;history模式和hash模式。 History 模式&#xff1a; ~~~~~~~~ History模式使用瀏覽器的history.pushState API …

紅帆OA SQL注入漏洞復現

0x01 產品簡介 紅帆iOffice.net從最早滿足醫院行政辦公需求&#xff08;傳統OA&#xff09;&#xff0c;到目前融合了衛生主管部門的管理規范和眾多行業特色應用&#xff0c;是目前唯一定位于解決醫院綜合業務管理的軟件&#xff0c;是最符合醫院行業特點的醫院綜合業務管理平…

Lnton羚通關于如何使用nanoPC-T4 安裝OpenCV?

nanoPC-T4 安裝 OpenCV Note: OpenCV has been pre-installed in FriendlyCore/FriendlyDesktop (Version after 201905) and does not require manual installation. Please download the latest FriendlyCore/FriendlyDesktop Image file from the following URL: http://do…

springboot 自定義注解

1、引入maven依賴&#xff08;版本太低也會導致不生效&#xff09; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId><version>2.7.10</version></dependency…

深度分析納斯達克上市公司慧擇的競爭優勢和投資價值

來源&#xff1a;猛獸財經 作者&#xff1a;猛獸財經 一、保險行業的現狀、競爭與機遇 在疫情期間&#xff0c;很多行業的經營理念與經營方式&#xff0c;甚至客戶行為、客戶需求都發生了變化&#xff0c;進而催生出新的機遇。保險行業亦是如此&#xff0c;受疫情影響&#xf…

用Python編程實現百度自然語言處理接口的對接,助力你開發智能化處理程序

用Python編程實現百度自然語言處理接口的對接&#xff0c;助力你開發智能化處理程序 隨著人工智能的不斷進步&#xff0c;自然語言處理&#xff08;Natural Language Processing&#xff0c;NLP&#xff09;成為了解決文本處理問題的重要工具。百度自然語言處理接口提供了一系…

騰訊開啟2024校招,主要招聘5大類崗位

近日&#xff0c;騰訊的大動作一個接一個&#xff0c;前腳剛公布2023上半年財報&#xff0c;后腳就開啟了2024校招&#xff0c;不得不讓人感嘆騰訊真速度&#xff01; 此次招聘對象為畢業時間在2023年9月至2024年8月期間的2024屆應屆畢業生&#xff0c;覆蓋北上廣深等多個城市…

異步編程框架Seastar介紹

使用Seastar進行異步(Asynchronout)編程 介紹 我們在本文中介紹的Seastar&#xff0c;是一個用于在現代多核機器上&#xff0c;編寫高效復雜的服務器應用程序的C庫。 傳統上&#xff0c;用于編寫服務器應用程序的編程語言庫和框架已經分為兩個不同的陣營&#xff1a;那些注重…

環境與能源創新專題:地級市綠色創新、碳排放與環境規制數據

數據簡介&#xff1a;推動綠色發展&#xff0c;促進人與自然和諧共生是重大戰略舉措。綠色發展強調“綠水青山就是金山銀山”&#xff0c;人與自然和諧共生重在正確處理生態環境保護與經濟發展的關系。在著力于實現綠色發展的過程中&#xff0c;綠色創新是綠色發展的重要驅動因…

關于API數據接口獲取商品的數據的說明

獲取商品數據已經成為許多應用程序的重要組成部分。為了實現這一目標&#xff0c;許多公司和技術開發者使用API數據接口來獲取相關數據。本文將詳細介紹如何使用API數據接口獲取商品數據&#xff0c;并使用Python作為編程語言示例來展示相關代碼。 API數據接口是一種通信協議&…

WPF的CheckBox中的三個狀態

WPF的CheckBox中的三個狀態 CheckBox控件和RadioButton控件是繼承自ToggleButton類&#xff0c;這意味著用戶可切換他們的開關狀態&#xff0c;其中IsChecked屬性是可空的Boolean類型&#xff0c;這意味著該屬性可以設置為true&#xff0c;false或null。 null值表示不確定狀態…

spring.HttpMessageNotReadableException: JSON parse error

實體類如下&#xff1a; Value public class Search{//搜索內容String value;//是否模糊搜索boolean fuzzy true; //其實這樣寫并不是“默認”模糊搜索&#xff0c;而是“一定是”模糊搜索 }spring.HttpMessageNotReadableException: JSON parse error: Cannot construct ins…

GPU Microarch 學習筆記 [1]

WARP GPU的線程從thread grid 到thread block&#xff0c;一個thread block在CUDA Core上執行時&#xff0c;會分成warp執行&#xff0c;warp的顆粒度是32個線程。比如一個thread block可能有1024個線程&#xff0c;分成32個warp執行。 上圖的CTA&#xff08;cooperative thre…

10條SQL優化技巧

一、一些常見的SQL實踐 &#xff08;1&#xff09;負向條件查詢不能使用索引 select * from order where status!0 and stauts!1 not in/not exists都不是好習慣 可以優化為in查詢&#xff1a; select * from order where status in(2,3) &#xff08;2&#xff09;前導模…

Codeforces Round 893 (Div. 2)B題題解

文章目錄 [The Walkway](https://codeforces.com/contest/1858/problem/B)問題建模問題分析1.分析所求2.如何快速計算每個商販被去除后的餅干數量代碼 The Walkway 問題建模 給定n個椅子&#xff0c;其中有m個位置存在商販&#xff0c;在商販處必須購買餅干吃&#xff0c;每隔…

Python程序設計——字符串處理的特殊方法

學習目標&#xff1a; 學習如何創建字符串使用len、min和max函數獲取一個字符串的長度、串中的最大和最小的字符使用下標運算符([])訪問字符串中的元素使用截取運算符str[ start:end]從較長的字符串中得到一個子串使用運算符連接兩個字符串&#xff0c;通過*運算符復制一個字符…

【Odroid C4】交叉編譯工具鏈安裝以及QT交叉編譯環境搭建

【Odroid C4】交叉編譯工具鏈安裝以及QT交叉編譯環境搭建 虛擬機環境&#xff0c;UBUNTU20.04 文章目錄 【Odroid C4】交叉編譯工具鏈安裝以及QT交叉編譯環境搭建一、Odroid C4交叉編譯工具鏈安裝二、QT下載及編譯安裝1.QT下載2.交叉編譯QT 配置QtCreator可以[參考](https://bl…

快速入門vue3新特性和新的狀態管理庫pinia

(創作不易&#xff0c;感謝有你&#xff0c;你的支持&#xff0c;就是我前行的最大動力&#xff0c;如果看完對你有幫助&#xff0c;請留下您的足跡&#xff09; 目錄 Vue3.3新特性 defineOptions defineModel pinia 介紹 與 Vuex 3.x/4.x 的比較 安裝 核心概念 定義…

前饋神經網絡多分類任務

pytorch深度學習的套路都差不多&#xff0c;多看多想多寫多測試&#xff0c;自然就會了。主要的技術還是在于背后的數學思想和數學邏輯。 廢話不多說&#xff0c;上代碼自己看。 import torch import numpy as np import torch.nn as nn import torchvision import torchvisi…

【騰訊云Cloud Studio實戰訓練營】使用Cloud Studio社區版快速構建React完成點餐H5頁面還原

陳老老老板&#x1f9b8; &#x1f468;?&#x1f4bb;本文專欄&#xff1a;生活&#xff08;主要講一下自己生活相關的內容&#xff09; &#x1f468;?&#x1f4bb;本文簡述&#xff1a;生活就像海洋,只有意志堅強的人,才能到達彼岸。 &#x1f468;?&#x1f4bb;上一篇…