Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c語言 Stack作業 [找高手]

試寫c語言程式將輸入之任意中置式(Infix) 改成末置式(Postfix) , 令A=9 , B=3 , C=5 , D=2 並以末置式處理symbol 及operator方式逐步計算其值。

(a) A+B-C*D

(b) (A+B)*(C-D)/(A-B)

請用c語言撰寫,不要用c++喔,謝謝!

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    已此程式作修改吧!!~

    此為本人上課範例程式碼,是以檔案輸入,這部份可以自行修正。

    /*可算出浮點值來及可加入大中小括弧

    Design a program that calculates the value of an arithmetic expression.

    */

    /*結構定義*/

    struct DataStack{

    float data;

    struct DataStack *next;

    };

    typedef struct DataStack DataNode;

    typedef DataNode *DataLink;

    struct OperaStack{

    char opera;

    struct OperaStack *next;

    };

    typedef struct OperaStack OperaNode;

    typedef OperaNode *OperaLink;

    /*相關函式*/

    DataLink Dpush(DataLink stack,float value){

    DataLink new_node;

    new_node=(DataLink)malloc(sizeof(DataNode));

    new_node->data=value;

    new_node->next=stack;

    stack=new_node;

    return stack;

    }

    OperaLink Opush(OperaLink stack,char opera){

    OperaLink new_node;

    new_node=(OperaLink)malloc(sizeof(OperaNode));

    new_node->opera=opera;

    new_node->next=stack;

    stack=new_node;

    return stack;

    }

    DataLink Dpop(DataLink stack,float *temp){

    DataLink top;

    top = stack ;

    stack = stack->next;

    *temp=top->data;

    free(top);

    return stack;

    }

    OperaLink Opop(OperaLink stack,char *temp){

    OperaLink top;

    top = stack ;

    stack = stack->next;

    *temp=top->opera;

    free(top);

    return stack;

    }

    float convert(float n1,float n2,char opera){

    switch(opera){

    case'+':return (n1+n2);

    case'-':return (n1-n2);

    case'*':return (n1*n2);

    case'/':return (n1/n2);

    default:return 0;

    }

    }

    int priority(char opera){

    switch(opera){

    case'+':

    case'-':return 0;

    case'*':

    case'/':return 1;

    default:return -1;

    }

    }

    #include<stdio.h>

    #include<stdlib.h>

    #include<ctype.h>

    #include"tree.h"

    char string[256];//字串最大可輸入長度

    void input(void);

    float Solution(char *string);

    void output(float solution);

    void main(void)

    {

    float solution;

    input();

    solution=Solution(string);

    output(solution);

    }

    2007-11-30 10:59:40 補充:

    void input(void)

    {

    char filename[10];

    FILE *fp;

    //開啟檔案

    printf("Please input filename:");

    scanf("%s",filename);

    fp=fopen(filename,"r");

    if(fp==NULL)

    printf("File open error");

    fscanf(fp,"%s",string);

    fclose(fp);

    printf("%s",string);

    }

    2007-11-30 10:59:58 補充:

    void output(float solution)

    {

    printf("=%f&#92;n",solution);

    }

    2007-11-30 11:00:18 補充:

    void output(float solution)

    {

    printf("=%f&#92;n",solution);

    }

    2007-11-30 11:03:34 補充:

    放棄,請至http://163.13.132.116/My/2007-11-30.txt%E4%B8%8B%E...

    Source(s): 自己+上課範例程式碼
Still have questions? Get your answers by asking now.