第3课知识点

  1. 热身

1.1. 范围判断
读入一个整数,若这个数大于1并且小于100,则输出yes。

#include <iostream>
using namespace std ;
int main() {
    int a ;
    cin >> a;
    if( 1<a &&a <100) cout<<"yes" ;//&&表示并且
    return 0 ;
}

1.2. 两个变量交互值
输入两个正整数a和b,试交换a、b的值(使a的值等于b,b的值等于a)。

#include <iostream>
using namespace std ;
int main() {
    int a , b ;
    cin >> a >> b ;
    a = a + b ;
    b = a - b ;
    a = a - b ;
    cout << a << " " << b ;
    return 0 ;
}

1.3. 小数的除法
两个整数a和b分别作为分子和分母,既分数ab,求它的浮点数值(双精度浮点数,保留小数点后9位)。

#include <iostream>
#include <iomanip>
using namespace std ;
int main(){
    double a , b ;
    double result ;
    cin >> a >> b ;
    result =  a / b ;
    cout << fixed << setprecision(9) << result ;
    return 0 ;
}
  1. 控制台显示任意图形
    2.1. 原理
    2.2. 可用的转字符画的工具
    2.2.1. ASCgen2.0.0
    2.2.2. 根据文字生成字符画:
    http://patorjk.com/software/taag
    http://www.network-science.de/ascii/
    2.2.3. 根据图片生成字符画:
    http://www.degraeve.com/img2txt.php
    http://life.chacuo.net/convertphoto2char
    2.2.4. 根据流程图生成字符画:
    http://asciiflow.com/
    参考文献:
    资源:在线生成ascii字符画网站
    2.3. string的多行定义
string image = 
"aaa\n"
"bbb\n"
"ccc\n" ;
  1. if选择结构

3.1. 只有一种选择

if(1)
{
}

3.2. 有两种选择

if(1)
{
}
else
{
}

3.3. 有多种选择

if(1)
{
}
else if(1)
{
}
else
{
}
  1. 比较运算
    4.1. a>b 表示判断a是否大于b,是的话,运算结果返回1/真,不是的话,运算结果返回0/假。
    4.2. a<b 表示判断a是否小于b,是的话,运算结果返回1/真,不是的话,运算结果返回0/假。
    4.3. a==b 表示判断a是否等于b
    4.4. a<=b 表示判断a是否小于等于b
    4.5. a>=b 表示判断a是否大于等于b

  2. 游戏登录

#include <iostream>
using namespace std ;
int main() {
    string username  = "liao" ;//提前设置正确的用户名
    string password  = "hao" ;//提前设置正确的密码
    string username1 ;
    string password1 ;
    cout << "请输入用户名:"  ;
    cin >> username1 ;
    cout << "请输入密码:"  ;
    cin >> password1 ;
    if( username1 ==  username &&  password1 == password  ) {
        cout << "恭喜登录成功!"  << endl ;
    } else {
        cout << "登录失败。"  << endl ;
    }
    return 100 ;
}
  1. 计算器v0.2
