实验二

#include<iostream>
using namespace std;
int main()
{  char x;  
  cout<<"Menu:A(dd) D(elete) S(ort)Q(uit) Select one:"; 
    while(true)
  {  cin>>x;
    if(x=='A')
    cout<<"数据已增加"<<endl;
	else if(x=='D') 
	cout<<"数据已删除"<<endl;
	else if(x=='S') 
	cout<<"数据已排列"<<endl;
	else if(x=='Q') 
	break;
   }
return 0;
}         //2.28 else if 用法
#include<iostream> 
using namespace std;
int main()
{   char x;
    cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:";
      while(true)
{     cin>>x;
    if(x=='A'||x=='D'||x=='S')
 {
       switch(x)
     { 
       case 'A': cout<<"数据已经增加"<<endl;
                break; 
       case 'D': cout<<"数据已经删除"<<endl;
              break;
       case 'S': cout<<"数据已经排序"<<endl;
              break; 
     }
 } 
   else if(x=='Q')
   break;
}
     return 0;
}//2.28 switch用法  总结这一题相比之下比较简单,但是还是有容易出错的地方的,我意识到定义一个字符型变量,然而却在后面输入时候忘记了''未能一次性成功
#include<iostream>
using namespace std;
int main()
{ int i,n,t;
  cin>>t;
  cout<<"the prime number from 1 to"<<t<<"is"<<2<<endl;
    for(n=3;n<=t;n++)
  {  for(i=2;i<=(n-1);i++)
     if(n%i==0) break;
     if(i==n)
     cout<<n<<endl;
  } 
    return 0;
}2.29for用法

#include<iostream> 
using namespace std;
int main() 
{  int n,t,i;
   cin>>t;
   cout<<"the prime from 1 to "<<t<<"is"<<2<<endl;
   do
 {  for(i=2;i<=n-1;i++)
    if(n%i==0)
    break;
    if(n==i)
    cout<<n<<endl;
    n++;
 }while(n<=t);
  return 0;
}2.29do while 用法
#include<iostream> 
using namespace std;
int main()
{   int n=3,i,t;
    cin>>t;
    cout<<"the prime number from 1 to "<<t<<"is"<<2<<endl;
    while(n<=t)
 { 
    for(i=2;i<=n-1;i++)
    if(n%i==0)
    break;
    if(n==i)
    cout<<n<<endl;
    n++;
 }
     return 0;    
}2.29while 本次程序第一是判断素数,本人使用的不是简单的方法,而是最原始的从2除到m-1的方法,我试了下函数调用来做但是自己基础不行,未能取得很好效果,所以这次展示的自己的原始方法,并不是最优解法,还需改进
#include<iostream> 
#include<cstdlib>
using namespace std;
int main()
{   int i=rand()%1000;
    int m;
    cout<<"now the computer has set a number,please guess it.";
    cin>>m;
    do
    { if(m>i)
      cout<<"sorry,it's too big."<<endl;
      else
      cout<<"sorry,it's too small."<<endl;
      cout<<"please try it again."<<endl;
      cin>>m;
    } while(m!=i);
    if(m==i)
    cout<<"you are so clever.Congulations!"<<endl;
    return 0;
}3.32  do while 用法
#include<iostream> 
#include<cstdlib>
using namespace std;
int main()
{  int i=rand()%1000;
   int m;
   cout<<"the computer just set a number."<<"now please guess it."<<endl;
   cin>>m;
  while(m!=i)
 { if(m>i)
   cout<<"it too big!"<<endl;
   else 
   cout<<"it too small!"<<endl;
   cout<<"please try again."<<endl;
   cin>>m;
   }
 if(m==i)
  cout<<"You are so clever.Congratulation!"<<endl;
   return 0;    
} while 用法  本次语句方面还算简单,主要巩固的while do while的用法,但是本程序引入了随机数,需注意

 

#include<iostream> 
using namespace std;
int main()
{  enum color{red,yellow,blue,white,black};
   color p;
   int x,y,z,n=0;
   for(x=red;x<=black;x++)
   for(y=x+1;y<=black;y++)
   for(z=y+1;z<=black;z++)
   if(x!=y&&x!=z&&y!=z)
   n++;
   cout<<"totally "<<n<<" kinds of different ways"<<endl;
   return 0;
   
} 这个代码我用了3次for语句但是这个还不是最好的方法,未能有很好的效果

 


 



 

posted @ 2018-03-25 21:44  宇神  阅读(130)  评论(1)    收藏  举报