因此,我正在创建一个程序,该程序读入名为"Bankfile.txt"的文件,第一个数字“3”表示我们正在使用多少帐户。数字“123”“467”和“499”是银行帐号,每个号码旁边的数字是其原始余额。在我的代码中,我使用2D数组扫描它们。我认为我在扫描它们时有一切正确但是当我运行程序时,帐号被打印得非常奇怪,如here所示。关于为什么要这样打印的任何想法?
谢谢!
#include <stdio.h>
int main(){
FILE* file;
file = fopen("bankfile.txt","r");
int accounts,b=1,w=2,d=3,u=4,i,j,accNum,origBal;
float interest = .0185;
float test;
float accountInfo[accNum][origBal];
fscanf(file, "%d", &accounts);
for(i = 0; i < accounts; i++)
{
fscanf(file, "%d", &accountInfo[i]);
for(j = 0; j < 1; j++)
{
fscanf(file, "%f", &accountInfo[i][j]);
printf("Account %d has a balance of $%.2f\n", accountInfo[i], accountInfo[i][j]);
}
}
system("pause");
return 0;
}
答案 0 :(得分:1)
好的,这里有不 二维数组 - 这在概念上是错误的。帐号只有一个余额。所以你所拥有的只是一个维度,但你的数据有几个字段...这就是你在C中使用function eval_force()
% eval_force.m IS USED FOR EVALUATING FORCE
% THE STRATEGY USUALLY ADOPTED FOR A LENNARD-JONES OR AS A MATTER OF FACT
% ANY PAIR-WISE INTERACTING SYSTEM IS AS FOLLOWS:
% 1. EVALUATE THE DISTANCE BETWEEN TWO PAIRS OF ATOMS
% 2. ENSURE THAT MINIMUM IMAGE CONVENTION (MIC) IS FOLLOWED
% 3. IF THE DISTANCE OBTAINED THROUGH MIC IS GREATER THAN THE CUT OFF
% DISTANCE MOVE TO NEXT PAIR
% 4. ELSE EVALUATE POTENTIAL ENERGY AND CALCULATE FORCE COMPONENTS
% 5. F(i,j) = -F(j,i)
global MASS KB TEMPERATURE NUM_ATOMS LENGTH TSTEP;
global EPS SIG R_CUT GAMMA POT_E;
global POSITION VELOCITY FORCE STO;
dr = zeros(3,1);
drh = zeros(3,1);
FORCE(:) = 0.0;
POT_E = 0.0;
for ( i=1:NUM_ATOMS )
for ( j=i+1:NUM_ATOMS )
dist2 = 0.0; % VARIABLE dist2 STORES DISTANCE BETWEEN PAIR (i,j)
% FIRST FIND OUT THE DIFFERENCE IN X,Y AND Z COORDINATES
% VARIABLE dr IS USED FOR THIS PURPOSE
for(k = 1:3)
dr(k) = POSITION(i,k) - POSITION(j,k);
% THESE STEPS ENSURE MINIMUM IMAGE CONVENTION IS FOLLOWED
if(dr(k) > LENGTH/2.0)
dr(k) = dr(k) - LENGTH;
end
if(dr(k) < -LENGTH/2.0)
dr(k) = dr(k) + LENGTH;
end
% MINIMUM IMAGE CONVENTION ENDS HERE
dist2 = dist2 + dr(k)*dr(k); % dist2 IS BASED UPON MIC
end
if(dist2 <= R_CUT*R_CUT) % IF THE CUT OFF CRITERIA IS SATISFIED
dist2i = power(SIG,2)/dist2;
dist6i = power(dist2i,3);
dist12i = power(dist6i,2);
POT_E = POT_E + EPS * (dist12i - 2*dist6i) + 33.34 * EPS * power(sqrt(dist2) - SIG,2)/(2 * power(SIG,2)); % STORES THE POTENTIAL ENERGY
Ff = 12.0 * EPS * (dist12i-dist6i) - 33.34 * EPS * (sqrt(dist2) - SIG)/(dist2i * sqrt(dist2) * power(SIG,2));
Ff = Ff * dist2i;
for(k = 1:3)
FORCE(i,k) = FORCE(i,k) + Ff*dr(k)- GAMMA*VELOCITY(i,k);
FORCE(j,k) = FORCE(j,k) - Ff*dr(k)- GAMMA*VELOCITY(j,k);
end
end
end
end
end
的地方。这里有一些示例代码可以产生你期望的输出:
struct
首先阅读第一行,然后根据需要只分配#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* data structure holding a bank account */
typedef struct account
{
int id;
double balance;
} account;
int main(void)
{
int cap = 1024; /* initial capacity of account array */
int count = 0; /* number of accounts */
char buf[1024]; /* buffer for a line of text */
char *tok; /* token from text line */
FILE *bankfile;
int i;
account *accounts = malloc(cap * sizeof(account));
bankfile = fopen("bankfile.txt", "r");
while (fgets(buf, 1024, bankfile))
{
int accId;
tok = strtok(buf, " \t\r\n");
if (!tok) continue;
accId = atoi(tok);
if (accId > 0)
{
/* first token in line was a positive integer */
tok = strtok(0, " \t\r\n");
if (tok)
{
/* there was a second token in the same line, then we found
* an account with a balance */
accounts[count].id = accId;
accounts[count].balance = atof(tok);
if (++count == cap)
{
cap *= 2;
accounts = realloc(accounts, cap * sizeof(account));
}
}
}
}
fclose(bankfile);
for (i = 0; i < count; ++i)
{
printf("Account %d has a balance of $%.2f\n",
accounts[i].id, accounts[i].balance);
}
free(accounts);
return 0;
}
的元素,可以简化这一过程。
当然,对于制作,将错误检查添加到account
和fopen()
以及朋友......
答案 1 :(得分:0)
首先,我认为你不了解二维数组的概念。当你有一个2D数组(foo
)并添加两个数组时,它将看起来像这样。
int foo[][] = { {1,2,3} {4,5,6} };
当你致电foo[0]
时,它实际上会引用您添加的第一个数组(即[1,2,3]
),而foo[1]
将引用第二个数组。您应该使用两个名为accountNum
和accountBal
的单独数组。
最后,您永远不会给accNum
或origBal
赋予值,这会使您的2D数组成为空数组。因此,您可以根据accounts
变量为它们动态分配内存。
记住这一点,你应该改变你的主要逻辑来解决这些问题。
fscanf(file, "%d", &accounts);
accountNum = malloc(sizeof(int) * accounts);
accountBal = malloc(sizeof(float) * accounts);
for(i = 0; i < accounts; i++) {
fscanf(file, "%d", &accountNum[i]);
fscanf(file, "%f", &accountBal[i]);
printf("Account %d has a balance of $%.2f\n", accountNum[i], accountBal[i]);
}
free(accountBal);
free(accountNum);