C++编程实例-while循环结构
2016-01-26 00:00
483 查看
实验2 while循环结构
【实验目的】
通过本实验,掌握循环结构程序设计的编程方法,掌握循环方面的编程技巧。
【实验要求】
⑴学会使用while语句;
⑵掌握循环结构程序设计方法;
【实验内容】
以下程序任选两个
从键盘上输入一组数(以输入0为结束),求累加和,并找出最大/小值。
#include<iostream>
using namespace std;
int main(){
int i=1,sum=0,x,max,min;
float r;
cout<<"Input No."<<i<<" number: ";
cin>>x;
max=x; min=x;
while(x!=0){
sum+=x;
i++;
if(max<x) max=x;
if(min>x) min=x;
cout<<"Input No."<<i<<" number: ";
cin>>x;
}
i--; r=(float)sum/i;
cout<<"Average="<<r<<endl<<"Max="<<max<<"/nMin="<<min<<endl;
return 0;
}
判断素数
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Please Input a number: "; cin>>x;
int f=2;
while(x%f!=0)
f++;
if(x==f)
cout<<x<<" is prime number./n"<<endl;
else
cout<<x<<" is not prime number./n"<<endl;
return 0;
}
判断从键盘上输入的一串字符中小写字母、数字字符及其它字符的数量。
#include<iostream>
using namespace std;
int main(){
char ch;
int n1=0,n2=0,n3=0;
cout<<"Input some characters: "; cin>>ch;
while(ch != '?'){
if((ch>='a')&&(ch<='z')) n1++;
else if ((ch>='0')&&(ch<='9')) n2++;
else n3++;
cin>>ch;
}
cout<<"/nn1="<<n1<<"/nn2="<<n2<<"/nn3="<<n3;
return 0;
}
【实验目的】
通过本实验,掌握循环结构程序设计的编程方法,掌握循环方面的编程技巧。
【实验要求】
⑴学会使用while语句;
⑵掌握循环结构程序设计方法;
【实验内容】
以下程序任选两个
从键盘上输入一组数(以输入0为结束),求累加和,并找出最大/小值。
#include<iostream>
using namespace std;
int main(){
int i=1,sum=0,x,max,min;
float r;
cout<<"Input No."<<i<<" number: ";
cin>>x;
max=x; min=x;
while(x!=0){
sum+=x;
i++;
if(max<x) max=x;
if(min>x) min=x;
cout<<"Input No."<<i<<" number: ";
cin>>x;
}
i--; r=(float)sum/i;
cout<<"Average="<<r<<endl<<"Max="<<max<<"/nMin="<<min<<endl;
return 0;
}
判断素数
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Please Input a number: "; cin>>x;
int f=2;
while(x%f!=0)
f++;
if(x==f)
cout<<x<<" is prime number./n"<<endl;
else
cout<<x<<" is not prime number./n"<<endl;
return 0;
}
判断从键盘上输入的一串字符中小写字母、数字字符及其它字符的数量。
#include<iostream>
using namespace std;
int main(){
char ch;
int n1=0,n2=0,n3=0;
cout<<"Input some characters: "; cin>>ch;
while(ch != '?'){
if((ch>='a')&&(ch<='z')) n1++;
else if ((ch>='0')&&(ch<='9')) n2++;
else n3++;
cin>>ch;
}
cout<<"/nn1="<<n1<<"/nn2="<<n2<<"/nn3="<<n3;
return 0;
}
相关文章推荐
- C++编程实例-友元
- C++编程实例-分支结构
- C++编程实例-I/O流
- C++ 对象与指针
- C++函数重载
- C++编程实例-函数
- C++编程实例-多重循环结构
- C++ 面向过程与面向对象的区别
- C++中const与static在面向过程代码中的异同
- C++标准编程:虚函数与内联
- C++ 实例导航
- C++动态创建二维数组
- 2012年第三届蓝桥杯C/C++程序设计本科B组省赛 夺冠概率(编程大题)
- C++:七种qsort排序方法
- C++编程实例-多态(一)
- C++构造函数的幕后工作
- C++直接操纵虚函数表方法
- C/C++ qsort()快速排序的用法
- C++动态建立矩阵及矩阵相乘
- C++编程实例-运算符重载