来谈谈C++ 位运算 & | << >> ^ ~ %

老实说,我对+ = * / % && || ==一些比较简单的运算符比较熟悉。对位运算就陌生了,主要用的少。我觉得高手用的会比较多,因为位运算速度比较快。位运算应该适用于大多数的语言,不限于c++

 

1.&

 

如果两个相应的二进制位都为1,则该位的结果值为1;否则为0。

注:下面都用8位的 unsigned char 来做例子。

 

&简单举例:

 

11&3  = 3

 

     00001011

&   00000011

=   00000011 = 3

 

&比较实用的例子:

比如我们经常要用的是否被2整除,一般都写成   if(n % 2 == 0)

可以换成 if((n&1) == 0)

 

2. |

 

如果两个相应的二进制位只要有一个是1,结果就是1;否则为0。

 

| 简单例子:

 

11 | 3 = 11

     00001011

|    00000011

=   00001011 = 11

 

| 比较实用的例子

可以用一个unsigned int 来存储多个布尔值。比如一个文件有读权限,写权限,执行权限。看起来要记录3个布尔值。我们可以用一个unsigned int也可以完成任务。

 

一个数r来表示读权限,它只更改个位来记录读权限的布尔值

00000001  (表示有读权限)

00000000  (表示没有读权限)

 

一个数w表示写权限,它只用二进制的倒数第二位来记录布尔值

00000010 (表示有写权限)

00000000 (表示没有写权限)

 

一个数x表示执行权限,它只用倒数第三位来记录布尔值

00000100 (表示有执行权限)

00000000 (表示没有执行权限)

 

那么一个文件同时没有3种权限就是

~r | ~ w | ~ x 即为 00000000,就是0

 

只有读的权限就是

r | ~w | ~x 即为 00000001,就是1

只有写的权限就是

~r | w | ~x 即为 00000010,就是2

一个文件同时有3种权限就是

r | w | x 即为 00000111,就是7

 

3. << 向左移位移

 

<<简单例子(向左移一位,右边自动补0)

11 << 1 = 22

  00001011 << 1

  00010110 = 22

相当于二进制的每个数都变成当前值的两倍,结果就是变成当前值的两倍。

n * 2 == (n << 1)

推广下就是(注意可能会溢出)

 

4. >> 向右位移

 

>>简单例子(向右移一位,左边自动补1(补0?))

11 >> 1 =  5

00001011 >> 1

00000101 = 5

注意到最后一位的1被干掉了。

比较实用的例子是:

int n = n / 2     等价于   int n = n >> 1  等价于 int n >>= 1

 

 

5. ^ 异或

 

两个相同的数会变成0,反之是1

例子:

11^3 = 8

     00001011

^   00000011

=   00001000 = 8

 

我觉得理解异或,一定要用异或来解下面的题目:

Given an array of integers, every element appearstwice except for one. Find that single one.

就是一个数组中,所有数字都出现了两次,只有一个没有

比如 int t = {1,2,3,3,2,1,5} 要找到5。

用异或就完美了,所有相同的都会消失,留下来的就是5了。我发现异或是嫉妒成双成对的。

