BRS5672023 最近的时间轴更新
BRS5672023

BRS5672023

V2EX 第 537762 号会员,加入于 2021-03-16 12:54:51 +08:00
今日活跃度排名 9689
根据 BRS5672023 的设置,主题列表被隐藏
二手交易 相关的信息,包括已关闭的交易,不会被隐藏
BRS5672023 最近回复了
9 小时 39 分钟前
回复了 Tardis07 创建的主题 Linux 寻找一个支持排除窗口的 Linux 录屏工具
@Tardis07 黑框啥意思?所以你是想要除了你自己别人都看不见这个窗口吗?而不只是别人看不到这个窗口的内容?
16 小时 6 分钟前
回复了 Tardis07 创建的主题 Linux 寻找一个支持排除窗口的 Linux 录屏工具
niri wm 作为一个窗口管理器有这个功能 https://github.com/YaLTeR/niri/wiki/Configuration:-Window-Rules#block-out-from ,然而其录屏功能需要 xdg-desktop-portal-gnome 来实现,而我在 archlinux 上使用 niri wm 却不能正常启动 xdg-desktop-portal-gnome 这个服务。。
6 天前
回复了 BRS5672023 创建的主题 C++ C++ 新手问下有没有办法链式定义一个类
@blacktail 只是随便写了下化简的部分;重点也不是在化简的部分,但是当然希望我的计算结果能够输出一个简化过的形式。。

另外程序写的还是有挺多问题的,包括甚至不能计算展开多项式的乘积。。
7 天前
回复了 BRS5672023 创建的主题 C++ C++ 新手问下有没有办法链式定义一个类
上面的程序输出的结果是
```
Polynomial before simplification:
3 * pow(p, 2) * pow(q, 1) + 5 * pow(p, 2) * pow(q, 1) + 2 * pow(p, 1) * pow(q, 1)
Polynomial after simplification:
8 * pow(p, 2) * pow(q, 1) + 2 * pow(p, 1) * pow(q, 1)
```
7 天前
回复了 BRS5672023 创建的主题 C++ C++ 新手问下有没有办法链式定义一个类
@ipwx 我试了一下,大概是下面的形式
```
#include <iostream>
using namespace std;

#include <string>
#include <vector>

class Mono {
public:
int coefficient;
char variable1;
int power1;
char variable2;
int power2;

Mono(int a, char p, int n = 0, char q = '1', int m = 0) : coefficient(a), variable1(p), power1(n), variable2(q), power2(m) {}

bool isSameTerm(const Mono & other) {
return(variable1 == other.variable1 && power1 == other.power1 && variable2 == other.variable2 && power2 == other.power2);
}

};

Mono operator * (int coef, const Mono & mono) {
return Mono(coef * mono.coefficient, mono.variable1, mono.power1, mono.variable2, mono.power2);
}

Mono operator * (const Mono & lhs, const Mono & rhs) {
if (lhs.variable2 == '1' && rhs.variable2 == '1') {
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1, rhs.variable1, rhs.power1);
}
else if (lhs.variable1 == rhs.variable1 && lhs.variable2 == '1') {
if (rhs.variable2 == '1') {
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1);
}
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, lhs.variable2, lhs.power2);
}
else if (lhs.variable1 == rhs.variable1 && rhs.variable2 == '1') { // We do not need consider the case when the second variable of lhs is '1', which was already contained in the last case.
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, rhs.variable2, rhs.power2);
}
else if (lhs.variable1 == rhs.variable1 && lhs.variable2 == rhs.variable2) {
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, lhs.variable2, lhs.power2 + rhs.power2);
}
else if (lhs.variable1 == rhs.variable2 && lhs.variable2 == rhs.variable1) {
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power2, lhs.variable2, lhs.power2 + rhs.power1);
}
throw -1;
}

Mono pow(char variable, int power) {
return Mono(1, variable, power);
}

class Poly {
friend ostream & operator << (ostream & os, const Poly & poly);
private:
vector<Mono> terms;

public:
Poly & addTerm(const Mono & term) {
terms.push_back(term);
return * this;
}

Poly & operator + (const Mono & term) {
addTerm(term);
return * this;
}

void simplify() {
for (size_t i = 0; i < terms.size(); i++) {
for (size_t j = i + 1; j < terms.size(); j++) {
if (terms[i].isSameTerm(terms[j])) {
terms[i].coefficient = terms[i].coefficient + terms[j].coefficient;
terms.erase(terms.begin() + j);
j--;
}
}
}
}
};

ostream & operator << (ostream & os, const Poly & poly) {
for (size_t i = 0; i < poly.terms.size(); i++) {
const auto & term = poly.terms[i];
if (term.coefficient != 0) {
if (term.power1 == 0 && term.power2 == 0) {
os << term.coefficient;
}
else if (term.power1 == 0) {
os << term.coefficient << " * pow(" << term.variable2 << ", " << term.power2 << ")";
}
else if (term.power2 == 0) {
os << term.coefficient << " * pow(" << term.variable1 << ", " << term.power1 << ")";
}
else {
os << term.coefficient << " * pow(" << term.variable1 << ", " << term.power1 << ") * " << "pow(" << term.variable2 << ", " << term.power2 << ")";
}
if (i < poly.terms.size() - 1) {
os << " + ";
}
}
}
return os;
}

int main() {
try {
Poly poly;
poly = poly + (3 * pow('p', 2) * pow('q', 1)) + (5 * pow('p', 2) * pow('q', 1)) + (2 * pow('p', 1) * pow('q', 1));
cout << "Polynomial before simplification:\n" << poly << endl;

poly.simplify();
cout << "Polynomial after simplification:\n" << poly << endl;
}

catch(int err0) {
cout << "You must input polynomials of two variables with aligned variables for multiplication!" << endl;
cout << "Error number: " << err0 << endl;
}
return 0;
}
```
问题是我这样写就必须这样定义一个多项式
```
Poly poly;
poly = poly + (3 * pow('p', 2) * pow('q', 1)) + (5 * pow('p', 2) * pow('q', 1)) + (2 * pow('p', 1) * pow('q', 1));
```
还有就是无法区分 pow(p, n) * pow(q, m) 和 pow(q, m) * pow(p, n) 这两种形式(除非我指定两个 char 的顺序);不过第二个问题就是我一开始想要的对于非交换的多项式的计算。。(然后我期待可以计算两个多项式的对易子,最后计算得到关于 p q 的对易子的一个多项式)
@yanqiyu