#include <iostream>
#include <iomanip>
using namespace std ;
int main() {
    double num1  ;
    double num2  ;
    string symbol ;
    double result ;
    cin >>  num1 ;
    cin >> symbol ;
    cin >>  num2 ;
    if( "+" == symbol ) {
        result = num1 + num2 ;
    }

    if( "-" == symbol ) {
        result = num1 - num2 ;
    }

    if( "*" == symbol ) {
        result = num1 * num2 ;
    }

    if( "/" == symbol ) {
        if ( 0 == num2) {
            cout << "除数不能为零!" ;
            result = 0 ;
        } else {
            result = num1 / num2 ;
        }
    }
    cout << fixed << setprecision(6)<< result ;
    return 100 ;
}
  1. 三部曲合成一部曲:"创建变量aaa"。
  2. 小游戏
    8.1. MP是魔法值或魔力值,HP是生命值。在现代电子游戏(特别是角色扮演游戏)中,玩家把扮演的角色的生命值叫做红(即HP health point),把魔法值叫做蓝(即MP mana point /magic point,前者翻译更准确为“魔法值”,后者更趋近于“魔法力”的意思)。一般使用蓝色条表示内力值、法力值、魔法值、真气值等,用于使用技能(法术、魔法、武功、道术等)。简称“蓝”,与“红”同属游戏两大基本属性。
    8.2. 课堂练习
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std ;
int main() {
    int enemy_HP = 100 ;//敌人生命值
    int hero_HP = 100 ;//敌人生命值
    int hero_attack = 2 ;//英雄攻击力
    string hero = "里奥老师" ;//英雄名称
    cout << "┏—————————————————————┓" << endl;
    cout << "┆ 敌人血量:" << enemy_HP
         << "     VS    "
         << hero << "血量:" << hero_HP 
         << "┆" << endl ; 
    cout << "┗—————————————————————┛" << endl;

    int skill_key  ;//技能按键
    string skill_1_desc ;//1技能描述
    skill_1_desc = "急踏两步,一招「上步撩刀」,由下而上撩向敌人小腹。" ;
    cout << "你释放几(1-4)技能?:" ;
    cin >> skill_key ;
    if ( 1 == skill_key  ) {//释放1技能
        cout << hero << skill_1_desc ;
    }
    enemy_HP = enemy_HP - hero_attack ;
    Sleep(1500) ;//1500毫秒(1.5秒)
    system("cls" ) ;

    cout << "┏—————————————————————┓" << endl;
    cout << "┆ 敌人血量:" << enemy_HP
         << "     VS    "
         << hero << "血量:" << hero_HP 
         << "┆" << endl ; 
    cout << "┗—————————————————————┛" << endl;
    cout << "你释放几(1-4)技能?:" ;
}
  1. 随机数
    9.1. 随机就是不确定,计算机生成的随机数是伪随机数
    9.2. c++生成随机数的两个步骤:
    9.2.1. 给定一个种子
    9.2.2. 用给定的种子生成随机数
    9.3. 课堂练习
#include <iostream>
#include <cstdlib>
using namespace std ;
int main() {
    int aaa ;
    srand(111) ;//设置种子为111
    aaa = rand() ;
    cout <<"生成一个随机数:"<< aaa <<endl ;
    srand(222) ;//设置种子为222
    aaa = rand() ;
    cout <<"再生成一个随机数:"<< aaa <<endl ;
    srand(333) ;//设置种子为333
    aaa = rand() ;
    cout <<"又生成一个随机数:"<< aaa <<endl ;
}

9.4. 用时间做种子,生成随机数

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std ;
int main() {
    int num001 ;

    int seed1  ;
    seed1 =  time(0) ;//获取当前时间
    srand(seed1) ;//用当前时间做种子
    num001 = rand() ;
    cout << "生成一个随机数:"<< num001 << endl ;

    int seed2 ;
    seed2 =  seed1 + 1  ;
    srand(seed2) ;//改变种子
    num001 = rand() ;
    cout << "再生成一个随机数:"<< num001 << endl ;

    int seed3 ;
    seed3 =  seed2 + num001  ;
    srand(seed3) ;//改变种子
    num001 = rand() ;
    cout << "又生成一个随机数:"<< num001 << endl ;

}
  1. 根据UNIX元年0时区时间/格林尼治时间(1970年1月1日0点0分0秒)计算当前时间
    10.1. 粗略的估算,计算的时分秒不准确
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std ;
/*对照表 
时间          秒
1分钟         60
1小时         3600
1天              86400
1周              604800
1月(30.44天)  2629743
1年(365.24天) 31556736
*/
int main() {
    
    long total_seconds  ;//总秒数 
    int left ;//中转秒数变量 
    total_seconds = time(0) ;
    
    int year = total_seconds / 31556736 ; 
    cout << "过了"<< year <<"年"<< endl ;
    left = total_seconds % 31556736 ;
    
    int month = left / 2629743 ;
    cout << "过了"<< month <<"月"<< endl ;
    left = left % 2629743 ;
    
    int day = left / 86400 ;
    cout << "过了"<< day <<"天"<< endl ;
    left = left % 86400 ;
    
    int hour = left / 3600 ;
    cout << "过了"<< hour <<"小时"<< endl ;
    left = left % 3600 ;
    
    int minute = left / 60 ;
    cout << "过了"<< minute <<"分钟"<< endl ;
    left = left % 60 ;
    
    int second = left ;
    cout << "过了"<< second <<"秒"<< endl ;
}

10.2. 较精确的计算
10.2.1. 判断闰年

#include <iostream>
using namespace std;

int main() {
    int year;
    cout << "输入年份: ";
    cin >> year;
    if (year % 4 == 0) {
        if (year % 100 == 0) {
            // // 这里如果被 400 整除是闰年
            if (year % 400 == 0)
                cout << year << " 是闰年";
            else
                cout << year << " 不是闰年";
        } else
            cout << year << " 是闰年";
    } else
        cout << year << " 不是闰年";
    return 0;
}

