7302人加入学习
(18人评价)
C++编程系列 第一季编程基础

制作于2018年2月7日

价格 免费

// 04-字符类型.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()

    cout << "字母大小写转换程序" << endl;
    cout << endl;
    cout << "请输入字母:";
    char a;
    char c;
    cin >> a;
    cout << endl;

    if (a>=65 && a<=90)
    {
        a = a + 32;
        cout <<"字母转换结果为:"<< a << endl;
    }
    else if (a >= 97 && a <= 122)
    {
        a = a - 32;
        cout << "字母转换结果为:"<< a << endl;
    }
    cout << endl;
    
    return 0;
}

[展开全文]

C++11的新遍历方式:

for(int 临时变量 : 数组)

{

}

 

#include <iostream>

using namespace std;

int main()

{

//do {

// // 循环体

//} while (判断);

 

int i = 100;

//do

//{

// cout << "创建敌人" << endl;

//} while (i>100);

// do while 循环体次数 >=1 的

 

//while (i>100)

//{

// cout << "创建敌人" << endl;

//}

// while 循环体次数 >=0 的

 

int scores[] = { 234,2,42,3,2,42,42,32 };

//for (int i = 0; i < 8; i++)

//{

// cout << scores[i] << endl;

//}

 

// C++11的标准

//for (int temp : scores)

//{

// //cout << temp << endl;

// temp = 10; // 笔记:不起作用

//}

//for (int temp : scores)

//{

// cout << temp << endl;

//}

 

 

for (int& temp : scores) // 注意:int& temp

{

//cout << temp << endl;

temp = 10;// temp = temp * 2; // 起到作用了,因为 int & temp,引用类型

}

for (int temp : scores)

{

cout << temp << endl;

}

 

return 0;

}

[展开全文]

char 定义字符类型

字符  单引号获取定义   c=‘2a’

 

[展开全文]
  1. switch只能判断两个值是否相等 
  2.  
[展开全文]

运算符可以直接引用

乘除的结果类型与输入的数有关

 

[展开全文]
  1. break 跳出循环不再执行
  2. continue 跳出当前循环,执行下一次循环  
[展开全文]

c语言字符串数组

char 数组【】=“   ”;

c++字符串

string 变量=“   ”;

[展开全文]

判断字符串数组

用 strcmp 判断数组 零相等, 非零不相等

[展开全文]

#denfine 别名 字符串(无等号,分号视情况而定)

类型别名

typedef 类型别名 别名

[展开全文]

#include <iostream>

using namespace std;

 

int main()

{

int scores[] = { 233,123,23,123,345 };

 

int scores2[4][5] = {

{123,12,321,2,2},

{123,2,13,12,31},

{123,12,3,343,4},

{234,345,4,543,3}

};

//cout << scores2[1][1] << endl; // 2

 

//for (int i = 0; i < 5; i++) // 5

//{

// for (int j = 0; j < 6; j++) // 6 5*6 30次

// {

// cout << "i:" << i << " j:" << j << endl;

// }

//}

 

//for (int i = 0; i < 4; i++)

//{

// for (int j = 0; j < 5; j++)

// {

// cout << scores2[i][j] << " ";

// }

// cout << endl;

//}

 

for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 4; j++)

{

cout << scores2[j][i] << " ";

}

cout << endl;

}

// 123 123 123 234

// 12 2 12 345

// 321 13 3 4

// 2 12 343 543

// 2 31 4 3

 

return 0;

}

[展开全文]
void LearnCppTest::getDateTime()
{
    long time;
    cout << "请输入秒:" ;
    cin >> time;
    if(!(long)time){
        cout << "请输入正确的格式:" << time << endl;
        return;
    }
    cout << "你输入的秒数为:" << time << endl;
    long d = time/(60*60*24);
    long h = (time%(60*60*24)) / (60*60);
    long m = ((time%(60*60*24)) % (60*60)) / 60;
    long s = ((time%(60*60*24)) % (60*60)) % 60;
    cout << d << "天" << h << "小时" << m << "分钟" << s<< "秒" << endl;
}
[展开全文]

练习题

1,下面的代码会打印什么内容

    int i;

    for(int i = 0; i < 5; i++)

        cout << i;

        cout << endl;

结果:01234

 

2,下面的代码会打印什么内容

    int j;

    for(j = 0; j < 11; j+=3)

        cout << j;

    cout << endl << j << endl;

结果:

0369

12

 

3,下面的代码会打印什么内容

    int j = 5;

    while(++j<9)

        cout << j++ << endl;

结果:

4,下面的代码会打印什么内容

    int k = 8;

    do

        cout << "k = " << k << endl;

    while(k++ < 5);

5,编写一个打印 1 2 4 8 16 32 64 的for循环

6,编写一个程序,让用户输入两个整数,输出这两个整数之间(包括这两个整数)所有整数的和。

    比如 2 5    里面整数有 2 4 = 6

7,编写一个程序,让用户可以持续输入数字,每次输入数字,报告当前所有输入的和。当用户输入0的时候,程序结束。

 

 

 

 

#include <iostream>
using namespace std;

