oc实现小型学生管理系统
Posted wacyy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oc实现小型学生管理系统相关的知识,希望对你有一定的参考价值。
首先,创建一个工程,然后加入两个cocoaclass,分别命名为Student 和 StudentSystem.
然后就可以开始写代码喽
1.Student类--Student.h文件
```
#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
```
2.Student类--Student.m文件
```
#import "Student.h"
int allgrades=0;
@implementation Student
//extern int allgrades;
@synthesize stuID,name,age,score;
-(id)init//构造函数
{
if (self = [super init]) {
stuID = 0;
name = nil;
age = 0;
score = 0;
}
else
{
self = nil;
}
return self;
}
-(void)inputStudent
{
NSLog(@"请依次输入学生的姓名,学号,年龄,成绩(格式:ganyu 10086 18 100)");
char CharName[20];
int intId;
int intAge,intScore;
scanf("%s %d %d %d",CharName,&intId,&intAge,&intScore);
allgrades+=intScore;
[self setName:[[NSString alloc]initWithUTF8String:CharName]];
[self setStuID:intId];
[self setAge:intAge];
[self setScore:intScore];
}
-(void)printStudent
{
NSLog(@"学号:%d,姓名:%@,年龄:%d,成绩: %d",self.stuID,self.name,self.age,self.score);
}
-(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
```
#import "Student.h"
int allgrades=0;
@implementation Student
//extern int allgrades;
@synthesize stuID,name,age,score;
-(id)init//构造函数
{
if (self = [super init]) {
stuID = 0;
name = nil;
age = 0;
score = 0;
}
else
{
self = nil;
}
return self;
}
-(void)inputStudent
{
NSLog(@"请依次输入学生的姓名,学号,年龄,成绩(格式:ganyu 10086 18 100)");
char CharName[20];
int intId;
int intAge,intScore;
scanf("%s %d %d %d",CharName,&intId,&intAge,&intScore);
allgrades+=intScore;
[self setName:[[NSString alloc]initWithUTF8String:CharName]];
[self setStuID:intId];
[self setAge:intAge];
[self setScore:intScore];
}
-(void)printStudent
{
NSLog(@"学号:%d,姓名:%@,年龄:%d,成绩: %d",self.stuID,self.name,self.age,self.score);
}
-(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
```
3.StudentSystem类--StudentSystem.h 文件
3.StudentSystem类--StudentSystem.h 文件
```
#import <Foundation/Foundation.h>
#import "Student.h"
extern int allgrades;
@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
#import <Foundation/Foundation.h>
#import "Student.h"
extern int allgrades;
@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
```
4.StudentSystem类--StudentSystem.m文件
```
#import "StudentSystem.h"
extern int allgrades;
@implementation StudentSystem
@synthesize studentArray;
-(id)init
{
if (self = [super init]) {
studentArray = [[NSMutableArray alloc]init];
}
else
self = nil;
return self;
}
-(void)countStudent
{
unsigned long int a=[self.studentArray count];
NSLog(@"一共有%lu 位同学",a);
double b=allgrades/a;
NSLog(@"allgrades的值为:%d",allgrades);
NSLog(@"大家的平均成绩是%lf",b);
}
-(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];
allgrades-=stu.score;
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;
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的姓名,年龄,成绩(格式:ganyu 10086 18 100)",studentID);
allgrades-=[stu score];
char CharName[20];
int intId;
int intAge,intScore;
scanf("%s %d %d %d",CharName,&intId,&intAge,&intScore);
[stu setName:[[NSString alloc]initWithUTF8String:CharName]];
[stu setStuID:intId];
[stu setAge:intAge];
[stu setScore:intScore];
allgrades+=[stu score];
return ;
}
}
NSLog(@"未找到学号为%d的学生.",studentID);
}
@end
#import "StudentSystem.h"
extern int allgrades;
@implementation StudentSystem
@synthesize studentArray;
-(id)init
{
if (self = [super init]) {
studentArray = [[NSMutableArray alloc]init];
}
else
self = nil;
return self;
}
-(void)countStudent
{
unsigned long int a=[self.studentArray count];
NSLog(@"一共有%lu 位同学",a);
double b=allgrades/a;
NSLog(@"allgrades的值为:%d",allgrades);
NSLog(@"大家的平均成绩是%lf",b);
}
-(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];
allgrades-=stu.score;
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;
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的姓名,年龄,成绩(格式:ganyu 10086 18 100)",studentID);
allgrades-=[stu score];
char CharName[20];
int intId;
int intAge,intScore;
scanf("%s %d %d %d",CharName,&intId,&intAge,&intScore);
[stu setName:[[NSString alloc]initWithUTF8String:CharName]];
[stu setStuID:intId];
[stu setAge:intAge];
[stu setScore:intScore];
allgrades+=[stu score];
return ;
}
}
NSLog(@"未找到学号为%d的学生.",studentID);
}
@end
```
5.main文件
5.main文件
```
#import <Foundation/Foundation.h>
#import "Student.h"
#import "StudentSystem.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"欢迎来到计科某班考试成绩管理系统");
StudentSystem *class= [[StudentSystem alloc]init];
while (YES) {
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];
NSLog(@"已排序");
break;
case 9:
[class motifyStudent];
break;
case 0:
return 0;
default:
break;
}
}
}
return 0;
}
#import <Foundation/Foundation.h>
#import "Student.h"
#import "StudentSystem.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"欢迎来到计科某班考试成绩管理系统");
StudentSystem *class= [[StudentSystem alloc]init];
while (YES) {
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];
NSLog(@"已排序");
break;
case 9:
[class motifyStudent];
break;
case 0:
return 0;
default:
break;
}
}
}
return 0;
}
```
//关于上面还有一些功能没有实现,有兴趣的话可以尝试一下呦
//关于上面还有一些功能没有实现,有兴趣的话可以尝试一下呦
以上是关于oc实现小型学生管理系统的主要内容,如果未能解决你的问题,请参考以下文章
nodejs+express文件操作实现小型学生管理系统(增删改查)-纯享版
本课程培养学生在充分进行需求分析的基础上,根据实际需求,建立一个中小型单位的计算机网络,并加以管理