10.2.2. 考虑闰年
从1970年到2022年所有的闰年,13个闰年。
1972
1976
1980
1984
1988
1992
1996
2000
2004
2008
2012
2016
2020
10.2.3. 精确的计算
子程序time(0)返回是格林尼治时间,和北京时间有8个小时的时差。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    cout << "普通的年有"<<7*31 + 4*30 + 28<< "天"<< endl ; 
    cout << "闰年的年有"<<7*31 + 4*30 + 29<< "天"<< endl ; 
    cout << "从2022年1月1日0点0分0秒到1970年1月1日0点0分0秒恰好经过了:"<<2022-1970<<"年"<< endl ; 
    int leap_year_count = 13 ;
    cout << "这期间闰年有"<< leap_year_count <<"个" << endl ;
    int common_year_count = 52 - 13 ;
    cout << "这期间平年有"<< common_year_count <<"个" << endl ;     
    long leap_year_second = 1*60*60*24*366 ;
    cout << "一个闰年有" << leap_year_second << "秒"<< endl ;  
    long common_year_second = 1*60*60*24*365 ;
    cout << "一个平年有" << common_year_second << "秒"<< endl ;    
    long  second1 =   leap_year_second * 13 + common_year_second * 39 ;
    cout << "从2022年1月1日0点0分0秒到1970年1月1日0点0分0秒恰好经过了:"<<second1<<"秒"<< endl ; 
    long second2 = time(0) ;
    cout << "从现在到1970年1月1日0点0分0秒恰好经过了:"<<second2<<"秒"<< endl ;
    cout << "----- ----- ----- ----- ----- ----- ----- ----- -----" << endl ;
    cout << "问题简化为:" << endl ; 
    int second3 = second2 - second1 ;
    cout << "已知从现在到2022年1月1日0点0分0秒恰好经过了"<<second3<<"秒"<< endl ;
    cout << "问你现在是?月?日?时?分?秒"<< endl ; 
    int day = second3 / 86400 ;//1天86400秒 
    cout << "过了" <<  day  << "天" << endl ; 
    second3 = second3 % 86400 ;
    cout << "扣除" <<  day  << "天,还剩" << second3<<"秒"<< endl ;
    int hour  = second3 / 3600 ;//1个小时3600秒 
    cout << "过了" <<  hour  << "小时" << endl ; 
    second3 = second3 % 3600 ;
    cout << "扣除" <<  hour  << "小时,还剩" << second3<<"秒"<< endl ;
    int minute  = second3 / 60 ;//1分钟60秒 
    cout << "过了" <<  minute  << "分钟" << endl ; 
    second3 = second3 % 60 ;
    cout << "扣除" <<  minute  << "分钟,还剩" << second3<<"秒"<< endl ;
    cout << "所以当前时间(格林尼治标准):是2022年"<< day+1 << "日"<< hour << "时" << minute << "分" << second3 << "秒"<< endl ;
    cout << "刚好再加大约8个小时就是北京时间:是2022年" << day+1 << "日"<< hour+8 << "时" << minute << "分" << second3 << "秒"<< endl ;
    cout << "----- ----- ----- ----- ----- ----- ----- ----- -----" << endl ;
    return 0;
}
  1. 思考与作业
    11.1. 利用随机数,完善课堂小游戏里中的攻击伤害,并尽可能的完善这个小游戏。
    11.2. 利用time(0),计算当前的系统时间。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录: 1.应用程序五种状态2.OC的反射机制3.UITableViewCell的卡顿你是怎么优化的?4.说一下H...
    鼬殿阅读 810评论 0 1
  • 技术点: 不定期更新补充 页面引用svg symbol标签创建icon p:nth-child(2) 与 p:nt...
    wwmin_阅读 1,524评论 0 52
  • 1/75 1认识Python语言 2/75 序言 培训最终的目标是什么? 衡量一个合格的软件工程师的标准是什么? ...
    清清子衿木子水心阅读 4,139评论 0 1
  • Buid-in web server内置了一个简单的Web服务器 把当前目录作为Root Document只需要这...
    Success85阅读 960评论 1 1
  • 可否使用 == 来判断两个NSString类型的字符串是否相同?为什么? 不能。==判断的是两个变量的值的内存地址...
    渐z阅读 621评论 0 0