这是浙江大学C Programming的课程作业。 而DDL是17:50 2023/4/25 (6H Left)QAQ
我课程落后,上周花时间准备期中考试,对 LinkedList , DevCPP 等知之甚少。 程序中的大部分代码不是我自己写的:( (我已经计划了编程练习的五一节,以弥补我的错误)
输入一个多边形,只输出它的第一个字符。 例如
对用链表表示的单变量n次多项式进行加减乘运算,并支持多文件
单变量 n 次多项式可以表示为最多包含 (n+1) 项的降序链表。链表中的每个节点存储每一项的系数和度数。
多项式加减法,循环遍历同次项的系数加减。对于多项式乘法,对每一项执行多项式加法和减法。
要求:
功能要求: 该程序总是有一个初始值为 0 的多项式 p。 多项式输入函数可以将 p 更改为输入多项式。 多项式计算运算需要输入一个新的多项式p',并将p和p'之间的运算结果赋值给p。 多项式输出操作可以按次数从大到小的顺序输出p。
输入格式要求: 函数选择是一个数字 f,0 ≤ f ≤ 5,在单独的一行上。 输入多项式在一行中,各项之间没有空格,并以换行符结尾。 多项式的每一项的形式为 aX^i、aX、X 或 a。系数a和次数i为大于1的正整数。如果i为1,则可以省略。如果 a 为 1 且 i 不为 0,则可以省略。 多项式只包含字符“X”。 每个术语由“+”或“-”连接。不保证条款的顺序。 例如: 8X^6-2X+45+2X^3 -12+X 0
输出格式要求: 在程序开始时和每次操作后,应打印菜单项。 菜单项应输出为 ASCII 字符,非 ASCII 字符应视为无效。 输出多项式应满足以下要求:
多文件组织要求: 至少应该使用一个头文件和一个源文件来定义和操作多项式链表。 至少应使用一个头文件和源文件来处理多项式输入和输出。 头文件:定义各种变量和函数声明。 源文件:定义头文件中声明的函数。
注意事项: 不要将所有文件包含在一个文件中并进行编译。 当使用动态内存分配时,必须释放内存并且不应该有任何内存泄漏。
#include <stdio.h>
#include <stdlib.h>
#include "polyinit.h"
#include "io.h"
#include "calc.h"
int main(void){
int op_code = 0;
PolyPtr p = createPoly();
PolyPtr p1 = createPoly();
PolyPtr p2 = createPoly();
p = NULL;
p1= NULL;
p2= NULL;
do{
//显示控制台菜单
display_console();
//读取操作序号
scanf("%d", &op_code);
//操作
switch(op_code){
case 0:
//printf("Exit The Program\n");
break;
case 1://Input
p = read_poly();
p1= copy_sorted_poly( p );
break;
case 2://Addition
p2 = copy_sorted_poly( read_poly() );
p = poly_add(p1, p2);
break;
case 3://Subtraction
p2 = copy_sorted_poly( read_poly() );
p = poly_sub(p1, p2);
break;
case 4://Multiplication
p2 = copy_sorted_poly( read_poly() );
p = poly_mul(p1, p2);
break;
case 5:
print_poly( p );
break;
default:
//printf("Invalid Input\n");
break;
}
}while(op_code);
//释放内存
destroyPoly(p);
destroyPoly(p1);
destroyPoly(p2);
return 0;
}
//polyinit means Polynomial Initialize
//this part includes LinkedList, Polynomial Initalize, Basic Operatioin Funtions
#ifndef _POLYINIT_H_
#define _POLYINIT_H_
typedef struct PolyNode{
int coef;//coefficient, Xishu
int expo;//exponent, Zhishu
struct PolyNode *next;
}PolyNode, *PolyPtr;
//Function Prototypes BEGIN
PolyPtr createPoly();
void destroyPoly(PolyPtr);
PolyPtr copy_sorted_poly(PolyPtr p);
//Function Prototypes END
#endif /* _POLYINIT_H_ */
#include <stdlib.h>
#include "polyinit.h"
PolyPtr createPoly() {
PolyPtr head = (PolyPtr)malloc(sizeof(PolyNode));
head->next = NULL;
head->coef = 0;
return head;
}
void destroyPoly(PolyPtr head) {
PolyPtr p = head;
PolyPtr q = NULL;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
}
PolyPtr copy_sorted_poly(PolyPtr p) {
PolyPtr p1 = NULL;
PolyPtr prev, curr, newNode;
for (curr = p; curr != NULL; curr = curr->next) {
newNode = (PolyPtr) malloc(sizeof(PolyNode));
newNode->coef = curr->coef;
newNode->expo = curr->expo;
newNode->next = NULL;
if (p1 == NULL || curr->expo > p1->expo) {
newNode->next = p1;
p1 = newNode;
} else {
prev = p1;
while (prev->next != NULL && curr->expo < prev->next->expo) {
prev = prev->next;
}
newNode->next = prev->next;
prev->next = newNode;
}
}
return p1;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "io.h"
#include "polyinit.h"
void display_console(){
printf("1-Polynomial Input;\n");
printf("2-Polynomial Addition(+);\n");
printf("3-Polynomial Subtraction(-);\n");
printf("4-Polynomial Multiplication(*);\n");
printf("5-Polynomial Output;\n");
printf("0-Exit;\n");
}
PolyPtr read_poly()
{
PolyPtr p, rear, t;
p = (PolyPtr)malloc(sizeof(PolyNode));
rear = p;
rear->next = NULL;
char c;
int coef, expo;
int flag_coef = 1, flag_expo = 0, flag_minus = 0;
coef = 0;
expo = 0;
while ((c = getchar()) != '\n')
{
if (c == '-')
{
flag_minus = 1;
continue;
}
if (isdigit(c))
{
if (flag_expo)
{
expo = expo * 10 + c - '0';
}
else
{
coef = coef * 10 + c - '0';
}
}
else if (c == 'X' || c == 'x')
{
flag_coef = 0;
flag_expo = 1;
}
else if (c == '+' || c == '-')
{
if (flag_minus)
{
coef = -coef;
flag_minus = 0;
}
if (flag_coef)
{
coef = 1;
}
if (coef != 0)
{
t = (PolyPtr)malloc(sizeof(PolyNode));
t->coef = coef;
t->expo = expo;
rear->next = t;
rear = t;
rear->next = NULL;
}
coef = 0;
expo = 0;
flag_coef = 1;
flag_expo = 0;
}
}
if (flag_minus)
{
coef = -coef;
}
if (flag_coef)
{
coef = 1;
}
if (coef != 0)
{
t = (PolyPtr)malloc(sizeof(PolyNode));
t->coef = coef;
t->expo = expo;
rear->next = t;
rear = t;
rear->next = NULL;
}
t = p;
p = p->next;
free(t);
return p;
}
void print_poly(PolyPtr p) {
if (p == NULL) {
printf("0\n");
return;
}
while (p) {
if (p->coef != 0) {
if (p->expo == 0) {
printf("%d", p->coef);
} else if (p->expo == 1) {
printf("%dX", p->coef);
} else {
printf("%dX^%d", p->coef, p->expo);
}
if (p->next && p->next->coef > 0) {
printf("+");
}
}
p = p->next;
}
printf("\n");
}
#ifndef _IO_H_
#define _IO_H_
#include "polyinit.h"
//Function Prototypes BEGIN
void display_console();
PolyPtr read_poly();//Read in polynomial
void print_poly(PolyPtr p);//Output By Decline Order
//Function Prototypes END
#endif /* _IO_H_*/
#ifndef _CALC_H_
#define _CALC_H_
//Function Prototypes BEGIN
PolyPtr poly_add(PolyPtr p1, PolyPtr p2);
PolyPtr poly_sub(PolyPtr p1, PolyPtr p2);
PolyPtr poly_mul(PolyPtr p1, PolyPtr p2);
//Function Prototypes END
#endif /* _CALC_H_*/
#include <stdio.h>
#include <stdlib.h>
#include "polyinit.h"
PolyPtr poly_add(PolyPtr p1, PolyPtr p2) {
PolyPtr res = (PolyPtr)malloc(sizeof(PolyNode));
PolyPtr cur = res;
while (p1 && p2) {
if (p1->expo == p2->expo) {
int sum = p1->coef + p2->coef;
if (sum != 0) {
cur->next = (PolyPtr)malloc(sizeof(PolyNode));
cur = cur->next;
cur->coef = sum;
cur->expo = p1->expo;
}
p1 = p1->next;
p2 = p2->next;
}
else if (p1->expo > p2->expo) {
cur->next = (PolyPtr)malloc(sizeof(PolyNode));
cur = cur->next;
cur->coef = p1->coef;
cur->expo = p1->expo;
p1 = p1->next;
}
else {
cur->next = (PolyPtr)malloc(sizeof(PolyNode));
cur = cur->next;
cur->coef = p2->coef;
cur->expo = p2->expo;
p2 = p2->next;
}
}
cur->next = p1 ? p1 : p2;
PolyPtr tmp = res;
res = res->next;
free(tmp);
return res;
}
PolyPtr poly_sub(PolyPtr p1, PolyPtr p2){
PolyPtr cur = p2;
while (cur) {
cur->coef *= -1;
cur = cur->next;
}
return poly_add(p1, p2);
}
PolyPtr poly_mul(PolyPtr p1, PolyPtr p2) {
PolyPtr head = NULL, tail = NULL;
PolyPtr cur1 = p1, cur2 = p2;
// 遍历链表1中的每一项
while (cur1) {
// 遍历链表2中的每一项
while (cur2) {
// 计算新的指数和系数
int new_expo = cur1->expo + cur2->expo;
int new_coef = cur1->coef * cur2->coef;
// 在结果链表中寻找是否已经存在该指数的项
PolyPtr cur = head;
while (cur) {
if (cur->expo == new_expo) {
cur->coef += new_coef;
break;
}
cur = cur->next;
}
// 如果结果链表中不存在该指数的项,则插入新的节点
if (!cur) {
PolyPtr new_node = (PolyPtr)malloc(sizeof(PolyNode));
new_node->coef = new_coef;
new_node->expo = new_expo;
new_node->next = NULL;
// 将新节点插入到结果链表尾部
if (!head) {
head = tail = new_node;
} else {
tail->next = new_node;
tail = new_node;
}
}
cur2 = cur2->next;
}
cur1 = cur1->next;
cur2 = p2;
}
return head;
}
在 Embarcadero Dev C++ 中调试。我坐在断路器上,然后单击“下一步”和“继续”,然后蓝色高亮栏消失了...... 我不知道如何调试 LinkedList 和多文件程序。
如果你有空,希望你检查我的代码并指出问题所在。 你可能会认出我是初学者......