看板 C_and_CPP 關於我們 聯絡資訊
請問各位強者 我想把字串寫入堆疊,然後再從堆疊中取出 請問我的程式該怎麼改!!因為一直出現錯誤 #include <stdio.h> #include <stdlib.h> #define MAXSTACK 100 /*定義最大堆疊容量*/ char stack[MAXSTACK]; //堆疊的陣列宣告 int top=-1; //堆疊的頂端 int isEmpty(); void push(char string); char pop(); int main(int argc, char *argv[]) { char string[3]; int i; printf("請依序輸資料:\n"); fgets(string[0], 100, stdin); push(string) fgets(string[0], 100, stdin); push(string); fgets(string[0], 100, stdin); push(string); printf("====================\n"); while(!isEmpty()){ printf("堆疊彈出的順序為:%d\n",pop()); } pop(); return 0; } /*判斷是否為空堆疊*/ int isEmpty() { if(top==-1) { return 1; } else { return 0; } } /*將指定的資料存入堆疊*/ void push(string) { if(top>=MAXSTACK){ printf("堆疊已滿,無法再加入\n"); } else { top++; stack[top]=string; } } /*從堆疊取出資料*/ char pop(){ char data; if(isEmpty()) { printf("堆疊已空\n"); }else { data=stack[top]; top--; return data; } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.228.242.36 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1528357909.A.6AF.html
Schottky: 字串處理的部分整個錯了 06/07 15:54
jerryh001: fgets用法完全錯誤 06/07 15:54
jerryh001: 而且這種語法錯誤應該會有錯誤訊息 下次請先google 06/07 15:56
sarafciel: 你如果想印字元迴圈內那個printf要用%c才對 06/07 20:08
cphe: 應該不只上面提到的問題....... 06/16 12:06