無聊題,中綴表達式。一口氣寫了290行,就如炎炎烈日下當頭一兜涼水般的爽快。
#include <iostream> #include <string> #include <vector> #include <stack> #include <cctype> using namespace std; string str; int cs = 1; int lev ( char c ) { if ( c == '+' || c == '-' ) return 1; if ( c == '*' ) return 2; if ( c == '(' || c == '@' ) return 0; return -1; } void print ( const vector <int> &v ) { int i; for ( i = 0; i < v.size (); i ++ ) { if ( v[i] == -1000 ) printf ( "x" ); else if ( v[i] >= 0 ) printf ( "%d", v[i] ); else printf ( "%c", v[i] + 500 ); } printf ( "/n" ); } void proc () { int i = str.find_first_of ( '=' ); string left = str.substr ( 0, i ); string right = str.substr ( i + 1, str.size () ); left += '@', right += '@'; stack <int> is; stack <int> xs; stack <char> cs; vector <int> iv; int sum = 0; cs.push ( '@' ); for ( i = 0; i < left.size (); i ++ ) { if ( left[i] == 'x' ) { iv.push_back ( -1000 ); } else if ( isdigit ( left[i] ) ) { sum = sum * 10 + left[i] - '0'; if ( !isdigit ( left[i + 1] ) ) { iv.push_back ( sum ); sum = 0; } } else if ( left[i] != '@' ) { if ( left[i] == '(' ) { cs.push ( '(' ); } else if ( left[i] == ')' ) { while ( cs.top () != '(' ) { iv.push_back ( cs.top () - 500 ); cs.pop (); } cs.pop (); } else { char ch = left[i]; if ( lev ( ch ) > lev ( cs.top () ) ) cs.push ( ch ); else { while ( lev ( cs.top () ) >= lev ( ch ) ) { iv.push_back ( cs.top () - 500 ); cs.pop (); } cs.push ( ch ); } } } else { while ( cs.top () != '@' ) { iv.push_back ( cs.top () - 500 ); cs.pop (); } } } int la = 0, lb = 0; // ax = b for ( i = 0; i < iv.size (); i ++ ) { if ( iv[i] >= 0 ) { is.push ( iv[i] ); xs.push ( 0 ); } else if ( iv[i] == -1000 ) { is.push ( 0 ); xs.push ( 1 ); } else { char ch = iv[i] + 500; int a1 = xs.top (); xs.pop (); int a2 = xs.top (); xs.pop (); int b1 = is.top (); is.pop (); int b2 = is.top (); is.pop (); if ( ch == '+' ) { xs.push ( a1 + a2 ); is.push ( b1 + b2 ); } if ( ch == '-' ) { xs.push ( a1 - a2 ); is.push ( b1 - b2 ); } if ( ch == '*' ) { if ( a1 == 0 && a2 == 0 ) xs.push ( 0 ); if ( a1 == 0 && a2 != 0 ) xs.push ( a2 * b1 ); if ( a2 == 0 && a1 != 0 ) xs.push ( a1 * b2 ); is.push ( b1 * b2 ); } } } la = xs.top (); lb = is.top (); xs.pop (); is.pop (); //printf ( "%d %d/n", a, b ); //print ( iv ); iv.clear (); for ( i = 0; i < right.size (); i ++ ) { if ( right[i] == 'x' ) { iv.push_back ( -1000 ); } else if ( isdigit ( right[i] ) ) { sum = sum * 10 + right[i] - '0'; if ( !isdigit ( right[i + 1] ) ) { iv.push_back ( sum ); sum = 0; } } else if ( right[i] != '@' ) { if ( right[i] == '(' ) { cs.push ( '(' ); } else if ( right[i] == ')' ) { while ( cs.top () != '(' ) { iv.push_back ( cs.top () - 500 ); cs.pop (); } cs.pop (); } else { char ch = right[i]; if ( lev ( ch ) > lev ( cs.top () ) ) cs.push ( ch ); else { while ( lev ( cs.top () ) >= lev ( ch ) ) { iv.push_back ( cs.top () - 500 ); cs.pop (); } cs.push ( ch ); } } } else { while ( cs.top () != '@' ) { iv.push_back ( cs.top () - 500 ); cs.pop (); } } } int ra = 0, rb = 0; // ax = b for ( i = 0; i < iv.size (); i ++ ) { if ( iv[i] >= 0 ) { is.push ( iv[i] ); xs.push ( 0 ); } else if ( iv[i] == -1000 ) { is.push ( 0 ); xs.push ( 1 ); } else { char ch = iv[i] + 500; int a1 = xs.top (); xs.pop (); int a2 = xs.top (); xs.pop (); int b1 = is.top (); is.pop (); int b2 = is.top (); is.pop (); if ( ch == '+' ) { xs.push ( a1 + a2 ); is.push ( b1 + b2 ); } if ( ch == '-' ) { xs.push ( a1 - a2 ); is.push ( b1 - b2 ); } if ( ch == '*' ) { if ( a1 == 0 && a2 == 0 ) xs.push ( 0 ); if ( a1 == 0 && a2 != 0 ) xs.push ( a2 * b1 ); if ( a2 == 0 && a1 != 0 ) xs.push ( a1 * b2 ); is.push ( b1 * b2 ); } } } ra = xs.top (); rb = is.top (); xs.pop (); is.pop (); //printf ( "%d %d/n", la, lb ); //printf ( "%d %d/n", ra, rb ); int a = la - ra, b = rb - lb; if ( a == 0 && b == 0 ) printf ( "Infinitely many solutions./n" ); else if ( a == 0 && b != 0 ) printf ( "No solution./n" ); else printf ( "x = %f/n", double ( b ) / double ( a ) ); } int main () { //freopen ( "in.txt", "r", stdin ); while ( getline ( cin, str ) ) { printf ( "Equation #%d/n", cs ++ ); proc (); printf ( "/n" ); } return 0; }
本文分享自華爲雲社區《10分鐘搞懂各種內存溢出案例!!(含完整源碼,建議收藏)》,作者:冰 河。 作爲程序員,多多少少都會遇到一些內存溢出的場景,如果你還沒遇到,說明你工作的年限可能比較短,或者你根本就是個假程序員!哈哈,開個玩笑。今天,我
Ai technology 前言 AppBuilder上線了低代碼製作組件功能,可以通過工作流的方式構建自定義組件,完成簡單Agent無法完成的複雜功能,使得生成的文本更加定製化,
最近在刷一些問題的時候看到有下面一個問題 上面問的是當輸入的字符串爲什麼的時候返回 True 總結 在做題目的時候,第一次還做錯了。 這是因爲解答這個題目的時間只有 3 分鐘,沒有自己看題目 後來拿着程序跑了下。 p
簡介: 搶紅包是我們生活常用的社交功能, 這個功能最主要的特點就是用戶的併發請求高, 在系統設計上, 可以使用非常多的辦法來扛住用戶的高併發請求, 在本文中簡要介紹使用Redis緩存中間件來實現搶紅包算法, Redis是一個在內存中基
ClickHouse以性能好被大家所熟知,而一個數據庫的性能優化是一個龐大的系統性工程。本文着眼於ClickHouse內部的基礎數據結構,以揭露ClickHouse性能優化的冰山一角。 在軟件工程中並不是所有的執行路徑都需要優化,只有關鍵執
本文分享自華爲雲社區《【華爲雲MySQL技術專欄】MySQL內存增長問題分析案例》,作者:GaussDB 數據庫。 前言 在現網環境中,偶爾會遇到客戶實例內存OOM(Out Of Memory,即內存耗盡或溢出)的情況。MySQL數據庫
背景 這兩年來大模型及其熱門,不僅各大廠家的模型層出不窮,各類RGA、Agent應用也花樣繁多。這也帶火了一批基礎設施,比如Langchain、向量數據庫(也叫矢量數據庫-Vector Database)等。現在市場上的向量庫種類特別繁多
本文分享自華爲雲社區《MySQL全文索引源碼剖析之Insert語句執行過程》 ,作者:GaussDB 數據庫。 1. 背景介紹 全文索引是信息檢索領域的一種常用的技術手段,用於全文搜索問題,即根據單詞,搜索包含該單詞的文檔,比如在瀏覽器
二叉樹 「二叉樹 binary tree」是一種非線性數據結構,代表“祖先”與“後代”之間的派生關係,體現了“一分爲二” 的分治邏輯。與鏈表類似,二叉樹的基本單元是節點,每個節點包含值、左子節點引用和右子節點引用。 每個節點都有兩個引
本文主要內容 背景 在Java語言中,程序運行的時候,會產生很多對象,而對象信息也只是在程序運行的時候纔在內存中保持其狀態,一旦程序停止,內存釋放,對象也就不存在了。 怎麼能讓對象永久的保存下來呢?--------對象序列化 。 何
List 是Java中非常常用的數據類型。 List 是有序的 Collection。 Java List 一共三個實現類:分別是 ArrayList、 Vector 和 LinkedList。 ArrayList(數組) Array
TiDB Serverless 上的向量化功能終於開始邀約體驗啦!本文是來自 TiDB 社區用戶對 TiDB Vector 功能初體驗的詳細分享,hey-hoho 介紹了他從申請體驗到實際操作的全過程,包括創建 TiDB Vector 實例
Zilliz 持續爲 AI 應用開發者賦能! 近期,Zilliz 與智源研究院達成合作,將多種 BGE(BAAI General Embedding) 開源模型與開源向量數據庫 Milvus 集成。得益於 Milvus 2.4 最新推出的
結構特點 List 和 Set 是存儲單列數據的集合,Map 是存儲鍵和值這樣的雙列數據的集合;List 中存儲的數據是有順序,並且允許重複;Map 中存儲的數據是沒有順序的,其鍵是不能重複的,它的值是可以有重複的,Set 中存儲的數據
2024年3月22日,期待已久的ISO 11898-2:2024版本橫空出世,標誌着新一代CAN總線的時代即將到來(ISO/FDIS 11898-1:2023也處於最終投票階段)。搭載着2048Byte的CANXL列車已蓄勢