>> 特别是如果我把 getValidInput 函数的这个判断给注释掉的话,那么在我输入 "2" 使用 "显示联系人" 这个功能时,需要再输入一个回车才会显示 "请按回车键继续" 的字符

> cin.ignore(numeric_limits<streamsize>::max(), '\n'); 会一直等输入(堵塞)直到遇到回车,要是缓冲区里面没有回车的话。

这个地方为什么它在 case 1 和 case 2 的时候行为不一致呢?是因为我在使用 case 2 的 showPerson 函数里面没有任何输入所以需要我先输入一个回车吗?然后我加入的 cin.eof() 的判断就相当于把 cin.ignore 这一行给注释掉了所以在我一开始选择 case 的 getValidInput 里就会把我最初输入的 1 或者 2 给保留下来,然后 case 2 里的 pause 就不再需要我再输入一个换行符了,大概是这个原因?
@sagaxu 这个方法不错,感谢
@cnbatch 直接按照你给出的说法替换后在我进行添加联系人的操作后的输出结果是

Press enter to continue1 、添加联系人
2 、显示联系人
3 、删除联系人
4 、查找联系人
5 、修改联系人
6 、清空联系人
0 、退出通讯录

不太清楚怎么回事。。
12 天前
回复了 TrackBack 创建的主题 Linux 联想 thinkbook 在 Linux 下风扇不转正常吗
我是 14+ (G6+ AHP) 8845h 版的,风扇没有问题,不过比如 s-tui 里读不到风扇转速。。我估计也是没法调转速的。。

另外,之前买过一个戴尔的本子,本身 Linux 兼容性还是很不错的,就是风扇策略有点离谱。。然后查了一下,发现控制风扇的程序是逆向出来的,并且因为我买的本子太新了还不能用(不过至少 s-tui 里还能读到风扇转速)
14 天前
回复了 ityspace 创建的主题 Linux 推荐一个平铺化桌面 Niri
另外 github 里面有一些讨论很有意思,比如这个 overview 的概念 https://github.com/YaLTeR/niri/discussions/352#discussioncomment-10094739
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1055 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 60ms · UTC 19:36 · PVG 03:36 · LAX 12:36 · JFK 15:36
Developed with CodeLauncher
♥ Do have faith in what you're doing.