第3章 C++程序设计初步(5)
8、循环结束后,循环变量的值是多少?
循环结束后,循环变量一般会超过终值(从小到大循环)除非遇到break跳出循环。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c84e6aef5c72f5875743e4c4ba1c2611.png)
for循环的变化
for(表达式1;表达式2;表达式3)
表达式1:只做一次。功能是用于变量的初始化。
表达式2:在进入循环前计算。功能用于判断循环是否执行。
表达式3:循环体执行结束后计算表达式3.功能循环变量的变化。
3个表达式每部分都可以省略。但是注意两个分号不能省略
如果省略表达式1:将计算表达式1的功能放到循环前。注意分号。
#include < iostream>
using namespace std;
int main()
{
int s=0,i;
i=1;
for(;i<=100;i++)
{
s=s+i;
}
cout<<“s=”<<s<<endl;
cout<<“i=”<<i<<endl;
return 0;
}
如果省略表达式2:可以把表达式2的功能放到循环内第1条语句。
#include < iostream>
using namespace std;
int main()
{
int s=0,i;
for(i=1;;i++)
{
if(i>100)
break;
s=s+i;
}
cout<<“s=”<<s<<endl;
cout<<“i=”<<i<<endl;
return 0;
}
可以将表达式3的功能放到循环体的最后。
如果省略表达式3:
#include < iostream>
using namespace std;
int main()
{
int s=0,i;
for(i=1;i<=100;)
{
s=s+i;
i++;
}
cout<<“s=”<<s<<endl;
cout<<“i=”<<i<<endl;
return 0;
}
可以同时省略2部分,甚至3部分。
#include < iostream>
using namespace std;
int main()
{
int s=0,i;
i=1;
for(;i<=100;)
{
s=s+i;
i++;
}
cout<<“s=”<<s<<endl;
cout<<“i=”<<i<<endl;
return 0;
}
#include < iostream>
using namespace std;
int main()
{
int s=0,i;
i=1;
for(; ; )//相当于while(1)
{
if(i>100)
break;
s=s+i;
i++;
}
cout<<“s=”<<s<<endl;
cout<<“i=”<<i<<endl;
return 0;
}
3种循环之间的互换(用三种方法求1到100的和)。
For:一般情况下用于事先知道要循环多少次。从哪里开始,到哪里结束。
While与do。。。while 不知道执行多少次。知道什么条件下执行循环。(求pai的值 求e的值)While先判断,后执行循环体。Do…while 至少执行一次循环体。
例题:
1、求一些正数的和与平均值。(个数未 知)
#include < iostream>
using namespace std;
int main()
{
double sum=0,average;
double num;
int count=0;//计数器
cout<<“计算一些正数的和与平均值:(输入负数结束)”<<endl;
while(1)
{
cin>>num;
if(num<0)
break;
else
{
sum+=num;
count++;
}
}
average=sum/count;
cout<<“这些数的总和是:”<<sum<<endl;
cout<<“这些数的平均值是:”<<average<<endl;
return 0;
}
#include < iostream>
using namespace std;
int main()
{
double sum=0,average;
double num;
int count=0;//计数器
cout<<“计算一些正数的和与平均值:(输入负数结束)”<<endl;
while(1)
{
cin>>num;
if(num<0)
break;
sum+=num;
count++;
}
average=sum/count;
cout<<“这些数的总和是:”<<sum<<endl;
cout<<“这些数的平均值是:”<<average<<endl;
return 0;
}
2、 输入一行字符,将所有的字母转换为大写字母(或者统计每种字符的个数)。
#include < iostream>
using namespace std;
int main()
{
char ch;
ch=getchar(); //cin>>ch;
while(ch!=’\n’)
{
if(ch>=‘a’ && ch<=‘z’)
ch-=’ ‘;
else if(ch>=‘A’ && ch<=‘Z’)
ch+=32;
cout<<ch;
ch=getchar();
}
cout<<endl;
return 0;
}
#include < iostream>
using namespace std;
int main()
{
char ch;
while((ch=getchar())!=’\n’) //ch=getchar()必须放到()中
{
if(ch>=‘a’ && ch<=‘z’)
ch-=’ ‘;
else if(ch>=‘A’ && ch<=‘Z’)
ch+=32;
cout<<ch;
}
cout<<endl;
return 0;
}
#include
using namespace std;
int main()
{
char ch;
//统计每种字符的出现次数
int upper=0,lower=0,digit=0,other=0;
while((ch=getchar())!=’\n’) //ch=getchar()必须放到()中
{
if(ch>=‘A’ && ch<=‘Z’)
upper++;
else if(ch>=‘a’ && ch<=‘z’)
lower++;
else if(ch>=‘0’ && ch<=‘9’)
digit++;
else
other++;
}
cout<<“大写字符的个数为:”<<upper<<endl;
cout<<“小写字符的个数为:”<<lower<<endl;
cout<<“数字字符的个数为:”<<digit<<endl;
cout<<“其他字符的个数为:”<<other<<endl;
return 0;
}
3、密码情书的书写
#include < iostream>
using namespace std;
int main()
{
char ch;
while((ch=getchar())!=’@’)
{
if(ch>=‘a’ && ch<=‘z’ ||ch>=‘A’ && ch<=‘Z’)
{
ch=ch+4;
}
cout<<ch;
}
return 0;
}
#include < iostream>
using namespace std;
int main()
{
char ch;
while((ch=getchar())!=’@’)
{
//wxyz —>abcd
if(ch>=‘a’ && ch<=‘z’)
{
ch=ch+4;
if(ch>‘z’)
ch-=26;
}
else if(ch>=‘A’ && ch<=‘Z’)
{
ch=ch+4;
if(ch>‘Z’)
ch-=26;
}
cout<<ch;
}
return 0;
}
#include < iostream>
using namespace std;
int main()
{
char ch;
int mima;
cout<<“你来写情书,我来给你加密:”<<endl;
cout<<“请输入这次使用什么密码?”<<endl;
cin>>mima;
fflush(stdin);
while((ch=getchar())!=’@’)
{
if(ch>=‘a’ && ch<=‘z’)
{
ch=ch+mima;
if(ch>‘z’)
ch-=26;
}
else if(ch>=‘A’ && ch<=‘Z’)
{
ch=ch+mima;
if(ch>‘Z’)
ch-=26;
}
cout<<ch;
}
return 0;
}
4、判断一个数是否为素数(只能被1和本身整除的数—)素数)判断num是否为素数。用2到num-1做为除数去求余。
#include < iostream>
using namespace std;
int main()
{
int num,i;
cout<<“请输入一个大于1的正整数,判断是否为素数:”;
cin>>num;
for(i=2;i<=num/2;i++)
{
//num对i求余, 1 能整除 2 不能整除
//能整除 不需要继续判断
//不能整除 需要继续用一下个数求余
if(num%i==0)
{
break;
}
}
//循环结束:是遇到了break跳出循环,还是循环变量超出界限
if(i>num/2)
{
cout<<num<<“是一个素数”<<endl;
}
else
{
cout<<num<<“不是一个素数”<<endl;
}
return 0;
}
5、找出某个范围内的素数
#include < iostream>
using namespace std;
int main()
{
int num,i;
while(1)
{
cout<<“请输入一个大于1的正整数,判断是否为素数:”;
cin>>num;
if(num<=1) break;
for(i=2; i<=num/2; i++)
{
if(num%i==0)
{
break;
}
}
if(i>num/2)
{
cout<<num<<“是一个素数”<<endl;
}
else
{
cout<<num<<“不是一个素数”<<endl;
}
}
return 0;
}
#include < iostream>
using namespace std;
int main()
{
int num,i;
for(num=100; num<=200; num++)
{
for(i=2; i<=num/2; i++)
{
if(num%i==0)
{
break;
}
}
if(i>num/2)
{
cout<<num<<“是一个素数”<<endl;
}
}
return 0;
}
#include < iostream>
using namespace std;
int main()
{
int num,i;
int count=0;//每行输出5个
for(num=100; num<=200; num++)
{
for(i=2; i<=num/2; i++)
{
if(num%i= =0)
{
break;
}
}
if(i>num/2)
{
cout<<num<<" ";
count++;
if(count%5==0) //此处的判断必须放在if内
cout<<endl;
}
}
return 0;
}
6、再次求一元二次方程的解。(如何写出完善的程序)
#include < iostream>
#include < cmath>
using namespace std;
int main()
{
double a,b,c,x1,x2;
cout<<“输入一元二次方程的三个系数a,b,c” <<endl;
cin>>a>>b>>c;
double delta;
delta=bb-4ac;
if(delta>0)
{
x1=(-b+sqrt(delta))/(2a);
x2=(-b-sqrt(delta))/(2a);
cout<<“方程在实数范围内有两个不相等的解:”<<endl;
cout<<x1<<endl<<x2<<endl;
}
else if(delta==0)
{
x1=(-b+sqrt(delta))/(2a);
cout<<“方程在实数范围内有两个相等的解:”<<endl;
cout<<x1<<endl;
}
else
{
cout<<“方程在实数范围内无解!!”<<endl;
}
return 0;
}
作业:输入两个正整数m和n,求其最大公约数。