C++第二章作业
1.(1)if...else 用法
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char X;
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
while(cin>>X){
if(X=='A')
cout<<"Data has been added"<<endl;
else if(X=='D')
cout<<"Data has been deleted"<<endl;
else if(X=='S')
cout<<"Data has been sorted"<<endl;
else if(X=='Q')
break;
else
cout<<"Data does not exist"<<endl;
}
return 0;
}
(2)switch 用法
#include <iostream>
using namespace std;
int main()
{
char X;
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
while(cin>>X){
if(X=='Q')
break;
switch(X){
case 'A': cout<<"Data has been added"<<endl;
break;
case 'D': cout<<"Data has been deleted"<<endl;
break;
case 'S': cout<<"Data has been sorted"<<endl;
break;
default: cout<<"Data does not exist"<<endl;
break;
}
}
return 0;
}
2.(1)for 用法
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int i,j,k;
for(i=2;i<=100;i++)
{
k=sqrt((double)i);
for(j=2;j<=k;j++)
{
if(i%j==0)
break;
}
if(j>k)
cout<<i <<" ";
}
return 0;
}
(2)while 用法
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int i=2,j,k;
while(i<=100)
{
k=sqrt((double)i);
for(j=2;j<=k;j++)
{
if(i%j==0)
break;
}
if(j>k)
cout<<i <<" ";
i++;
}
return 0;
}
(3)do...while 用法
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int i=2,j,k;
do
{
k=sqrt((double)i);
for(j=2;j<=k;j++)
{
if(i%j==0)
break;
}
if(j>k)
cout<<i <<" ";
i++;
}while(i<=100);
return 0;
}
3.(1)while 用法
#include<iostream>
using namespace std;
int main()
{
int i=23,j;
cout<<"Guess the num between 1 and 100:"<<endl;
while(cin>>j)
{
if(j>i&&j<=100)
cout<<"bigger than the num"<<endl;
else if(j>=1&&j<i)
cout<<"lower than the num"<<endl;
else if(j==i)
cout<<"Congratulations! You're right."<<endl;
else
cout<<"Error"<<endl;
}
return 0;
}
(2)do...while 用法
#include<iostream>
using namespace std;
int main()
{
int i=23,j;
cout<<"Guess the num between 1 and 100:"<<endl;
do
{
if(j>i&&j<=100)
cout<<"bigger than the num"<<endl;
else if(j>=1&&j<i)
cout<<"lower than the num"<<endl;
else if(j==i)
cout<<"Congratulations! You're right."<<endl;
else
cout<<"Error"<<endl;
}while(cin>>j);
return 0;
}
4.
#include<iostream>
using namespace std;
int main()
{
cout<<"the ways to go together is ";
int i,sum=1;
for(i=5;i>=3;i--)
{
sum*=i;
}
for(i=3;i>=1;i--)
{
sum/=i;
}
cout <<sum;
return 0;
}
最新文章
- iOS开发 iOS10推送必看(基础篇)
- Spark算上下基线
- 2015弱校联盟(1) - I. Travel
- Centos6.7下安装配置VPN
- Linux用户组管理
- RDLC系列之六 打印纸张的大小(未解决)
- innodb_io_capacity >;=innodb_lru_scan_depth*inoodb_buffer_pool_instances。与 checkpoint
- linux文件权限解说
- C++ Primer 学习笔记_75_模板与泛型编程 --模板定义
- ASP.NET - URL中参数加密解密操作
- CSS滚动条样式定制
- ORACLE 多表关联 UPDATE 语句
- 最小化spring XML配置,Spring提供了4种自动装配策略。
- 利用Python中SocketServer 实现客户端与服务器间非阻塞通信
- LeetCode解题录-51~100
- tp5中代替tp3.2中的一些方法
- go语言之进阶篇runtime包中 Gosched Goexit GOMAXPROCS的使用
- 对话框(VC_Win32)
- oralce 存储过程、函数和触发器
- Javascript中的继承与Prototype