- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用链接列表乘以多项式。这是代码,但这给了我不正确的结果。
#include<stdio.h>
#include<stdlib.h>
struct node {
int coefficient, exponent;
struct node *next;
};
struct node *hPtr1, *hPtr2, *hPtr3;
/*
* creates new node and fill the given data
*/
struct node * buildNode(int coefficient, int exponent) {
struct node *ptr = (struct node *) malloc(sizeof (struct node));
ptr->coefficient = coefficient;
ptr->exponent = exponent;
ptr->next = NULL;
return ptr;
}
/* insert data in decending order - based on exponent value */
void polynomial_insert(struct node ** myNode, int coefficient, int exponent) {
struct node *lPtr, *pPtr, *qPtr = *myNode;
lPtr = buildNode(coefficient, exponent);
/* inserting new node at appropriate position */
if (*myNode == NULL || (*myNode)->exponent < exponent) {
*myNode = lPtr;
(*myNode)->next = qPtr;
return;
}
/* placing new node between two nodes or end of node */
while (qPtr) {
pPtr = qPtr;
qPtr = qPtr->next;
if (!qPtr) {
pPtr->next = lPtr;
break;
}
else if ((exponent < pPtr->exponent) && (exponent > qPtr->exponent)){
lPtr->next = qPtr;
pPtr->next = lPtr;
break;
}
}
return;
}
/* inserting new node with resultant data into the output list (n1) */
void polynomial_add(struct node **n1, int coefficient, int exponent) {
struct node *x = NULL, *temp = *n1;
if (*n1 == NULL || (*n1)->exponent < exponent) {
/* adding at the front */
*n1 = x = buildNode(coefficient, exponent);
(*n1)->next = temp;
} else {
while (temp) {
if (temp->exponent == exponent) {
/* updating the co-efficient value alone */
temp->coefficient = temp->coefficient + coefficient;
return;
}
if (temp->exponent > exponent && (!temp->next || temp->next->exponent < exponent)) {
/* inserting in the middle or end */
x = buildNode(coefficient, exponent);
x->next = temp->next;
temp->next = x;
return;
}
temp = temp->next;
}
x->next = NULL;
temp->next = x;
}
}
void polynomial_multiply(struct node **n1, struct node *n2, struct node *n3) {
struct node * temp;
int coefficient, exponent;
temp = n3;
/* if both input list are absent, then output list is NULL */
if (!n2 && !n3)
return;
/* input list 1(n2) is absent, then output list is input list2 (n3) */
if (!n2) {
*n1 = n3;
} else if (!n3) {
/*
* list n3 is absent, then o/p list is n2
*/
*n1 = n2;
} else {
while (n2) {
while (n3) {
/* multiply coefficient & add exponents */
coefficient = n2->coefficient * n3->coefficient;
exponent = n2->exponent + n3->exponent;
n3 = n3->next;
/* insert the above manipulated data to o/p list */
polynomial_add(n1, coefficient, exponent);
}
n3 = temp;
n2 = n2->next;
}
}
return;
}
/* delete the given input list */
struct node * polynomial_deleteList(struct node *ptr) {
struct node *temp;
while (ptr){
temp = ptr->next;
free(ptr);
ptr = temp;
}
return NULL;
}
void polynomial_view(struct node *ptr) {
int i = 0;
int flag=0;
while (ptr) {
if(ptr->exponent != 0 || ptr->exponent != 1 ){
if(ptr->coefficient > 0 && flag==0 ){
printf("%dx^%d", ptr->coefficient,ptr->exponent);
flag++;
}
else if (ptr->coefficient > 0 && flag==1 )
printf("+%dx^%d", ptr->coefficient,ptr->exponent);
else if(ptr->coefficient < 0)
printf("%dx^%d", ptr->coefficient,ptr->exponent);
}
else if (ptr->exponent == 0){
if(ptr->coefficient > 0 && flag==0 ){
printf("%d", ptr->coefficient);
flag++;
}
else if (ptr->coefficient > 0 && flag==1 )
printf("+%d", ptr->coefficient);
else if(ptr->coefficient < 0)
printf("%d", ptr->coefficient);
}
else if( ptr->exponent == 1 ){
if(ptr->coefficient > 0 && flag==0 ){
printf("%dx", ptr->coefficient);
flag++;
}
else if (ptr->coefficient > 0 && flag==1 )
printf("+%dx", ptr->coefficient);
else if(ptr->coefficient < 0)
printf("%dx", ptr->coefficient);
}
ptr = ptr->next;
i++;
}
printf("\n");
return;
}
int main (int argc, char *argv[]) {
int coefficient, exponent, i, n;
int count;
printf("-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=\n");
printf(" Multiplication of Two Polynomials\n");
printf("-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=\n");
printf("Enter the number of coefficients in the multiplicand:");
scanf("%d",&count);
for(i=0;i<count;i++){
printf("Enter the coefficient part:");
scanf("%d", &coefficient);
printf("Enter the exponent part:");
scanf("%d",&exponent);
polynomial_insert(&hPtr1, coefficient, exponent);
}
printf("Enter the number of coefficients in the multiplier:");
scanf("%d",&count);
for(i=0;i<count;i++){
printf("Enter the coefficient part:");
scanf("%d", &coefficient);
printf("Enter the exponent part:");
scanf("%d",&exponent);
polynomial_insert(&hPtr2, coefficient, exponent);
}
printf("Polynomial Expression 1: ");
polynomial_view(hPtr1);
printf("Polynomial Expression 2: ");
polynomial_view(hPtr2);
polynomial_multiply(&hPtr3, hPtr1, hPtr2);
printf("Output:\n");
polynomial_view(hPtr3);
printf("-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
hPtr1 = polynomial_deleteList(hPtr1);
hPtr2 = polynomial_deleteList(hPtr2);
hPtr3 = polynomial_deleteList(hPtr3);
return 0;
}
我期望的输出如下
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-= -=-=-=-=–=
两个多项式的乘法
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=- =-=-=–=
输入被乘数的系数个数:2
输入系数部分:3
输入指数部分:2
输入系数部分:2
输入指数部分:3
输入乘数中的系数个数:2
输入系数部分:4
输入指数部分:2
输入系数部分:1
输入指数部分:3
多项式1:2x^3+3x^2
多项式表达式2:1x^3+4x^2
输出:
2x^6+11x^5+12x^4
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-
但是接下来的输出是
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=
Multiplication of Two Polynomials
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=
Enter the number of coefficients in the multiplicand:3
Enter the coefficient part:10
Enter the exponent part:x
Enter the coefficient part:Enter the exponent part:Enter the coefficient part:Enter the exponent part:Enter the number of coefficients in the multiplier:Enter the coefficient part:Enter the exponent part:Enter the coefficient part:Enter the exponent part:Enter the coefficient part:Enter the exponent part:Polynomial Expression 1: 10x^0+10x^0+10x^0
Polynomial Expression 2: 10x^0+10x^0+10x^0
Output:
900x^0
-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
上面的代码有什么错误?
最佳答案
在示例输入中,您为指数部分指定了 x
。
Enter the exponent part:x
字符x
被读入一个指数
,它是一个整数。阅读这样做的结果是什么?
来自C语言标准(n1256):
7.19.6.4 The scanf function
Synopsis
1. #include int scanf(const char * restrict format, ...);
Description
2. The scanf function is equivalent to fscanf with the argument stdin interposed before the arguments to scanf.7.19.6.2 The fscanf function ...
- Except in the case of a % specifier, the input item (or, in the case of a %n directive, the count of input characters) is converted to a type appropriate to the conversion specifier. If the input item is not a matching sequence, the execution of the directive fails: this condition is a matching failure. Unless assignment suppression was indicated by a *, the result of the conversion is placed in the object pointed to by the first argument following the format argument that has not already received a conversion result. If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
这将进一步导致polynomial_insert
函数出现问题。在调用 polynomial_insert
函数之前,您可以检查 scanf
是否成功,以及从 std 输入读取的值是否为 coefficient
和 exponent
变量在可接受的范围内。
请参阅@JonathanLeffler 的评论,它很好地解释了这一点。
关于c - 使用链表进行两个多项式相乘的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51934645/
int SIZE = 512; p = new BigInteger(SIZE, 15, new Random()); q = new BigInteger(SIZE, 15, new
我正在寻找一种方法来扩展以下形式的逻辑表达式(在字符串中): “(A或B)和((C和D)或E)” 在Python中生成所有正集的列表,即 ['A and C and D', 'A and E', 'B
我正在从 MySQL 数据库中提取一组数字,并尝试对它们进行一些简单的数学运算来计算要放入发票中的总价格,但 PHP 不配合。我认为这可能是类型强制问题,因此我尝试添加一些 intval 和 floa
给定一个包含多列字典的数据框,我如何将数据框中的键相加和/或相乘以获得一列 A B {"ab":1,
我有 2 个 UIImageView - 一个在底部并显示默认图像(如照片)- 在第二个 UIImageView 上您可以在其中绘制。 我想从两个图像创建一个 UIImage,并将其保存为新图像。 我
我有一项作业,并且我已经写完了前两部分,但我只是不知道如何找到最小的数字。我应该提到它在 jFrame (gui) 中。它应该看起来像这样: 假设我有一个数字列表 (10 5 8 7 4 9),我想知
我是 python 的新手,但是有没有办法将矩阵与 0 和符号相乘?例如,见下文: import sympy as sym import numpy as np teams=np.matrix([[1
让我们为变量赋值: thisIsANumberVariable % +5 #adds 5 to thisIsANumberVariable thisIsANumberVariable [1] 8 th
我正在尝试以下操作: Eigen::SparseMatrix bijection(2 * face_count, 2 * vert_count); /* initialization */ Eigen
我必须创建一个没有 * 或 / 运算符的乘法函数。我已经做了这样的方法。 for(int i=0; i < number1; i++){ result += number2; } System
让我们为变量赋值: thisIsANumberVariable % +5 #adds 5 to thisIsANumberVariable thisIsANumberVariable [1] 8 th
这个问题已经有答案了: Convert String to double in Java (14 个回答) 已关闭 9 年前。 我的代码有问题。所以我知道我不能将我所拥有的乘以字符串,但我真的不知道有
我正在尝试以下操作: Eigen::SparseMatrix bijection(2 * face_count, 2 * vert_count); /* initialization */ Eigen
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我需要将矩阵和 vector 相乘。 为了实现这一点,我编写了一个带参数的函数: float** M 尺寸的最大值:m x n。 float* V 长度为 n 的 vector 。 float* R
我正在尝试找出一个好的循环展开来将两个矩阵相乘。 例如,如果我们想要对 NxN 矩阵求和: void SumMatrix(int *M, int n, int *result) { int i,
如果我创建一个像这样的字符串 mutiples=[1,2,3,4,5] 我希望能够使用 .forEach 将它们相乘,我该怎么做?我最好的猜测是: var total=0 multiples=[1,2
我试图将 price(1-3) 标签中的数据乘以 counterValue 以显示所选每个选项的价格 到目前为止,我的代码可以将counterValue 乘以所选选项Btn(1-3) 的因子 被选中
我有两个大小相同的 3-D 数组 a 和 b np.random.seed([3,14159]) a = np.random.randint(10, size=(4, 3, 2)) b = np.ra
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 4 年前。 Improve t
我是一名优秀的程序员,十分优秀!