C++编程实例-while循环结构
2009-12-16 17:39
423 查看
实验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++编程实例-while循环结构
- C++编程实例-多重循环结构
- C++编程实例-多重循环结构
- 黑马程序员_java编程基础第3天1-5 循环结构while;do while;for
- 黑马程序员_java编程基础第3天1-5 循环结构while;do while;for
- 解决C++中误输入导致while结构死循环的问题
- c语言—循环结构编程实例
- 编程作业50页3题 分别使用 do-while和 for 循环计算1+1/2!+1/3!+1/4!...+1/20!
- C++编程实例-继承(一)
- LinuxC/C++编程基础(22) 使用thrift/rpc开发简单实例
- Java_20_循环结构_While循环_Do-while循环(执行顺序:初始化、布尔表达式条件判断、循环体、迭代)
- 对Linux下shell编程之for循环的实例讲解
- C++编程实例-类与指针
- SQL中的 if 结构和循环(while)结构
- .NET入门(九)测试各种循环结构,for,while和do while
- [c++作业报告]第4次试:循环结构
- C++作业4——循环结构程序设计
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- 套接字地址结构笔记----C++网络编程
- C# 循环结构while,do-while和for的区别以及跳转语句