MinterOne 2016-06-02 13:43 采纳率: 0%
浏览 1199
已结题

c++关于关于函数调用的问题

#include
#include
#include
using namespace std;
class Plass
{
public:
int fun();
int judge();
private:
int mid;
int flag;
int num;
int sign;
int bott;

};
int main()
{

Plass A;
A.fun();
A.judge();
return 0;
}
int Plass::fun()
{

cout<<"随机生成的数组: ";
srand((int)time(NULL)); //利用时间函数time(),产生不同的随机种子
int a[10];
for(int i=0;i {a[i]=rand()%100+1; //rand()5100用于产生0-99之间的随机数
cout cout cout int temp=0;
//采用冒泡排序给数组a[10]按从小到大排序
for(int i=0;i {
for(int j=i+1;j {
if(a[i]>=a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
continue;
}
else
continue;
}
cout<<a[i]<<' ';
}
cout<<endl;

}

int Plass::judge()
{
//调用fun()函数中的a[10]数组
?????
怎么在这里调用fun()函数中的a[10]数组,实现下面的功能
//判断所输入的数字符不符合要求

int mid=0,sign=0,flag=1,legal=0;

while(flag)
{
    int b;
    cout<<"请再次输入一个正整数用于检测: ";
    cin>>b;
    int i;
 //判断要检查的数在不在数组范围内 
    if(b<a[0]||b>a[9])    
    flag=-1;
    int top,bott;
    top =0;
    bott =10;
    while(sign==0&&(top<=bott))
    {
        {
            mid =(top+bott)/2;
            if(b<a[mid])
              bott=mid+1;
            else if(b>a[mid])
            top = mid-1;
            else if(b==a[mid])
            {
                cout<<d<<"是数组第"<<mid+1<<"个元素"<<endl;
                sign =1;
                break;
            }
        }
    } 
    //判断有无次数并输出 
    if (!sign||flag==-1)
    cout<<"无此数"<<endl;
    char c;
    cout<<"是否考虑重新输入数字(Y/N):";
    fflush(stdin);
    cin>>c;
    if(c=='Y')
    flag=1;
    else
    flag=0;

}

}

  • 写回答

1条回答 默认 最新

  • 小灸舞 2016-06-02 14:23
    关注

    把a数组设成Plass的成员变量或者弄成全局变量或者作为fun的返回值都行的

     #include<iostream>
    #include<stdlib.h>
    #include <time.h> 
    using namespace std;
    class Plass
    {
        public:
            int fun();
            int judge();
        private:
            int mid;
            int flag;
            int num;
            int sign;
            int bott;
            int a[10];
    
    };
    int main()
    {   
        Plass A;
        A.fun(); 
        A.judge();
        return 0;
    } 
    int Plass::fun()
    {   
        cout<<"随机生成的数组: ";
        srand((int)time(NULL));                        //利用时间函数time(),产生不同的随机种子 
        //int a[10];
        for(int i=0;i<10;i++)
           {a[i]=rand()%100+1;                           //rand()5100用于产生0-99之间的随机数
            cout<<a[i]<<' ';}
        cout<<endl;
        cout<<"排序后数组: ";
        int temp=0;
        //采用冒泡排序给数组a[10]按从小到大排序
         for(int i=0;i<9;i++)
         {
            for(int j=i+1;j<10;j++)
            {
                if(a[i]>=a[j])
                {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
                continue;
                }
                else
                continue;
            }
            cout<<a[i]<<' ';
        }
        cout<<endl;
            return 0;
    }
    
    int Plass::judge()
    {
        //调用fun()函数中的a[10]数组 
        //????? 
        //怎么在这里调用fun()函数中的a[10]数组,实现下面的功能
        //判断所输入的数字符不符合要求
    
        int mid=0,sign=0,flag=1,legal=0;
    
        while(flag)
        {
            int b;
            cout<<"请再次输入一个正整数用于检测: ";
            cin>>b;
            int i;
         //判断要检查的数在不在数组范围内 
            if(b<a[0]||b>a[9])    
            flag=-1;
            int top,bott;
            top =0;
            bott =10;
            while(sign==0&&(top<=bott))
            {
                {
                    mid =(top+bott)/2;
                    if(b<a[mid])
                      bott=mid+1;
                    else if(b>a[mid])
                    top = mid-1;
                    else if(b==a[mid])
                    {
                        cout<<b<<"是数组第"<<mid+1<<"个元素"<<endl;
                        sign =1;
                        break;
                    }
                }
            } 
            //判断有无次数并输出 
            if (!sign||flag==-1)
            cout<<"无此数"<<endl;
            char c;
            cout<<"是否考虑重新输入数字(Y/N):";
            fflush(stdin);
            cin>>c;
            if(c=='Y')
            flag=1;
            else
            flag=0;
    
        }
        return 0;
    }
    

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)