C语言用到运算符和库函数

char *p = "linux";        //该字符串是存于code段的

int  a = sizeof(p);        //a = 4

int b = strlen(p);        //b = 5,不包含字符串结尾的'\0'

        //原型:size_t strlen(const char *s);      //参数为字符串指针,size_t为int


sizeof

    得到变量、类型占用内存空间大小

strlen

    判断字符串长度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我理解你的问题了。实现表达式求解需要用到一个栈来存储运算符和操作数,并按照一定的优先级进行计算。以下是一个示例程序,其用到了几个重要的栈库函数(如stack_push、stack_pop等): ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> typedef struct { int capacity; int top; int* data; } Stack; Stack* stack_new(int capacity) { Stack* s = (Stack*)malloc(sizeof(Stack)); s->capacity = capacity; s->top = -1; s->data = (int*)malloc(capacity * sizeof(int)); return s; } void stack_push(Stack* s, int value) { if (s->top == s->capacity - 1) { fprintf(stderr, "Error: stack is full.\n"); exit(1); } s->top++; s->data[s->top] = value; } int stack_pop(Stack* s) { if (s->top == -1) { fprintf(stderr, "Error: stack is empty.\n"); exit(1); } int value = s->data[s->top]; s->top--; return value; } int stack_top(Stack* s) { if (s->top == -1) { fprintf(stderr, "Error: stack is empty.\n"); exit(1); } return s->data[s->top]; } int is_operator(char ch) { return ch == '+' || ch == '-' || ch == '*' || ch == '/'; } int operator_precedence(char op) { switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; default: fprintf(stderr, "Error: unsupported operator '%c'.\n", op); exit(1); } } int evaluate_expression(char* expression) { Stack* operands = stack_new(strlen(expression)); Stack* operators = stack_new(strlen(expression)); int i = 0; while (expression[i]) { if (isdigit(expression[i])) { int value = expression[i] - '0'; i++; while (isdigit(expression[i])) { value = value * 10 + (expression[i] - '0'); i++; } stack_push(operands, value); } else if (is_operator(expression[i])) { while (!stack_top(operators)) { char op = stack_top(operators); if (operator_precedence(op) >= operator_precedence(expression[i])) { int b = stack_pop(operands); int a = stack_pop(operands); switch (op) { case '+': stack_push(operands, a + b); break; case '-': stack_push(operands, a - b); break; case '*': stack_push(operands, a * b); break; case '/': stack_push(operands, a / b); break; default: fprintf(stderr, "Error: unsupported operator '%c'.\n", op); exit(1); } stack_pop(operators); } else { break; } } stack_push(operators, expression[i]); i++; } else if (isspace(expression[i])) { i++; } else { fprintf(stderr, "Error: invalid character '%c'.\n", expression[i]); exit(1); } } while (!stack_top(operators)) { char op = stack_top(operators); int b = stack_pop(operands); int a = stack_pop(operands); switch (op) { case '+': stack_push(operands, a + b); break; case '-': stack_push(operands, a - b); break; case '*': stack_push(operands, a * b); break; case '/': stack_push(operands, a / b); break; default: fprintf(stderr, "Error: unsupported operator '%c'.\n", op); exit(1); } stack_pop(operators); } int result = stack_pop(operands); free(operands->data); free(operands); free(operators->data); free(operators); return result; } int main() { char* expression = "3 * ( 4 + 5 ) - 6 / 2"; int result = evaluate_expression(expression); printf("%s = %d\n", expression, result); return 0; } ``` 这个程序能够处理带括号的表达式,并按照正确的优先级进行计算。你可以尝试将其复制到本地环境运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值