看板 C_and_CPP 關於我們 聯絡資訊
原文恕刪 用setjmp/longjmp來模擬while loop 這裡有測試結果 http://ideone.com/PetGBe 以下是程式碼 #include <stdio.h> #include <setjmp.h> int abs(int value) { jmp_buf buf; char buffer[16]; snprintf(buffer, sizeof(buffer), "%d+", value); char *cursor = buffer; switch (setjmp(buf)) { case 0: ++cursor; longjmp(buf, *cursor); case '-': *cursor = ' '; ++cursor; longjmp(buf, *cursor); case '+': *cursor = '\0'; sscanf(buffer, "%d", &value); return value; default: ++cursor; longjmp(buf, *cursor); } } int main(void) { int value; while (1 == scanf("%d", &value)) { printf("%d\n", abs(value)); } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.147.112 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1460389303.A.EAA.html
wtchen: 請勿再回文,原作者已射後不理(雖然整個討論挺有趣的) 04/12 02:55
Schottky: 我倒覺得像這種狀況,大家玩得開心就好 04/12 03:07
Schottky: 已經沒有人在管原PO到底為何有這種怪要求了 XD 04/12 03:08
wtchen: 是挺開心的,不過3篇回文也夠了 04/12 03:46
wtchen: 見好就收 04/12 03:47