Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 成绩册的2d阵列_Objective C_Macos_Cocoa_Parsing_Csv - Fatal编程技术网

Objective c 成绩册的2d阵列

Objective c 成绩册的2d阵列,objective-c,macos,cocoa,parsing,csv,Objective C,Macos,Cocoa,Parsing,Csv,这是我第一次尝试用mac编程。学习曲线是陡峭的。我有一个.csv文件,其中有几列ascii数据,这些数据组织为空白字段、用户名、名称(格式为“姓氏、名中间名”)、作业1级(整数)、作业2级(整数)。。。。作业n等级(整数)、平均等级(删除一个等级)、章节编号(整数)。 在作业栏中,他们可以用一个文本条目代替分数:I代表生病,X代表豁免等。 我需要计算每个作业的平均分数。我是。第9999节中学生的平均成绩为。。我已经研究了各种解析方案,但在我对objective c的理解中,这些方案过于复杂,不知

这是我第一次尝试用mac编程。学习曲线是陡峭的。我有一个.csv文件,其中有几列ascii数据,这些数据组织为空白字段、用户名、名称(格式为“姓氏、名中间名”)、作业1级(整数)、作业2级(整数)。。。。作业n等级(整数)、平均等级(删除一个等级)、章节编号(整数)。 在作业栏中,他们可以用一个文本条目代替分数:I代表生病,X代表豁免等。 我需要计算每个作业的平均分数。我是。第9999节中学生的平均成绩为。我已经研究了各种解析方案,但在我对objective 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;

}
//main.m
#进口
int main(int argc,const char*argv[]
{
@自动释放池
{
//在这里插入代码。。。
//示例代码的开头************************************
//内容是指向包含文件中所有数据的字符串的指针
//contentarray是指向数组的指针,其中每个索引都是文件中的一行(用分隔符分隔\n
//itemarray是指向数组的指针,其中主索引行是文件的第n行,列是该行的第n项。
NSInteger itemcount,rowcount;
NSString*pathwithgradefile=@“/some_path/sampledata.csv”;
//n错误*错误=nil;
NSString*content=[NSString stringWithContentsOfFile:PathWithGrade文件编码:NSUTF8StringEncoding错误:nil];
NSLog(@“%@”,内容);
从文件中创建行数组-此部分有效。
NSArray*contentArray=[content componentsSeparatedByString:@“\n”];//csv文件行以换行符结尾
//contentarray的输出检查。
NSLog(@“\n逐行列出contentArray\n”);
行数=0;
for(contentArray中的NSString*项){
//显示项
NSLog(@“\n\r”);
NSLog(@“行计数:%lid内容:%@”,行计数,[contentArray objectAtIndex:rowcount]);
rowcount++;//此部分有效
}
//解析为二维数组,每行包含行和项
//这一部分是假的——我真的不知道如何继续。
itemcount=0;
for(contentArray中的NSString*项){
NSArray*itemArray=[ItemComponentsSeparatedByString:@“,”];
//记录第一项
NSLog(@“\n\r”);
NSLog(@“项计数:%lid内容:%@”,项计数,[itemArray objectAtIndex:rowcount]);
itemcount++;
}        
}
返回0;
}

如果您的数据不是连续的,或者您想要使用的数字索引中有空白,最好使用带有NSNumber键的字典。类似的东西可能会满足您的需要

NSMutableDictionary *outputData = [NSMutableDictionary dictionaryWithCapacity: 5];
NSUInteger sectionColumnIndex = 3; // What ever column you want to use as the top level
for (NSString *item in contentArray) {
    NSArray *itemArray = [item componentsSeparatedByString:@","];
    NSNumber *sectionNumber = [NSNumber numberWithInt:[[itemArray objectAtIndex: sectionColumnIndex] intValue]];
    if(![outputData objectForKey: sectionNumber]){
        NSMutableArray *sectionEntries = [NSMutableArray arrayWitCapacity: 5];
        [outputData setObject: sectionEntries forKey: sectionNumber];
    }

    NSMutableArray *sectionEntries = [outputData objectForKey: sectionNumber];
    [sectionEntries addObject: itemArray];
}        

注意:这可能有输入错误,但应该让您开始。

简而言之,您有一个contentArray,您想创建2D数组吗?