能帮我写一个C语言关于计算器软件能进行大数想加,进制转换和判断括号是否匹配以及对表达式求职的代码吗
时间: 2024-03-21 08:38:11 浏览: 55
当然可以!以下是一个简单的计算器软件的C语言代码,可以进行大数相加,进制转换,判断括号是否匹配,以及对表达式求值:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK_SIZE 100
#define MAX_NUM_LENGTH 100
typedef struct {
int top;
char stack[MAX_STACK_SIZE];
} Stack;
// 初始化栈
void initStack(Stack *stack) {
stack->top = -1;
}
// 判断栈是否为空
int isEmpty(Stack *stack) {
return stack->top == -1;
}
// 判断栈是否已满
int isFull(Stack *stack) {
return stack->top == MAX_STACK_SIZE - 1;
}
// 入栈
void push(Stack *stack, char c) {
if (isFull(stack)) {
printf("Stack is full!\n");
return;
}
stack->stack[++stack->top] = c;
}
// 出栈
char pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty!\n");
return '\0';
}
return stack->stack[stack->top--];
}
// 获取栈顶元素
char top(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty!\n");
return '\0';
}
return stack->stack[stack->top];
}
// 判断括号是否匹配
int isMatch(char *exp) {
Stack stack;
initStack(&stack);
for (int i = 0; i < strlen(exp); i++) {
if (exp[i] == '(') {
push(&stack, exp[i]);
} else if (exp[i] == ')') {
if (isEmpty(&stack)) {
return 0;
}
pop(&stack);
}
}
return isEmpty(&stack);
}
// 计算大数加法
char *bigIntAdd(char *num1, char *num2) {
int len1 = strlen(num1);
int len2 = strlen(num2);
int len = len1 > len2 ? len1 : len2;
char *res = (char *) malloc((len + 2) * sizeof(char));
memset(res, '0', len + 1);
res[len + 1] = '\0';
int carry = 0;
for (int i = len1 - 1, j = len2 - 1, k = len; i >= 0 || j >= 0; i--, j--, k--) {
int n1 = i >= 0 ? num1[i] - '0' : 0;
int n2 = j >= 0 ? num2[j] - '0' : 0;
int sum = n1 + n2 + carry;
carry = sum / 10;
res[k] = sum % 10 + '0';
}
if (carry > 0) {
res[0] = carry + '0';
} else {
res++;
}
return res;
}
// 进制转换
char *convertBase(char *num, int base1, int base2) {
char *digits = "0123456789ABCDEF";
char *res = (char *) malloc((strlen(num) + 1) * sizeof(char));
strcpy(res, num);
char *p = res;
int n = 0;
while (*p != '\0') {
n = n * base1 + strchr(digits, *p++) - digits;
}
p = res;
int i = 0;
while (n > 0) {
res[i++] = digits[n % base2];
n /= base2;
}
res[i] = '\0';
strrev(res);
return res;
}
// 计算表达式的值
int calc(char *exp) {
Stack numStack;
Stack opStack;
initStack(&numStack);
initStack(&opStack);
char *p = exp;
while (*p != '\0') {
if (isdigit(*p)) {
int n = 0;
while (isdigit(*p)) {
n = n * 10 + (*p - '0');
p++;
}
push(&numStack, n);
} else if (*p == '(') {
push(&opStack, *p);
p++;
} else if (*p == ')') {
while (top(&opStack) != '(') {
int num2 = pop(&numStack);
int num1 = pop(&numStack);
char op = pop(&opStack);
int res = 0;
if (op == '+') {
res = num1 + num2;
} else if (op == '-') {
res = num1 - num2;
} else if (op == '*') {
res = num1 * num2;
} else if (op == '/') {
res = num1 / num2;
}
push(&numStack, res);
}
pop(&opStack);
p++;
} else if (*p == '+' || *p == '-') {
while (!isEmpty(&opStack) && top(&opStack) != '(') {
int num2 = pop(&numStack);
int num1 = pop(&numStack);
char op = pop(&opStack);
int res = 0;
if (op == '+') {
res = num1 + num2;
} else if (op == '-') {
res = num1 - num2;
} else if (op == '*') {
res = num1 * num2;
} else if (op == '/') {
res = num1 / num2;
}
push(&numStack, res);
}
push(&opStack, *p);
p++;
} else if (*p == '*' || *p == '/') {
while (!isEmpty(&opStack) && (top(&opStack) == '*' || top(&opStack) == '/')) {
int num2 = pop(&numStack);
int num1 = pop(&numStack);
char op = pop(&opStack);
int res = 0;
if (op == '+') {
res = num1 + num2;
} else if (op == '-') {
res = num1 - num2;
} else if (op == '*') {
res = num1 * num2;
} else if (op == '/') {
res = num1 / num2;
}
push(&numStack, res);
}
push(&opStack, *p);
p++;
} else {
p++;
}
}
while (!isEmpty(&opStack)) {
int num2 = pop(&numStack);
int num1 = pop(&numStack);
char op = pop(&opStack);
int res = 0;
if (op == '+') {
res = num1 + num2;
} else if (op == '-') {
res = num1 - num2;
} else if (op == '*') {
res = num1 * num2;
} else if (op == '/') {
res = num1 / num2;
}
push(&numStack, res);
}
return pop(&numStack);
}
int main() {
char num1[MAX_NUM_LENGTH];
char num2[MAX_NUM_LENGTH];
char exp[MAX_NUM_LENGTH];
int base1, base2;
// 大数相加
printf("Enter two numbers: ");
scanf("%s %s", num1, num2);
printf("Result: %s\n", bigIntAdd(num1, num2));
// 进制转换
printf("Enter a number and two bases: ");
scanf("%s %d %d", num1, &base1, &base2);
printf("Result: %s\n", convertBase(num1, base1, base2));
// 判断括号是否匹配
printf("Enter an expression: ");
scanf("%s", exp);
printf("The expression is %s\n", isMatch(exp) ? "matched" : "not matched");
// 计算表达式的值
printf("Enter an expression: ");
scanf("%s", exp);
printf("Result: %d\n", calc(exp));
return 0;
}
```
注意:本代码仅作参考,可能存在一些未经充分测试的漏洞和错误,需要根据具体情况进行修改和完善。
阅读全文