编程实现一元多项式的加法运算。第一个一元多项式A;第二个多项式B。以(0,0作为输入结束)C语言
时间: 2023-05-15 08:06:30 浏览: 105
实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int coef; // 系数
int exp; // 指数
struct Node *next;
} Node, *Polynomial;
Polynomial createPoly() {
Polynomial head = (Polynomial) malloc(sizeof(Node));
head->next = NULL;
Node *tail = head;
int coef, exp;
scanf("%d %d", &coef, &exp);
while (coef != 0 || exp != 0) {
Node *node = (Node *) malloc(sizeof(Node));
node->coef = coef;
node->exp = exp;
node->next = NULL;
tail->next = node;
tail = node;
scanf("%d %d", &coef, &exp);
}
return head;
}
Polynomial addPoly(Polynomial A, Polynomial B) {
Polynomial C = (Polynomial) malloc(sizeof(Node));
C->next = NULL;
Node *tail = C;
Node *a = A->next, *b = B->next;
while (a && b) {
if (a->exp > b->exp) {
Node *node = (Node *) malloc(sizeof(Node));
node->coef = a->coef;
node->exp = a->exp;
node->next = NULL;
tail->next = node;
tail = node;
a = a->next;
} else if (a->exp < b->exp) {
Node *node = (Node *) malloc(sizeof(Node));
node->coef = b->coef;
node->exp = b->exp;
node->next = NULL;
tail->next = node;
tail = node;
b = b->next;
} else {
int sum = a->coef + b->coef;
if (sum != 0) {
Node *node = (Node *) malloc(sizeof(Node));
node->coef = sum;
node->exp = a->exp;
node->next = NULL;
tail->next = node;
tail = node;
}
a = a->next;
b = b->next;
}
}
while (a) {
Node *node = (Node *) malloc(sizeof(Node));
node->coef = a->coef;
node->exp = a->exp;
node->next = NULL;
tail->next = node;
tail = node;
a = a->next;
}
while (b) {
Node *node = (Node *) malloc(sizeof(Node));
node->coef = b->coef;
node->exp = b->exp;
node->next = NULL;
tail->next = node;
tail = node;
b = b->next;
}
return C;
}
void printPoly(Polynomial poly) {
Node *p = poly->next;
if (!p) {
printf("0 0\n");
return;
}
while (p) {
printf("%d %d", p->coef, p->exp);
p = p->next;
if (p) {
printf(" ");
} else {
printf("\n");
}
}
}
int main() {
Polynomial A = createPoly();
Polynomial B = createPoly();
Polynomial C = addPoly(A, B);
printPoly(C);
return 0;
}
```
第二个问题:lua closure factory 完整代码
```lua
function newCounter()
local count = 0
return function()
count = count + 1
return count
end
end
c1 = newCounter()
print(c1()) -- 1
print(c1()) -- 2
c2 = newCounter()
print(c2()) -- 1
print(c1()) -- 3
```
第三个问题:中文加密
中文加密可以使用简单的替换算法,将每个汉字替换成另一个汉字或者一个字母。下面是一个简单的中文加密算法示例:
```python
import random
# 加密字典
encrypt_dict = {
'你': 'a',
'好': 'b',
'吗': 'c',
'我': 'd',
'很': 'e',
'不': 'f',
'是': 'g',
'的': 'h',
'一': 'i',
'个': 'j',
'人': 'k',
'在': 'l',
'有': 'm',
'他': 'n',
'她': 'o',
'们': 'p',
'这': 'q',
'那': 'r',
'就': 's',
'也': 't',
'和': 'u',
'不': 'v',
'吧': 'w',
'啊': 'x',
'呢': 'y',
'哦': 'z',
}
# 解密字典
decrypt_dict = {v: k for k, v in encrypt_dict.items()}
def encrypt(text):
result = ''
for c in text:
if c in encrypt_dict:
result += encrypt_dict[c]
else:
result += c
return result
def decrypt(text):
result = ''
for c in text:
if c in decrypt_dict:
result += decrypt_dict[c]
else:
result += c
return result
text = '你好,世界!'
encrypted_text = encrypt(text)
print(encrypted_text)
decrypted_text = decrypt(encrypted_text)
print(decrypted_text)
```
输出结果:
```
ab,世界!
你好,世界!
```
阅读全文