0%

C++之循环

C++之循环……,包括for循环与while循环、break与continue、读取数字的循环。

For循环与二维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
using namespace std;

const int Cities=5;
const int Years=4;

//for循环与二维数组
int main(void)
{
//指针数组
const char * cities[Cities]=
{
"Changsha",
"Xiamenll",
"Kaifeng",
"Shenzheng",
"Shanghai"
};
//二维数组
int maxtemps[Years][Cities]=
{
{96,100,87,101,105},
{96,98,91,107,104},
{97,101,93,108,107},
{98,103,95,109,108}
};

//二维数组的遍历访问
for(int city=0;city<Cities;city++)
{
cout<<cities[city]<<":\t";
for(int year=0;year<Years;year++)
{
cout<<maxtemps[year][city]<<"\t";
}
cout<<endl;
}

return 0;
}

结果如下:

image-20221203174550427

  • 练习1:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include<iostream>
    using namespace std;

    int main(void)
    {
    int row;
    cout<<"Please enter the number of rows:";
    cin>>row;

    for(int i=0;i<row;i++)
    {
    for(int j=0;j<row-i-1;j++)
    cout<<".";
    for(int j=0;j<=i;j++)
    cout<<"*";
    cout<<endl;
    }
    return 0;
    }

    结果如下:

    image-20221203185125019

    image-20221203185159015


While循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<ctime>
using namespace std;

//利用while循环实现延时控制
int main(void)
{
cout<<"Enter the delay time,in seconds:";
float secs;
cin>>secs;

clock_t delay=secs*CLOCKS_PER_SEC;//CLOCKS_PER_SEC表示每秒对应的系统时间

cout<<"starting\n";
clock_t start=clock();//clock()返回程序开始后执行的系统时间

while(clock()-start<delay);
cout<<"done\n";
return 0;
}

Break与Continue

  • break是退出当前循环
  • Continue是回到循环开始处继续执行
    • 在for循环中,continue语句使程序直接跳到更新表达式处,即下面程序中的i++处
    • 对于while循环,continue语句直接跳转到判断表达式处,即下面程序中i<name.size()处
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<string>
using namespace std;

int main(void)
{
string name="ssyttmsl";
for(int i=0;i<name.size();i++)
{
if(name[i]!='m')
cout<<name[i]<<" ";
else
continue;
}
cout<<endl;
for(int i=0;i<name.size();i++)
{
if(name[i]!='m')
cout<<name[i]<<" ";
else
break;
}
cout<<endl;
int i=0;
while(i<name.size())
{
if(name[i]!='m')
cout<<name[i]<<" ";
else
continue;
i++;
}
cout<<endl;
return 0;
}

结果如下:

image-20221207172547747

读取数字的循环

  • 若用户输入的类型与定义的类型不匹配,那么cin对象中的一个错误标记将被设置,对cin方法的调用将返回false

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include<iostream>
    using namespace std;

    int main(void)
    {
    int data;
    while(cin>>data)
    {
    cout<<"right!"<<endl;
    }
    cout<<"wrong!"<<endl;
    return 0;
    }

    结果如下:

    image-20221207173342647

  • 当程序发现用户输入错误内容想跳过重新输入时,应采取3个步骤

    • cin.clear()重置cin的错误标记位
    • while(cin.get()!='\n');删除错误输入
    • 提示用户再输入
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    #include<iostream>
    using namespace std;

    const int Max=5;
    int main(void)
    {
    int golf[Max];
    cout<<"Please enter your golf scores."<<endl;
    cout<<"You must enter "<<Max<<" rounds"<<endl;

    for(int i=0;i<Max;i++)
    {
    cout<<"Round #"<<i+1<<": ";
    while(!(cin>>golf[i]))
    {
    cin.clear();
    while (cin.get()!='\n');
    cout<<"Please enter a number:";
    }
    }
    double total=0.0;
    for(int i=0;i<Max;i++)
    {
    total+=golf[i];
    }
    cout<<"Average score:"<<total/Max<<endl;

    return 0;
    }

    结果如下:

    image-20221207174811462

欢迎来到ssy的世界