list
来源:互联网 发布:掐指算法 编辑:程序博客网 时间:2024/06/08 06:15
Polynomial addition
#include <iostream>#include <list>#include <string>using namespace std;class Term{private: int coef; int exp;public: Term(int coef_, int exp_):coef(coef_),exp(exp_){} int getCoef(){return coef;} int getExp(){return exp;} bool operator<(Term& t){ return exp < t.getExp(); } void show(){ cout<<coef<<'x'<<exp; }};class Polynomial{private: list<Term> p;public: void addTerm(const Term& t){ p.push_back(t); } Polynomial addPolynomial(Polynomial& o){ Polynomial rs; p.sort(); o.p.sort(); auto src = p.begin(); auto des = o.p.begin(); int coef = 0; int exp = 0; while((src!=p.end())&&(des!=o.p.end())){ int tempexp1 = (*src).getExp(); int tempexp2 = (*des).getExp(); if (tempexp1==tempexp2){ coef = (*src).getCoef() + (*des).getCoef(); exp = tempexp1; src++; des++; } else if (tempexp1<tempexp2){ coef = (*src).getCoef(); exp = tempexp1; src++; } else if (tempexp1>tempexp2){ coef = (*des).getCoef(); exp = tempexp2; des++; } Term temp_t(coef, exp); rs.p.push_back(temp_t); } if (src!=p.end()){ list<Term> temp_p(src, p.end()); rs.p.splice(rs.p.end(), temp_p); } if (des!=o.p.end()){ list<Term> temp_p(des, o.p.end()); rs.p.splice(rs.p.end(), temp_p); } return rs; } void printPolynomial(){ auto itr = p.begin(); while (itr!=p.end()){ itr->show(); if((++itr)!=p.end()){ cout<<'+'; } } cout << endl; }};int main(){ Term t1(2, 6); Term t2(3, 4); Term t3(5, 2); Term t4(6,0); Polynomial p1; Polynomial p2; Polynomial p3; p1.addTerm(t1); p1.addTerm(t2); p2.addTerm(t1); p2.addTerm(t2); p2.addTerm(t3); p2.addTerm(t4); p3 = p1.addPolynomial(p2); p3.printPolynomial(); return 0;}
Some basic operation for stl list.
#include<iostream>#include<list>using namespace std;template <class T>void print(T& l){ typedef typename T::iterator Titerator; Titerator itr = l.begin(); while(itr!=l.end()){ cout << *itr << endl; itr++; }}int main(){ list<int> l1; l1.push_back(1); l1.push_back(5); l1.push_back(3); l1.push_back(10); l1.sort(); list<int> l2; l2.push_back(2); l2.push_back(8); l2.push_back(6); l2.push_back(9); l2.sort(); l1.merge(l2); cout << "List 1: " << endl; print<list<int> >(l1); cout << "List 2: " << endl; print<list<int> >(l2);}// another example#include<iostream>#include<list>#include<string>using namespace std;class Student{private: string NO; string Name; string University; int Score;public: Student(string NO_,string Name_,string Univ_,int Score_) :NO(NO_),Name(Name_),University(Univ_),Score(Score_){} string getNO(){ return NO; } string getName(){ return Name; } string getUniv(){ return University; } int getScore(){ return Score; } void printInfor(){ cout<<NO<<"\t"<<Name<<"\t"<<University<<"\t"<<Score<<endl; } bool operator<(Student& s){ return NO < s.getNO(); } bool operator==(Student& s){ return NO == s.getNO(); }};class System {private: list<Student> s;public: void add(const Student& s_){ s.push_back(s_); } void merge(System sys){ s.sort(); sys.s.sort(); s.merge(sys.s); s.unique(); } void show(){ list<Student>::iterator itr = s.begin(); while(itr!=s.end()){ itr->printInfor(); itr++; } }};int main(){ System sm1; System sm2; Student s1("1001", "Tim", "U1", 670); Student s2("1002", "Stone", "U2", 650); Student s3("1003", "Sam", "U3", 660); Student s4("1004", "David", "U4", 640); Student s5("1005", "Jone", "U5", 630); sm1.add(s1); sm1.add(s2); sm1.add(s5); sm2.add(s5); sm2.add(s4); sm2.add(s3); sm2.add(s1); sm1.merge(sm2); sm1.show(); return 0;}
0 0
- List
- list
- List
- LIST
- list
- list
- List
- list
- list
- List
- list
- LIST
- List
- List
- List
- list
- list
- list
- BZOJ 2947 Poi2000 促销 set
- js获取url参数值的两种方式详解
- sendToTarget与sendMessage
- 项目四 用类解决关于图书馆的书的问题
- Problem F 字符串替换
- list
- HDU 2795 Billboard(单点更新)
- Dos下常用的网络相关的命令
- 【Java二十周年】Java注解处理器
- CVTE实习应聘经验
- Android常见面试题汇集
- 【黑马程序员】---Foundation框架总结01
- Miscosoft Visual Studio项目guid取值
- 高仿优酷Android客户端图片左右滑动(自动切换)