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: 开发工具 ...
随机推荐
- 设置 DNS 服务器转发试验
一.主节点的配置 1.yum install bind -y 安装 DNS 服务 2.vim /etc/named.conf 编辑 DNS 的配置文件 3. vim /etc/named.rfc191 ...
- gcc悄无声色将静态函数内联了
说到内联,可能你还停在十几年前甚至二十多年前的C++教典,c++有内联关键字inline,甚至还用来与c做区分.c99开始c引入inline,gcc比c99早实现对inline支持,vc中c没有关键字 ...
- Vue学习之Vue模拟后台数据
前后端项目的开发,需要前端和后端同时进行开发,有时后端开发人员还未完成API接口可以提供给前端调用,因此就需要模拟前端请求后端接口,后端返回数据.一般开发中都是返回json数据格式来完成前后端的交互. ...
- 移动端vue项目的图片上传插件
有一移动端项目,使用的vant-ui.可是vant自带的Uploader似乎不支持一次选择多张图片上传的功能. 于是乎:在https://www.npmjs.com/查找发现找到 vue-upload ...
- 防火墙和SELinux复习02
1.防火墙 防火墙主要起隔离作用,严格的过滤入站,允许出站.又分为硬件防火墙和软件防火墙,硬件防火墙主要保护一群机器,而软件防火墙主要保护本机. 防火墙相关命令:systemctl status fi ...
- kafka-manager新手安装入门指南
Kafka-manager安装教程 使用环境 ubuntu18.04 Java 8 一.下载kafka 官网下载地址如下 https://www.apache.org/dyn/closer.cgi?p ...
- scss--函数 (Functions)--unit
(Sass::Script::Value::String) unit($number) Returns the unit(s) associated with a number. Complex un ...
- JavaScript基础目录
一.JavaScript简介 1.JavaScript用途 2.JavaScript的诞生 3.JavaScript从丑小鸭到金凤凰 4.JavaScript非常好学 5.学习方法 二.Hello W ...
- java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type
今天做项目测试接口,查询数据时出现以下错误,记录一下. 查询语句和错误信息: 实体类属性 原因是 由于字段 total和receive 在实体类中使用的是 int类型,但是数据库中查询出来的数据为nu ...
- KFC-位置分页爬虫
import requests import json if name == 'main': #记录总数 raw_count=0 #查询页数 page=1 #存储数据 data=[] #判断有无数据条 ...