#include <iostream> #define MAXSIZE 100 #define OK 1 #define ERROR 0 #define OVERFLOW -2 using namespace std; typedef struct {//符号栈 char* base; char* top; int stacksize; }SqStack1; int InitStack1(SqStack1& S) {//符号栈初始化 return OK; } int Push1(SqStack1& S, char e) {//符号栈入栈 return OK; } int Pop1(SqStack1& S) {//符号栈出栈 return OK; } char GetTop1(SqStack1 S) {//符号栈取栈顶元素 return ERROR; } typedef struct {//数字栈 double* base; double* top; int stacksize; }SqStack2; int InitStack2(SqStack2&
```cpp #include <iostream> #include <iomanip> #include <cstring> #include <cmath> #define MAXSIZE 100 #define OK 1 #define ERROR 0 #define OVERFLOW -2 using namespace std; typedef struct { char* base; char* top; int stacksize; } SqStack1; int InitStack1(SqStack1& S) { S.base = new char[MAXSIZE]; if (!S.base) return OVERFLOW; S.top = S.base; S.stacksize = MAXSIZE; return OK; } int Push1(SqStack1& S, char e) { if (S.top - S.base == S.stacksize) return ERROR; *S.top++ = e; return OK; } int Pop1(SqStack1& S) { if (S.top == S.base) return ERROR; S.top--; return OK; } char GetTop1(SqStack1 S) { if (S.top == S.base) return ERROR; return *(S.top - 1); } typedef struct { double* base; double* top; int stacksize; } SqStack2; int InitStack2(SqStack2& S) { S.base = new double[MAXSIZE]; if (!S.base) return OVERFLOW; S.top = S.base; S.stacksize = MAXSIZE; return OK; } int Push2(SqStack2& S, double e) { if (S.top - S.base == S.stacksize) return ERROR; *S.top++ = e; return OK; } int Pop2(SqStack2& S) { if (S.top == S.base) return ERROR; S.top--; return OK; } double GetTop2(SqStack2 S) { if (S.top == S.base) return ERROR; return *(S.top - 1); } double Calculate(double a, char op, double b) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': if (fabs(b) < 1e-6) { cout << "除数不能为0" << endl; exit(0); } return a / b; default: return 0; } } char Precede(char a, char b) { if (a == '+' || a == '-') { if (b == '+' || b == '-' || b == ')' || b == '=') return '>'; else return '<'; } else if (a == '*' || a == '/') { if (b == '(') return '<'; else return '>'; } else if (a == '(') { if (b == ')') return '='; else if (b == '=') return ' '; else return '<'; } else if (a == ')') { if (b == '(') return ' '; else return '>'; } else if (a == '=') { if (b == '=') return '='; else if (b == ')') return ' '; else return '<'; } return ' '; } int main() { string str; while (getline(cin, str)) { if (str == "=") break; SqStack1 OPTR; // 运算符栈 SqStack2 OPND; // 操作数栈 InitStack1(OPTR); InitStack2(OPND); Push1(OPTR, '='); int i = 0; int flag = 0; // 负数标志 int decimal = 0; // 小数标志 double num = 0; double factor = 0.1; while (i < str.length()) { if (str[i] == ' ') { i++; continue; } // 处理数字 if ((str[i] >= '0' && str[i] <= '9') || (str[i] == '-' && (i == 0 || str[i-1] == '('))) { if (str[i] == '-') { flag = 1; i++; } num = 0; decimal = 0; factor = 0.1; while ((str[i] >= '0' && str[i] <= '9') || str[i] == '.') { if (str[i] == '.') { decimal = 1; i++; continue; } if (!decimal) { num = num * 10 + (str[i] - '0'); } else { num = num + (str[i] - '0') * factor; factor *= 0.1; } i++; } if (flag) { num = -num; flag = 0; } Push2(OPND, num); } // 处理运算符 else { char theta; char priority = Precede(GetTop1(OPTR), str[i]); switch (priority) { case '<': Push1(OPTR, str[i]); i++; break; case '=': Pop1(OPTR); i++; break; case '>': theta = GetTop1(OPTR); Pop1(OPTR); double b = GetTop2(OPND); Pop2(OPND); double a = GetTop2(OPND); Pop2(OPND); double result = Calculate(a, theta, b); Push2(OPND, result); break; } } } cout << fixed << setprecision(2) << GetTop2(OPND) << endl; } return 0; } ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 处理负数时要注意区分减号和负号,根据上下文判断 2. 浮点数比较要使用精度判断,避免直接使用等号 3. 栈操作时要检查栈空栈满情况,防止越界访问 [2025-11-14 14:57:43 | AI写代码神器 | 1360点数解答]
- [DeepSeek] #include <iostream> #define MAXSIZE 100 #define OK 1 #define ERROR 0 #define OVERFLOW -2 using namespace std; typedef struct {//符号栈 char* base; char* top; int stacksize; }SqStack1; int InitStack1(SqStack1& S) {//符号栈初始化 return OK; } int Push1(SqStack1& S, char e) {//符号栈入栈 return OK; } int Pop1(SqStack1& S) {//符号栈出栈 return OK; } char GetTop1(SqStack1 S) {//符号栈取栈顶元素 return ERROR; } typedef struct {//数字栈 double* base; double* top; int stacksize; }SqStack2; int InitStack2(SqStack2&(1360点数解答 | 2025-11-14 14:57:43)28
- [字节豆包] #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define maxsize 100 typedef char elemtype; typedef struct node { elemtype data; struct node* lchild; struct node* rchild; } btnode; typedef struct { btnode* data[maxsize]; int top; } stacktype; void initstack(stacktype* st) { st->top = -1; } bool stackempty(stacktype* st) { return st->top == -1; } bool push(stacktype* st, btnode* e) { if (st->top < maxsize - 1) { st->data[++st->top] = e;(95点数解答 | 2024-12-10 13:17:25)169
- [字节豆包] #include<iostream> #include<string> #include<iomanip> #include<fstream> #include<stdlib.h> using namespace std; #define ok 1 #define error 0 #define overflow -2 typedef int status; //status 是函数返回值类型,其值是函数结果状态代码。 typedef int elemtype; //elemtype 为可定义的数据类型,此设为int类型 struct book { string id;//isbn string name;//书名 double price;//定价 }; typedef struct lnode { book data; //结点的数据域 struct lnode *next; //结点的指针域 } lnode, *linklist; //linklist为指向结构体lnode的指针类型 string head_1, head_2, head_3; int lengt(35点数解答 | 2024-10-31 17:38:15)195
- [阿里通义] #include<stdio.h> #include<malloc.h>//动态存储分配函数头文件 #include<math.h>//包含数学函数的文件 #include<string.h>//一个和字符串处理相关的头文件 #include<process.h>//包含用于和宏指令的作用声明 #define error 0 //宏定义 #define ok 1 #define over -2 #define listinitsize 20 #define listincrement 5 #define namelen 8 #define majorlen 20 #define init_n 3 typedef int status; //自定义类型语句 status i 等价于 int i typedef char statusc; typedef struct{ statusc number[10]; //学号 statusc name[namelen+1]; //姓名 statusc gender; //性别 statusc telephone[12]; //电话 st(1142点数解答 | 2024-11-07 19:08:54)198
- [字节豆包] // rod6.cpp : defines the entry point for the console application. // #include "stdafx.h" // consoleapplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <stdio.h> #include <math.h> #include <iostream> #include <conio.h> #include <cstdlib> using namespace std; double ro[10001]; double th[10001]; double e[10001]; double a[10001]; double b[10001]; double y[10001]; double x[10001]; double d[10001]; double d[10001]; double da[10001]; double db[10001]; double dc[10001]; double z[10001]; d(55点数解答 | 2024-08-16 15:22:27)250
- [字节豆包] #define _crt_secure_no_warnings #include <iostream> #include <cstdlib> #include <cmath> #include <vector> #include <cstdio> using namespace std; // 你的代码... const int nx = 784, nb = 500, ny = 10;//输入层x,隐藏层b,输出层y节点数 const double eta = 0.1;//学习率 struct node { double val{}; double bias{}; vector<double> weight; } x[nx], b[nb], y[ny];//输入层x,隐藏层b,输出层y double g[ny], e[nb];//用于反向传播 double trainx[nx], trainy[ny];//训练数据 double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); } double(34点数解答 | 2024-09-06 08:43:52)297
- [GPT] #define _crt_secure_no_warnings #include <iostream> #include <cstdlib> #include <cmath> #include <vector> #include <cstdio> using namespace std; // 你的代码... const int nx = 784, nb = 500, ny = 10;//输入层x,隐藏层b,输出层y节点数 const double eta = 0.1;//学习率 struct node { double val{}; double bias{}; vector<double> weight; } x[nx], b[nb], y[ny];//输入层x,隐藏层b,输出层y double g[ny], e[nb];//用于反向传播 double trainx[nx], trainy[ny];//训练数据 double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); } double(1309点数解答 | 2024-09-06 08:46:09)312
- [字节豆包] 使用一维数组实现双栈cpp代码#include <iostream> #include <cstdlib> using namespace std; #define maxsize 10 typedef struct { int* stack; //初始化后栈内均为0 int top[2]; // top为两个栈顶指针,记录了栈顶元素在数组中的下标。top[0]表示0栈,top[1]表示1栈 //初始情况top[0]=-1,top[1]=maxsize }dualstack; void push(dualstack &s,int i,int x) { //x表示要入栈的元素,i表示要入的栈,i取值为0和1 //当栈满时输出 “栈满” 加换行符 //请在此处填入代码 } int pop(dualstack &s,int i) { //i表示要出栈的栈,i取值为0和1 //当栈(543点数解答 | 2024-11-29 09:50:17)205
- [GPT] 使用一维数组实现双栈cpp代码#include <iostream> #include <cstdlib> using namespace std; #define maxsize 10 typedef struct { int* stack; //初始化后栈内均为0 int top[2]; // top为两个栈顶指针,记录了栈顶元素在数组中的下标。top[0]表示0栈,top[1]表示1栈 //初始情况top[0]=-1,top[1]=maxsize }dualstack; void push(dualstack &s,int i,int x) { //x表示要入栈的元素,i表示要入的栈,i取值为0和1 //当栈满时输出 “栈满” 加换行符 //请在此处填入代码 } int pop(dualstack &s,int i) { //i表示要出栈的栈,i取值为0和1 //当栈空时输出“0栈空”或“1栈空”,加换行符 //当栈空时返回0,栈非空时返回出栈元素值 //请在此处填入代码 }(643点数解答 | 2024-11-29 10:01:30)220
- [字节豆包] #include <iostream> #include <cstdlib> using namespace std; #define MAXSIZE 10 typedef struct { int* stack; //初始化后栈内均为0 int top[2]; // top为两个栈顶指针,记录了栈顶元素在数组中的下标。top[0]表示0栈,top[1]表示1栈 //初始情况top[0]=-1,top[1]=MAXSIZE }DualStack; void push(DualStack &S,int i,int x) { //x表示要入栈的元素,i表示要入的栈,i取值为0和1 //当栈满时输出 “栈满” 加换行符 //请在此处填入代码 } int pop(DualStack &S,int i) { //i表示要出栈的栈,i取值为0和1 //当栈空时输出“0栈空”或“1栈空”(637点数解答 | 2025-11-05 14:11:09)30
- [字节豆包] #include <iostream> #include <stdlib.h> #include <stdio.h> #include<string.h> using namespace std; #define null 0 #define maxsize 100 typedef char datatype; typedef struct node { datatype data[maxsize]; int length; }seqstring; void creat(seqstring *s)//建立字符串顺序表 { scanf("%s",s->data); s->length=strlen(s->data); } void print(seqstring str)//输出字符串 ,以'\0'结尾 { printf("list is:%s\n",str.data); } /*在字符串str中从第i个位置起取长度为len的子串(i从1开始),函数返回子串指针,若子串超出边界返回null。*/ seqstring* substring((146点数解答 | 2024-10-16 08:51:55)197
- [字节豆包] #include <iostream> #include <queue> #include <stack> using namespace std; #define mvnum 100 /* 预定义图的最大顶点数 */ typedef char vertextype; /* 顶点信息数据类型 */ typedef struct arcnode /* 边表结点 */ { int adjvex; /* 邻接点 */ struct arcnode* nextarc; /* 指向下一条边的指针 */ } arcnode; typedef struct vnode /* 头结点类型 */ { vertextype data; /* 顶点信息 */ arcnode* firstarc; /* 邻接链表头指针 */ } vnode, adjlist[mvnum]; typedef struct /* 邻接表类型 */ { adjlist vertice(50点数解答 | 2024-11-20 23:42:31)178