?
1
2
3
4
5
6
int singleNumber( int A[], int n) { 
         for ( int i = 1; i < n; ++i){ 
            A[0] ^= A[i]; 
        
         return A[0]; 
     }

还有就是用不tmp值来交换两个数

 

?
1
2
3
4
5
6
7
//不用temp交换两个整数
void swap( int & x , int & y)
{
     x ^= y;
     y ^= x;
     x ^= y;
}

 

扯点别的,^在lua中表示pow的意思,这是要逆天。

 

6 ~

 

这个在加法中用到

x-y = x + ~y + 1

所以~y = – y  -1

 

比如 ~11 = -11 -1 = -12

 

7 再扯下%

 

居然有道题目是这样的: 求 100 % 8的 优化解法。我们知道:

8刚好是2的3次方

所以 100 % 8 == 100 – math.floor(100 / 8) * 8 == 100 -  ((100 >> 3) << 3)

 

8.位运算优先级

 

总的来说比较低,一定要加括号。


转载:http://www.waitingfy.com/archives/1004

Tags: 位运算

#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;map&gt; #include &lt;ctime&gt; #include &lt;cstdlib&gt; #include &lt;thread&gt; #include &lt;chrono&gt; #include &lt;algorithm&gt; #include &lt;sstream&gt; #include &lt;memory&gt; #include &lt;functional&gt; #include &lt;cmath&gt; #include &lt;iomanip&gt; #include &lt;fstream&gt; using namespace std; // ============ 智能思考引擎 ============ class DeepThinker { private: bool deepMode; int thinkTime; double accuracy; public: DeepThinker() { srand(time(0)); deepMode = false; thinkTime = 2; accuracy = 0.7; } void enableDeepMode() { deepMode = true; thinkTime = 5; accuracy = 0.9; cout &lt;&lt; &quot;?? 深度思考模式已开启!&quot; &lt;&lt; endl; cout &lt;&lt; &quot;思考时间: &quot; &lt;&lt; thinkTime &lt;&lt; &quot;秒, 准确率: &quot; &lt;&lt; accuracy * 100 &lt;&lt; &quot;%&quot; &lt;&lt; endl; } void disableDeepMode() { deepMode = false; thinkTime = 2; accuracy = 0.7; cout &lt;&lt; &quot;? 快速响应模式已开启!&quot; &lt;&lt; endl; } bool isDeepMode() const { return deepMode; } void thinking() const { cout &lt;&lt; &quot;?? 思考中&quot;; int dots = deepMode ? 6 : 3; for(int i = 0; i &lt; dots; i++) { cout &lt;&lt; &quot;.&quot;; cout.flush(); this_thread::sleep_for(chrono::milliseconds(500)); } cout &lt;&lt; endl; } double getAccuracy() const { return accuracy + (rand() % 10) / 100.0; } string getResponseType() const { vector&lt;string&gt; types = { &quot;分析型&quot;, &quot;创意型&quot;, &quot;逻辑型&quot;, &quot;批判型&quot;, &quot;情感型&quot;, &quot;技术型&quot;, &quot;哲学型&quot;, &quot;实践型&quot; }; return types[rand() % types.size()]; } }; // ============ 情感识别模块 ============ class EmotionRecognizer { private: map&lt;string, vector&lt;string&gt;&gt; emotionKeywords; map&lt;string, int&gt; emotionCount; string toLower(const string&amp; str) { string result = str; transform(result.begin(), result.end(), result.begin(), ::tolower); return result; } vector&lt;string&gt; splitWords(const string&amp; text) { vector&lt;string&gt; words; stringstream ss(text); string word; while(ss &gt;&gt; word) { words.push_back(toLower(word)); } return words; } public: EmotionRecognizer() { // 初始化情感关键词 emotionKeywords[&quot;开心&quot;] = {&quot;开心&quot;, &quot;高兴&quot;, &quot;快乐&quot;, &quot;幸福&quot;, &quot;喜欢&quot;, &quot;爱&quot;, &quot;棒&quot;, &quot;优秀&quot;, &quot;美好&quot;, &quot;微笑&quot;}; emotionKeywords[&quot;伤心&quot;] = {&quot;伤心&quot;, &quot;难过&quot;, &quot;悲伤&quot;, &quot;痛苦&quot;, &quot;哭&quot;, &quot;孤独&quot;, &quot;寂寞&quot;, &quot;失望&quot;, &quot;糟糕&quot;, &quot;讨厌&quot;}; emotionKeywords[&quot;生气&quot;] = {&quot;生气&quot;, &quot;愤怒&quot;, &quot;恼火&quot;, &quot;烦躁&quot;, &quot;不爽&quot;, &quot;讨厌&quot;, &quot;恨&quot;, &quot;发火&quot;, &quot;暴怒&quot;, &quot;气愤&quot;}; emotionKeywords[&quot;好奇&quot;] = {&quot;什么&quot;, &quot;怎么&quot;, &quot;为什么&quot;, &quot;何时&quot;, &quot;哪里&quot;, &quot;哪个&quot;, &quot;谁&quot;, &quot;解释&quot;, &quot;问题&quot;, &quot;好奇&quot;, &quot;想知道&quot;}; emotionKeywords[&quot;平静&quot;] = {&quot;平静&quot;, &quot;安宁&quot;, &quot;安静&quot;, &quot;放松&quot;, &quot;冷静&quot;, &quot;宁静&quot;, &quot;平和&quot;, &quot;轻松&quot;, &quot;舒适&quot;}; emotionKeywords[&quot;困惑&quot;] = {&quot;困惑&quot;, &quot;不清楚&quot;, &quot;疑惑&quot;, &quot;不确定&quot;, &quot;迷惑&quot;, &quot;茫然&quot;, &quot;不懂&quot;, &quot;不明白&quot;, &quot;混乱&quot;}; emotionKeywords[&quot;中性&quot;] = {&quot;中性&quot;, &quot;一般&quot;, &quot;普通&quot;, &quot;还好&quot;, &quot;正常&quot;, &quot;还行&quot;}; // 初始化情感计数 for(const auto&amp; pair : emotionKeywords) { emotionCount[pair.first] = 0; } } string detectEmotion(const string&amp; text) { vector&lt;string&gt; words = splitWords(text); map&lt;string, int&gt; scores; // 计算每个情感的关键词匹配数 for(const auto&amp; pair : emotionKeywords) { string emotion = pair.first; scores[emotion] = 0; for(const string&amp; keyword : pair.second) { for(const string&amp; word : words) { if(word.find(keyword) != string::npos) { scores[emotion]++; emotionCount[emotion]++; } } } } // 找出最高分的情感 string dominantEmotion = &quot;neutral&quot;; int maxScore = 0; for(const auto&amp; pair : scores) { if(pair.second &gt; maxScore) { maxScore = pair.second; dominantEmotion = pair.first; } } // 如果所有分数都很低,返回中性 if(maxScore == 0) { return &quot;neutral&quot;; } return dominantEmotion; } string getEmoji(const string&amp; emotion) const { map&lt;string, string&gt; emojiMap = { {&quot;开心&quot;, &quot;??&quot;}, {&quot;伤心&quot;, &quot;??&quot;}, {&quot;生气&quot;, &quot;??&quot;}, {&quot;疑惑&quot;, &quot;??&quot;}, {&quot;安心&quot;, &quot;??&quot;}, {&quot;不满意&quot;, &quot;??&quot;}, {&quot;无语&quot;, &quot;??&quot;} }; return emojiMap[emotion]; } void showEmotionStats() const { cout &lt;&lt; &quot;\n?? 情感统计:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; for(const auto&amp; pair : emotionCount) { if(pair.second &gt; 0) { cout &lt;&lt; getEmoji(pair.first) &lt;&lt; &quot; &quot; &lt;&lt; pair.first &lt;&lt; &quot;: &quot; &lt;&lt; pair.second &lt;&lt; &quot;次&quot; &lt;&lt; endl; } } cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } }; // ============ 知识库系统 ============ class KnowledgeBase { private: map&lt;string, vector&lt;string&gt;&gt; knowledge; map&lt;string, string&gt; facts; map&lt;string, vector&lt;string&gt;&gt; examples; public: KnowledgeBase() { // 数学知识 knowledge[&quot;数学&quot;] = { &quot;数学是研究数量、结构、空间和变化的一门学科&quot;, &quot;基础运算包括加法、减法、乘法、除法&quot;, &quot;几何学研究形状、大小和空间关系&quot;, &quot;代数学研究数学符号和运算规则&quot;, &quot;微积分研究变化率和累积量&quot; }; // 编程知识 knowledge[&quot;编程&quot;] = { &quot;C++是一种通用的编程语言,支持面向对象编程&quot;, &quot;变量是存储数据的容器&quot;, &quot;函数是执行特定任务的代码块&quot;, &quot;循环用于重复执行代码&quot;, &quot;条件语句用于根据不同条件执行不同代码&quot; }; // 科学知识 knowledge[&quot;科学&quot;] = { &quot;物理学研究物质、能量和它们之间的相互作用&quot;, &quot;化学研究物质的组成、性质和变化&quot;, &quot;生物学研究生命体和生命过程&quot;, &quot;天文学研究天体及其现象&quot;, &quot;地球科学研究地球及其过程&quot; }; // 事实数据 facts[&quot;pi&quot;] = &quot;3.141592653589793&quot;; facts[&quot;e&quot;] = &quot;2.718281828459045&quot;; facts[&quot;golden_ratio&quot;] = &quot;1.618033988749895&quot;; facts[&quot;light_speed&quot;] = &quot;299792458 m/s&quot;; facts[&quot;gravity&quot;] = &quot;9.80665 m/s2&quot;; // 数学示例 examples[&quot;加法&quot;] = {&quot;1 + 1 = 2&quot;, &quot;2 + 3 = 5&quot;, &quot;10 + 15 = 25&quot;}; examples[&quot;减法&quot;] = {&quot;5 - 3 = 2&quot;, &quot;10 - 4 = 6&quot;, &quot;20 - 7 = 13&quot;}; examples[&quot;乘法&quot;] = {&quot;2 &times; 3 = 6&quot;, &quot;4 &times; 5 = 20&quot;, &quot;7 &times; 8 = 56&quot;}; examples[&quot;除法&quot;] = {&quot;6 &divide; 2 = 3&quot;, &quot;15 &divide; 3 = 5&quot;, &quot;24 &divide; 6 = 4&quot;}; } vector&lt;string&gt; getKnowledge(const string&amp; category) const { auto it = knowledge.find(category); if(it != knowledge.end()) { return it-&gt;second; } return vector&lt;string&gt;(); } string getFact(const string&amp; key) const { auto it = facts.find(key); if(it != facts.end()) { return it-&gt;second; } return &quot;未找到该信息&quot;; } vector&lt;string&gt; getExamples(const string&amp; operation) const { auto it = examples.find(operation); if(it != examples.end()) { return it-&gt;second; } return vector&lt;string&gt;(); } bool hasCategory(const string&amp; category) const { return knowledge.find(category) != knowledge.end(); } vector&lt;string&gt; getAllCategories() const { vector&lt;string&gt; categories; for(const auto&amp; pair : knowledge) { categories.push_back(pair.first); } return categories; } void showAllCategories() const { cout &lt;&lt; &quot;\n?? 知识库分类:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; for(const auto&amp; category : getAllCategories()) { cout &lt;&lt; &quot;? &quot; &lt;&lt; category &lt;&lt; endl; } cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } }; // ============ 数学求解器 ============ class MathSolver { private: KnowledgeBase knowledge; double evaluateExpression(const string&amp; expr) { // 简单的表达式求值(仅支持基本运算) size_t plus = expr.find(&#39;+&#39;); size_t minus = expr.find(&#39;-&#39;); size_t multiply = expr.find(&#39;*&#39;); size_t divide = expr.find(&#39;/&#39;); if(plus != string::npos) { double a = stod(expr.substr(0, plus)); double b = stod(expr.substr(plus + 1)); return a + b; } else if(minus != string::npos) { double a = stod(expr.substr(0, minus)); double b = stod(expr.substr(minus + 1)); return a - b; } else if(multiply != string::npos) { double a = stod(expr.substr(0, multiply)); double b = stod(expr.substr(multiply + 1)); return a * b; } else if(divide != string::npos) { double a = stod(expr.substr(0, divide)); double b = stod(expr.substr(divide + 1)); if(b != 0) { return a / b; } else { throw runtime_error(&quot;除数不能为0&quot;); } } return stod(expr); } public: MathSolver() {} void solveEquation(const string&amp; equation) { cout &lt;&lt; &quot;\n?? 解方程: &quot; &lt;&lt; equation &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; try { double result = evaluateExpression(equation); cout &lt;&lt; &quot;结果: &quot; &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; result &lt;&lt; endl; // 提供解释 cout &lt;&lt; &quot;解释: 这是基本的算术运算。&quot; &lt;&lt; endl; } catch(const exception&amp; e) { cout &lt;&lt; &quot;错误: &quot; &lt;&lt; e.what() &lt;&lt; endl; } } void showMathHelp() const { cout &lt;&lt; &quot;\n?? 数学帮助:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; cout &lt;&lt; &quot;支持的运算:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 加法: a + b&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 减法: a - b&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 乘法: a * b&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 除法: a / b&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 平方根: sqrt(x)&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 幂运算: a ^ b&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } void showMathFacts() { cout &lt;&lt; &quot;\n?? 数学常数:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; cout &lt;&lt; &quot;&pi; (pi): &quot; &lt;&lt; knowledge.getFact(&quot;pi&quot;) &lt;&lt; endl; cout &lt;&lt; &quot;e (自然常数): &quot; &lt;&lt; knowledge.getFact(&quot;e&quot;) &lt;&lt; endl; cout &lt;&lt; &quot;黄金比例: &quot; &lt;&lt; knowledge.getFact(&quot;golden_ratio&quot;) &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } void showExamples(const string&amp; operation) { vector&lt;string&gt; examples = knowledge.getExamples(operation); if(!examples.empty()) { cout &lt;&lt; &quot;\n?? &quot; &lt;&lt; operation &lt;&lt; &quot;示例:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; for(const auto&amp; example : examples) { cout &lt;&lt; &quot;? &quot; &lt;&lt; example &lt;&lt; endl; } cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } } }; // ============ 对话管理器 ============ class DialogueManager { private: vector&lt;pair&lt;string, string&gt;&gt; conversationHistory; KnowledgeBase knowledge; EmotionRecognizer emotionRecognizer; public: DialogueManager() {} void addMessage(const string&amp; speaker, const string&amp; message) { conversationHistory.push_back(make_pair(speaker, message)); } void showConversation() const { cout &lt;&lt; &quot;\n?? 对话历史:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; for(const auto&amp; entry : conversationHistory) { cout &lt;&lt; entry.first &lt;&lt; &quot;: &quot; &lt;&lt; entry.second &lt;&lt; endl; } cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } void clearConversation() { conversationHistory.clear(); cout &lt;&lt; &quot;? 对话历史已清空&quot; &lt;&lt; endl; } string generateResponse(const string&amp; userInput, const string&amp; emotion) { // 根据用户输入和情感生成回应 vector&lt;string&gt; responses; if(emotion == &quot;开心&quot;) { responses = { &quot;听起来很棒!??&quot;, &quot;很高兴听到这个!&quot;, &quot;这真是个好消息!&quot;, &quot;继续保持这样的好心情!&quot; }; } else if(emotion == &quot;难过&quot;) { responses = { &quot;我理解你的感受... ??&quot;, &quot;如果你需要倾诉,我在这里。&quot;, &quot;有时候感到悲伤是正常的。&quot;, &quot;希望你能感觉好些。&quot; }; } else if(emotion == &quot;疑惑&quot;) { responses = { &quot;这是个很好的问题!??&quot;, &quot;让我想想怎么回答...&quot;, &quot;我对这个问题很感兴趣。&quot;, &quot;这是个值得深入探讨的话题。&quot; }; } else if(emotion == &quot;生气&quot;) { responses = { &quot;我理解你的愤怒... ??&quot;, &quot;冷静下来,我们可以好好谈谈&quot;, &quot;有时候表达愤怒也是必要的。&quot;, &quot;深呼吸,我在这里倾听。&quot; }; } else { responses = { &quot;我明白了。&quot;, &quot;有趣的观点。&quot;, &quot;继续说,我在听。&quot;, &quot;好的,我理解了。&quot; }; } // 随机选择一个回应 srand(time(0)); int index = rand() % responses.size(); // 添加一些智能回复逻辑 if(userInput.find(&quot;?&quot;) != string::npos) { return responses[index] + &quot; 这是个需要思考的问题。&quot;; } else if(userInput.find(&quot;!&quot;) != string::npos) { return responses[index] + &quot; 听起来很重要!&quot;; } return responses[index]; } int getConversationLength() const { return conversationHistory.size(); } }; // ============ 学习系统 ============ class LearningSystem { private: map&lt;string, int&gt; userInterests; vector&lt;string&gt; learnedConcepts; int interactionCount; public: LearningSystem() : interactionCount(0) { // 初始化兴趣计数器 userInterests[&quot;数学&quot;] = 0; userInterests[&quot;科学&quot;] = 0; userInterests[&quot;编程&quot;] = 0; userInterests[&quot;其它&quot;] = 0; } void recordInteraction(const string&amp; topic) { interactionCount++; // 更新兴趣 if(userInterests.find(topic) != userInterests.end()) { userInterests[topic]++; } else { userInterests[&quot;其它&quot;]++; } } void learnConcept(const string&amp; concept) { if(find(learnedConcepts.begin(), learnedConcepts.end(), concept) == learnedConcepts.end()) { learnedConcepts.push_back(concept); cout &lt;&lt; &quot;?? 学习了新概念: &quot; &lt;&lt; concept &lt;&lt; endl; } } string getFavoriteTopic() const { string favorite; int maxCount = 0; for(const auto&amp; pair : userInterests) { if(pair.second &gt; maxCount) { maxCount = pair.second; favorite = pair.first; } } return favorite; } void showLearningStats() const { cout &lt;&lt; &quot;\n?? 学习统计:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; cout &lt;&lt; &quot;总交互次数: &quot; &lt;&lt; interactionCount &lt;&lt; endl; cout &lt;&lt; &quot;已学概念: &quot; &lt;&lt; learnedConcepts.size() &lt;&lt; endl; cout &lt;&lt; &quot;最感兴趣的主题: &quot; &lt;&lt; getFavoriteTopic() &lt;&lt; endl; cout &lt;&lt; &quot;\n详细兴趣统计:&quot; &lt;&lt; endl; for(const auto&amp; pair : userInterests) { if(pair.second &gt; 0) { cout &lt;&lt; &quot;? &quot; &lt;&lt; pair.first &lt;&lt; &quot;: &quot; &lt;&lt; pair.second &lt;&lt; &quot;次&quot; &lt;&lt; endl; } } if(!learnedConcepts.empty()) { cout &lt;&lt; &quot;\n已学概念列表:&quot; &lt;&lt; endl; for(size_t i = 0; i &lt; min(learnedConcepts.size(), size_t(5)); i++) { cout &lt;&lt; &quot;? &quot; &lt;&lt; learnedConcepts[i] &lt;&lt; endl; } if(learnedConcepts.size() &gt; 5) { cout &lt;&lt; &quot;... 等 &quot; &lt;&lt; learnedConcepts.size() - 5 &lt;&lt; &quot; 个概念&quot; &lt;&lt; endl; } } cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } }; // ============ DeepSeek 3.0 主类 ============ class DeepSeek3 { private: DeepThinker thinker; KnowledgeBase knowledge; MathSolver mathSolver; DialogueManager dialogue; EmotionRecognizer emotionRec; LearningSystem learner; bool running; public: DeepSeek3() : running(true) { cout &lt;&lt; &quot;==========================================&quot; &lt;&lt; endl; cout &lt;&lt; &quot; DeepSeek v3.0测试版 作者张琛&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==========================================&quot; &lt;&lt; endl; cout &lt;&lt; &quot;功能特性:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? ?? 智能思考引擎&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? ?? 情感识别系统&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? ?? 综合知识库&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? ?? 数学求解器&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? ?? 对话管理系统&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? ?? 自适应学习系统&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==========================================&quot; &lt;&lt; endl; } void start() { while(running) { showMainMenu(); int choice; cin &gt;&gt; choice; cin.ignore(); switch(choice) { case 1: startDialogue(); break; case 2: mathMode(); break; case 3: knowledgeMode(); break; case 4: toggleThinkingMode(); break; case 5: showStats(); break; case 6: help(); break; case 7: exitSystem(); break; default: cout &lt;&lt; &quot;? 无效选择,请重试&quot; &lt;&lt; endl; } } } private: void showMainMenu() { cout &lt;&lt; &quot;\n====== DeepSeek v3.0测试版 主菜单 ======&quot; &lt;&lt; endl; cout &lt;&lt; &quot;当前模式: &quot; &lt;&lt; (thinker.isDeepMode() ? &quot;?? 深度思考&quot; : &quot;? 快速响应&quot;) &lt;&lt; endl; cout &lt;&lt; &quot;1. ?? 开始对话&quot; &lt;&lt; endl; cout &lt;&lt; &quot;2. ?? 数学求解&quot; &lt;&lt; endl; cout &lt;&lt; &quot;3. ?? 知识查询&quot; &lt;&lt; endl; cout &lt;&lt; &quot;4. ?? 切换思考模式&quot; &lt;&lt; endl; cout &lt;&lt; &quot;5. ?? 查看统计&quot; &lt;&lt; endl; cout &lt;&lt; &quot;6. ? 帮助&quot; &lt;&lt; endl; cout &lt;&lt; &quot;7. ?? 退出&quot; &lt;&lt; endl; cout &lt;&lt; &quot;请选择 (1-7): &quot;; } void startDialogue() { cout &lt;&lt; &quot;\n?? 对话模式 (输入 &#39;exit&#39; 结束对话)&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; string input; while(true) { cout &lt;&lt; &quot;\n你: &quot;; getline(cin, input); if(input == &quot;exit&quot;) { break; } if(input.empty()) { continue; } // 情感识别 string emotion = emotionRec.detectEmotion(input); string emoji = emotionRec.getEmoji(emotion); cout &lt;&lt; &quot;检测到情感: &quot; &lt;&lt; emotion &lt;&lt; &quot; &quot; &lt;&lt; emoji &lt;&lt; endl; // 学习系统记录 learner.recordInteraction(&quot;general&quot;); // 生成回应 thinker.thinking(); string response = dialogue.generateResponse(input, emotion); cout &lt;&lt; &quot;\nDeepSeek: &quot; &lt;&lt; response &lt;&lt; endl; // 添加到对话历史 dialogue.addMessage(&quot;你&quot;, input); dialogue.addMessage(&quot;DeepSeek&quot;, response); // 学习相关概念 if(input.find(&quot;数学&quot;) != string::npos) { learner.recordInteraction(&quot;math&quot;); learner.learnConcept(&quot;数学基础&quot;); } else if(input.find(&quot;编程&quot;) != string::npos || input.find(&quot;代码&quot;) != string::npos) { learner.recordInteraction(&quot;programming&quot;); learner.learnConcept(&quot;编程概念&quot;); } else if(input.find(&quot;科学&quot;) != string::npos) { learner.recordInteraction(&quot;science&quot;); learner.learnConcept(&quot;科学知识&quot;); } } } void mathMode() { cout &lt;&lt; &quot;\n?? 数学求解模式&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; mathSolver.showMathHelp(); cout &lt;&lt; &quot;\n请输入数学表达式 (如: 3 + 4): &quot;; string expression; getline(cin, expression); if(!expression.empty()) { try { thinker.thinking(); mathSolver.solveEquation(expression); // 记录学习 learner.recordInteraction(&quot;math&quot;); learner.learnConcept(&quot;数学运算: &quot; + expression); // 询问是否需要更多帮助 cout &lt;&lt; &quot;\n是否需要查看更多数学信息?(y/n): &quot;; char choice; cin &gt;&gt; choice; cin.ignore(); if(choice == &#39;y&#39; || choice == &#39;Y&#39;) { mathSolver.showMathFacts(); cout &lt;&lt; &quot;\n查看运算示例:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;1. 加法示例&quot; &lt;&lt; endl; cout &lt;&lt; &quot;2. 减法示例&quot; &lt;&lt; endl; cout &lt;&lt; &quot;3. 乘法示例&quot; &lt;&lt; endl; cout &lt;&lt; &quot;4. 除法示例&quot; &lt;&lt; endl; cout &lt;&lt; &quot;请选择 (1-4): &quot;; int exampleChoice; cin &gt;&gt; exampleChoice; cin.ignore(); switch(exampleChoice) { case 1: mathSolver.showExamples(&quot;加法&quot;); break; case 2: mathSolver.showExamples(&quot;减法&quot;); break; case 3: mathSolver.showExamples(&quot;乘法&quot;); break; case 4: mathSolver.showExamples(&quot;除法&quot;); break; } } } catch(const exception&amp; e) { cout &lt;&lt; &quot;? 错误: &quot; &lt;&lt; e.what() &lt;&lt; endl; } } } void knowledgeMode() { cout &lt;&lt; &quot;\n?? 知识查询模式&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; knowledge.showAllCategories(); cout &lt;&lt; &quot;\n请选择知识类别 (输入类别名): &quot;; string category; getline(cin, category); if(knowledge.hasCategory(category)) { thinker.thinking(); cout &lt;&lt; &quot;\n&quot; &lt;&lt; category &lt;&lt; &quot;知识:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; vector&lt;string&gt; items = knowledge.getKnowledge(category); for(const auto&amp; item : items) { cout &lt;&lt; &quot;? &quot; &lt;&lt; item &lt;&lt; endl; } cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; // 记录学习 learner.recordInteraction(category); learner.learnConcept(&quot;知识类别: &quot; + category); } else { cout &lt;&lt; &quot;? 未找到该类别&quot; &lt;&lt; endl; } } void toggleThinkingMode() { if(thinker.isDeepMode()) { thinker.disableDeepMode(); } else { thinker.enableDeepMode(); } } void showStats() { cout &lt;&lt; &quot;\n?? 系统统计信息&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; // 对话统计 cout &lt;&lt; &quot;对话轮数: &quot; &lt;&lt; dialogue.getConversationLength() &lt;&lt; endl; // 情感统计 emotionRec.showEmotionStats(); // 学习统计 learner.showLearningStats(); // 思考模式 cout &lt;&lt; &quot;\n当前思考模式: &quot; &lt;&lt; (thinker.isDeepMode() ? &quot;深度思考&quot; : &quot;快速响应&quot;) &lt;&lt; endl; cout &lt;&lt; &quot;准确率: &quot; &lt;&lt; fixed &lt;&lt; setprecision(1) &lt;&lt; thinker.getAccuracy() * 100 &lt;&lt; &quot;%&quot; &lt;&lt; endl; cout &lt;&lt; &quot;响应类型: &quot; &lt;&lt; thinker.getResponseType() &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } void help() { cout &lt;&lt; &quot;\n? DeepSeek v3.0测试版 帮助&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; cout &lt;&lt; &quot;主要功能:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;1. 对话模式: 进行自然对话,系统会识别你的情感&quot; &lt;&lt; endl; cout &lt;&lt; &quot;2. 数学求解: 解决基本的数学问题&quot; &lt;&lt; endl; cout &lt;&lt; &quot;3. 知识查询: 访问预定义的知识库&quot; &lt;&lt; endl; cout &lt;&lt; &quot;4. 思考模式: 切换深度/快速思考模式&quot; &lt;&lt; endl; cout &lt;&lt; &quot;5. 系统统计: 查看使用统计和学习进度&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; cout &lt;&lt; &quot;使用技巧:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 在对话中使用情感词汇会获得更贴切的回应&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 深度思考模式下准确率更高但响应较慢&quot; &lt;&lt; endl; cout &lt;&lt; &quot;? 系统会学习你的兴趣并调整回应&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; } void exitSystem() { cout &lt;&lt; &quot;\n?? 最终统计报告:&quot; &lt;&lt; endl; cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl; showStats(); cout &lt;&lt; &quot;\n感谢使用 DeepSeek v3.0测试版!&quot; &lt;&lt; endl; cout &lt;&lt; &quot;期待下次与您对话!&quot; &lt;&lt; endl; cout &lt;&lt; &quot;再见!??&quot; &lt;&lt; endl; running = false; } }; int main() { DeepSeek3 deepseek; deepseek.start(); return 0; } 这是代码,对其进行改错并使其能在 Dev - C++ 中运行
最新发布
12-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值