天才吃鸡少女

导航

实验1

1:使用if .....else 判断

#include <iostream>
using namespace std;
int main () {
char x;
cout<<"Menu: A(add) D(elect) S(ort) Q(uite) select one:"<<endl;
cin>>x;
while(x)
 {  if(x=='A')
    cout<<"数据已增加."<<endl; 
       else if(x=='D') 
    cout<<"数据已删除."<<endl; 
       else if(x=='S') 
    cout<<"数据已排序."<<endl;
       else  if (x=='Q') 
          break;
       else
        cout<<"Menu: A(add) D(elect) S(ort) Q(uite) select one:"<<endl;
       cin>>x; 
}

return 0; 
}

 

1:使用switch语句实现程序

#include <iostream>
using namespace std;
int main () {
char x;
cout<<"Menu: A(add) D(elect) S(ort) Q(uite) select one:"<<endl;
cin>>x;
while(x!='Q')
 {  switch(x)
     {case 'A': cout<<"数据已增加."<<endl; break;
      case 'D': cout<<"数据已删除."<<endl; break;
      case 'S': cout<<"数据已排序."<<endl; break;
      default  : cout<<"Menu: A(add) D(elect) S(ort) Q(uite) select one:"<<endl;
     }
    cout<<"Menu: A(add) D(elect) S(ort) Q(uite) select one:";
 cin>>x;
}
return 0;
}

 

2:  1-100的质数(while)

#include <iostream>
#include <math.h>
using namespace std;
int main(){
int x=2,n=1;
while(x<101)
  {int i=2;
    while(i<x&&x%i!=0)
     i++;
   if(i==x)
  {cout<<x;
   if(n%5==0)
   cout<<endl;
    else
   cout<<"  ";
     n++;}
     x++;
   }
    return 0;
}

 

2: do while

#include<iostream>
using namespace std;
int main(){
    int x=2,n=1,i=2;
    do
    {if(x%i==0&&i!=x)
      {x++;
      continue; }
    else if(i==x)
      {cout<<x++;
        if(n%5==0)
          cout<<endl;
        else
          cout<<"  ";
        n++;
        i=2;}
    else
      i++;
    }while(x<101);
    return 0;
}

2:   for

 

#include <iostream>
#include <math.h>
using namespace std;
int main(){
    int i,j,k,f,n=0;
     for(i=2;i<=100;i++)
        {f=1;
         k=sqrt(i);
         for (j=2;j<=k;j++){
         if(i%j==0)
           {f=0;
           break;}
        } if(f)          
           {n++;
           cout<<i<<"  ";
           if(n%5==0)
          cout<<endl;} 
        }
      return 0;
}

 

 

3:猜数字(while)

#include <iostream>
using namespace std;
int main (){
    int a=68,x;
    cin>>x;
    while(a)
    { if (x>a)
      cout<<"The true number is lower !"<<endl;
    else if (x<a)
      cout<<"The true number is bigger !"<<endl; 
    else if (x==a)
      {cout<<"You are the winner!";break;}
      cin>>x;
}

    return 0;
}

3 do...while

#include <iostream>
using namespace std;
int main (){
    int a=68,x;
    cin>>x;
    do 
    { if (x>a)
      cout<<"The true number is lower !"<<endl;
    else if (x<a)
      cout<<"The true number is bigger !"<<endl; 
    else if (x==a)
      {cout<<"You are the winner!";break;}
      cin>>x;
}while(a);

    return 0;
}

 

4:取出3个不同颜色的球,有几种方法?

    #include <iostream>
    using namespace std;
    int main(){
         enum color{red,yellow,blue,white,black};
         color p;
         int i,j,k,n=0;
         for(i=red;i<=black;i++)  
        {  for(j=i+1;j<=black;j++) 
          { if(i!=j)
            { for(k=j+1;k<=black;k++)  
                { if(k!=i&&k!=j)    
                        n=n+1;
                }
            }
          }
        }
        
        cout<<n<<endl;
        return 0;
    } 

实验总结:

对于enum枚举类型不熟悉,没有很好的掌握运用。

对于break,continue本来也不是完全的分清楚运用,但是上课之后老师讲之后就明白了。

 

评论:

 https://www.cnblogs.com/laboratory-X/p/10546983.html

 https://www.cnblogs.com/yfwg/p/10545933.html

 https://www.cnblogs.com/0906mxyd/p/10549332.html

 

posted on 2019-03-16 21:08  天才吃鸡少女  阅读(185)  评论(2编辑  收藏  举报