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
```
```
#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 文件
#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 "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文件
#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实现小型学生管理系统的更多相关文章
- Java 小型学生管理系统心得
这个学生管理系统相对来说比较简单,主要就是复习下java怎么连接数据库,然后你怎么来实现这个功能,我简单的说下思路吧. 首先你要构思好这个界面,他包括增删查改这些基本功能,然后你去分析这些功能都能怎么 ...
- 【IOS开发笔记02】学生管理系统
端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...
- 【IOS开发笔记01】学生管理系统(上)
端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...
- C程序范例(2)——学生管理系统”链表“实现
1.对于学生管理系统,能够实现的方法有许多,但是今天我们用链表的方法来实现.虽然初学者很可能看不懂,但是不要紧,这是要在整体的系统的学习完C语言之后,我才编写出的程序.所以大家不必要担心.在这里与大家 ...
- jsp学习之基于mvc学生管理系统的编写
mvc开发模式:分别是 model层 view层 Control层 在学生管理系统中,model层有学生实体类,数据访问的dao层,view层主要是用于显示信息的界面,Control层主要是servl ...
- java版本的学生管理系统
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Frame; import java.awt.event.Ac ...
- 学生管理系统-火车订票系统 c语言课程设计
概要: C 语言课程设计一---学生管理系统 使使用 C 语言实现学生管理系统.系统实现对学生的基本信息和考试成绩的 管理.采用终端命令界面,作为系统的输入输出界面.采用文件作为信息存储介质. 功能描 ...
- Java学生管理系统项目案例
这是一个不错的Java学生管理系统项目案例,希望能够帮到大家的学习吧. 分代码如下 package com.student.util; import java.sql.Connection; impo ...
- Java+Mysql+学生管理系统
最近正在学java和数据库,想起以前写的学生管理系统,都是从网上下载,敷衍了事.闲来无事,也就自己写了一个,不过功能实现的不是很多. 开发语言:java: 开发环境:Mysql, java: 开发工具 ...
随机推荐
- paper sharing :学习特征演化的数据流
特征演化的数据流 数据流学习是近年来机器学习与数据挖掘领域的一个热门的研究方向,数据流的场景和静态数据集的场景最大的一个特点就是数据会发生演化,关于演化数据流的研究大多集中于概念漂移检测(有监督学习) ...
- ubuntu触摸板双指滑动,页面滚动方向
setting——mouse & Touchpad——Natural scrolling 跟我的另一台本子一样了-
- 【原创】(十一)Linux内存管理slub分配器
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- 执行yaml.load()出现警告信息:YAMLLoadWarning: callingyaml.load() without Loader=..
执行yaml.load()出现警告信息:YAMLLoadWarning: callingyaml.load() without Loader=... 原因: yaml5.1版本后弃用了yaml.loa ...
- odoo12 修行基础篇之 添加字段 (一)
本人刚刚接触odoo12,大概有2个多月的时间,这两天有点时间,就集中写下博客. 本来是打算整理成笔记,想到这段时间的开发经历,着实感觉网上有关odoo的资料太少,学习资料也不多,既然与odoo有缘, ...
- 基于HTTP协议的WAF绕过
一,畸形包绕过 1.先关闭burpsuite长度更新,为get请求,先使用bp的method转换为POST请求 2.get请求中空格使用%20代替,Connection改为keep-alive 二,分 ...
- WebGL简易教程(十三):帧缓存对象(离屏渲染)
目录 1. 概述 2. 示例 2.1. 着色器部分 2.2. 初始化/准备工作 2.2.1. 着色器切换 2.2.2. 帧缓冲区 2.3. 绘制函数 2.3.1. 初始化顶点数组 2.3.2. 传递非 ...
- pymongo的基本操作和使用--练习
1.将MongoDB注册到电脑中 安装好MongoDB之后,如何使用MongoDB呢?来到安装目录D:/MongoDB/bin会有如下列表: 其中,mongod.exe是服务端,mongo.exe是客 ...
- exc_bad_instruction(code=EXC_I386_INVOP,subcode=0x0) 错误
对象存储异常 对象存储要遵守NSCoding协议 #import "EmotionModel.h" @interface EmotionModel()<NSCoding> ...
- Jpa支持LocalDateTime类型持久化
package com.boldseas.porscheshop.common.config; import javax.persistence.AttributeConverter; import ...