V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
V2EX  ›  BRS5672023  ›  全部回复第 2 页 / 共 3 页
回复总数  50
1  2  3  
2024 年 10 月 21 日
回复了 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 、退出通讯录

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

另外,之前买过一个戴尔的本子,本身 Linux 兼容性还是很不错的,就是风扇策略有点离谱。。然后查了一下,发现控制风扇的程序是逆向出来的,并且因为我买的本子太新了还不能用(不过至少 s-tui 里还能读到风扇转速)
2024 年 10 月 14 日
回复了 ityspace 创建的主题 Linux 推荐一个平铺化桌面 Niri
另外 github 里面有一些讨论很有意思,比如这个 overview 的概念 https://github.com/YaLTeR/niri/discussions/352#discussioncomment-10094739
2024 年 10 月 8 日
回复了 bitvector 创建的主题 Linux 有购买 Thinkbook 14+ 2024 成功安装 Linux 的大佬吗?
@BRS5672023 这个问题原来在 libcamera 0.3.2 上已经修复了。。
2024 年 10 月 8 日
回复了 bitvector 创建的主题 Linux 有购买 Thinkbook 14+ 2024 成功安装 Linux 的大佬吗?
@imjiaoyuan 耗电问题你看和 pipewire 的这个 bug 有没有关系,我的 amd 机型也遇到了耗电的问题。。

https://www.reddit.com/r/linux/comments/1em8biv/psa_pipewire_has_been_halving_your_battery_life/
2024 年 10 月 6 日
回复了 ityspace 创建的主题 Linux 推荐一个平铺化桌面 Niri
@Immortal 优势就是打开新窗口不会改变已经打开窗口的大小,niri 上的 workspace 相当于几条不相交的带子,每条都可以在水平方向无限延伸,但是其实在每一条带子上定位一个窗口就没那么简单了(如果一条带子上的窗口数目足够多的情况下)
2024 年 10 月 6 日
回复了 bitvector 创建的主题 Linux 有购买 Thinkbook 14+ 2024 成功安装 Linux 的大佬吗?
话说 8840hs 的版本貌似有一个 bios 升级后显存会自动被设置成 512M 的 bug ,虽然 bios 选项里显示的还是之前设置的显存大小,需要改一下设置才能调回去(比如先调到另一个值,然后重启再进入 bios 把它调到原来的值。。)
2024 年 10 月 6 日
回复了 ityspace 创建的主题 Linux 推荐一个平铺化桌面 Niri
挺好用的,什么时候支持 floating window 就更好了。。

还有相比 hyprland 的一大劣势是不支持 text-input-v1 ,现在 chromium 上的 text-input-v3 用起来还没那么完善( electron 应该还不支持 text-input-v3 ?)
2024 年 9 月 3 日
回复了 Tlaster 创建的主题 Linux 记录一下再一次尝试使用 Linux 作为主力系统失败的经历
2024 年 9 月 1 日
回复了 Tlaster 创建的主题 Linux 记录一下再一次尝试使用 Linux 作为主力系统失败的经历
@EliStone 花屏我怀疑是 780m 核显的问题。。桌面端 rdna2 和 rdna3 显卡上都没有这种问题,但我在 redmi book 和 thinkbook 14+ 上都遇到了这个问题。。
@test10101 可以 blacklist ideapad_laptop 这个 module 解决问题(但是 blacklist 之后 conservation mode 和性能模式无法更改)。。另外 amd 的机器还有用 fn 键调亮度导致关机的问题,合盖和 fn key 导致关机的问题都可以用 https://github.com/ty2/ideapad-laptop-tb2024g6plus/tree/feature/amd-fn-fix 解决,但也有一个小问题是 fn 关闭麦克风的快捷键无法使用,这和 blacklist ideapad_laptop 之后的 fn key 的行为是一致的(其他功能不受影响)
2024 年 6 月 3 日
回复了 KinsleyNg 创建的主题 Linux 这个 Linux 的 chrome 硬解就这么难处理吗
@KinsleyNg 上游相关的 patch 都已经 merge 了,现在 arch 上的 chromium 已经可以正常硬解了,不过 chromium 的 patch 好像是 arch backport 的,所以其他发行版可能还得等后续更新。。
2024 年 5 月 26 日
回复了 KinsleyNg 创建的主题 Linux 这个 Linux 的 chrome 硬解就这么难处理吗
现在 arch 的 aur 里已经有 patched 过的 google-chrome 能在 amdgpu + wayland 下使用 vaapi 了(不过好像 hevc 的解码还有问题。。另外启用 extra-testing 可以更新 mesa 24.1 了)[url=https://aur.archlinux.org/packages/google-chrome-wayland-vulkan]google-chrome-wayland-vulkan[/url]
2024 年 5 月 13 日
回复了 KinsleyNg 创建的主题 Linux Fedora 40 笔记本温度略高
我这边用 arch 使用 firefox 的体验也是 cpu 占用会更高一些,甚至在开启硬解对比不开硬解的 chromium 的情况下,看视频也是 chromium cpu 占用更低(桌面 5700x 和笔记本 8840hs 的体验都是如此),另外 firefox 在油管页面滚动时的 cpu 占用似乎格外的高;不过 chromium 软解 4K 视频就开始有点吃力了。。
2024 年 4 月 26 日
回复了 KinsleyNg 创建的主题 Linux 这个 Linux 的 chrome 硬解就这么难处理吗
mesa 24.1 应该可以 xwayland 使用 chromium 硬解了 [frontends/va: Only export one handle for contiguous planes]( https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26165)。。

不过我用 hyprland 以及使用 hidpi 的情况下 xwayland 的体验就。。
2024 年 4 月 26 日
回复了 KinsleyNg 创建的主题 Linux 这个 Linux 的 chrome 硬解就这么难处理吗
nvtop 里可以看,如果有 enc 显示非零占用就说明调用了硬件解码
2024 年 4 月 22 日
回复了 penzi 创建的主题 Windows 华为 MateBook X Pro 2024 上手吐槽一下这个触摸板
之前的触控板力反馈就感觉很弱了,这一代的反馈是更弱了?
1  2  3  
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   918 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 18ms · UTC 21:14 · PVG 05:14 · LAX 14:14 · JFK 17:14
♥ Do have faith in what you're doing.