C++实验一

实验结论

2-28 if...else

#include <iostream>
using namespace std;
int main()
{char c;
while (true)
{cout<<"Menu: A(dd) D(elect) S(ort) Q(uit) Select one: ";cin>>c;
 if(c=='A')
       {cout<<"Data added"<<endl;
        continue;
       }
   else
       if(c=='D')
          {cout<<"Data deleted"<<endl;
           continue;
          }
       else
           if(c=='S')
              {cout<<"Data sorted"<<endl;
               continue;
              }
           else
              break;
}
 return 0;
}

     switch

#include <iostream>
using namespace std;
int main()
{ char c;
 while (true) 
  {cout<<"Menu: A(dd) D(elect) S(ort) Q(uit) Select one: ";cin>>c;
   switch(c) 
   { case 'A':cout<<"Data added"<<endl;continue;
     case 'D':cout<<"Data deleted"<<endl;continue;
     case 'S':cout<<"Data sorted"<<endl;continue;
     case 'Q':break;
   }
   break;
 }
return 0;
}

 

2-29 while

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

     do...while

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

     for

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

 

2-32 while

#include <iostream>
using namespace std;
int main()
{ int n=63,m;
  cout<<"please guess the nunmber(1-100) and input it: ";cin>>m;
  while(n!=m)
    {if(m<n)
      {cout<<"bigger than the number: ";cin>>m;}
     else 
      {cout<<"smaller than the number: ";cin>>m;}
    }
  cout<<"Bingo! You guess the number!"<<endl;
  return 0;
 } 

     do...while

#include <iostream>
using namespace std;
int main()
{ int n=63,m;
  cout<<"please guess the nunmber(1-100) and input it: ";cin>>m;
  do
    {if(m<n)
      {cout<<"bigger than the number: ";cin>>m;}
     else 
      {cout<<"smaller than the number: ";cin>>m;}
    }while(n!=m);
  cout<<"Bingo! You guess the number!"<<endl;
  return 0;
 } 

 

2-34

#include <iostream>
using namespace std;
int main()
{ int i,j,k,r=0;
  int c1,c2,c3;
  cout<<"Red: 0,Yellow: 1,Blue: 2,White: 3,Black:4"<<endl; 
  int a[5]={0,1,2,3,4};
  for(i=0;i<=4;i++)
    {c1=a[i];
     for(j=i+1;j<=4;j++)
       {c2=a[j];
        for(k=j+1;k<=4;k++)
          {c3=a[k];
           r++;
           cout<<c1<<c2<<c3<<endl;}
        c2=a[j]; 
       } 
      c1=a[i];
     }
  cout<<"Total:"<<r<<endl;
  return 0;
}

 

 

实验总结与体会

1.好久没有做题了,手生。再加上基础知识不够扎实,在编程的过程中格外困难,自身还是有蛮大的提升空间。

2.2-34不会用枚举法,请大佬指点。

3.以上程序有不足或者可以改进之处,请大佬提点。

转载于:https://www.cnblogs.com/Ann-88/p/10527233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值