发布网友 发布时间:2022-04-23 18:54
共1个回答
热心网友 时间:2023-10-13 19:59
给出具体问题追问输入如{(c0,n0),(c1,n1)}+{(c2,n2)}的多项式相加或相减式,c为多项式系数,n为幂,输出运算后的c与n。如果回答后采纳可加分,急求谢谢追答public class Demo {
public static void main(String[] args) throws Exception {
long[] C = new long[10000];
long[] N = new long[10000];
int i = 0, j = 0, l;
char a, b, c = '\0';
char temp;
for (i = 0; c != '\n' && (a = (char) System.in.read()) != '\n' ;) {
if (a == '{') {
do {
temp = (char) System.in.read();
while((temp = (char) System.in.read()) != ',') {
C[i] = C[i]*10+(temp-'0');
}
while((temp = (char) System.in.read()) != ')') {
N[i] = N[i]*10+(temp-'0');
}
b = (char) System.in.read();
if (c == '-') {
C[i] = 0 - C[i];
}
i++;
} while (b != '}');
c = (char) System.in.read();
} else {
System.out.printf("ERROR\n");
return;
}
}
if (i == 0) {
System.out.printf("ERROR");
return;
}
l = i;// l表示总项数
for (i = 0; i < l; i++) {
for (j = i + 1; j <= l; j++) {
if (N[i] == N[j] && N[i] != -1) {
C[i] = C[i] + C[j];
C[j] = 0;
N[j] = -1;
}
}
}
for (i = 0; i <= l; i++) {
if (C[i] != 0) {
System.out.printf("%d,%d\n", C[i], N[i]);
}
}
}
}