首页 > 文章 > c++实验1

c++实验1

  • 2—28 if…else
#include <iostream>
using namespace std;
int main()
{char n;
 cout<<"Menu: A(dd) D(elete) S(ort) Q(uit), Select one: ";
 cin>>n;
 while(n!=Q)
 {if(n==A)
    cout<<"Date has been added."<<endl;
   else if(n==D)
         cout<<"Date has been deleted."<<endl;
      else if(n==S) 
             cout<<"Date has been sorted."<<endl;
           else if(n==Q)
                   break;
  cout<<"Menu: A(dd) D(elete) S(ort) Q(uit), Select one: ";
  cin>>n;
 }
 return 0; 
}

分享图片

 

  • 2—28 switch 
#include <iostream>
using namespace std;
int main()
{char n;
 cout<<"Menu: A(dd) D(elete) S(ort) Q(uit), Select one: ";
 cin>>n;
 while(n!=Q)
 {switch(n)
  {case A: cout<<"Date has been added."<<endl;break;
   case D: cout<<"Date has been deleted."<<endl;break;
   case S: cout<<"Date has been sorted."<<endl;break;
   case Q: break;
  }
  cout<<"Menu: A(dd) D(elete) S(ort) Q(uit), Select one: ";
  cin>>n;
 }
 return 0;
}

分享图片

 

  • 2—29 do…while
#include <iostream>
#include <cmath> 
using namespace std;
int main()
{int i=2,j,f,t=0;
 do
  {f=1;
   for(j=2;j<=sqrt(i);j++)
       if(i%j==0)
        {f=0;break;}
   if(f==1)
      cout<<i<<endl;
   i++;
    }while(i<100);
 return 0;
}

分享图片

 

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

分享图片

 

  •  2—29 while 
#include <iostream>
#include <cmath> 
using namespace std;
int main()
{int i=2,j,f,t=0;
 while(i<100) 
    {f=1;
     for(j=2;j<=sqrt(i);j++)
        if(i%j==0)
          {f=0;break;}
     if(f==1)
        cout<<i<<endl;
     i++;
    }
 return 0;
}

分享图片

 

  • 2—32 do while
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{int guessNumber,n;
 srand(time(NULL));
 guessNumber=rand()%100+1;
 cout<<"Your guess number is(1~100): ";
 cin>>n;
 do 
 {if(n<guessNumber)
    {cout<<"Bigger than this,please enter a number: ";cin>>n;}
  else if(n>guessNumber)
         {cout<<"Smaller than this,please enter a number: ";cin>>n;}
        else if(n==guessNumber)
                {cout<<"Congratulations!You gussed it~";break;}
  cout<<endl;
 }while(1);
 return 0;
}

分享图片

 

  • 2—32 while
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{int guessNumber,n;
 srand(time(NULL));
 guessNumber=rand()%100+1;
 cout<<"Your guess number is(1~100): ";
 cin>>n;
 while(1)
 {if(n<guessNumber)
    {cout<<"Bigger than this,please enter a number: ";cin>>n;}
  else if(n>guessNumber)
         {cout<<"Smaller than this,please enter a number: ";cin>>n;}
        else if(n==guessNumber)
                {cout<<"Congratulations!You gussed it~";break;}
  cout<<endl;
 }
 return 0;
}

分享图片

 

  •  2—34 第一版
#include<iostream>
using namespace std;
enum ChoiceResult{red,yellow,blue,white,black};
int comm(int n,int k)
{if(k>n)
    return 0;
 else if(n==k||k==0)
         return 1;
      else
         return comm(n-1,k)+comm(n-1,k-1);
 } 
int main()
{int i,j,m;
 for(i=red;i<=black;i++)
    for(j=i+1;j<=black;j++)
       {if(i!=j)
           for(m=j+1;m<=black;m++)
              if((i!=m)&&(j!=m))
                 {switch(i)
                   {case 0:{cout<<"red ";break;}
                    case 1:{cout<<"yellow ";break;}
                    case 2:{cout<<"blue ";break;}
                    case 3:{cout<<"white ";break;}
                    case 4:{cout<<"black ";break;}
                   }
                   switch(j)
                   {case 0:{cout<<"red ";break;}
                    case 1:{cout<<"yellow ";break;}
                    case 2:{cout<<"blue ";break;}
                    case 3:{cout<<"white ";break;}
                    case 4:{cout<<"black ";break;}
                   }
                   switch(m)
                   {case 0:{cout<<"red ";break;}
                    case 1:{cout<<"yellow ";break;}
                    case 2:{cout<<"blue ";break;}
                    case 3:{cout<<"white ";break;}
                    case 4:{cout<<"black ";break;}
                   }
                   cout<<endl;
                 }
       }
 cout<<"C(5,3)="<<comm(5,3)<<endl;
 return 0;
} 

分享图片

  • 2—34 第二版

            (第一版switch重复,参考书籍,用子函数来完成重复的操作)

#include<iostream>
using namespace std;
enum ChoiceResult{red,yellow,blue,white,black};
int comm(int n,int k)
{if(k>n)
    return 0;
 else if(n==k||k==0)
         return 1;
      else
         return comm(n-1,k)+comm(n-1,k-1);
 } 
int result(int t)
{switch(t)
 {case 0:{cout<<"red ";break;}
  case 1:{cout<<"yellow ";break;}
  case 2:{cout<<"blue ";break;}
  case 3:{cout<<"white ";break;}
  case 4:{cout<<"black ";break;}
 }
}
int main()
{int i,j,m;
 for(i=red;i<=black;i++)
    for(j=i+1;j<=black;j++)
       {if(i!=j)
           for(m=j+1;m<=black;m++)
              if((i!=m)&&(j!=m))
                 {result(i);result(j);result(m);cout<<endl;}
       }
 cout<<"C(5,3)="<<comm(5,3)<<endl;
 return 0;
}

分享图片

 

实验小结:

1、第2—32题,查阅了无所不知的度娘,我学会了随机数的编写。

2、学习了新知识,也不能忘记了以前学习过的小细节,比如在编写第2—28题时,我忘记了字符常量要使用单引号。

3、第2—34题,参考课本P75-76,学习了用递归法计算组合数。

3、编程是一个日积月累的过程,只有通过自己的实际操作,才能知道自己的不足和错误之处。

时间:2019-03-15 20:50:34阅读(21)评论(0) 收藏(0) 【+收藏】
评论
暂无评论!
登录后评论!
加入时间:2018-03-07
关注此人发短消息
文章分类
默认分类(27429
格物致知 关注的人(0
格物致知 被(1)人关注
联系我们 - 留言反馈
© 2017 版权所有 鲁ICP备17052893号