实验一
2-28 while
#include <iostream>
using namespace std;
int main ()
{
while(true)
{ char n;
cout<<"Menu : A (dd) D (elete) S (ort) Q (uit), Select one :"<<endl;
cin>>n;
if(n=='A')
{cout<<"data has been added"<<endl;
continue;
}
else if(n=='D')
{cout<<"data has been deleted"<<endl;
continue;
}
else if(n=='S')
{cout<<"data has been sorted"<<endl;
continue;
}
else if (n=='Q')
break;
else
cout<<"please input another number"<<endl;
}
return 0;
}

2-28 DO WHILE
#include <iostream>
using namespace std;
int main()
{ while(true)
{ char n;
cout<<"Menu : A (dd) D (elete) S (ort) Q (uit), Select one :"<<endl;
cin>>n;
switch(n)
{ case'A' : cout<<"data has been added"<<endl;
continue;
case'D' : cout<<"data has been deleted"<<endl;
continue;
case'S' : cout<<"data has been sorted"<<endl;
continue;
case'Q' : break;
default : cout<<"please input another number"<<endl;
}
}return 0;
}

2-29 while
#include <iostream>
using namespace std;
int main()
{ int i=2,j,n,f;
while(i<=100)
{f=1;
n=i/2;
j=2;
while(j<=n)
{if (i%j==0)
{f=0;
break;
}
j++;
}
if(f==1)
cout<<i<<endl;
i++;
}
return 0;
}

2-29 do while
#include <iostream>
#include <cmath>
using namespace std;
int main()
{ int i=2,j,n,m;
do
{m=1;
n=i/2;
j=2;
while(j<=n)
{if (i%j==0)
{m=0;
break;
}
j++;
}
if(m)
cout<<i<<endl;
i++;
}while(i<=100);
return 0;
}

2-29 for
#include <iostream>
using namespace std;
int main()
{ int i,j,f,n;
for(i=2;i<=100;i++)
{f=1;
n=i/2;
for(j=2;j<=n;j++)
{if(i%j==0)
{f=0;
break;}
}
if(f==1)
cout<<i<<endl;
}
return 0;
}

2-32 while
#include <iostream>
using namespace std;
int main()
{ int a=62,b;
cout<<"Please input a number:"<<endl;
cin>>b;
while(a>b)
{cout<<"smaller than the number"<<endl;
cin>>b;
}
while(a<b)
{cout<<"bigger than the number"<<endl;
cin>>b;
}while(a==b)
{
cout<<"Congratulations!You got it!"<<endl;
break;}
return 0;
}

2-32 do while
#include <iostream>
using namespace std;
int main()
{ int a=62,b;
cout<<"Please input a number:"<<endl;
cin>>b;
do
{cout<<"smaller than the number"<<endl;
cin>>b;
}while(a>b);
do
{cout<<"bigger than the number"<<endl;
cin>>b;
}while(a<b);
do
{cout<<"Congratulations!You got it!"<<endl;
break;
}while(a==b);
return 0;
}

2-34
#include <iostream>
using namespace std;
int main()
{
int i,j,k,n=0;
for(i=1;i<=5;i++)
{for(j=i;j<=5;j++)
if(i!=j)
{for(k=j+1;k<=5;k++)
if(k!=i&&k!=j){
n++;
cout<<i<<" "<<j<<" "<<k<<endl;}
}
}
cout<<n<<endl;
return 0;
}


浙公网安备 33010602011771号