int main()
{
    //for (int i = 0; i < 5; i++)
    //    cout << i;
    //cout << endl;// 结果:01234

    int i = 9;
    //++i;
    //--i;
    //cout << i << endl;
    //int result = (i++) + 1; // 结果:10 10
    //int result = ++i + 1; // 结果:11 10
    //cout << result << " " << i << endl;
    
    //{
    //    cout << i++ << endl; // 9
    //    cout << i << endl; // 10
    //}

    //{
    //    cout << ++i << endl; // 10
    //    cout << i << endl; // 10
    //}
    
    //{
    //    cout << i-- << endl; // 9
    //    cout << i << endl; // 8
    //}
    {
        cout << --i << endl; // 8
        cout << i << endl; // 8
    }
    return 0;
}

[展开全文]

3,下面的代码会打印什么内容

    int j = 5;

    while(++j<9)

        cout << j++ << endl;

结果:

6

8

  

 int j = 5;
    while (++j < 9) // j=j+1 ; j<9
        cout << j++ << endl; // cout<<j<<endl; j=j+1;
    // 6
    // 8

 

 

4,下面的代码会打印什么内容

    int k = 8;

    do

        cout << "k = " << k << endl;

    while(k++ < 5);

结果:

 k = 8

 

 

5,编写一个打印 1 2 4 8 16 32 64 的for循环

siki:

for(int i = 1; i <= 64; i*=2)

{

    cout<<i<<endl;

}

    //j=1;    res=1;
    //j=1*2;  res=2;
    //j=2*2;  res=4
    //j=4*2;  res=8;
    //j=8*2;  res=16;
    //j=16*2; res=32;
    //j=32*2; res=64

 

 

自写:

#include <iostream>
#include <cmath>
using namespace std;

int main()

{

    for (int a = 0; a < 7; a++)
    {
        cout << pow(2, a) << " ";
    }
    //a=0,res=1
    //a=1,res=2
    //a=2,res=4
    //a=3,res=8
    //a=4,res=16
    //a=5,res=32
    //a=6,res=64

    return 0;

}

 

 

 

 

6,编写一个程序,让用户输入两个整数,输出这两个整数之间(包括这两个整数)所有整数的和。

    比如 2 5    里面整数有 2 4 = 6

 

 

    cout << "请输入第一个数字:";
    int num1;
    cin >> num1;
    cout << "请输入第二个数字:";
    int num2;
    cin >> num2;

    int total = 0;

    for (int i = num1; i <= num2; i++)
    {
        total += i;
    }
    cout << total << endl; // 14
    // 2 - 5    2 3 4 5

[展开全文]

auto关键字和练习题

自动推断类型,一般我们定义一个变量的时候是要指定它的类型,通过auto关键字可以不用指定它的类型

auto a = ' a ' ;

cout << a << endl ;

auto b = 100 ;

cout << b << endl ;

变量等于 类型 名字 = 初始值

 

[展开全文]

7,编写一个程序,让用户可以持续输入数字,每次输入数字,报告当前所有输入的和。当用户输入0的时候,程序结束。

siki:

    float total = 0;
    while (true)
    {
        cout << "请输入一个数字:";
        float number;
        cin >> number;

        if (number == 0)
        {
            break;
        }

        total += number;
        cout << "当前所有输入的和为:" << total << endl;
    }

 

自写:

    bool execute = true;
    float totalNum = 0;
    while (execute)
    {
        float temp;
        cout << "请输入一个数字:";
        cin >> temp;
        if (temp == 0)
        {
            execute = false;
        }
        totalNum += temp;
        cout << "当前和为:" << totalNum << endl;
    }

[展开全文]

int hp=0;

// 单独if

if(hp<=0)

{

//cout<<:"true"<<endl;

cout<<"游戏结束"<<endl;

}

 

if(hp<=0)

cout<<"游戏结束:<<endl;

 

if(hp<=0)

{

cout<<"游戏结束"<<endl;

}

else

{

cout<<"游戏继续"<<endl;

}

 

 

// if 嵌套

int age = 17;

if(age<18)

{

cout<<"你可以玩3个小时"<<endl;

}

else

{

//cout<<"你可以玩10个小时"<<endl;

if(age<50)

{

cout<<"你可以玩10个小时"<<endl;

}

else

{

cout<<"你可以玩2个小时"<<endl;

}

}

 

//if else if

if(age<18)

{

cout<<"你可以玩3个小时"<<endl;

}

else if(age<50)

{

cout<<"你可以玩10个小时"<<endl;

}

else if(age<80)

{

cout<<"你可以玩2个小时"<<endl;

}

else

{

cout<<"你不能玩这个游戏"<<endl;

}

[展开全文]

\0表示字符串结束

char webset[]="jbgysafuay"

[展开全文]

用了另外一个方法

        int total=0;
        int a;
        do
        {
            cout << "请输出一个数字:";
            cin >> a;
            total += a;
            cout << "当前数字总和为:" << total << endl;
        } while (a != 0);

 

[展开全文]

int see[20]={12,545,66,6};

声明和初始化是一起的

 

[展开全文]

授课教师

SiKi学院老师

课程特色

下载资料(1)
视频(58)