c++ Primer Plus(第六版)第十章习题,写代码之路

来源:互联网 发布:wear of it 编辑:程序博客网 时间:2024/05/18 22:44

c++ Primer Plus(习题10.1)

/*第10.1题的头文件*//*主要声明一个类*/#ifndef BANKCOUNT_H#define BANKCOUNT_H#include<string>class Bankcount{private:std::string name;std::stringnum;double balance;public:Bankcount(const std::string &n, const std::string &m, double cash=0.0);void show() const;void deposit(double cash);void withdraw(double cash);};#endif
/*定义*/#include<iostream>#include"bankcount.h"Bankcount::Bankcount(const std::string &n, const std::string &m, double cash){name = n;num = m;balance = cash;}void Bankcount::deposit(double cash){balance += cash;}void Bankcount::withdraw(double cash){balance -= cash;}void Bankcount::show()const{using std::cout;using std::endl;cout << "Countname:  " << name << endl<< "Countnumber:  " << num << endl<< "Banlance:     " << balance << endl;}

/*真身,需和10.1定义一起编译,且包含头文件bankcount.h*/#include<iostream>#include"bankcount.h"int main(){using namespace std;Bankcount count1=Bankcount{"Saw","3084",20.1};count1.deposit(100);count1.withdraw(10);cout << "Here the result: \n";count1.show();return 0;}/*测试成功,其他算了*/
c++ Primer Plus(习题10.2)

/*10.2头文件*/#pragma once#ifndef PERSON_H#define PERSON_H#include<string>class Person {private:static const int LIMT=25;std::string lname;//名char fname[LIMT];//姓public:Person() { lname = ""; fname[0] = '\0'; } //记得里面的分号不能少Person(const std::string &ln, const char *fn="Heyyou");void show()const;//这个fristname,在前void Formalshow()const;//这个lastname在前};#endif // !PERSON_H
/*10.2定义,隐藏数据用的*/#include<string>#include<iostream>#include<cstring>#include"Person.h"Person::Person(const std::string &ln, const char*fn)   //定义不用加那个啥默认参数{lname = ln;strcpy(fname,fn);}void Person::show()const{std::cout << "Name: " << fname<< "," << lname << "\n";}void Person::Formalshow()const{std::cout << "Dear: " << lname<< "," << fname << "\n";}
#include<iostream>#include"Person.h"int main(){using namespace std;Person one;Person two("Smythecraft");Person three("Bimwiddy", "sam");one.show();one.Formalshow();two.show();two.Formalshow();three.show();three.Formalshow();return 0;}


c++ Primer Plus(习题10.3)

c++ Primer Plus(习题10.4)

//重新编写的golf类#ifndef GOLFCLASS_H#define GOLFCLASS_H#include<iostream>class Golf{private: char fullname[40];//不用作用域为类的常量了int handicap;public:Golf(char *name = "no name", int hc = 0);void setgolf(const char*name, int hc); void sethand(int n);void show()const;};#endif // !GOLFCLASS_H

#include"golfclass.h"Golf::Golf(char*s,int hc){std::strcpy(fullname, s);handicap = hc;}void Golf::setgolf(const char*name, int hc)//用于重新设置对象的值{std::strcpy(fullname, name);handicap = hc;}void  Golf::sethand(int n)//重新设置hanghip的值{handicap = n;}void Golf::show()const{std::cout << "Fullname: " << fullname << std::endl<< "Handicap: " << handicap << std::endl;}

//题目感觉理解错了,setgolf和sethand应该返回一个对象//凑合着用着//这题运行时出现,对象被重复引用,编译器发神经了#include"golfclass.h"#include<iostream>int main(){using namespace std;cout << "Show default value:\n";Golf a;a.show();//重新设置aa.setgolf("Saw", 100);cout << "After set golf:\n";a.show();cout << "Only set hangdicap:\n";a.sethand(66);a.show();cout << "Bye!\n";return 0;}


c++ Primer Plus(习题10.5)

/*10.5题目头文件,创建一个包含结构的类*/#pragma once#ifndef CUSTOMER_H#define CUSTOMER_Htypedef struct customer{char fullname[30];double payment;}Item;class Customer {private:static const int NUM = 3;Item items[NUM];int top;double total;public:Customer();bool isfull()const;bool isempty()const;bool push(const Item&item);bool pop(Item&item);void showtotal()const;   //用来显示卖家的总收入};#endif // !CUSTOMER_H
/*包含头文件Customer.h*/#include"Customer.h"#include<iostream>Customer::Customer()//一个空的栈{total = 0;top = 0;}bool Customer::isfull()const {return top ==NUM;}bool Customer::isempty()const {return top == 0;}bool Customer::push(const Item &item){if (top < NUM){items[top++] = item;return true;}elsereturn false;}bool Customer::pop(Item &item){if (top > 0){item = items[--top];total += item.payment;return true;}elsereturn false;}void Customer::showtotal()const{std::cout<< "***total***" <<total<< "***my income.***\n";}
/*这题更多的是书上的stack,adt接口书上已经写好了*/#include<iostream>#include"Customer.h"#include<ctype.h>int main(){using namespace std;Customer st;//创建一个空栈char ch;Item temp;cout << "Please enter A to add a customer name and payment,\n"<< "D to delete a customer,or Q to exit.\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 << "Customer name: ";cin.getline(temp.fullname, 30);cout << "Paymeng: ";cin >> temp.payment;if (st.isfull())cout << "stack is full!\n";elsest.push(temp);break;case'd':case'D':if (st.isempty())cout << "stack already empty.\n";else{st.pop(temp);cout << "Customer " << temp.fullname << " been delete!\n";st.show();}break;}cout << "Please enter A to add a customer name and payment,\n"<< "D to delete a customer,or Q to exit.\n";}cout << "Bye!\n";return 0;}
c++ Primer Plus(习题10.6)

