我正在尝试编写一个 C 程序来生成求解线性方程组的 R 文件。主要是,我有六个嵌套的 for 循环来获取系数为整数 0 - 9 的每次迭代:
ax + by + c = dx + ey + f
a_2x + b_2y + c_2 = d_2x + e_2y + f
每个方程都是一个由 6 个整数系数组成的数组。在将系数传递给generateContentForSystems 之前,我在主函数中设置了系数值。
但是,我的打印语句返回:
numx的值:-000-0 numy的值:000-0 numz的值:00-0 numx_2的值:0-0 numy_2的值:-0 numz_2的值:0
我相信这是因为错误的指针运算。我现在正在尝试从一个指针到一个数组(在 main 中)并有一个数组的数组。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "scaffold.c"
void * generateContentForSystems(int * row1, int * row2) {
static int index = 0;
int x = row1[0] - row1[3];
int y = row1[1] - row1[4];
int z = row1[5] - row1[2];
int x_2 = row2[0] - row2[3];
int y_2 = row2[1] - row2[4];
int z_2 = row2[5] - row2[2];
int prod1 = x * y_2;
int prod2 = x_2 * y;
int determinant = prod1 - prod2;
if (determinant != 0) {
printf("the value of determinant: %d", determinant);
char * error1;
char Q[1000];
strcpy(Q, "emake <- function(){\noptions(\"warn\"=-1)\ne <- 0\nfor (n in 0:2000){\ne <- e+ 1/(factorial(n))\n}\nreturn(e)\n}\ne <- emake()\n");
char numx[1];
char numy[1];
char numz[1];
char numx_2[1];
char numy_2[1];
char numz_2[1];
sprintf(numx, "%d", x);
sprintf(numy, "%d", y);
sprintf(numz, "%d", z);
sprintf(numx_2, "%d", x_2);
sprintf(numy_2, "%d", y_2);
sprintf(numz_2, "%d", z_2);
//debug:
printf("value of numx:%s value of numy:%s value of numz:%s value of numx_2:%s value of numy_2:%s value of numz_2:%s", numx, numy, numz, numx_2, numy_2, numz_2);
strcat(Q, "A = array(c(");
strcat(Q, numx);
strcat(Q, ", ");
strcat(Q, numx_2);
strcat(Q, ", ");
strcat(Q, numy);
strcat(Q, ", ");
strcat(Q, numy_2);
strcat(Q, "), dim = c(2,2,1))\n");
strcat(Q, "b = c(");
strcat(Q, numz);
strcat(Q, ", ");
strcat(Q, numz_2);
strcat(Q, ")\n");
strcat(Q, "solve(A[,,1],b)\n");
char filename[100];
char snum[5];
itoa(index, snum);
index++;
strcpy(filename, "practice/");
strcat(filename, snum);
strcat(filename, ".R");
FILE * F = fopen(filename, "w");
fputs(Q, F);
fclose(F);
char path[1024];
char command[300];
strcpy(command, "Rscript ");
strcat(command, "practice/");
debug("After Rscript formation");
strcat(command, snum);
strcat(command, ".R");
FILE * fp = popen(command, "r");
if (!fp) { //validate file is open
return NULL;
}
while (fgets(path, sizeof(path) - 1, fp) != NULL) {
debug("in Primary While Loop");
fflush(stdout);
printf("the solution: %s", path);
if (strstr(path, ".") > strstr(path, "with absolute error") || strstr(path, ".5 ") != NULL) {
printf("answer was accepted");
}
}
}
}
int main() {
int arrayIndexes = 0;
int ** myArray = malloc(1 * sizeof( * myArray));
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 10; b++) {
for (int c = 0; c < 10; c++) {
for (int d = 0; d < 10; d++) {
for (int e = 0; e < 10; e++) {
for (int f = 0; f < 10; f++) {
myArray[arrayIndexes] = malloc(6 * sizeof(int));
myArray[arrayIndexes][0] = a;
myArray[arrayIndexes][1] = b;
myArray[arrayIndexes][2] = c;
myArray[arrayIndexes][3] = d;
myArray[arrayIndexes][4] = e;
myArray[arrayIndexes][5] = f;
if (arrayIndexes > 0) {
for (int i = 0; i < arrayIndexes; i++) {
generateContentForSystems(myArray[arrayIndexes], myArray[i]);
}
}
++arrayIndexes;
myArray = realloc(myArray, (arrayIndexes + 1) * sizeof( * myArray));
}
}
}
}
}
}
for (int n = 0; n = arrayIndexes; n++) {
free(myArray[n]);
}
free(myArray);
return 0;
}
最佳答案
您的数字字符串不够长:
char numx[1];
char numy[1];
char numz[1];
char numx_2[1];
char numy_2[1];
char numz_2[1];
字符串由字符序列加上一个空终止字节组成。因此,即使是单个数字也需要至少为 2 的数组大小。当您使用 sprintf 将数字的文本表示形式写入其中一个数组时,您会在数组末尾写入。这会调用 undefined behavior .
这些数组需要足够大,以容纳您可能传入的任何值,包括需要时的负号。
char numx[10];
char numy[10];
char numz[10];
char numx_2[10];
char numy_2[10];
char numz_2[10];
关于创建二维数组并设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44891206/