1 // PRJ.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include "math.h" 6 #include "stdlib.h" 7 8 typedef struct charType 9 { 10 char charArray[30]; 11 int top,buttom; 12 } CT; 13 14 typedef struct DoubleType 15 { 16 double doubleArray[30]; 17 int top,buttom; 18 } DT; 19 20 //如下代码 是使得字符串转为双精度浮点 21 double chartof(char arr[],int n) 22 { 23 int i,k=-1,jishu=1,su=1; 24 long t=0; 25 for(i=0;i<n;i++) 26 { 27 if(arr[i]==‘.‘){k=i;} 28 else{ 29 t*=jishu; 30 t+=(int)arr[i]-0x30; 31 jishu=10; 32 } 33 } 34 if(k==-1)return (double)t; 35 else return (double)t/pow(10,n-k-1); 36 37 } 38 //转换为浮点结束 39 40 //数据结构-浮点栈 和它的三大操作 D_INIT D_PUSH D_POP 41 42 43 void D_INIT(DT &dt) 44 { 45 dt.top=dt.buttom=0; 46 } 47 48 void D_PUSH(DT &dt,double d) 49 { 50 dt.doubleArray[dt.top++]=d; 51 } 52 53 double D_POP(DT &dt) 54 { 55 56 return dt.doubleArray[--dt.top]; 57 } 58 // 数据结构 完毕 59 60 61 //数据结构-操作栈 和它的三大操作 C_INIT C_PUSH C_POP 62 63 64 void C_INIT(CT & ct) 65 { 66 ct.top=ct.buttom=0; 67 } 68 69 void C_PUSH(CT &ct,char c) 70 { 71 ct.charArray[ct.top++]=c; 72 } 73 74 char C_POP(CT &ct) 75 { 76 return ct.charArray[--ct.top]; 77 } 78 // 数据结构 完毕 79 80 81 int main(int argc, char* argv[]) 82 { 83 84 char shu[100];int i=0,j=0; 85 double f,ff; char c,cc,record; 86 char temp[100]; 87 88 ///初始化 89 DT dt;CT ct; 90 D_INIT(dt);C_INIT(ct); 91 92 ///初始化结束 93 C_PUSH(ct,‘#‘);//栈底为# 94 record=‘c‘; 95 printf("请您输入一个四则运算表达式,以等号结束!\n"); 96 97 while((c=getchar())!=‘\n‘) 98 { 99 100 101 102 temp[j++]=c; 103 //压栈问题 104 if(c>=‘0‘&&c<=‘9‘||c==‘.‘){ 105 shu[i++]=c; 106 107 } 108 else 109 { 110 if(i!=0){ f=chartof(shu,i);D_PUSH(dt,f);record=‘d‘;i=0;} 111 112 while((cc=C_POP(ct))!=‘#‘) 113 { 114 115 switch(c) 116 { 117 case ‘+‘: 118 if(cc==‘(‘) {C_PUSH(ct,cc); goto label1;} 119 if(cc==‘+‘) {ff=D_POP(dt);ff+=D_POP(dt);D_PUSH(dt,ff);} 120 if(cc==‘-‘) {ff=D_POP(dt);ff=D_POP(dt)-ff;D_PUSH(dt,ff);} 121 if(cc==‘*‘) {ff=D_POP(dt);ff*=D_POP(dt);D_PUSH(dt,ff);} 122 if(cc==‘/‘) {ff=D_POP(dt);ff=D_POP(dt)/ff;D_PUSH(dt,ff);} 123 break; 124 125 case ‘-‘: 126 if(cc==‘(‘) {C_PUSH(ct,cc); goto label1;} 127 if(cc==‘+‘) {ff=D_POP(dt);ff+=D_POP(dt);D_PUSH(dt,ff);} 128 if(cc==‘-‘) {ff=D_POP(dt);ff=D_POP(dt)-ff;D_PUSH(dt,ff);} 129 if(cc==‘*‘) {ff=D_POP(dt);ff*=D_POP(dt);D_PUSH(dt,ff);} 130 if(cc==‘/‘) {ff=D_POP(dt);ff=D_POP(dt)/ff;D_PUSH(dt,ff);} 131 break; 132 133 case ‘*‘: 134 if(cc==‘(‘) {C_PUSH(ct,cc); goto label1;} 135 if(cc==‘+‘) {C_PUSH(ct,‘+‘);goto label1;} 136 if(cc==‘-‘) {C_PUSH(ct,‘-‘);goto label1;} 137 if(cc==‘*‘) {ff=D_POP(dt);ff*=D_POP(dt);D_PUSH(dt,ff);} 138 if(cc==‘/‘) {ff=D_POP(dt);ff=D_POP(dt)/ff;D_PUSH(dt,ff);} 139 break; 140 141 case ‘/‘: 142 if(cc==‘(‘) {C_PUSH(ct,cc); goto label1;} 143 if(cc==‘+‘) {C_PUSH(ct,‘+‘);goto label1;} 144 if(cc==‘-‘) {C_PUSH(ct,‘-‘);goto label1;} 145 if(cc==‘*‘) {ff=D_POP(dt);ff*=D_POP(dt);D_PUSH(dt,ff);} 146 if(cc==‘/‘) {ff=D_POP(dt);ff=D_POP(dt)/ff;D_PUSH(dt,ff);} 147 break; 148 149 case ‘=‘: 150 151 if(cc==‘+‘) {ff=D_POP(dt);ff+=D_POP(dt);D_PUSH(dt,ff);} 152 if(cc==‘-‘) {ff=D_POP(dt);ff=D_POP(dt)-ff;D_PUSH(dt,ff);} 153 if(cc==‘*‘) {ff=D_POP(dt);ff*=D_POP(dt);D_PUSH(dt,ff);} 154 if(cc==‘/‘) {ff=D_POP(dt);ff=D_POP(dt)/ff;D_PUSH(dt,ff);} 155 break; 156 157 case ‘(‘: 158 C_PUSH(ct,cc); 159 goto label1; 160 break; 161 case ‘)‘: 162 if(cc==‘+‘) {ff=D_POP(dt);ff+=D_POP(dt);D_PUSH(dt,ff);} 163 if(cc==‘-‘) {ff=D_POP(dt);ff=D_POP(dt)-ff;D_PUSH(dt,ff);} 164 if(cc==‘*‘) {ff=D_POP(dt);ff*=D_POP(dt);D_PUSH(dt,ff);} 165 if(cc==‘/‘) {ff=D_POP(dt);ff=D_POP(dt)/ff;D_PUSH(dt,ff);} 166 if(cc==‘(‘) {goto label2;} 167 break; 168 169 default: 170 printf("格式错误!\n"); 171 172 return 0; 173 174 break; 175 176 } 177 /////运算处理结束 178 179 180 181 182 } 183 184 C_PUSH(ct,‘#‘); 185 186 label1: if(ct.top!=ct.buttom){ cc=C_POP(ct); 187 188 if((record==‘c‘||record==‘a‘)&&cc==‘(‘&&c==‘-‘){C_PUSH(ct,cc);D_PUSH(dt,0.0f);} 189 else C_PUSH(ct,cc); 190 } 191 C_PUSH(ct,c); 192 record=‘c‘; 193 194 195 } //栈内处理 196 197 198 199 200 label2: record=‘a‘; 201 202 }//所有文字输入while结束 203 204 temp[j]=‘\0‘; 205 206 printf("%s%.2f\n",temp,D_POP(dt)); 207 208 209 return 0; 210 }
时间: 2024-10-12 13:40:04