这是我第一次尝试用mac编程任何东西。学习曲线陡峭。我有一个.csv文件,有几列ascii数据组织空白字段,用户名,名称(格式为“姓氏,名字中间名”),赋值1级(整数),赋值2级(整数)....赋值n等级(整数),等级平均值(下降一个等级),区号(整数)。 代替等级他们可能是在作业栏中的文本条目:我生病了,X免除等。 我需要为每个作业分段计算坡度平均值。 I'e。第9999条学生的平均成绩为__。我已经看了各种解析方案,但是在我了解目标c知道如何使用它们的过程中,在这一点上太复杂了,更不用说它们是如何工作的。所以我要链接更具体,更不通用的东西。二年级数组
我写了一些代码将文件读入长字符串。 我已经编写代码来将字符串分解成一行代码行(由学生n分级) 我对如何将行数组分解为二维条目行数组有困难。 一旦我这样做了,我想通过查看每个成员的每个成员列表来计算每个作业的成绩平均值。如果该行的学生在我正在计算的部分中,我想对数字成绩进行求和,跳过任何S或X条目并除以数字条目数以获得该作业的平均值。
最终我会输出一个csv文件,其中包含部分编号,每个作业的平均成绩,每个作业的累积成绩。
因此,对于我的第一个问题,如何将数组拆分为条目以及如何使用数字索引访问单个条目?任何建设性的指导都会得到最优秀的接受。
这是我迄今所做的:
// main.m
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool
{
// insert code here...
// beginning of sample code ************************************
// content is a pointer to a string containing all the data from the file
// contentarray is a pointer to an array where each index is a line from the file (separated by \n
// itemarray is a pointer to an array where the primary index - row is the nth line of the file and column is the nth item in the line.
NSInteger itemcount, rowcount;
NSString *pathwithgradefile = @"/some_path/sampledata.csv";
// NSError *error = nil;
NSString *content = [NSString stringWithContentsOfFile:pathwithgradefile encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",content);
Make array of lines from the file - This part works.
NSArray *contentArray = [content componentsSeparatedByString:@"\n"]; // csv file line ends with newline
// output check of contentarray.
NSLog(@" \nlist contentArray line by line\n");
rowcount=0;
for (NSString *item in contentArray) {
// display item
NSLog(@"\n\r");
NSLog(@"row count: %lid Content: %@",rowcount, [contentArray objectAtIndex:rowcount]);
rowcount++; // this section works
}
// parse into 2d array with rows and items in each row
// this section is bogus - I am really clueless on how to proceed.
itemcount=0;
for (NSString *item in contentArray) {
NSArray *itemArray = [item componentsSeparatedByString:@","];
// log first item
NSLog(@"\n\r");
NSLog(@"item count: %lid Content: %@",itemcount, [itemArray objectAtIndex:rowcount]);
itemcount++;
}
}
return 0;
}
总之,你有一个contentArray,你想制作二维数组? – 2013-04-11 17:34:11