一位朋友给我留下了一个代码,它应该打开 2 个文件solution.txt
并priority.csv
生成第三个文件result.txt
作为输出。
文件结构:
solution.txt
包含用逗号分隔的目标、名称和值的有序列表,我知道文件包含多少行:
target0,name0,value0,name1,value1,name2,value2,name3,value3 target1,name4,value4,name5,value5,name6,value6,name7,value7 target2,name8,value8,name9,value9,name10,value10,name11,value11 ...etc
- 的一些
solution.txt
行仅包含一个target
值:
目标3
目标4
目标5 ...等。
priority.csv
包含颜色名称的索引列表,我知道有多少颜色名称:
1,颜色名称1
2、颜色名称2
3、颜色名称3
下面的代码应该执行以下操作:
- 请问solution.txt包含多少行数据
- 问有多少颜色名称包含在
priority.csv
- 将
name
(s) 和相关联value
的 (s)solution.txt
按定义的顺序放置priority.csv
- 创建一个数组,其中包含与颜色数一样多的列,以及与
solution.txt
- 用与每个&
value
关联的 (s)填充此数组line
colorname
- 用 0 填充其他情况
由于某些我不明白的原因,该代码不起作用。如果你有勇气编译,这里有一些例子放在 solution.txt 和 priority.txt 中:
解决方案.txt
99985,CIN,0.624049619347,OR0,0.36925123875,K2M,0.00387491644559,gY6D,0.00282422545715
99986,CIN,0.624372658354,OR0,0.369683600811,K2M,0.00365124527159,gY6D,0.00229249556329
99987,CIN,0.624695697361,OR0,0.370115962872,K2M,0.0034275740976,gY6D,0.00176076566943
99988,CIN,0.625018736368,OR0,0.370548324933,K2M,0.0032039029236,gY6D,0.00122903577557
99989,CIN,0.625341775375,OR0,0.370980686994,K2M,0.00298023174961,gY6D,0.00069730588171
99990,CIN,0.625664814382,OR0,0.371413049055,K2M,0.00275656057561,gY6D,0.000165575987851
优先级.csv
1,CIN
2,K2M
3,gY6D
4,OR0
在这个例子中,线的数量是 6,有 4 种颜色
这就是代码,我列出了可能的错误,并在控制台上打印了 error2 和 error4:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
int COLORS = 0;
int LINES = 0;
cout << endl << "Original Data (solution.txt):" << endl << endl << "How many lines?" << endl;
cin >> LINES;
cout << "How many colors in the list?" << endl;
cin >> COLORS;
char Coul[COLORS][LINES];
//1- reading the file priority.csv
for (int i=0; i<COLORS; i++) Coul[i][0]='\0';
FILE *Read=fopen("priority.csv","rt");
if (Read == NULL) {
printf("Error 1");
return(0);
}
char line[120];
int N;
for (int i=0; i<COLORS; i++)
{
char name[8];
fgets(line,15,Read);
sscanf(line,"%d,%s",&N,name);
if (N == i) strcpy(Coul[i], name);
else {
printf("Error 2"); // Error2
break;
}
}
fclose(Read);
//2- reading the file solution.txt and writing the result.
Read=fopen("solution.txt","rt");
if (Read == NULL) {
printf("Error 3"); // Error3
return(0);
}
FILE *Write=fopen("result.txt","wt");
for (int i=1; i<LINES; i++)
{
char c[4][8]; // 4 color names
float v[4]; // 4 values
fgets(line,119,Read);
sscanf(line,"%d,%s,%f,%s,%f,%s,%f,%s,%f",
&N, c[0], &v[0], c[1], &v[1], c[2], &v[2], c[3], &v[3]);
if (N == i)
{
if (strlen(c[0]) == 0) // the line is empty
{
fprintf(Write,"%d ",i);
for (int i=0; i<COLORS; i++) fprintf(Write,"0 ");
fprintf(Write,"\n");
}
else
{
fprintf(Write,"%d ",i);
int r[4];
// we search the rang of c[ordre]
for (int order=0; order<4; order++)
{
for (int ir=0; ir<COLORS; ir++)
{
if (strcmp(c[order], Coul[ir]) == 0) r[order]=ir;
}
}
for (int ir=0; ir<r[0]; ir++) fprintf(Write,"0 ");
fprintf(Write,"%d ",v[0]);
for (int ir=r[0]+1; ir<r[1]; ir++) fprintf(Write,"0 ");
fprintf(Write,"%d ",v[1]);
for (int ir=r[1]+1; ir<r[2]; ir++) fprintf(Write,"0 ");
fprintf(Write,"%d ",v[2]);
for (int ir=r[2]+1; ir<r[3]; ir++) fprintf(Write,"0 ");
fprintf(Write,"%d ",v[3]);
for (int ir=r[3]+1; ir<57; ir++) fprintf(Write,"0 ");
fprintf(Write,"\n");
}
}
else {
printf("Error 4");
break;
} //Error4
}
fclose(Write);
fclose(Read);
}
你能帮忙调试吗?我迷路了!阿德里安