实验2

2-28 (1)

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

 

实验2

 

 

 

 

 

2-28(2)

实验2实验2
//2-28-2
#include<iostream>
using namespace std;
int main()
{
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit) Select one:";
    char a;
    for(;1;)
    {
        cin>>a;
        if(a=='Q'){cout<<"程序退出";break;}
        switch(a)
        {
            case('A'):cout<<"数据已增加"<<endl;break;
            case('D'):cout<<"数据已删除"<<endl;break;
            case('S'):cout<<"数据已排序"<<endl;break;
        }
    }
    return 0;
}
        
2-28(2)

实验2

 

 

 

 

2-29 找出100以内的质数

实验2实验2
#include<iostream>
using namespace std;
int A(int x); //判断一个数是否是质数的函数 
int main()
{
    int a;
    cout<<'2'<<' ';
    for(a=3;a<101;a++)
    {
        if(A(a))                          //把数输入到函数中判断,若是质数则返回1输出此数,否则返回0不输出
        {cout<<a<<' ';}
    }
        return 0; 
}    //main 结束 
    int A(int x)
    {
        int b=2;
        while(b<x)
        {
            if(x%b==0)
            {return 0;break;}
            b++;
         } return 1;
    }

 
2-29-1
实验2实验2
#include<iostream>
using namespace std;
int A(int x);
int main()
{
    int a;
    cout<<'2'<<' ';
    for(a=3;a<101;a++)
    {
        if(A(a))                         
        {cout<<a<<' ';}
    }
        return 0; 
}    

int A(int x)
{
    int c=2;
    for(;c<x;c++)
    {
        if(x%c==0){return 0;}
    }
    return 1;
}
2-29-2
实验2实验2
#include<iostream>
using namespace std;
int A(int x);
int main()
{
    int a;
    cout<<'2'<<' ';
    for(a=3;a<101;a++)
    {
        if(A(a))                         
        {cout<<a<<' ';}
    }
        return 0; 
}    

int A(int x)
{
    int c=2;
    do
    {
        if(x%c==0){return 0;}
        c++;
    }while(c<x);
    return 1;
}
2-29-3

实验2

 思路:质数因数只有1和本身。也就是,一旦一个数有除了1与自己以外的因数,那么它就不是质数;

判断函数中就是通过循环,在大于1小于a的数中找a的因数。一旦找到,即确认是非质数,立即结束循环。否则是质数

 

 

 

2-32 随机数猜大小

实验2实验2
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
    int a,b;
    char c;
    while(1)
    {
        srand((unsigned)time(NULL));
        a=rand()%(100-1); 
        cout<<"1-100内的随机数已生成,输入你猜测的数:";
        cin>>b;
        while(a!=b)
        {
        if(a>b){cout<<"偏小"<<endl;}
        else if(a<b){cout<<"偏大"<<endl;}
        cin>>b;
        }
            cout<<"答案正确。是否再次进行游戏?Y/N"<<endl;
            cin>>c;
            if(c=='Y'){continue;}
            else if(c=='N'){break;} 
    }
    return 0;
 } 
2-32-1
实验2实验2
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
    int a,b;
    char c;
    do
    {
        srand((unsigned)time(NULL));
        a=rand()%(100-1); 
        cout<<"1-100内的随机数已生成,输入你猜测的数:";
        cin>>b;
        do{
        if(a>b){cout<<"偏小"<<endl;}
        else if(a<b){cout<<"偏大"<<endl;}
        cin>>b;
        }while(a!=b);
        cout<<"答案正确。是否再次进行游戏?Y/N"<<endl;
        cin>>c;
        if(c=='Y'){continue;}
        else if(c=='N'){break;} 
    }while(1);
    return 0;
 } 
2-32-2

实验2

 

 

2-34 排列组合问题(?)

实验2实验2
#include<iostream>
using namespace std;
int main()
{
    int a,b,c=1,d=1;
    cout<<"共有几种不同颜色的球:";//第一次摸球有a种可能 
    cin>>a; 
    cout<<"共摸几次:";//最后一次有(a-b),共b次 
    cin>>b;
    for(;b!=0;b--)
    {
        d*=b;//实际是一次摸出b个,除去排列产生的重复结果 
        c*=a;
        a--; 
        
    }
    c/=d;    
    cout<<""<<c<<"种可能"<<endl;
    
 } 
2-34

实验2

 

 

 

验证性实验部分

1.函数声明和函数定义的理解

函数声明≈int a,在使用该函数前(主函数前)声明,之后找个位置定义,表明该函数的作用。

区别:声明写在开头主函数前,int 函数名()加分号,定义不用加分号。

 

2.形参:出现在函数声明与定义的括号内,本身不占内存。用于接受传递的实参的值。形参的值的改变不影响原本实参的值。

实参:在函数内声明,有实际意义。

函数参数用于接受调用该函数时传递的实参。返回值将运行的结果传回调用处。

 

3.值传递:形参的改变不会影响到对应实参;

引用传递,形参的改变会影响实参,形参地址和实参相同