這是我第一次嘗試用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