实验1

2-28

(1)if...else

#include <iostream>
using namespace std;
int main() {
    while (true) {    
    char a;
    cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:" <<endl;
    cin>>a;
    if (a=='A')
    {cout<<"Date has been added"<<endl;
       continue;}
      else if (a=='D')
        {cout<<"Date has been deleted"<<endl;
          continue;}
        else if (a=='S')
          {cout<<"Date has been sorted"<<endl;
             continue;}
           else if (a=='Q')
              break;
             else
               {cout<<"Input error"<<endl;
                  continue;}
    }
return 0;
}
2-28-1

 

(2)switch

#include <iostream>
using namespace std;
int main() {
    while (true) {
        char c;
        cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
        cin>>c;
        switch (c){
            case('A'):cout<<"Date has been added"<<endl;
                    continue;
            case('D'):cout<<"Date has been deleteed"<<endl;
                    continue;
            case('S'):cout<<"Date has been sorted"<<endl;
                    continue;
            case('Q'):break;
            default:cout<<"Input error"<<endl;
                    continue;
        }break;
    }
return 0;
}
2-28-2

 

 

2-29

(1)while

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int n=2,i,k,a=0;
    while(n<=100){
        k=sqrt(n);
        for(i=2;i<=k;i++)
        if(n%i==0)
        break;
        if(i>k){
        a++;
        cout<<n;
        cout<<"   ";}
        if(a%5==0)
        cout<<endl;
        n++;
    }
return 0;
}
2-29-1

 

(2)do while

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int n=2,i,k,a=0;
    do{
        k=sqrt(n);
        for(i=2;i<=k;i++)
        if(n%i==0)
        break;
        if(i>k){
        a++;
        cout<<n;
        cout<<"   ";}
        if(a%5==0)
        cout<<endl;
        n++;
    }while(n<=100);
return 0;
}
2-29-2

 

(3)for

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int n,i,k,a=0;
    for(n=2;n<=100;n++){
        k=sqrt(n);
        for(i=2;i<=k;i++)
        if(n%i==0)
        break;
        if(i>k){
        a++;
        cout<<n;
        cout<<"   ";}
        if(a%5==0)
        cout<<endl;
    }
return 0;
}
2-29-3

 

 

2-32

(1)while

#include <iostream>
using namespace std;
int main(){
    int a=41,b;
    cout<<"Please guess a number."<<endl;
    cin>>b;
    while(a!=b){
        while(a>b){
        cout<<"The number is bigger than yours."<<endl;
        cin>>b;}
        while(a<b){
        cout<<"The number is smaller than yours."<<endl;
        cin>>b;}
    }
    cout<<"You guessed it."<<endl;
    return 0;
}
2-32-1

 

(2)do...while

#include <iostream>
using namespace std;
int main(){
    int a=41,b;
    cout<<"Please guess a number."<<endl;
    cin>>b;
    do{
        if(a>b){
        cout<<"The number is bigger than yours."<<endl;
        cin>>b;}
        if(a<b){
        cout<<"The number is smaller than yours."<<endl;
        cin>>b;}
    }while(a!=b);
    cout<<"You guessed it."<<endl;
    return 0;
}
2-32-2

 

 

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;
}
2-34

 

 

 

 

 

https://www.cnblogs.com/libing-072921/p/10536335.html

https://www.cnblogs.com/Yyaoyyy/p/10547742.html

https://www.cnblogs.com/jzgjzg/p/10555540.html

posted @ 2019-03-17 16:17  mzzzy  阅读(144)  评论(1编辑  收藏  举报