C++ 实验一

2-28-1 if...else语句

#include<iostream>
using namespace std;
int main()
{ char a;
  cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
  cin>>a;
  while(a)
  {
   if(a=='A')
        cout<<"数据已经增加"<<endl; 
   else if(a=='D')   
        cout<<"数据已经删除"<<endl;
   else if(a=='S')
        cout<<"数据已经排序"<<endl; 
   else if(a=='Q')
        break; 
   else
        cout<<"输入错误"<<endl; 
        
     cin>>a; 
  }
    
   return 0;
} 
2-28-1

 

2-28-2 switch语句

#include<iostream>
using namespace std;
int main()
{
    char a;
    cout << "Menu:A(dd) D(elete) S(ort) Q(uit),Select one:" << endl;
    cin >> a;
    while (a!='Q')
    {
        switch (a)
        {
        case'A':cout << "数据已经增加" << endl;break;
        case'D':cout << "数据已经删除" << endl;break;
        case'S':cout << "数据已经排序" << endl;break;
        default:cout << "输入错误" << endl;
        }
        cin >> a;
    }
    return 0;
}
2-28-2

 

2-29-1 while循环语句

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

 

2-29-2 do...while语句

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

 

2-29-3 for循环语句

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

 

2-32-1 while语句

#include<iostream>
using namespace std;
int main()
{ int n=66,f;
   cout<<"please guess the number(1-100):";
   cin>>f;
   while(n!=f)
   {if(f<n)
    {cout<<"bigger than the number:";
     cin>>f;
    }
    if(f>n)
     {cout<<"smaller than the number:";
    cin>>f;
     }
   }
   cout<<"Yes! You guess it!"<<endl;
   return 0;
 } 
2-32-1

 

2-32-2 do...while 语句

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

 

2-34

#include<iostream>
using namespace std;
int main()
{int i,j,k,t=0;
  cout<<"Red:0,Yellow:1,Blue:2;White:3,Black:4"<<endl;
  for(i=0;i<=4;i++)
  {for(j=i+1;j<=4;j++)
  {if(j==i)continue;
    for(k=j+1;k<=4;k++)
    {if(k==i||k==j) continue;
     cout<<i<<j<<k<<endl;
     t++;
    }
  }
  }
  cout<<"total:"<<t<<endl;
  return 0; 
 } 
2-34

 

之前对没有使用过这个软件,非常不熟练,存在着许多问题,希望在以后的学习中,能够有进步。

我个人觉得自己的代码写得不太好,希望大佬多多指点。

 

https://www.cnblogs.com/DADABu-21/p/10529599.html

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

https://www.cnblogs.com/lovecpp/p/10520456.html

posted @ 2019-03-16 20:08  鲈鱼很沉  阅读(187)  评论(2编辑  收藏  举报