Data_Structure/作業/exam/exam_2.cpp

297 lines
9.3 KiB
C++
Raw Permalink Normal View History

2025-01-20 21:25:33 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define MAX 100
// === <20><><EFBFBD><EFBFBD><EFBFBD>ܼƫŧi ===
char input[MAX];
char postfix[MAX];
char postfix_1[MAX]; //<2F><><EFBFBD>ܥ<EFBFBD>
char stack[MAX];
double numStack[MAX];
int top = -1;
int numTop = -1;
// === <20><EFBFBD>ŧi ===
int get_priority(char op);
int is_operator(char c);
double calculate(double a, double b, char op);
void convert_to_postfix();
double evaluate_postfix();
double evaluate_infix(char *expr);
double parse_number(char **expr);
int get_priority(char c){
if(c=='+' || c=='-'){
return 1;
2025-01-20 21:30:53 +08:00
}else if(c=='*' || c=='/' || c=='%' ){
2025-01-20 21:25:33 +08:00
return 2;
2025-01-20 21:30:53 +08:00
}else if(c=='^' || c=='s' || c=='c' ){
2025-01-20 21:25:33 +08:00
return 3;
}else{
return 0;
}
}
// === <20>ˬd<CBAC>r<EFBFBD><72><EFBFBD>O<EFBFBD>_<EFBFBD><5F><EFBFBD>B<EFBFBD><42><EFBFBD>l ===
int is_operator(char c) {
2025-01-20 21:30:53 +08:00
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c == '%' ||c == 's' || c == 'c');
2025-01-20 21:25:33 +08:00
}
// === <20><><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD><42> ===
double calculate(double a, double b, char op) {
switch(op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/':
if(b != 0) return a / b;
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD><47><EFBFBD>Ƥ<EFBFBD><C6A4><EFBFBD>s<EFBFBD>I\n");
exit(1);
case '^': return pow(a, b);
2025-01-20 21:30:53 +08:00
case '%': return (int)a % (int)b;
case 's': return sin(b);
case 'c': return cos(b);
2025-01-20 21:25:33 +08:00
default: return 0;
}
}
//<2F><><EFBFBD>ܥ<EFBFBD>
void convert_to_postfix_forprint() {
int i = 0, j;
int postfix_index = 0;
top = -1;
char number_str[MAX];
int is_negative;
while(input[i] != '\0') {
2025-01-20 21:30:53 +08:00
if(isdigit(input[i]) || input[i] == '.' || (input[i] == '-' && (i == 0 || input[i-1] == '(' || is_operator(input[i-1])))) {
2025-01-20 21:25:33 +08:00
// <20>B<EFBFBD>z<EFBFBD>Ʀr<C6A6>]<5D>]<5D>A<EFBFBD>t<EFBFBD>ơ^
is_negative = 0;
int num_len = 0;
// <20>ˬd<CBAC>O<EFBFBD>_<EFBFBD><5F><EFBFBD>t<EFBFBD><74>
if(input[i] == '-') {
is_negative = 1;
i++;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʀr<C6A6>r<EFBFBD><72>
while(input[i] != '\0' && (isdigit(input[i]) || input[i] == '.')) {
number_str[num_len++] = input[i++];
}
number_str[num_len] = '\0';
i--; // <20>^<5E>h<EFBFBD>@<40>Ӧ<EFBFBD><D3A6>m<EFBFBD>A<EFBFBD>]<5D><><EFBFBD>~<7E>h while <20>|<7C>A<EFBFBD>[<5B>@
// <20><><EFBFBD>X<EFBFBD>Ʀr
printf("%s ", number_str);
if(is_negative) {
printf("- ");
}
// <20>N<EFBFBD>Ʀr<C6A6>[<5B>J<EFBFBD><4A><EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46>
for(j = 0; j < num_len; j++) {
postfix_1[postfix_index++] = number_str[j];
}
postfix_1[postfix_index++] = ' ';
if(is_negative) {
postfix_1[postfix_index++] = '-';
postfix_1[postfix_index++] = ' ';
}
}
else if(input[i] == '(') {
stack[++top] = input[i];
}
else if(input[i] == ')') {
while(top >= 0 && stack[top] != '(') {
printf("%c ", stack[top]);
postfix_1[postfix_index++] = stack[top];
postfix_1[postfix_index++] = ' ';
top--;
}
if(top >= 0) top--;
}
else if(is_operator(input[i])) {
// <20>p<EFBFBD>G<EFBFBD><47><EFBFBD>O<EFBFBD>B<EFBFBD>z<EFBFBD>t<EFBFBD>ƪ<EFBFBD><C6AA>
2025-01-20 21:30:53 +08:00
if(input[i] == '+' && input[i-1] == '('){
}
else if(input[i-1] != 'o'){
if(!(input[i] == '-' && (i == 0 || input[i-1] == '(' || is_operator(input[i-1])))) {
2025-01-20 21:25:33 +08:00
while(top >= 0 && stack[top] != '(' &&
(get_priority(stack[top]) > get_priority(input[i]) ||
(get_priority(stack[top]) == get_priority(input[i]) && input[i] != '^'))) {
printf("%c ", stack[top]);
postfix_1[postfix_index++] = stack[top];
postfix_1[postfix_index++] = ' ';
top--;
2025-01-20 21:30:53 +08:00
}
2025-01-20 21:25:33 +08:00
stack[++top] = input[i];
2025-01-20 21:30:53 +08:00
}
}
2025-01-20 21:25:33 +08:00
}
i++;
}
while(top >= 0) {
printf("%c ", stack[top]);
postfix_1[postfix_index++] = stack[top];
postfix_1[postfix_index++] = ' ';
top--;
}
postfix_1[postfix_index] = '\0';
printf("\n");
}
// === <20><EFBFBD><E0B4AB><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46> ===
//<2F>p<EFBFBD><70><EFBFBD><EFBFBD>
void convert_to_postfix() {
int i = 0 , j;
int postfix_index = 0;
top = -1;
while(input[i] != '\0') {
2025-01-20 21:30:53 +08:00
2025-01-20 21:25:33 +08:00
if(isdigit(input[i]) || input[i] == '.' || (input[i] == '-' && (i == 0 || input[i-1] == '(' || is_operator(input[i-1])))) { // <20>ˬd<CBAC>O<EFBFBD>_<EFBFBD><5F><EFBFBD>Ʀr<C6A6>B<EFBFBD>p<EFBFBD><70><EFBFBD>I<EFBFBD>άO<CEAC>t<EFBFBD><74><EFBFBD>]<5D>B<EFBFBD>O<EFBFBD><4F><EFBFBD>F<EFBFBD><46><EFBFBD><EFBFBD><EFBFBD>}<7D>l<EFBFBD>A<EFBFBD>κ<EFBFBD><CEBA><EFBFBD><EFBFBD>b<EFBFBD><62><EFBFBD>A<EFBFBD><41><EFBFBD>ιB<CEB9><42><EFBFBD>ū᭱<C5AB>^
char *end;
double num = strtod(&input[i], &end); //strtod <20><><EFBFBD>r<EFBFBD><EFBFBD><EAB4AB><EFBFBD>B<EFBFBD>I<EFBFBD><49>
int len = end - &input[i]; //<2F>p<EFBFBD><70><EFBFBD>Ʀr<C6A6>r<EFBFBD><EFBFBD><EAAABA><EFBFBD><EFBFBD> <20>Ʀr<C6A6><72><EFBFBD>ݪ<EFBFBD><DDAA>O<EFBFBD><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}<7D><EFBFBD><EEB1BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m
2025-01-20 21:30:53 +08:00
printf("%.1f ", num);
2025-01-20 21:25:33 +08:00
for(j = 0; j < len; j++) {
postfix[postfix_index++] = input[i+j];
}
postfix[postfix_index++] = ' ';
i += len - 1;
2025-01-20 21:30:53 +08:00
}else if(input[i] == '(') {
2025-01-20 21:25:33 +08:00
stack[++top] = input[i];
}
2025-01-20 21:30:53 +08:00
else if(input[i] == ')') {
2025-01-20 21:25:33 +08:00
while(top >= 0 && stack[top] != '(') {
2025-01-20 21:30:53 +08:00
printf("%c ", stack[top]);
2025-01-20 21:25:33 +08:00
postfix[postfix_index++] = stack[top];
postfix[postfix_index++] = ' ';
top--;
2025-01-20 21:30:53 +08:00
}if(top >= 0) top--;
}else if(is_operator(input[i])) {
if(input[i] == '+' && input[i-1] == '('){
}else if(input[i-1]!='o' ){
while(top >= 0 && stack[top] != '(' && (get_priority(stack[top]) > get_priority(input[i]) || (get_priority(stack[top]) == get_priority(input[i]) && input[i] != '^'))) {
printf("%c ", stack[top]);
2025-01-20 21:25:33 +08:00
postfix[postfix_index++] = stack[top];
postfix[postfix_index++] = ' ';
top--;
2025-01-20 21:30:53 +08:00
}
2025-01-20 21:25:33 +08:00
stack[++top] = input[i];
2025-01-20 21:30:53 +08:00
}
2025-01-20 21:25:33 +08:00
}
i++;
}
while(top >= 0) {
2025-01-20 21:30:53 +08:00
printf("%c ", stack[top]);
2025-01-20 21:25:33 +08:00
postfix[postfix_index++] = stack[top];
postfix[postfix_index++] = ' ';
top--;
}
postfix[postfix_index] = '\0';
2025-01-20 21:30:53 +08:00
printf("\n");
2025-01-20 21:25:33 +08:00
}
// === <20>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46> ===
double evaluate_postfix() {
int i;
numTop = -1;
char *token = strtok(postfix, " ");
2025-01-20 21:30:53 +08:00
printf("\n<EFBFBD>p<EFBFBD><EFBFBD><EFBFBD>L<EFBFBD>{<7B>G\n");
2025-01-20 21:25:33 +08:00
while(token != NULL) {
2025-01-20 21:30:53 +08:00
printf("<EFBFBD>B<EFBFBD>z<EFBFBD>аO<EFBFBD>G'%s'\n", token);
2025-01-20 21:25:33 +08:00
if(isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
double num = atof(token);
numStack[++numTop] = num;
2025-01-20 21:30:53 +08:00
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>Ʀr<EFBFBD>G%.2f\n", num);
}else if(token[0] == 's'|| token[0] == 'c' ){
if(numTop < 1) {
printf("<EFBFBD>B<EFBFBD><EFBFBD><EFBFBD>l '%c' <20>ʤ֨<CAA4><D6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>\n", token[0]);
exit(1);
}
double b = numStack[numTop--];
double result = calculate(0, b, token[0]);
numStack[++numTop] = result;
printf("<EFBFBD>p<EFBFBD><EFBFBD><EFBFBD>G%c %.2f = %.2f\n", token[0], b, result);
}else if(is_operator(token[0])) {
2025-01-20 21:25:33 +08:00
if(numTop < 1) {
2025-01-20 21:30:53 +08:00
printf("<EFBFBD>B<EFBFBD><EFBFBD><EFBFBD>l '%c' <20>ʤ֨<CAA4><D6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>\n", token[0]);
2025-01-20 21:25:33 +08:00
exit(1);
}
double b = numStack[numTop--];
double a = numStack[numTop--];
double result = calculate(a, b, token[0]);
numStack[++numTop] = result;
2025-01-20 21:30:53 +08:00
printf("<EFBFBD>p<EFBFBD><EFBFBD><EFBFBD>G%.2f %c %.2f = %.2f\n", a, token[0], b, result);
2025-01-20 21:25:33 +08:00
}
else {
2025-01-20 21:30:53 +08:00
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>аO '%s'\n", token);
2025-01-20 21:25:33 +08:00
exit(1);
}
2025-01-20 21:30:53 +08:00
printf("<EFBFBD><EFBFBD><EFBFBD>e<EFBFBD><EFBFBD><EFBFBD>|<7C>G");
2025-01-20 21:25:33 +08:00
for(i = 0; i <= numTop; i++) {
2025-01-20 21:30:53 +08:00
printf("%.2f ", numStack[i]);
2025-01-20 21:25:33 +08:00
}
2025-01-20 21:30:53 +08:00
printf("\n\n");
2025-01-20 21:25:33 +08:00
token = strtok(NULL, " ");
}
if(numTop != 0) {
2025-01-20 21:30:53 +08:00
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD>p<EFBFBD><EFBFBD><E2B5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|<7C><><EFBFBD>Ѿl %d <20>Ӥ<EFBFBD><D3A4><EFBFBD>\n", numTop + 1);
2025-01-20 21:25:33 +08:00
exit(1);
}
return numStack[numTop];
}
int main() {
2025-01-20 21:30:53 +08:00
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><EFBFBD>ܦ<EFBFBD>(<28>䴩+-*/%^()<29>B<EFBFBD><42>) => ");
2025-01-20 21:25:33 +08:00
fgets(input, MAX, stdin);
input[strcspn(input, "\n")] = 0;
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><EFBFBD>ܪk<EFBFBD><EFBFBD><EFBFBD>G ");
convert_to_postfix_forprint(); //<2F><><EFBFBD>ܥ<EFBFBD>
2025-01-20 21:30:53 +08:00
printf("\n<EFBFBD>p<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><EFBFBD>ܪk<EFBFBD><EFBFBD><EFBFBD>G ");
2025-01-20 21:25:33 +08:00
convert_to_postfix();
double postfix_result = evaluate_postfix();
2025-01-20 21:30:53 +08:00
printf("\n<EFBFBD>B<EFBFBD><EFBFBD>G<EFBFBD>G%.4f", postfix_result);
2025-01-20 21:25:33 +08:00
printf("\t(<28><><EFBFBD>T<EFBFBD>Ȭ<EFBFBD><C8AC>G");
if(strcmp(input,"10+20*(50-30)/2^4-6*8")==0){
printf("%g",10+20*(50-30)/pow(2,4)-6*8);
}else if(strcmp(input,"10+20*(-50+30)/-2^4-6*8")==0){
printf("%g",10+20*(-50+30)/pow(-2,4)-6*8);
}else if(strcmp(input,"10.5+20.8*(50.1-30.6)/2.5^4-6.6*8.2")==0){
printf("%g",10.5+20.8*(50.1-30.6)/pow(2.5,4)-6.6*8.2);
}else if(strcmp(input,"10.5+20.8*(-50.1+30.6)/2.5^-4-6.6*8.2")==0){
printf("%g",10.5+20.8*(-50.1+30.6)/pow(2.5,-4)-6.6*8.2);
2025-01-20 21:30:53 +08:00
}else if(strcmp(input,"10-30*(-40-20)%2^4+7.5*-6")==0){
printf("%g",10-30*(-40-20)%(int)pow(2,4)+7.5*-6);
}else if(strcmp(input,"1+sin((-50.2+sin(3.8*20)*cos(-30-100%40))*2^-2.5)*cos(123.456)")==0){
printf("%g",1+sin((-50.2+sin(3.8*20)*cos(-30-100%40))*pow(2,-2.5))*cos(123.456));
}else if(strcmp(input,"100*(sin(sin(sin(50.2*cos((30+100%40)*sin(60/5.5)))+20*(50/30.0)^3.5-6*8)))^2")==0){
printf("%g",100*pow(sin(sin(sin(50.2*cos((30+100%40)*sin(60/5.5)))+20*pow(50/30.0,3.5)-6*8)),2));
}else if(strcmp(input,"100*(sin(sin(sin(+50.2*cos((30+100%40)*sin(60/5.5)))+20*(+50/30.0)^3.5-6*8)))^2")==0){
printf("%g",100*pow(sin(sin(sin(50.2*cos((30+100%40)*sin(60/5.5)))+20*pow(50/30.0,3.5)-6*8)),2));
2025-01-20 21:25:33 +08:00
}printf(")");
2025-01-20 21:30:53 +08:00
2025-01-20 21:25:33 +08:00
return 0;
}