//
// Student.h
// OC实现学生成绩管理系统
//
// Created by lichan on 13-11-28.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject<NSCoding>
{
@protected
int stuID;
NSString * name;
int age;
int score;
}
@property int stuID;
@property NSString * name;
@property int age;
@property int score;
-(id)init;
-(void)inputStudent;
-(void)printStudent;
//-(void)modifyStudent;
@end
Student.m文件
//
// Student.m
// OC实现学生成绩管理系统
//
// Created by lichan on 13-11-28.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "Student.h"
static int staticID = 1;
@implementation Student
@synthesize stuID,name,age,score;
-(id)init
{
if (self = [super init]) {
stuID = staticID;
staticID++;
name = nil;
age = 0;
score = 0;
}
else
{
self = nil;
}
return self;
}
-(void)inputStudent
{
NSLog(@"请输入学号为%d的姓名,年龄,成绩(格式ag:lichan 18 100)",stuID);
char CharName[20];
int intAge,intScore;
scanf("%s %d %d",CharName,&intAge,&intScore);
[self setName:[[NSString alloc]initWithUTF8String:CharName]];
[self setAge:intAge];
[self setScore:intScore];
// NSLog(@"录入完成!");
}
-(void)printStudent
{
NSLog(@"学号%d,姓名:%@,年龄:%d,成绩: %d",self.stuID,self.name,self.age,self.score);
// NSLog(@"打印完成.");
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:stuID forKey:@"id"];
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeInt:age forKey:@"age"];
[aCoder encodeInt:score forKey:@"score"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.stuID = [aDecoder decodeIntForKey:@"id"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
self.score = [aDecoder decodeIntForKey:@"score"];
}
return self;
}
@end
系统数组的实现:
//
// StudentSystem.h
// OC实现学生成绩管理系统
//
// Created by lichan on 13-11-28.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
@interface StudentSystem : NSObject
@property NSMutableArray *studentArray;
-(id)init;
-(void)printStudentArray;
-(void)addStudentArray:(Student *)stu;
-(void)findStudentByStuID;
-(void)removeStudentArrayByStuID;
-(void)writeToFile;
-(void)readFromFile;
-(void)countStudent;
-(void)SortStudentArray;// >0升序,<0降序
-(void)motifyStudent;
@end
.m文件
//
// StudentSystem.m
// OC实现学生成绩管理系统
//
// Created by lichan on 13-11-28.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "StudentSystem.h"
@implementation StudentSystem
@synthesize studentArray;
-(id)init
{
if (self = [super init]) {
studentArray = [[NSMutableArray alloc]init];
}
else
self = nil;
return self;
}
-(void)countStudent
{
//NSLog(@"一共有 %d 位学生", [[self studentArray] count]);
NSLog(@"一共有%lu 位同学",[self.studentArray count]);
}
-(void)printStudentArray
{
for (Student *stu in studentArray) {
[stu printStudent];
}
NSLog(@"array 打印完毕.");
}
-(void)addStudentArray:(Student *)stu
{
[[self studentArray]addObject:stu];
// NSLog(@"添加完成");
}
-(void)findStudentByStuID
{
NSLog(@"请输入你要查找的同学的学号:");
int studentID;
scanf("%d",&studentID);
for (Student *stu in studentArray) {
if (stu.stuID == studentID) {
[stu printStudent];
return ;
}
}
NSLog(@"未找到学号为%d的学生.",studentID);
}
-(void)removeStudentArrayByStuID
{
NSLog(@"请输入你要删除的同学的学号:");
int studentID;
scanf("%d",&studentID);
for (Student *stu in studentArray) {
if (stu.stuID == studentID) {
[studentArray removeObject:stu];
NSLog(@"删除完成.");
return ;
}
}
NSLog(@"未找到学号为%d的学生.",studentID);
}
-(void)SortStudentArray// >0升序,<0降序
{
NSLog(@"按照学号排序请输入 (id)");
NSLog(@"按姓名排序请输入(name)");
NSLog(@"按照年龄排序请输入(age)");
NSLog(@"按照成绩排序(score)请输入:");
char charKey[10] ;
scanf("%s",charKey);
NSString *tempkey = [NSString stringWithUTF8String:charKey];
NSString *key = [tempkey lowercaseString];
BOOL ascending = NO;
NSLog(@"是否开启降序模式( 默认不开启 )?(yes or no):");
char ascendingStr[10] ;
scanf("%s",ascendingStr);
NSString *ascendingKey =[[NSString stringWithUTF8String:ascendingStr] lowercaseString];
if ([ascendingKey isEqualToString:@"yes"])
ascending = YES;
else
ascending = NO;
if ([key isEqualToString:@"id"]) {
NSSortDescriptor *sortByID = [NSSortDescriptor sortDescriptorWithKey:@"stuID" ascending:ascending];
[studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByID]];
}
else if([key isEqualToString:@"name"])
{
NSSortDescriptor *sortByName= [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:ascending];
[studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByName]];
}
else if([ key isEqualToString:@"age"])
{
NSSortDescriptor *sortByAge = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:ascending];
[studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByAge]];
}
else if ([key isEqualToString:@"score"])
{
NSSortDescriptor *sortByScore = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:ascending];
[studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByScore]];
}
}
-(void)writeToFile
{
NSString *path = [NSString stringWithFormat:@"/Users/len/Desktop"];
NSLog(@"请输入你所要写入的文件名字:");
char sfilename[20];
scanf("%s",sfilename);
NSString *filename = [NSString stringWithUTF8String:sfilename];
NSString *filepath = [path stringByAppendingPathComponent:filename];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:studentArray ];
[data writeToFile:filepath atomically:YES];
}
-(void)readFromFile
{
NSString *path = [NSString stringWithFormat:@"/Users/len/Desktop"];
NSLog(@"请输入你所要读取的文件名字:");
char sfilename[20];
scanf("%s",sfilename);
NSString *filename = [NSString stringWithUTF8String:sfilename];
NSString *filepath = [path stringByAppendingPathComponent:filename];
//NSMutableData *data = [[NSMutableData alloc]initWithContentsOfFile:filepath];
NSMutableData *Data = [NSMutableData dataWithContentsOfFile:filepath];
self.studentArray = [NSKeyedUnarchiver unarchiveObjectWithData:Data];
}
-(void)motifyStudent
{
NSLog(@"请输入你要修改的同学的学号:");
int studentID;
scanf("%d",&studentID);
for (Student *stu in studentArray) {
if (stu.stuID == studentID) {
NSLog(@"修改学号为%d的姓名,年龄,成绩(格式ag:lichan 18 100)",studentID);
char CharName[20];
int intAge,intScore;
scanf("%s %d %d",CharName,&intAge,&intScore);
[stu setName:[[NSString alloc]initWithUTF8String:CharName]];
[stu setAge:intAge];
[stu setScore:intScore];
return ;
}
}
NSLog(@"未找到学号为%d的学生.",studentID);
}
@end
3.主函数
//
// main.m
// OC实现学生成绩管理系统
//
// Created by lichan on 13-11-28.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "StudentSystem.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *array = [NSArray arrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil];
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序
NSComparisonResult result = [obj1 compare:obj2];
return result;
}];
NSLog(@"排序后:%@",sortedArray);
NSLog(@"欢迎进入OC学生成绩管理系统");
StudentSystem *class= [[StudentSystem alloc]init];
while (YES) {
NSLog(@"请进行功能选择:");
NSLog(@"1:增加 2:打印");
NSLog(@"3:删除 4:查找");
NSLog(@"5:写入 6:读出");
NSLog(@"7:统计 8:排序");
NSLog(@"9:修改 0:退出");
int c;
scanf("%d",&c);
switch (c) {
case 1:
{
Student *stuTemp = [[Student alloc]init];
[stuTemp inputStudent];
[class addStudentArray:stuTemp];
break;
}
case 2:
[class printStudentArray];
break;
case 3:
[class removeStudentArrayByStuID];
break;
case 4:
[class findStudentByStuID];
break;
case 5:
[class writeToFile];
break;
case 6:
[class readFromFile];
break;
case 7:
[class countStudent];
break;
case 8:
[class SortStudentArray];
break;
case 9:
[class motifyStudent];
break;
case 0:
return 0;
default:
break;
}
}
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}