|
- #include "stdio.h"
- #include "math.h"
- #include "stdlib.h"
- struct OperationStack {
- char elem[100];
- short top;
- } operation;
- struct DataStack{
- float elem[100];
- short top;
- } opnd;
- char w,theta,choose;
- float result1,a,b,data;
- char precede[17][17];
- FILE *f;
- void Initialization(void);
- void Read(void);
- char GetOpStackTop(void);
- int MyOrder(char);
- void PushOperationStack(char w);
- char PopOperationStack(void);
- int main() {
- float result;
- Initialization();
- choose='Y';
- do {
- opnd.top=0 ;
- operation.top=0 ;
- PushOperationStack('=');/*INI_STACK*/
- Read();
- while (!((w=='=')&&(GetOpStackTop()=='='))) /*当运算符不只=时循环*/
- switch (precede[MyOrder(GetOpStackTop())][MyOrder(w)])
- {
- case '<': PushOperationStack(w);/*栈顶运算符比目前读入运算优先级小
- 当前读入的运算符入栈*/
- Read();/*读入下一个字符 */
- break;
- case '=': //char PopOperationStack(void);/*优先级相等时即'(',')'相遇时脱括号*/
- (operation.top)--;
- Read();
- break;
- case '>': theta=PopOperationStack();/*栈顶运算符比目前读入运算优先级高
- 则退栈并将结果入栈作下一运算的操作数*/
- float PopData(void);
- a=PopData();
- float PushData(float data);
- float operate(float,char);
- PushData(operate(a,theta));
- break;
- case '!': printf("Error!Check the input.\n");
- //goto a_b;
- default:break;
- }
- float PopData(void);
- printf("%f\n",PopData());
- printf("Continue?Y/N:");
- scanf("%c",&choose);
- }while (choose=='Y');
- //switch (choose)
- //{case 'Y': goto a_a;break;
- // default : printf("Thank you!\n");
- // }
- return;
- }
- void PushOperationStack(char w) {
- (operation.top)++;
- operation.elem[operation.top]=w;
- return;
- }
- void PushData(float data){
- (opnd.top)++;
- opnd.elem[opnd.top]=data;
- return;
- }
- char PopOperationStack(void) {
- char op;
- op=operation.elem[operation.top];
- (operation.top)--;
- return op;
- }
- float PopData(void) {
- float num;
- num=opnd.elem[opnd.top];
- (opnd.top)--;
- return num;
- }
- char GetOpStackTop(void) {
- return operation.elem[operation.top];
- }
- float gettop_data(void){
- return opnd.elem[opnd.top];
- }
- void Read(void) {
- float data;
- short scale;
- BEGIN_1:scanf("%c",&w);
- if ((w>='0')&&(w<='9')) {
- data=0 ;
- while ((w>='0')&&(w<='9')) {
- data=data*10+w-'0';
- scanf("%c",&w);
- }
- }/*以上读入整数部分,下面读入小数部分*/
- if (w=='.') {
- scale=1;
- scanf("%c",&w);
- while (w>='0'&&w<='9'){
- data=data*10 +w-48;/*48为'0'的ASCII值*/
- (scale)*=10;
- scanf("%c",&w);
- }
- (data)/=scale;/*小数点左移得到数据的真值*/
- }
- PushData(data);
- /*上面结束后W必定不在'0'..'9'之间*/
- /*下面主要是对三角函数和max,min运算符的读入*/
- switch (w) {
- case 'S': /*读入三角正弦w赋值为'B'*/
- case 's':scanf("%c",&w);
- switch (w) {
- case 'I':
- case 'i':scanf("%c",&w);
- if ( ( w=='n' ) || ( w=='N' ) ){
- w='B';
- }
- break;
- case 'E':
- case 'e':scanf("%c",&w);
- if ( ( w=='c' ) || ( w=='C' ) ){
- w='F';
- }
- break;
- }
- break;
- case 'C':
- case 'c':scanf("%c",&w);
- switch (w) {
- case 'O':
- case 'o':scanf("%c",&w);
- if ( ( w=='s' ) || ( w=='S' ) ){
- w='C';/*对应cos*/
- }
- else if ( ( w=='t' ) || ( w=='T') ){
- w='E';/*对应cot*/
- }
- break;
- case 'T':
- case 't':scanf("%c",&w);
- if ( ( w=='g' ) || ( w=='G' ) ){
- w='E';/*对应ctg*/
- }
- break;
- case 'S':
- case 's':scanf("%c",&w);
- if ( ( w=='c' ) || ( w=='C' ) ){
- w='G';/*对应csc*/
- }
- break;
- }
- break;
- case 'T':
- case 't':if ( ( getchar()=='a' ) || ( getchar()=='A' )){
- if ( ( getchar()=='n' ) || ( getchar()=='N' ) ){
- w='D';/*对应tan*/
- }
- }
- else if ( ( getchar()=='g' ) || ( getchar()=='G' ) ){
- w='D';/*对应tg*/
- }
- break;
- case 'M':
- case 'm':scanf("%c",&w);
- switch (w) {
- case 'A':
- case 'a':if ( ( getchar()=='a' ) || ( getchar()=='A' ) ) {
- if ( ( getchar()=='x' ) || ( getchar()=='X' ) ){
- w='H';/*对应max*/
- }
- }
- break;
- case 'I':
- case 'i':if ( ( getchar()=='n' ) || ( getchar()=='N' ) ){
- w='I';/*对应min*/
- }
- break;
- }
- break;
- case ',':goto BEGIN_1;/*跳转后为max和min读入下一个参数*/
- default:;/*此处省略了读入+,-,*,/,%,^等符号时的空操作*/
- }
- return;
- }
- float operate(float a,char theta) {
- float b;
- int c,d;
- switch (theta) {
- case '+':b=PopData();
- return a+b;
- case '-':b=PopData();
- return b-a;
- case '*':b=PopData();
- return a*b;
- case '/':b=PopData();
- if (b!=0)//I really don't
- //know why there is a error.
- return b/a;
- case '%':c=floor(a);
- b=PopData();
- d=floor(b);
- return d/c;/*库函数:取余 */
- case 'A':b=PopData();
- return pow(a,b);/*POWER(a,b)*/
- case 'B':return sin(a);
- case 'C':return cos(a);
- case 'D':return tan(a);
- case 'E':return 1/tan(a);
- case 'F':return 1/cos(a);
- case 'G':return 1/sin(a);
- case 'H':b=PopData();
- return (a>b?a:b);
- case 'I':b=PopData();
- return (a<b?a:b);
- default:break;
- }
- }
- void Initialization() {
- short i=0,j=0;
- char tmp_c='\0';
-
- if ((f=fopen("for_ok.txt","r"))==NULL){
- printf("open file error\n");
- exit(-1);
- };
- while (!feof(f)) {
- j=0;
- tmp_c=fgetc(f);
- //while (!(j==17)) {
- while ( tmp_c != '\n' ) {
- precede[i][j]=tmp_c;
- if (!feof(f)) tmp_c=fgetc(f);
- else break;
- j++;
- }
- i++;
- }
- fclose(f);
- }
- int MyOrder(char c){
- switch (c){
- case '^':return 0;/*POW*/
- case 'B':return 1;/*SIN*/
- case 'C':return 2;/*COS*/
- case 'D':return 3;/*TAN*/
- case 'E':return 4;/*CTAN*/
- case 'F':return 5;/*SEC*/
- case 'G':return 6;/*CSC*/
- case 'H':return 7;/*MAX*/
- case 'I':return 8;/*MIN*/
- case '(':return 9;
- case ')':return 10;
- case '+':return 11;
- case '_':return 12;
- case '*':return 13;
- case '/':return 14;
- case '%':return 15;
- case '=':return 16;
- }
- }
复制代码 |
|