c++实验一

2-28(1)

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

2-28(2)

#include <iostream>
using namespace std;
int main() {
char a;
while(true)
{cout<<"Menu:A(dd)D(elete)S(ort)Q(uit),Select one:"<<endl;
cin>>a;
switch (a)
{case 'A':{cout<<"数据已经增加"<<endl;continue;}
case 'D':{cout<<"数据已经减少"<<endl;continue;}
case 'S':{cout<<"数据已经排序"<<endl;continue;}
case 'Q':break;
default:{cout<<"程序结束"<<endl;continue;}
}
break;
}
return 0;
}

截图:

 

2-29(1)

 

#include <iostream>
using namespace std;
int main()
{int i=2,j,x,y;
while(i<101)
{x=1;
y=i/2;
j=2;
while(j<=y)
{if(i%j==0)
{x=0;
break;
}
j++;
}
if(x)
cout<<i<<" ";
i++;
}
}

2-29(2)

#include<iostream>
using namespace std;
int main()
{    int a=2,n=1,i=2;
    do
    {    if(a%i==0&&i!=a)
        {    a++;
            continue;
        } 
        else if(i==a)
        {    cout<<a++;
            if(n%5==0)
                cout<<endl;
            else
                cout<<"  ";
            n++;
            i=2;
        }
        else
            i++;
    }while(a<101);
    return 0;

 2-29(3)

#include<iostream>
using namespace std;
int main()
{    int i,j,n=1;
    for(i=2;i<=100;i++)
        for(j=2;j<=i;j++)
        {    if(i%j==0&&i!=j)
                break;
            if(i==j)
            {    cout<<i;
                if(n%5==0)
                    cout<<endl;
                else
                    cout<<"  ";
                n++;
            }
        }
    return 0;
}
截图:

2-32(1)

#include <iostream>
using namespace std;
int main()
{ int n=70,m;
  cout<<"please guess the nunmber(1-100): ";cin>>m;
  while(n!=m)
    {if(m<n)
      {cout<<"bigger than the number: ";cin>>m;}
     else 
      {cout<<"smaller than the number: ";cin>>m;}
    }
  cout<<"right!"<<endl;
  return 0;
 }
2-32(2)
#include <iostream>
using namespace std;
int main()
{ int n=70,m;
  cout<<"please guess the nunmber(1-100): ";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<<"right!"<<endl;
  return 0;
}
截图:

2-34

#include<iostream>
#include<iomanip>
using namespace std;
void qiu(int a)
{
switch(a)
{
case 1:cout<<"red ";break;
case 2:cout<<"yellow ";break;
case 3:cout<<"biue ";break;
case 4:cout<<"white ";break;
case 5:cout<<"black ";break;
}
}
int main()
{
int i,j,k,n=0;
for(i=1;i<=5;i++)
{
for(j=i+1;j<=5;j++)
{
for(k=j+1;k<=5;k++)
{
qiu(i);
qiu(j);
qiu(k);
cout<<endl;
n++;
}
}
}
cout<<"number:"<<n<<endl;
return 0;
}

截图:

 

 总结:上学期学的不够精,导致用起来十分生硬,虽然C++和c差不多,但一个寒假没用也确实忘了许多。这章重温了循环等操作,还得多加练习

https://www.cnblogs.com/lszz/p/10527444.html#4213050

https://www.cnblogs.com/lszz/p/10527444.html#4213050

https://www.cnblogs.com/KOKODA/p/10512049.html#4213043

 
 

 

转载于:https://www.cnblogs.com/cjj1/p/10561472.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值