empty()与remove([expr])

本文探讨了jQuery中empty()与remove()两种方法的区别。empty()仅清除元素内容但保留DOM结构,而remove()则完全从DOM中移除元素。

要用到移除指定元素的时候,发现empty()与remove([expr])都可以用来实现。可仔细观察效果的话就可以发现。

empty()是只移除了 指定元素中的所有子节点,拿$("p").empty()来说,他只是把<p>dsfsd</p>中的文本给移除了,而留下 了<p></p>,仍保留其在dom中所占的位置。

remove([expr])则是把其从dom中删除,而不会保留其所占的位置。
例:<p>Hello</p>

执行$("p").empty()其结果是
<p></p>

执行$("p").remove()其结果是

(所有元素都没有了)


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <math.h> #define MAX_EXPR_LEN 256 #define STACK_SIZE 100 typedef struct { double items[STACK_SIZE]; int top; } Stack; void init_stack(Stack *s) { s->top = -1; } int is_empty(Stack *s) { return (s->top == -1); } int is_full(Stack *s) { return (s->top >= STACK_SIZE - 1); } void push(Stack *s, double item) { if (!is_full(s)) { s->items[++(s->top)] = item; } else { printf("Stack Overflow\n"); } } double pop(Stack *s) { if (!is_empty(s)) { return s->items[(s->top)--]; } else { printf("Stack Underflow\n"); exit(EXIT_FAILURE); } } // Function to evaluate expressions using a stack-based approach. double eval_expr(char expr[]) { Stack operand_stack; init_stack(&operand_stack); char symbol; double op1, op2, result; int i = 0; while ((symbol = expr[i++]) != '\0') { if (isspace(symbol)) continue; if (isdigit(symbol) || symbol == '.') { ungetc(symbol, stdin); scanf("%lf", &result); getchar(); // Consume the remaining newline or space after number input push(&operand_stack, result); } else if (symbol == '+') { op2 = pop(&operand_stack); op1 = pop(&operand_stack); push(&operand_stack, op1 + op2); } else if (symbol == '-') { op2 = pop(&operand_stack); op1 = pop(&operand_stack); push(&operand_stack, op1 - op2); } else if (symbol == '*') { op2 = pop(&operand_stack); op1 = pop(&operand_stack); push(&operand_stack, op1 * op2); } else if (symbol == '/') { op2 = pop(&operand_stack); op1 = pop(&operand_stack); if (op2 == 0) { printf("Error: Division by zero.\n"); exit(EXIT_FAILURE); } push(&operand_stack, op1 / op2); } else if (symbol == '^') { op2 = pop(&operand_stack); op1 = pop(&operand_stack); push(&operand_stack, pow(op1, op2)); } else if (symbol == 'r' && expr[i++] == 't') { op1 = pop(&operand_stack); push(&operand_stack, sqrt(op1)); } else if (symbol == 'l' && expr[i++] == 'n') { op1 = pop(&operand_stack); push(&operand_stack, log(op1)); } else if (symbol == 'e' && expr[i++] == 'x' && expr[i++] == 'p') { op1 = pop(&operand_stack); push(&operand_stack, exp(op1)); } else if (symbol == 's' && expr[i++] == 'i' && expr[i++] == 'n') { op1 = pop(&operand_stack); push(&operand_stack, sin(op1)); } else if (symbol == 'c' && expr[i++] == 'o' && expr[i++] == 's') { op1 = pop(&operand_stack); push(&operand_stack, cos(op1)); } else if (symbol == 't' && expr[i++] == 'a' && expr[i++] == 'n') { op1 = pop(&operand_stack); push(&operand_stack, tan(op1)); } } return pop(&operand_stack); } int main() { char expression[MAX_EXPR_LEN]; printf("Enter mathematical expression with spaces between operators and operands:\n"); while (fgets(expression, sizeof(expression), stdin) != NULL) { expression[strcspn(expression, "\n")] = '\0'; // Remove trailing newline character. double result = eval_expr(expression); printf("Result: %.4f\n", result); printf("Enter another expression or press Ctrl+D to quit:\n"); } return 0; } 如何在该代码中输入小数进行计算
最新发布
05-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值