C 读取文本文件并在数组中存储列
我的文件如下所示:C 读取文本文件并在数组中存储列,c,arrays,readfile,C,Arrays,Readfile,我的文件如下所示: 01 01 5.00 1.50 7.50 02 01 4.00 3.00 12.00 02 02 3.00 4.00 12.00 03 01 4.50 3.00 13.50 03 01 7.50 2.50 18.75 03 01 6.00 0.50 3.00 04 01 2.00 3.00 6.00 04 02 2.00 3.00 6.00 05 01 1.50 3.00 4.50 07 01 5.00 1.00 5.00 09 01 1.50 6.00 9.00 in
01 01 5.00 1.50 7.50
02 01 4.00 3.00 12.00
02 02 3.00 4.00 12.00
03 01 4.50 3.00 13.50
03 01 7.50 2.50 18.75
03 01 6.00 0.50 3.00
04 01 2.00 3.00 6.00
04 02 2.00 3.00 6.00
05 01 1.50 3.00 4.50
07 01 5.00 1.00 5.00
09 01 1.50 6.00 9.00
int A[100] = {1, 2, 2, 3, 3, 3, 4, 4, 5, 7, 9}
int B[100] = {1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1}
double C[100] = {5.00, 4.00, 3.00, 4.50, 7.50, 6.00, 2.00, 2.00, 1.50, 5.00, 1.50}
double D[100] = {1.50, 3.00, 4.00, 3.00, 2.50, 0.50, 3.00, 3.00, 3.00, 1.00, 6.00}
我试图读取每一行并将每一列数据存储到单独的数组中。大概是这样的:
01 01 5.00 1.50 7.50
02 01 4.00 3.00 12.00
02 02 3.00 4.00 12.00
03 01 4.50 3.00 13.50
03 01 7.50 2.50 18.75
03 01 6.00 0.50 3.00
04 01 2.00 3.00 6.00
04 02 2.00 3.00 6.00
05 01 1.50 3.00 4.50
07 01 5.00 1.00 5.00
09 01 1.50 6.00 9.00
int A[100] = {1, 2, 2, 3, 3, 3, 4, 4, 5, 7, 9}
int B[100] = {1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1}
double C[100] = {5.00, 4.00, 3.00, 4.50, 7.50, 6.00, 2.00, 2.00, 1.50, 5.00, 1.50}
double D[100] = {1.50, 3.00, 4.00, 3.00, 2.50, 0.50, 3.00, 3.00, 3.00, 1.00, 6.00}
到目前为止,我有:
#include <stdio.h>
#inlcude <stdlib.h>
#define MAXSIZE 100
int main(int argc, char *argv[]) {
int i = 0;
int a, b;
double c, d;
int A[MAXSIZE], B[MAXSIZE];
double C[MAXSIZE], D[MAXSIZE]
while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {
A[i] = a;
B[i] = b;
C[i] = c;
D[i] = d;
}
return 0;
}
我希望能够重新认识到这一点,如果这是真的,我希望能够将第五列(最右边)的两倍相加。我希望它看起来像这样:
03 01
03 01
03 01
01 01 5.00 1.50 7.50
02 01 4.00 3.00 12.00
02 02 3.00 4.00 12.00
03 01 4.50 3.00 ---
03 01 7.50 2.50 ---
03 01 6.00 0.50 35.25
04 01 2.00 3.00 6.00
04 02 2.00 3.00 6.00
05 01 1.50 3.00 4.50
07 01 5.00 1.00 5.00
09 01 1.50 6.00 9.00
另一方面,我希望程序继续搜索出现的第一列/第二列。还有一点额外的背景信息,第一列是房间类型,第二列是房间实例,第五列是这些房间的总面积。这就是为什么我要对最右边的面积求和,因为房间类型与实例相同。\include
#include <stdio.h>
#inlcude <stdlib.h>
#define MAXSIZE 100
int main(int argc, char *argv[]) {
int i = 0;
int a, b;
double c, d;
int A[MAXSIZE], B[MAXSIZE];
double C[MAXSIZE], D[MAXSIZE]
while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {
A[i] = a;
B[i] = b;
C[i] = c;
D[i] = d;
i++; // your saviour
}
return 0;
}
#内因库德
#定义最大尺寸100
int main(int argc,char*argv[]){
int i=0;
INTA,b;
双c,d;
int A[MAXSIZE],B[MAXSIZE];
双C[MAXSIZE],D[MAXSIZE]
而(scanf(“%d%d%lf%lf”、&a、&b、&c、&d)==4){
A[i]=A;
B[i]=B;
C[i]=C;
D[i]=D;
i++;//你的救世主
}
返回0;
}
以下代码:
cleanly compiles
does not leave trash in unused entries in arrays
eliminates unneeded variables
checks to assure that arrays are not overflowed
removes ~1/2 the statements in the code as they are not needed
corrects the spelling of the word `include`
现在是代码:
#include <stdio.h>
#include <stdlib.h> // size_t
#define MAXSIZE (100)
int main( void )
{
int A[MAXSIZE] = {0};
int B[MAXSIZE] = {0};
double C[MAXSIZE] = {0.0};
double D[MAXSIZE] = {0.0};
for ( size_t i=0;
i< MAXSIZE && (scanf("%d %d %lf %lf",
&A[i], &B[i], &C[i], &D[i]) == 4);
i++);
return 0;
}
#包括
#包括//size\t
#定义最大尺寸(100)
内部主(空)
{
int A[MAXSIZE]={0};
int B[MAXSIZE]={0};
双C[MAXSIZE]={0.0};
双D[MAXSIZE]={0.0};
对于(尺寸i=0;
i
不确定我是否正确理解了你的问题,但我有一个想法:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
int main(int argc, char *argv[]) {
int i = 0;
int a, b;
double c, d;
int A[MAXSIZE], B[MAXSIZE];
double C[MAXSIZE], D[MAXSIZE];
int previousA=-1, previousB=-1;
double tempsum=0.0;
while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {
A[i] = a;
B[i] = b;
C[i] = c;
D[i] = d;
if (previousA==A[i] && previousB==B[i]) { //to check for recurrent values
tempsum+=(C[i]*D[i]);
}
else {
//do whatever you want with the last stored tempsum value, which is the result of the multiplication of the C and D elements needed
tempsum=(C[i]*D[i]); //then tempsum is reset to the current multiplication
}
i++;
}
return 0;
#包括
#包括
#定义最大尺寸100
int main(int argc,char*argv[]){
int i=0;
INTA,b;
双c,d;
int A[MAXSIZE],B[MAXSIZE];
双C[MAXSIZE],D[MAXSIZE];
int-previousA=-1,previousB=-1;
双温和=0.0;
而(scanf(“%d%d%lf%lf”、&a、&b、&c、&d)==4){
A[i]=A;
B[i]=B;
C[i]=C;
D[i]=D;
if(previousA==A[i]&&previousB==B[i]){//检查循环值
tempsum+=(C[i]*D[i]);
}
否则{
//使用最后存储的tempsum值执行任何操作,该值是所需C和D元素相乘的结果
tempsum=(C[i]*D[i]);//然后将tempsum重置为当前乘法
}
i++;
}
返回0;
}修改已批准答案的代码,以符合悬赏要求-
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
int main(int argc, char *argv[]) {
int i = 0;
int a, b;
double c, d, e;
int A[MAXSIZE], B[MAXSIZE];
double C[MAXSIZE], D[MAXSIZE], E[MAXSIZE];
while (scanf("%d %d %lf %lf %lf", &a, &b, &c, &d, &e) == 5) {
A[i] = a;
B[i] = b;
C[i] = c;
D[i] = d;
if (i > 0 && A[i] == A[i-1] && B[i] == B[i-1])
{
E[i] = E[i-1] + e;
E[i-1] = -1;
}
else
E[i] = e;
i++;
}
for (int j = 0; j < i; j++)
{
printf("%d %d %.2lf %.2lf %.2lf\n", A[j], B[j], C[j], D[j], E[j]);
}
return 0;
}
为什么要用索引存储在每个数组中?在
while
循环中添加i++
作为最后一条语句?发布的代码根本不会编译。编译时,始终启用所有警告,然后修复这些警告。(对于gcc
至少使用:-Wall-Wextra-pedantic
我也使用:-Wconversion-std=gnu99
)正如@kaylum所说,循环中的增量I,代码的其他部分看起来很好,您已经将它们存储在数组中。为了便于可读性和我们人类理解:1)遵循公理:每行只有一条语句,并且(最多)每条语句有一个变量声明。