#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED

struct Course
{
int id;
std::string name;
};

const int LEN = 7;
void print_help();
void print_all_course(Course* courses[LEN]);
void print_course_count(Course* courses[LEN]);
void print_maxlength(Course* courses[LEN]);
bool remove_last_course(Course* courses[LEN]);

#endif // MAIN_H_INCLUDED

#include <iostream>
#include "main.h"
using namespace std;

int main()
{
Course* courses[LEN] =
{
new Course{1,"Linux"},
new Course{2,"C++"},
new Course{3,"HTML"},
new Course{4,"NodeJS"},
new Course{5,"Shell"},
new Course{6,"Python"},
new Course{7,"C#"}
};

while(true)
{
char input = std::cin.get();
switch(input)
{
case '0':
print_help();
break;
case '1':
print_all_course(courses);
break;

case '2':
print_course_count(courses);
break;

case '3':
print_maxlength(courses);
break;

case '4':
{
bool ret = remove_last_course(courses);
if(ret)
print_all_course(courses);
else
cout << "there is no course any more !";
break;
}
case '5':
goto exit;
break;
}
}
exit:
cout << "bye !";
return 0;
}

void print_help()
{
cout << "操作命令输入0,打印出程序帮助信息,即每个操作命令的作用" << endl;
cout << "操作命令输入1,打印出程序中存储的所有课程ID和课程名" << endl;
cout << "输入2,打印出课程数量" << endl;
cout << "输入3,打印出名字最长的课程信息,如果有多个相同长度的课程名请都打印出" << endl;
cout << "输入4,删除最后一个课程,如果没有课程需要返回错误信息" << endl;
cout << "输入5,退出程序" << endl;
}

void print_all_course(Course* courses[LEN])
{
for(int i = 0; i < LEN; i++)
{
if(courses[i] != NULL)
cout << "id : " << (*courses[i]).id << "; name : " << (*courses[i]).name << endl;
}
}

void print_course_count(Course* courses[LEN])
{
int num;
for(num = 0; num < LEN; num++)
{
if(courses[num] == NULL)
break;
}
cout << "the count of course is : " << num << endl;
}

void print_maxlength(Course* courses[LEN])
{

int maxLen = 0;
for(int i = 0; i < LEN; i++)
{
if((*courses[i]).name.length() > maxLen)
maxLen = (*courses[i]).name.length();
}

cout << "the longest name of the courses : " << endl;

for(int i = 0; i < LEN; i++)
{
if((*courses[i]).name.length() == maxLen)
cout << (*courses[i]).name << endl;
}
}

bool remove_last_course(Course* courses[LEN])
{
for(int i = LEN - 1 ; i >= 0 ; i--)
{
if(courses[i] != NULL)
{
delete(courses[i]);
courses[i] = NULL;
return true;
}
}
return false;
}

c++ chap1 to chap 3的更多相关文章

  1. CHAP算法C++实现

    CHAP是一种挑战响应式协议. CHAP全称为:Challenge Handshake Authentication Protocol. CHAP密码 = 一个字节的标识符 + MD5(一个字节的标识 ...

  2. 详解eNSP下的PPP之MP、PAP/CHAP认证实验配置

    一.PPP MP实验(用虚拟模板配置) 1.拓扑图

  3. CHAP认证原理

    整个过程就是PPP协商过程,分三步:LCP.认证.NCP. 一 协议概述 PPP包含以下两个层次的协议: ·链路控制协议(LCP):负责建立.配置和测试数据链路连接 ·网络控制协议(NCP):负责建立 ...

  4. pap与chap协议

    1.pap:直接在网络上发送密码明文 2.chap: 网络上发送的是密码的密文;server给client发一段随机数(challenge),client利用随机数对密码进行加密,将用户名和加密后的密 ...

  5. PPP中的PAP和CHAP的区别

    PAP PAP是简单认证,明文传送,客户端直接发送包含用户名/口令的认证请求,服务器端处理并回应. CHAP CHAP是加密认证,先由服务器端给客户端发送一个随机码 challenge,客户端根据 c ...

  6. 神州数码广域网PPP封装CHAP认证配置

    实验要求:掌握PPP封装协议下的CHAP认证 拓扑如下 R1 enable 进入特权模式 config 进入全局模式 hostname R1 修改名称 interface s0/1 进入端口 ip a ...

  7. Chap1:全景图[Computer Science Illuminated]

    参考书目:Dale N . 计算机科学概论(原书第5版)[M]. 机械工业出版社, 2016 from library Chap1:全景图 1.1计算系统 1.2计算的历史 1.3计算工具与计算学科 ...

  8. CHAP认证(双向)

    实验要求:掌握CHAP认证配置 拓扑如下: R1enable 进入特权模式configure terminal    进入全局模式hostname R1 设置主机名 interface s0/0/0 ...

  9. PPP或PPPOE身份验证PAP和CHAP

    PPP或PPPOE都支持身份验证,有两种验证方式:PAP和CHAP. PAP,Passwd Authentication Protocol,密码验证协议,以客户端明文方式传递用户名和密码,服务器和本省 ...

随机推荐

  1. Hcm data loader for cancel work relationship

    好好总结一下这个task.没有做好的东西:1.现有的资料和各种各样的工具没有很好的利用起来,造成了用了很多的时间去了解和自己学习.非常的不舒服的.下次要避免这样的浪费时间,学会在工作中学习.现在以及以 ...

  2. 高级php面试题

    在网上看到一些高级php 的面试题目.. 最近接连面试了几家公司,有些重要问题记录一下,督促自己学习提高,同时希望给朋友们一些帮助.内容很多,一点点完善,一步步学习..有些是面试被问,有些是招聘要求, ...

  3. VC++ 如何让ScrollView视图显示滚动条

    CSize sizeTotal; sizeTotal.cx = ;//值设大点 sizeTotal.cy = ;//值设大点 SetScrollSizes(MM_TEXT, sizeTotal); 显 ...

  4. 更新lispbox中的ccl和slime版本

    首先C-x C-f然后输入~,找到.emacs文件,根据slime官方文档说明的添加如下代码到文件末尾,重启一下emacs,slime就编译好了,然后这段代码就可以删除.否则每次启动emacs就算不用 ...

  5. const 限定符

    1.定义const对象 const限定符把一个对象转换成一个常量 const int Bufsize = 512; 定义Bufsize 为常量并初始化为512.变量Bufsize仍然是一个左值,但是不 ...

  6. 开源GIS软件初探

    谈到GIS软件,首先让我们想到的便是GIS界的龙头大哥ESRI公司旗下的ArcGIS产品,从最初接触的version 9.2到如今的version 10.1,其发展可谓风生水起.MapInfo软件也不 ...

  7. [转载] python 计算字符串长度

    本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...

  8. [转载] 纯手打 第一篇:安装配置gradle

    本文转载自: http://www.cnblogs.com/uncle2000/p/4276833.html 一个bug 一个脚印的叫你们用gradle. 1介于网络上的很多资料都是老的 不适用与现在 ...

  9. PowerDesigner 16.5 反向PostgreSQL9.01 中 Unable to list the columns. SQLSTATE = 22003不良的类型值 short : t 解决方法

    Database➙Edit Current DBMS… General tab➙PostgreSQL 9.x➙Script➙Objects➙Column➙SqlListQuery or Tools➙R ...

  10. jQuery Mobile 图标

    jQuery 图标 如需在 jQuery Mobile 中向按钮添加图标,请使用 data-icon 属性: <a href="#anylink" data-role=&qu ...