編碼#include <stdio.h> #include <stdlib.h> /*副程式*/ int hanoiTower(int dishs, int peg1, int peg2, int peg3){ if(dishs == 1) printf("盤子從%d種到%d\n", peg1, peg3); else{ hanoiTower(dishs-1, peg1, peg3, peg2); printf("盤子從%d種到%d\n", peg1, peg3); hanoiTower(dishs-1, peg2, peg1, peg3); } } /*主程式*/ int main(){ hanoiTower(3,1,2,3); system("PAUSE"); return 0; }
編碼(主程式&副程式)需要前置函數stack.c#include <stdio.h> #include <stdlib.h> #include "stack.c" /*副程式*/ int isOpertor(char op){ switch(op){ case '+': case '-': case '*': case '/': return 1; default: return 0; } } /*副程式*/ int cal(int op, int operand1, int operand2){ switch((char)op){ case '*': return(operand2 * operand1); case '/': return(operand2 / operand1); case '+': return(operand2 + operand1); case '-': return(operand2 - operand1); } } /*副程式*/ int postfixEval(char *exp){ int operand1 = 0; int operand2 = 0; int pos; while(exp[pos] != '\0' && exp[pos] != '\n'){ if(isOpertor(exp[pos])){ operand2 = pop(); operand1 = pop(); push(cal(exp[pos], operand2, operand1)); }else push(exp[pos] - 48); pos++; } return pop(); } //主程式 int main(){ char exp[100]; printf("請輸入後序運算式==>"); gets(exp); printf("運算式:%s = %d\n",exp, postfixEval(exp)); system("PAUSE"); return 0; } 檔案
編碼(主程式)//Ch5-3-3 #include <stdio.h> #include <stdlib.h> #include "stack.c" /*檢查輸入是否為運算子*/ int isOperator(char op){ switch(op){ case '(': case ')': case '+': case '-': case '*': case '/': return 1; default: return 0; } } /*檢查輸入運算子的優先度*/ int priority(char op){ switch(op){ case '*': case '/': return 3; case '+': case '-': return 2; case '(': return 1; default: return 0; } } void postfix(char *infix){ int op, doit; int pos = 0; while(infix[pos] != '\0' && infix[pos] != '\n'){ if(isOperator(infix[pos])){ if(isStackEmpty() || infix[pos] == '(') push(infix[pos]); else if(infix[pos] == ')'){ doit = 1; while(doit){ op = pop(); if(op != '(') printf("%c", op); else doit = 0; } } else{ doit = 1; while(doit && !isStackEmpty()){ op = pop(); if(priority(infix[pos]) <= priority(op)) printf("%c", op); else{ push(op); doit = 0; } } push(infix[pos]); } }else printf("%c", infix[pos]); pos++; } while(!isStackEmpty()) printf("%c", pop()); printf("\n"); } int main(){ char exp[100]; printf("請輸入中序運算子==>"); gets(exp); printf("後序運算式:"); postfix(exp); return 0; } 編碼(宣告結構及副程式)//stack.c
struct Node { /* 堆疊節點的宣告 */ int data; /* 儲存堆疊資料 */ struct Node *next; /* 指向下一節點 */ }; typedef struct Node SNode; /* 堆疊節點的新型態 */ typedef SNode *LStack; /* 串列堆疊的新型態 */ LStack top = NULL; /* 堆疊頂端的指標 */ /* 抽象資料型態的操作函數宣告 */ extern int isStackEmpty(); extern void push(int d); extern int pop(); /* 函數: 檢查堆疊是否是空的 */ int isStackEmpty() { if ( top == NULL ) return 1; else return 0; } /* 函數: 將資料存入堆疊 */ void push(int d) { LStack new_node; /* 配置節點記憶體 */ new_node = (LStack)malloc(sizeof(SNode)); new_node->data = d; new_node->next = top; top = new_node; } /* 函數: 從堆疊取出資料 */ int pop() { LStack ptr; int temp; if ( !isStackEmpty() ) { ptr = top; top = top->next; temp = ptr->data; /* 取出資料 */ free(ptr); return temp; } else return -1; } #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; typedef struct node Node; Node* creates(void); // 建立堆疊 int isEmpty(Node*); // 堆疊已空 int stacktop(Node*); // 傳回頂端元素 Node* add(Node*, int); // 新增元素 Node* delete(Node*); // 刪除元素 void list(Node*); // 顯示所有內容 int main(void) { Node* sTop; int input, select; sTop = creates(); while(1) { printf("\n\n請輸入選項(-1結束):"); printf("\n(1)插入值至堆疊"); printf("\n(2)顯示堆疊頂端"); printf("\n(3)刪除頂端值"); printf("\n(4)顯示所有內容"); printf("\n\$c>"); scanf("%d", &select); if(select == -1) break; switch(select) { case 1: printf("\n輸入值:"); scanf("%d", &input); sTop = add(sTop, input); break; case 2: printf("\n頂端值:%d", stacktop(sTop)); break; case 3: sTop = delete(sTop); break; case 4: list(sTop); break; default: printf("\n選項錯誤!"); } } printf("\n"); return 0; } Node* creates() { return NULL; } int isEmpty(Node* top) { return (top == NULL); } int stacktop(Node* top) { return top->data; } Node* add(Node* top, int item) { Node* newnode; newnode = (Node*) malloc(sizeof(Node)); if(newnode == NULL) { printf("\n記憶體配置失敗!"); exit(1); } newnode->data = item; newnode->next = top; top = newnode; return top; } Node* delete(Node* top) { Node* tmpnode; tmpnode = top; if(tmpnode == NULL) { printf("\n堆疊已空!"); return NULL; } top = top->next; free(tmpnode); return top; } void list(Node* top) { Node* tmpnode; tmpnode = top; printf("\n堆疊內容:"); while(tmpnode != NULL) { printf("%d ", tmpnode->data); tmpnode = tmpnode->next; } }
編碼#include <stdio.h> |
demo.c | |
File Size: | 4 kb |
File Type: | c |