# c語言 Stack作業 [找高手]

(a) A+B-C*D

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

Rating

已此程式作修改吧!!~

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

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

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

*/

/*結構定義*/

struct DataStack{

float data;

struct DataStack *next;

};

typedef struct DataStack DataNode;

struct OperaStack{

char opera;

struct OperaStack *next;

};

typedef struct OperaStack OperaNode;

/*相關函式*/

new_node->data=value;

new_node->next=stack;

stack=new_node;

return stack;

}

new_node->opera=opera;

new_node->next=stack;

stack=new_node;

return stack;

}

top = stack ;

stack = stack->next;

*temp=top->data;

free(top);

return stack;

}

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;

//開啟檔案

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 補充：

Source(s): 自己+上課範例程式碼