/*10.6头文件*/#pragma once#ifndef MOVE_H#defineMOVE_Hclass Move {private:double x;double y;public:Move(double a = 0, double b = 0);  //默认构造函数void showmove()const;Move add(const Move &m)const;      //返回一个Move对象,且要加上提供的m对象的值void reset(double a = 0, double b = 0);};#endif // !MOVE_H
#include"move.h"#include<iostream>Move::Move(double a, double b){x = a;y = b;}Move Move::add(const Move &m)const{Move temp;              //使用一个临时的对象,不知道可不可以temp.x+=x+m.x;temp.y= y+m.y;return temp;}void Move::showmove()const{std::cout << "X=" << x<< "  Y= " << y << std::endl;}void Move::reset(double a,double b){x = a;y = b;}
/*简单的类的使用*/#include<iostream>#include"move.h"int main(){using namespace std;Move a,c;Move b = Move(1.1, 2.5);c=a.add(b);cout << "b source:  ";b.showmove();cout << "After reset:  ";b.reset();//重置b的x和y为零b.showmove();cout << "a source :";a.showmove();cout << "c is a+b :";c.showmove();return 0;}


c++ Primer Plus(习题10.7)

#pragma once#pragma execution_character_set("utf-8")//本文件为utf-8编码格式#ifndef PLOGE_H#define FLOGE_Hclass Ploge{private:enum { LQ = 19 };char name[LQ];int ci;public:Ploge(const char *n = "Plorg", int num = 0);//默认构造函数void setCI(int num);~Ploge() {};void show()const;};#endif // !PLORG_H

#include<iostream>#include"ploge.h"Ploge::Ploge(const char *nm, int num){std::strcpy(name, nm);ci = num;}void Ploge::setCI(int num)//重新设置CI的值{ci = num;}void Ploge::show()const{using std::cout;using std::endl;cout << "Name: " << name << endl<< "CI: " << ci << endl;}

#include<iostream>#include"ploge.h"int main(){using namespace std;Ploge a;a.show();Ploge b("Saw", 100);a = b;a.show();b.setCI(0);b.show();return 0;}


c++ Primer Plus(习题10.8)

//#pragma once#pragma execution_character_set("utf-8")//本文件为utf-8编码格式#ifndef LISTCLASS_H#define LISTCLASS_Htypedef int Item;class List{private:static const int NUM=5;Item items[NUM];int couts;//计数public:List(int couts = 0);//默认为空列表int report() { return couts; }bool push(Item &item);bool  pop(Item &item);bool isempty() { return couts == 0; }bool isfull() { return couts == NUM; }void visit(void(*pf)(Item &items));//用函数指针};void show(Item &items);//对每一项进行显示void change(Item &items);//对每一项进行重置#endif // !LISTCLASS_H

#include<iostream>#include"listclass.h"//类外的函数void show(Item &items){std::cout << items<<"  ";}void change(Item &items){using std::cin;using std::cout;using std::endl;cout << "\nEnter change value for every items: ";Item temp;cin >> temp;items = temp;cout << "Change success!\n";}//类的方法List::List(int a){couts = 0;}bool List::push(Item &item){if (isfull()){std::cout << "List is full!\n";return false;}items[couts++] = item;std::cout << item << " push in list.\n";return true;}bool List::pop(Item &item){if (isempty()){std::cout << "List is empty!\n";return false;}item=items[--couts];std::cout << item << " pop out list.\n";return true;}void List::visit(void(*pf)(Item &items)){for (int i = 0; i < couts; i++)pf(items[i]);//调用函数}

//这题要自己设计类,参考了别人的代码,本人也有点迷惑//这是实现文件,成功在此//一看别人的代码,思想也跟着别人走了,这很搞笑#include<iostream>#include"listclass.h"int main(){using namespace std;int i = 0;List a;cout << "Test isempty.\n";if (a.isempty())cout << "OK!\n";elsecout << "Try again.\n";List b;int temp[5];while (!b.isfull()){cout << "Enter #" << i+1 << " item:";cin >> temp[i];b.push(temp[i]);i++;}b.visit(show);b.visit(change);b.visit(show);//下面的仅供娱乐cout << "Extra test,for pop method.\n ";i = 0;while (!b.isempty()){b.pop(temp[i]);i++;}cout << "Test complete!\n";return 0;}

快过年年了,还有六天,要把旧的知识和习题清理一下,准备新课程

0 0
原创粉丝点击