diff --git a/Course-3.cpp b/Course-3.cpp new file mode 100644 index 00000000..8979f3ce --- /dev/null +++ b/Course-3.cpp @@ -0,0 +1,37 @@ +//求一元二次方程的根 +// 例如 :y = x2 + 10x + 17 +// 当 x = 2, 求 y = 41 +// 当 y = 2, 求 x = ? +#include +#include +float mysqrt(float n) { + // how to guess + float x = n/2; + while(...) { + if (x*x > n) + x = x/2; + else + x = (x+n)/2; + } + return 0.0f; +} +int main() { + float x1, x2; + float y, a, b, c, temp; + printf("Please input \n"); + scanf("%f %f %f %f", &y, &a, &b, &c); + c = c - y; + temp = b*b - 4* a*c; + if(temp >= 0) { + temp = sqrt(temp); + a = 2 * a; + x1 = (-b + temp)/(a); + x2 = (-b - temp)/(a); + printf("Results \n"); + printf("%f\n", x1); + printf("%f\n", x2); + } + else + printf("No Results \n"); + return 0; +} diff --git a/Eigen/plantuml b/Eigen/plantuml new file mode 100644 index 00000000..c081c426 --- /dev/null +++ b/Eigen/plantuml @@ -0,0 +1,16 @@ +@startuml + +PlainObjectBase <|-- Matrix +PlainObjectBase <|-- Array + +MatrixBase <|-- PlainObjectBase +ArrayBase <|-- PlainObjectBase + +DenseBase <|-- MatrixBase +DenseBase <|-- ArrayBase + +DenseCoeffsBase <|-- DenseBase + +class EigenBase <> +EigenBase <|-- DenseCoeffsBase +@enduml \ No newline at end of file diff --git a/FCdemo.cpp b/FCdemo.cpp new file mode 100644 index 00000000..c443973e --- /dev/null +++ b/FCdemo.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +// -std=c++11 +using namespace std; +//constant +class Person +{ +public: + Person(string lastname, string firstname) + : _firstname(firstname), _lastname(lastname) + { + } + string firstname() const + { + return _firstname; + } + string lastname() const + { + return _lastname; + } +private: + const string _firstname = nullptr; + const string _lastname = nullptr; +}; + +class PersonSortCriterion +{ +public: + bool operator()(const Person &p1, const Person &p2) const + { + return p1.lastname() < p2.lastname() || + (p1.lastname() == p2.lastname() && p1.firstname() < p2.firstname()); + } +}; +class PersonSortCriterion1 +{ +public: + bool operator()(const Person &p1, const Person &p2) const + { + return p1.lastname() > p2.lastname() || + (p1.lastname() == p2.lastname() && p1.firstname() > p2.firstname()); + } +}; + +int main(void) +{ + set coll; + //unordered_set coll; + Person p1("yu", "g"); + Person p2("an", "yu"); + Person p3("xiao", "ming"); + Person p4("xi", "biaoge"); + Person p5("u", "ong"); + coll.insert(p1); + coll.insert(p2); + coll.insert(p3); + coll.insert(p4); + coll.insert(p5); + for (auto i : coll) + { + cout << i.lastname() << ", " << i.firstname() << endl; + } + + return 0; +} diff --git a/HelloWorld.c b/HelloWorld.c index 3c79b6c5..2b1aa1e7 100644 --- a/HelloWorld.c +++ b/HelloWorld.c @@ -1,7 +1,23 @@ #include int main() { - printf("Hello World!\n"); - -//try Jenkins again + int x, y, z, temp; + // մӴС + scanf("%d %d %d", &x, &y, &z); + if ( y < z ) { + temp = y; + y = z; + z = temp; + } + else if ( x < y ) { + temp = x; + x = y; + y = temp; + } + else if ( y < z ) { + temp = y; + y = z; + z = temp; + } + printf("%d, %d, %d\n", x, y, z); return 0; } diff --git a/Mixins.java b/Mixins.java new file mode 100644 index 00000000..d5773143 --- /dev/null +++ b/Mixins.java @@ -0,0 +1,72 @@ +/** + * 组合模式 + * 在 TaggableImpl 中实现打标签的逻辑 + * Post 类和 TaggableImpl 类都实现 Taggable 接口 + * Post 类中创建一个 TaggableImpl 实例并在实现 Taggable 时将相应方法调用委托过去。 + */ + + +import java.util.List; +import java.util.ArrayList; + +interface Entity { + public int getId(); + public int getKind(); +} + +interface Taggable { + public void addTag(int tagId); + public List getTags(); +} + +class TaggableImpl implements Taggable { + private Entity target; + + public TaggableImpl(Entity target) { + this.target = target; + } + + public void addTag(int tagId) { + int id = target.getId(); + int kind = target.getKind(); + System.out.println("insert into ... values " + + id + ", " + + kind + ", " + + tagId + ")"); + } + + public ArrayList getTags() { + // query from database + return new ArrayList(); + } +} + +class Post implements Entity, Taggable { + public final static int KIND = 1001; + + private Taggable taggable; + private int id; + private String title; + + public Post(int id, String title) { + this.id = id; + this.title = title; + this.taggable = new TaggableImpl(this); + } + + public int getId() { + return id; + } + + public int getKind() { + return KIND; + } + + public void addTag(int tagId) { + taggable.addTag(tagId); // delegate + } + + public ArrayList getTags() { + return taggable.getTags(); // delegate + } +} \ No newline at end of file diff --git a/Operator.cpp b/Operator.cpp new file mode 100644 index 00000000..96ef7620 --- /dev/null +++ b/Operator.cpp @@ -0,0 +1,45 @@ +#include +class Parent { +public: + Parent(int n) { + flag = n; + std::cout << "Parent()" << flag << std::endl; + } + virtual ~Parent(){ + std::cout << "~~~Parent()" << flag << std::endl; + } + //other methods???? + void show(); +private: + //attributes?? + int flag; +}; +class Child : public Parent { +public: + Child(int n):Parent(n) { + flag = n; + std::cout << "Child()" << n << std::endl; + } + virtual ~Child(){ + std::cout << "~~~Child()" << flag << std::endl; + } + //other methods???? + //function object + void operator() (int n) { + } +private: + //attributes?? + int flag; + void operator= (const Child &c) { + std::cout << "operator = " << flag << std::endl; + } +}; +int main() { + Parent *p; + p = new Child(1); + Child c(2); + //c = Child(3); + //p->?????(); + delete p; + return 0; +} diff --git a/PQPrime.c b/PQPrime.c new file mode 100644 index 00000000..722c6409 --- /dev/null +++ b/PQPrime.c @@ -0,0 +1,2 @@ +// 求 p 和 q +// 当 p 和 q 都是素数,求 p 和 q \ No newline at end of file diff --git a/PQPrime.md b/PQPrime.md new file mode 100644 index 00000000..4ddcdc36 --- /dev/null +++ b/PQPrime.md @@ -0,0 +1,3 @@ +$p^q + q^p$是一个素数, +$p$和$q$也是素数, +求$p$和$q$ \ No newline at end of file diff --git a/Plan.md b/Plan.md new file mode 100644 index 00000000..de37878f --- /dev/null +++ b/Plan.md @@ -0,0 +1,81 @@ +# 8th Week +- OOD:矩阵 + - 方法:加、减、点乘、叉乘 + - 属性:随便 +- 动作:学生讲解作业(属于评估) + +## Activities +1. 学生讲解矩阵代码 +2. 分析矩阵的属性定义:数组,容器,Eigen实现 +3. Eigen的继承:Curiously recurring template pattern (CRTP),奇特的递归模板模式 + 1. 颠倒继承(Upside Down Inheritance),它的效果与“传统继承是通过派生类向基类添加功能”的应用正好相反的。 + 2. 不使用虚函数但是达到了多态的效果,大量应用于各种库中,比如Eigen库,WTL库。它没有虚函数的开销。 +4. 展示Eigen中Matrix的数据结构:实际上展示不下去,没法做 +5. 实际中看文档,读代码,照样使用Eigen库编程。 +6. 通过读源代码,学习一些新技术。 + +## 结果 +自我感觉效果好,交互太少,以上内容根本没讲完。CRTP只讲了开头。 + +# 9th Week + +作业:在上周矩阵的基础上,计算欧氏距离,周末再给。 + +周末忘记给作业,学生说没时间做。 + +CRTP的多态效果,与标准的多态比较。 + +Matrix的运算符重载,特别是()的重载。 + +## Activities +1. 多态的演示代码。优缺点。 +2. CRTP的演示代码,实现相同的多态。扩展static_cast +3. Eigen库中CRTP应用。 +4. Matrix中的运算符重载。 +5. ()的重载 + +## 结果 +课程的内容有点混乱。 + +# 10th Week + +作业:匈牙利算法 + +本周日补五一的课程,计划有点混乱了。 + +有个学生跑别的班听基础内容了,好像我讲得内容有点超越学生的认知了。 + +## Activities +1. Eigen里代码的继承关系与展示,Matrix 和 Array +2. 先找Eigen文档里关于 Matrix 和 Array 的说明 +3. 清理 Eigen 的源代码,Copy 出来单独展示与说明 +4. 画一个 PlantUML 的简要模型 + +# 11th Week + +多重继承与 Mix in-class 编程 + +## Activities + +1. C++的多重继承代码展示 +2. Java版本的Mixins模式代码展示 + +## 结果 + +估计同学们没有听懂 + +# 12th Week + +作业:把Java版本的Mixins写成C++版本的。 + +## Activities + +1. 讲解C++版本的Mixins代码 +2. 将C++的多重继承重构为模板类的继承 + 1. Persons作为Students的基类,具有学习的能力。Students + 2. Faculties作为Persons的子类,具有教书的能力。Faculties + 3. TAs本质是学生,具有Faculties的教书能力。Faculties> +3. 将CRTP的设计应用 + 1. Persons就是Students。 + 2. Persons就是Faculties。 + 3. Persons也是TAs,Students就是TAs,TAs也是Faculties。 \ No newline at end of file diff --git a/README.md b/README.md index 942a6b1b..48be79d5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# hello-world +# Hello-World C/C++编程初学者 # Git手册 @@ -17,4 +17,4 @@ git log --author="$(git config --get user.name)" --pretty=tformat: --numstat | g git shortlog -s ``` -[Code Reference](https://github.com/WangShouDao/count_github_push) \ No newline at end of file +[Code Reference](https://github.com/WangShouDao/count_github_push) diff --git a/StaticDemo.cpp b/StaticDemo.cpp new file mode 100644 index 00000000..9c3ceaa5 --- /dev/null +++ b/StaticDemo.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +class Person +{ +public: + string firstname() const + { + return _firstname; + } + string lastname() const + { + return this->_lastname; + } + static int howmany() { + return counter; + } + static Person *create() { + return new Person("aa", "bb"); + } +private: + Person(string lastname, string firstname) + : _firstname(firstname), _lastname(lastname) + { + counter ++; + } + const string _firstname = nullptr; + const string _lastname = nullptr; + static int counter; +}; +int Person::counter = 0; +int main(void) +{ + Person *p1 = Person::create(); + Person p2("ou", "you"); + //how many person? + std::cout << Person::howmany() << std::endl; + cout << p1.howmany() << std::endl; + cout << p2.howmany() << std::endl; + Person p4("xi", "biaoge"); + + return 0; +} + diff --git a/Student.cpp b/Student.cpp new file mode 100644 index 00000000..5eed701a --- /dev/null +++ b/Student.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +using namespace std; +class Student { +public: + Student(string &fn, string &ln, char s):sex(s) { + firstname = fn; + lastname = ln; + gpa = 0.0f; + grade = 1; + } + Student(const char fn[], const char ln[], char s):firstname(fn), lastname(ln), sex(s) { + gpa = 0.0f; + grade = 1; + } + ~Student() { + } + //method + void show_myself() { + std::cout << firstname << lastname << sex << grade << gpa << std::endl; + } + void study_time(int study_time) { + gpa = gpa + log(study_time); + gpa = gpa > 4.0f ? 4.0f : gpa; + } + int upgrade() { + int tmp = this->grade; + this->grade += 1; + this->grade = grade > 4 ? 4 : grade; + return tmp; + } +private: + float gpa; + std::string firstname, lastname; + char sex; + int grade; +}; + +int main() { + char buffer[5][100]={{"Mike", "Barnes", 'M'},{"Jim", "Nickerson", 'F'}} + string fn("Mike"), ln("Barnes"); + Student st1(fn, ln, 'M'), st2("Jim", "Nickerson", 'F'); + std::vector student_list; + student_list.push_pack(st1); + st1.upgrade(); + st1.study_time(60); + st2.study_time(1000); + st1.show_myself(); + st2.show_myself(); + return 0; +} diff --git a/SuperPrime.c b/SuperPrime.c index c418b363..ed445ae2 100644 --- a/SuperPrime.c +++ b/SuperPrime.c @@ -1,5 +1,6 @@ //超级素数:它本身,各位数字的和,各位数字的平方和,都是素数。 //求 100~10000 内的所有超级素数,及它们的平均数。 +// 2021-10-29 int isPrime(int x); int splitNum(int x, int num[]); diff --git a/SuperPrime/Prime.cpp b/SuperPrime/Prime.cpp new file mode 100644 index 00000000..884d8fec --- /dev/null +++ b/SuperPrime/Prime.cpp @@ -0,0 +1,97 @@ +#include"Prime.h" +#include + +Prime::Prime(int low , int high){ + for(int i = low ; i <= high ; i++){ + if(judgePrime(i)){ + p.push_back(i);//push进容器中 + } + } +} + +void Prime::showPrime(){ + for(int i = 0 ; i < p.size() ; i++){ + cout << p[i] << " ";//循环输出 + } + cout << endl; +} + +int Prime::getSize(){ + return p.size();//获取个数 +} + +//int *Prime::getPrime(){ +// int *arr = new int[p.size()];//动态分配数组以存储素数 +// for(int i = 0 ; i < p.size() ; i++){ +// arr[i] = p[i]; +// } +// return arr; +//} + +superPrime::superPrime(int low , int high){ + findPrime(low, high); +} + +//void superPrime::findsuperPrime(int low , int high){ +// Prime get;//创建一个实例 +// get.findPrime(low , high); +// int *super = get.getPrime();//获得存有素数的数组 +// for(int i = 0 ; i < get.getSize() ; i++){ +// if(judgeSuperPrime(super[i])){//判断是否为超级素数 +// sp.push_back(super[i]); //push进vector +// } +// } +// delete super; +//} + +void superPrime::findPrime(int low , int high){ + vector prime; + for(int i = low ; i <= high ; i++){ + if(Prime::judgePrime(i)){ + prime.push_back(i);//push进容器中 + } + } +// for(int i = 0 ; i < prime.size() ; i++){ +// cout << prime[i] << " "; +// } +// cout << prime.size() << endl; + for(int i = 0 ; i < prime.size() ; i++){ + if(superPrime::judgePrime(prime[i])){ + sp.push_back(prime[i]); + } + } +} + +void superPrime::showPrime(){ + for(int i = 0 ; i < sp.size() ; i++){ + cout << sp[i] << " "; + } + cout << endl; +} + +bool Prime::judgePrime(int a){ + if(a == 2){//2单独判断 + return true; + } + for(int i = 2 ; i <= sqrt(a) + 1 ; i++){ + if(a % i == 0){ + return false; + } + } + return true; +} + +bool superPrime::judgePrime(int a){ + int eve[20] = {0} , i = 0 , sum = 0 , largesum = 0;//sum为各位数的和,largesum是各位数平方和,i是脚标 + while(a){//循环提取各位数 + eve[i] = a % 10; + sum += eve[i]; + largesum += eve[i] * eve[i]; + i++; + a /= 10; + } + if(Prime::judgePrime(sum) && Prime::judgePrime(largesum) && Prime::judgePrime(a)){ + return true; + } + return false; +} diff --git a/SuperPrime/Prime.h b/SuperPrime/Prime.h new file mode 100644 index 00000000..3997ef95 --- /dev/null +++ b/SuperPrime/Prime.h @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +class Prime{ + public: + Prime(int low , int high);//构造函数 + Prime(int n); + bool judgePrime(int);//判断一个数是否为素数 + virtual bool isPrime(); + void showPrime();//打印出所选范围内的所有素数,空格分隔 + int getSize();//获取素数个数 +// int *getPrime();//将vector中的素数放入一个数组并返回首地址,以供superPrime使用 + protected: + vector p;//容器,存放素数 +}; + +class superPrime : public Prime { + public: + superPrime(int low , int high);//构造函数 + superPrime(int n); + virtual bool isPrime(); + private: + bool judgePrime(int);//判断是否为超级素数 + void findPrime(int , int);//找素数,放入vector +}; + + diff --git a/SuperPrime/README.md b/SuperPrime/README.md new file mode 100644 index 00000000..5d15aa35 --- /dev/null +++ b/SuperPrime/README.md @@ -0,0 +1,14 @@ +- main.cpp 程序的实现代码 +- Prime.h 素数和超级素数的声明代码 +- Prime.cpp 素数和超级素数的实现代码 +- SP.wsd PlantUML的脚本 + + +## [PlantUML](https://plantuml.com/zh/) +PlantUML是以脚本方式编写UML的工具。 + +参考 *SP.wsd*,官网上有详细的文档说明。 + +可以在VSCode中安装PlantUML插件 + +也可以直接访问[在线编辑器](https://www.plantuml.com/plantuml/uml/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000) \ No newline at end of file diff --git a/SuperPrime/SP.wsd b/SuperPrime/SP.wsd new file mode 100644 index 00000000..8a7fd638 --- /dev/null +++ b/SuperPrime/SP.wsd @@ -0,0 +1,14 @@ +@startuml +class Prime { + +bool judgePrime() + +void showPrime() + +int getSize() + #vector p +} +class SuperPrime { + +bool judgePrime() + +void showPrime() + -void findPrime(int,int) +} +Prime <|-- SuperPrime +@enduml \ No newline at end of file diff --git a/SuperPrime/main.cpp b/SuperPrime/main.cpp new file mode 100644 index 00000000..a90b3936 --- /dev/null +++ b/SuperPrime/main.cpp @@ -0,0 +1,59 @@ +#include"Prime.cpp" + +template +void func(const Type &a) +{ + //a.findPrime(100 , 999); + cout << "素数为:" << endl; + a.showPrime(); +} + +void func(Prime *p) +{ + cout << "素数为:" << endl; + p->showPrime(); +} +void prime(int a, int b) +{ + std::vector ps; + for (int i = 100; i < 999; ++i) { + Prime * p = new Prime(i); + if (p->isPrime()) + ps.push_back(p); + else + delete p; + } + for (Prime *p : ps) { + p->showPrime(); + delete p; + } +} +void superprime(int a, int b) +{ + std::vector ps; + for (int i = 100; i < 999; ++i) { + Prime * p = new SuperPrime(i); + if (p->isPrime()) + ps.push_back(p); + else + delete p; + } + for (Prime *p : ps) { + p->showPrime(); + delete p; + } +} +int main(){ + + Prime pm(100, 999); + func(pm); + func(&pm); + + SuperPrime sp(100, 999); + func(sp); + func(&sp); + + prime(100, 999); + superprime(100, 999); + return 0; +} diff --git a/SuperPrime-HW2.cpp b/SuperPrime_HW2.cpp similarity index 50% rename from SuperPrime-HW2.cpp rename to SuperPrime_HW2.cpp index 9f16866e..ec1c77f3 100644 --- a/SuperPrime-HW2.cpp +++ b/SuperPrime_HW2.cpp @@ -16,33 +16,70 @@ class Nature { ~Nature() { std::cout << "Destroy Nature as " << num << std::endl; } + bool isPrime() { + if(num == 1 || num == 0) + return false; + for(int i = 2; i <= (int)sqrt(num); i++) + { + if(num % i == 0) + return false; + } + return true; + } + int compare(const Nature &nat) { + if (num > nat.num) + return 1; + else if(num == nat.num) + return 0; + + return -1; + } +private: +}; +class SuperPrime : public Nature { +private: + int num; +public: + SuperPrime(int n):num(n) { + } + bool isPrime() { + Nature nat(num); + return nat.isPrime(); + } + int compare(const SuperPrime &nat) { + if (num > nat.num) + return 1; + else if(num == nat.num) + return 0; + + return -1; + } }; -class SuperPrime { +class Container { private: - std::vector natures; + std::vector natures; public: - SuperPrime(int a, int b) { + Container(int a, int b) { std::cout << "Create SuperPrime from " << a << " to " << b << std::endl; for(int i = a; i < b; i++) { - Nature nat(i); + SuperPrime nat(i); std::cout << "HAHA" << std::endl; - natures.push_back(nat); + if(nat.isSuperPrime()) + natures.push_back(nat); std::cout << "DDDDD" << std::endl; } } - ~SuperPrime() { + ~Container() { std::cout << "Destroy SuperPrime " << std::endl; } - Nature max() { - std::vector::iterate it = natures.begin(); + SuperPrime max() { + std::vector::iterate it = natures.begin(); Nature max(0); for(; it != natures.end(); it ++) { - if(it->isSuperPrime()) { - if (max.compare(*it)) { + if (max.compare(*it)) { max = *it; - } - } + } } return max; } diff --git a/SuperPrime_HW3.cpp b/SuperPrime_HW3.cpp new file mode 100644 index 00000000..9a9f3a2c --- /dev/null +++ b/SuperPrime_HW3.cpp @@ -0,0 +1,58 @@ +#include +#include +class Nature { +protected: + int num; +public: + Nature(int n) { + num = n; + } + virtual bool isPrime() { + for(int i = 2; i < num;i++) { + if(num % i == 0) + return false; + } + return true; + } + void show() { + std::cout << num << std::endl; + } +}; +class SuperPrime:public Nature { +private: +public: + SuperPrime(int n):Nature(n) { + } + bool isPrime() { + int tmp = num + 1; + int sum = 0, mul = 1, sqr = 0; + while(tmp != 0) { + tmp = tmp / 10; + int x = tmp % 10; + sum += x; + mul *= x; + sqr += x*x; + } + Nature nat(sum), nat1(mul), nat2(sqr); + return Nature::isPrime() && nat.isPrime() && nat1.isPrime()&& nat2.isPrime(); + } +}; +int main() { + //create some objects + std::vector sps; + for (int i = 100; i < 999; i++) { + Nature *nat = new SuperPrime(i); + if (nat->isPrime()) + sps.push_back(nat); + } + std::vector::iterator it; + for(it=sps.begin(); it!=sps.end();it++) { + (*it)->show(); + } + for(it=sps.begin(); it!=sps.end();it++) { + delete *it; + } + + //ij + return 0; +} diff --git a/convolution.c b/convolution.c new file mode 100644 index 00000000..bdb092aa --- /dev/null +++ b/convolution.c @@ -0,0 +1,4 @@ +// 卷积(Convolution)是当前神经网络的基础算法 +// 卷积的原理不复杂,网上有大量资料和实现,包括C语言实现 +// 大家自行设计一个卷积函数,输入一个向量和卷积核,输出卷积的结果 +// 2021-11-21 \ No newline at end of file diff --git a/google.c b/google.c index 78198f79..ed0baba4 100644 --- a/google.c +++ b/google.c @@ -1,4 +1,5 @@ //f(n)函数的定义为0~n中包含的1的个数 //f(13)是0~13中包含1的有 1,10,11,12,13,所以f(13)=6 //f(1) = 1。 -//求另一个f(n)=n的n值 \ No newline at end of file +//求另一个f(n)=n的n值 +// 2021-11-04 \ No newline at end of file diff --git a/homework.md b/homework.md new file mode 100644 index 00000000..4847e1d7 --- /dev/null +++ b/homework.md @@ -0,0 +1,2 @@ +有一个数组,将满足条件的元素删除,然后将后面的元素前移,使元素保持依序排列。 +比如 1 3 5 3 8 ? ?,删除 3,最后得到 1 5 8 ? ? ? ? \ No newline at end of file diff --git a/leapyear1.cpp b/leapyear1.cpp new file mode 100644 index 00000000..fd521533 --- /dev/null +++ b/leapyear1.cpp @@ -0,0 +1,16 @@ +#include +int main() +{ + int year; + scanf("%d", &year); + if (year % 4 == 0) + if (year % 100 == 0) //Refine ع + printf("%d 겻\n", year); + else if (year % 400 == 0) + printf("%d \n", year); + else + printf("%d 겻\n", year); + else + printf("%d 겻\n", year); + return 0; +} diff --git a/multi-inheritance.cpp b/multi-inheritance.cpp new file mode 100644 index 00000000..40d7af37 --- /dev/null +++ b/multi-inheritance.cpp @@ -0,0 +1,95 @@ +#include +using namespace std; + +template +class Person { + // Data members of person +public: + Person(int x) { cout << "Person::Person(int ) called" << endl; } + void learn() { + static_cast(*this).learn(); + } + void teach() { + static_cast(*this).teach(); + } +}; + +template +class Faculty : public Person { + // data members of Faculty +public: + Faculty(int x):Person(x) { + cout<<"Faculty::Faculty(int ) called"<< endl; + } + void learn() { + cout << "Faculty learn sth." << endl; + } + void teach() { + cout << "Faculty teach sth." << endl; + } +}; + +template +class Student : public Person { + // data members of Student +public: + Student(int x):Person(x) { + cout<<"Student::Student(int ) called"<< endl; + } + void learn() { + cout << "Student learn sth." << endl; + } + void teach() { + cout << "Student can't teach." << endl; + } +}; +/* +template +class TA : public T1 { + TA(int x):T1(x) { + cout << "TA::TA(int ) called" << endl; + } + void learn() { + T1::learn(); + } + void teach() { + static_cast(*this).teach(); + } + // TA constraints +}; +*/ + +class TA : public Person { +public: + TA(int x):st(x), fa(x) { + cout << "TA::TA(int ) called" << endl; + } + void learn() { + st.learn(); + } + void teach() { + fa.teach(); + } +private: + Student st; + Faculty fa; +}; +class TA : public Student, public Faculty { +public: + TA(int x):Student(x), Faculty(x) { + cout << "TA::TA(int ) called" << endl; + } + void learn() { + Student::learn(); + } + void teach() { + Faculty::teach(); + } +}; + +int main() { + TA ta1(30); + //TA ta1(30); + ta1.learn(); // Student's learn + ta1.teach(); // Faculty's teach +} \ No newline at end of file diff --git a/resolutions.c b/resolutions.c index a159db27..e987807d 100644 --- a/resolutions.c +++ b/resolutions.c @@ -1,10 +1,10 @@ -//选作题 //输入一个字符串,形如 //3072x1728,2720x1536,2560x1600,2560x1440,2304x1728,2048x1536,1920x1200,1920x1080,1440x900,1280x800,1280x720,1024x768,1024x640,800x600,800x500,800x450,640x480,640x400,640x360,480x360,480x300,480x270,320x240,320x200,320x180,240x180,176x144,160x120,160x100,160x90 //这是一个显示器支持的分辨率清单,中间逗号分隔。 //现在输入画面的宽和高,然后在上述清单中挑选一个最接近的分辨率输出 //比如输入2000,1000,输出的分辨率应该是1920x1080,因为其它分辨率的误差更大。 //完成如下程序 +// 2021-11-14 #include int main() { @@ -13,6 +13,7 @@ int main() scanf("%s", ress); scanf("%d %d", &x, &y); + // 学习一下 sscanf 可以方便处理 ress 字符串 printf("选中的分辨率是%s\n", "???"); return 0; diff --git a/round-robin.cpp b/round-robin.cpp new file mode 100644 index 00000000..6aaa9d1e --- /dev/null +++ b/round-robin.cpp @@ -0,0 +1,93 @@ +#include +#include +int main() { + int n, i; + char teams[1000][50]; // ά + int score[1000]={0}, pure[1000]={0}; + int champin[1000]; + // + scanf("%d", &n); + for(i = 0; i < n; i+=1) + scanf("%s", teams[i]); + gets(teams[999]); + for(i = 0; i < n*(n-1)/2; i+=1) { + char match[1000]; + char t1[50], t2[50]; + int s1, s2, j = 0; + int t1_index, t2_index; + //scanf("%s", match); + gets(match); + printf("%s\n", match); + while(match[j] != 0) { + if (match[j] == '-' || match[j] == ':') + match[j] = ' '; + j += 1; + } + // printf("%s\n", match); + // ϵۻֺ;÷ + sscanf(match, "%s %s %d %d", t1, t2, &s1, &s2); + printf("%s vs. %s == %d vs. %d \n", t1, t2, s1, s2); + for(j = 0; j < n; j += 1) { + if (strcmp(teams[j], t1) == 0) + t1_index = j; + if (strcmp(teams[j], t2) == 0) + t2_index = j; + } + if (s1 > s2) { + score[t1_index] += 3; + score[t2_index] += 0; + pure[t1_index] += s1 - s2; + pure[t2_index] -= s1 - s2; + } + else if (s1 == s2) { + score[t1_index] += 1; + score[t2_index] += 1; + pure[t1_index] += 0; + pure[t2_index] += 0; + } + else { + score[t1_index] += 0; + score[t2_index] += 3; + pure[t2_index] += s2 - s1; + pure[t1_index] -= s2 - s1; + } + } + for(i = 0; i < n; i += 1) { + //printf("%s, %d, %d\n", teams[i], score[i], pure[i]); + champin[i] = i; + } + for(i = 0; i < n-1; i+=1) { + for(int j = i+1; j < n; j+=1){ + if (score[champin[i]] < score[champin[j]]) { + int temp = champin[i]; + champin[i] = champin[j]; + champin[j] = temp; + } + else if (score[champin[i]] == score[champin[j]]) { + if (pure[champin[i]] < pure[champin[j]]) { + int temp = champin[i]; + champin[i] = champin[j]; + champin[j] = temp; + } + else if (pure[champin[i]] == pure[champin[j]]) { + //??? + } + } + } + } + int seq = 0; + for(i = 0; i < n; i += 1) { + //printf("%s, %d, %d\n", teams[i], score[i], pure[i]); + if (i == 0) { + printf("%s, %d\n", teams[champin[i]], seq);//score[champin[i]], pure[champin[i]]); + continue; + } + if (score[champin[i]] == score[champin[i-1]] && pure[champin[i]] == pure[champin[i-1]]) + printf("%s, %d\n", teams[champin[i]], seq);//score[champin[i]], pure[champin[i]]); + else { + seq += 1; + printf("%s, %d\n", teams[champin[i]], seq);//score[champin[i]], pure[champin[i]]); + } + } + return 0; +} diff --git a/xxf.cpp b/xxf.cpp new file mode 100644 index 00000000..6be38433 --- /dev/null +++ b/xxf.cpp @@ -0,0 +1,60 @@ + +#include +#include +#include +using namespace std; + +class Student +{ +private: + string name; // + char sex; //ԱУMŮF + string grade; //꼶 + float gpa; // +public: + Student(string n = "Unknown", char s = '?' , string gra = "Unknown", float g = 0) : + name(n), sex(s), grade(gra), gpa(g) {} + void show_myself(); //ӡϢ + void study_time(float study_time); //GPA +}; + +void Student::show_myself() //ӡϢ +{ + cout << "Name:" << name << endl; + cout << "Sex:" << sex << endl; + cout << "Grade:" << grade << endl; + cout << "GPA:" << gpa << endl; +} + +void Student::study_time(float study_time) //GPA +{ + gpa += log(study_time); + if (gpa > 4.0) gpa = 4.0; +} + +int main() +{ + Student Mike = { "Mike Barnes",'M',"Freshman",4 }; + Student Jim = { "Jim Nickerson",'M',"Dophomore",3 }; + Student Jack = { "Jack Indabox",'M',"Junior",2.5 }; + Student Jane = { "Jane Miller",'F',"Senior",3.6 }; + Student Mary = { "Mary Scott",'F',"Senior",2.7 }; + Student stu[5] = { Mike , Jim ,Jack ,Jane ,Mary }; + for (int i = 0; i < 5; i++) + { + stu[i].show_myself(); + cout << endl; + } + cout << "After studying......" << endl << endl; + int s_time[5] = { 60,100,40,300,1000 }; + for (int i = 0; i < 5; i++) + { + stu[i].study_time(s_time[i]); + stu[i].show_myself(); + cout << endl; + } + return 0; +} + + +