将这个程序加入减法,乘法和除法
时间: 2024-02-18 17:59:00 浏览: 76
好的,以下是一个包含加法、减法、乘法和除法的16进制计算器的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LEN 100
int hex_to_dec(char c) {
if (isdigit(c)) {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else {
return -1; // error
}
}
char dec_to_hex(int n) {
if (n >= 0 && n <= 9) {
return n + '0';
} else if (n >= 10 && n <= 15) {
return n - 10 + 'a';
} else {
return '?'; // error
}
}
void reverse_str(char* s) {
int len = strlen(s);
for (int i = 0, j = len - 1; i < j; i++, j--) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
char* add_hex(char* a, char* b) {
int len_a = strlen(a), len_b = strlen(b), len_c = 0;
char* c = (char*) malloc(sizeof(char) * (MAX_LEN + 1));
int carry = 0;
while (len_a > 0 || len_b > 0 || carry > 0) {
int x = (len_a > 0) ? hex_to_dec(a[--len_a]) : 0;
int y = (len_b > 0) ? hex_to_dec(b[--len_b]) : 0;
int z = x + y + carry;
carry = z / 16;
c[len_c++] = dec_to_hex(z % 16);
}
c[len_c] = '\0';
reverse_str(c);
return c;
}
char* subtract_hex(char* a, char* b) {
int len_a = strlen(a), len_b = strlen(b), len_c = 0;
char* c = (char*) malloc(sizeof(char) * (MAX_LEN + 1));
int borrow = 0;
while (len_a > 0 || len_b > 0) {
int x = (len_a > 0) ? hex_to_dec(a[--len_a]) : 0;
int y = (len_b > 0) ? hex_to_dec(b[--len_b]) : 0;
int z = x - y - borrow;
if (z < 0) {
z += 16;
borrow = 1;
} else {
borrow = 0;
}
c[len_c++] = dec_to_hex(z);
}
c[len_c] = '\0';
reverse_str(c);
return c;
}
char* multiply_hex(char* a, char* b) {
int len_a = strlen(a), len_b = strlen(b);
char* c = (char*) calloc(MAX_LEN + 1, sizeof(char));
for (int i = len_a - 1; i >= 0; i--) {
int carry = 0;
for (int j = len_b - 1; j >= 0; j--) {
int x = hex_to_dec(a[i]);
int y = hex_to_dec(b[j]);
int z = x * y + carry + hex_to_dec(c[i + j + 1]);
carry = z / 16;
c[i + j + 1] = dec_to_hex(z % 16);
}
c[i] = dec_to_hex(carry);
}
reverse_str(c);
return c;
}
char* divide_hex(char* a, char* b) {
int len_a = strlen(a), len_b = strlen(b);
char* c = (char*) calloc(MAX_LEN + 1, sizeof(char));
if (strcmp(b, "0") == 0) { // handle division by zero
strcpy(c, "error");
return c;
}
char* remainder = (char*) calloc(len_b + 1, sizeof(char));
strcpy(remainder, a);
for (int i = 0; i <= len_a - len_b; i++) {
int q = 0;
while (strcmp(remainder, b) >= 0) {
char* temp = subtract_hex(remainder, b);
strcpy(remainder, temp);
free(temp);
q++;
}
c[i] = dec_to_hex(q);
if (i < len_a - len_b) {
char temp[MAX_LEN + 1] = "";
strcat(temp, remainder);
strcat(temp, &a[i + len_b]);
strcpy(remainder, temp);
}
}
reverse_str(c);
return c;
}
int main() {
char a[MAX_LEN + 1], b[MAX_LEN + 1];
printf("Enter two hexadecimal numbers:\n");
scanf("%s %s", a, b);
char* c = add_hex(a, b);
printf("The sum is: %s\n", c);
free(c);
c = subtract_hex(a, b);
printf("The difference is: %s\n", c);
free(c);
c = multiply_hex(a, b);
printf("The product is: %s\n", c);
free(c);
c = divide_hex(a, b);
printf("The quotient is: %s\n", c);
free(c);
return 0;
}
```
这个程序实现了四种16进制计算:加法、减法、乘法和除法。在 `subtract_hex` 函数中,我们采用了借位法实现16进制数的减法。在 `multiply_hex` 函数中,我们采用了竖式乘法的方法实现16进制数的乘法。在 `divide_hex` 函数中,我们采用了长除法的方法实现16进制数的除法。如果除数为0,我们将返回 "error"。
需要注意的是,输入的两个16进制数必须以字符串形式输入,并且长度不能超过 `MAX_LEN`。
阅读全文