实验1-C++简单程序设计

2-28

#include<iostream>

using namespace std;

int main(){

         char choice;

         while(1)

         {

                  cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";

        cin>>choice;

             if(choice=='A') cout<<"Dates has been added"<<endl;

             else if(choice=='D') cout<<"Dates has been deleted"<<endl;

                  else if(choice=='S') cout<<"Dates has been sorted"<<endl;

        else if(choice=='Q') break;

         }

         return 0;

}

#include<iostream>

#include<cstdlib>

using namespace std;

int main(){

         char choice;

         while(1)

         {

                  cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";

        cin>>choice;

                  switch(choice)

                  {

                  case 'A': cout<<"Dates has been added"<<endl;break;

                  case 'D': cout<<"Dates has been deleted"<<endl;break;

                  case 'S': cout<<"Dates has been sorted"<<endl;break;

        case 'Q': exit(0); break;

        default : cout<<"no such choice"<<endl;

                  }

                  }

         return 0;

}

2-29

#include<iostream>

#include<cmath>

using namespace std;

int main(){

         int i,m,n,a;

         for(i=2;i<=100;i++)

         {

                  a=1;

                  m=sqrt(i);

                  for(n=2;n<=m;n++)

                  {

                          if(i%n==0)

                          {      

                                   a=0;

                              break;

                          }               

                  }

                  if(a==1) cout<<i<<" ";

         }

         return 0;

}

#include<iostream>

#include<cmath>

using namespace std;

int main()

{

    int a,i,m=1,n;

         do{

                  a=2;

                  n=0;

                  i=sqrt(m);

                  if(m==1) n=1;

                  else

                  {

                          while(a<=i)

                           {

                                   if(m%a==0)

                                   {

                                            n=1;

                                            break;

                                   }

                                   else a++;

                          }

                  }

                  if(n==0)

                          cout<<m<<" ";

                  m++;

         }while(m<=100);

         return 0;

}

2-32

#include<iostream>

using namespace std;

int main(){

         int n=66,i,m=1;

         while(m)

         {

     cout<<"Please guess the number(1-100):";

     cin>>i;

          if(i>n)

                   cout<<"bigger than number"<<endl;

          else if(i<n)

                   cout<<"smaller than number"<<endl;

          else if(i==n)

          {

                   cout<<"you are right"<<endl;

         m=0;

          }

         }

   return 0;

}

#include<iostream>

using namespace std;

int main()

{

         int n=66,i,t=1;

         do

         {

     cout<<"Please guess the number(1-100):";

     cin>>i;

          if(i>n)

                   cout<<"bigger than number"<<endl;

          else if(i<n)

                   cout<<"smaller than number"<<endl;

          else if(i==n)

          {

                   cout<<"you are right"<<endl;

         break;

          }

         }while(t=1);

    return 0;

}

2-34

#include<iostream>

using namespace std;

int main()

{

         int m,n,i,a=0;

         for(m=1;m<=5;m++)

         {

                  for(n=m+1;n<=4;n++)

                  {

                          for(i=n+1;i<=5;i++)

                          {

                                   cout<<m<<n<<i<<endl;

                              a++;

                          }

                  }

         }

         cout<<a<<endl;

         return 0;

}

1、上学期因为经常使用for循环导致对do--while和while形式使用十分生硬,甚至是想不起来使用。

2、经常出现可以用很简单方式解决的问题却要用很多不必要的代码,简单来说就是废话太多。

3、明显感觉C++的输出和输入要比c更简洁明了,更为方便。

猜你喜欢

转载自www.cnblogs.com/yfwg/p/10545933.html