これは、Mac で何かをプログラミングする最初の試みです。学習曲線は急勾配です。空白のフィールド、ユーザー名、名前(「姓、名のミドルネーム」の形式)、課題1グレード(整数)、課題2グレード(整数)で構成されたASCIIデータの列をいくつか含む.csvファイルがあります...課題n 評点 (整数)、評点平均 (1 評点を落とす)、セクション番号 (整数)。成績の代わりに、課題列にテキストを入力することもできます。I は病気、X は免除などです。各課題のセクションごとの成績平均を計算する必要があります。私は。セクション 9999 の生徒の平均成績は__です. 私はさまざまな構文解析スキームを見てきましたが、目的 c を理解するこの時点では非常に複雑すぎて、それらがどのように機能するかは言うまでもなく、それらの使用方法を知ることはできません。したがって、より具体的で普遍的ではないものにリンクしています。
ファイルを長い文字列に読み込むコードをいくつか書きました。文字列をコード行の配列に分割するコードを作成しました (生徒 n による成績)。行の配列をアイテムの行の 2D 配列に分割する方法がわかりません。それが終わったら、個々の課題の成績の各列を見て、各課題のセクション平均を計算したいと思います。その行の生徒が計算中のセクションにいる場合、数値成績を合計し、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;
}