c++问题,疑问,函数

怎么在主函数内调用这个

std::string& ClearAllSpace(std::string &str)
{
    int index = 0;
    if (!str.empty())
    {
        while ((index = str.find(' ', index)) != std::string::npos) {
            str.erase(index, 1);
        }
    }
    return str;
}
#include<iostream>
#include <cstring>
#include <stdio.h>
#include<ctype.h>
#define N 1000
using namespace std;
int Lengh(char a[])
{
    int i = 0, j = 0;

    while(a[i] != '\0')
    {
        if(a[i] != ' ')
        {
            if(a[i] != '0' && a[i] != '1')
                return 0;

            j++;
        }

        i++;
    }

    return j;
}

void mul(char int1[],char int2[],int n)
{
    char *sum = new char[2*(n+1)];
    char *temp = new char[2*(n+1)];
    printf("%*s\n",2*(n+1),int1);
    printf("€");
    printf("%*s\n",2*n+1,int2);
    printf("%*s",n+1,"×");
    for(int i = 0; i < n+2; i++)
        printf("-");
    printf("\n");

    for(int i = 0; i < 2*(n+1); i++) {
        sum[i] = '0';
        temp[i] = '0';
    }
    for(int i = n; i > -1; i--) {
        if(int2[i]=='1')
        {
            for(int k = 0; k < 2*(n+1); k++)
            {
                temp[k] = '0';
            }
            for(int j = n; j > -1; j--)
            {
                temp[j+i+1] = int1[j];
            }
            bool carry = false;
            for(int i = (2*n + 1); i > -1; i--)
            {
                if(!carry)
                {
                    if((sum[i]^temp[i])==0)
                    {
                        if((sum[i]=='1')&&(temp[i]=='1'))
                        {
                            carry = true;
                        }
                        sum[i] = '0';
                    }
                    else
                    {
                        sum[i] = '1';
                    }
                } else
                {
                    if((sum[i]^temp[i])==0)
                    {
                        carry = ((sum[i]=='1')&&(temp[i]=='1')) ? true : false;
                        sum[i] = '1';
                    }
                    else
                    {
                        sum[i] = '0';
                        carry = true;
                    }
                }
            }
            printf("%*s\n",n+2+i,int1);
        }
        else
        {
            printf("%*s",i+1,"");
            for(int k = 0; k < n+1; k++)
                printf("0");
            printf("\n");

        }

    }
    for(int i = 0; i < (n+1); i++)
    {
        int1[i] = sum[i];
    }
    for(int i = (n+1); i < 2*(n+1); i++)
    {
        int2[i-n-1] = sum[i];
    }
    for(int i = 0; i < 2*(n+1); i++)
        printf("-");
    printf("\n");
    for(int i=0; i<2*(n+1); i++)
        cout<<sum[i];
    cout<<endl;
}

std::string& ClearAllSpace(std::string &str)
{
    int index = 0;
    if (!str.empty())
    {
        while ((index = str.find(' ', index)) != std::string::npos) {
            str.erase(index, 1);
        }
    }
    return str;
}
int main()
{
    char *c = new char[N];
    char *d = new char[N];

    do
    {
        int a = 0, b = 0;
        cout << "请输入两个二进制乘数,可以加空格,输入quit退出程序" << endl;
        cout << "A=";
        cin.getline(c,N,'\n');

        if(strcmp(c, "quit") == 0)
            break;

        cout << "B=";
        cin.getline(d,N,'\n');
        if(strcmp(d, "quit") == 0)
            break;

        a = Lengh(c);
        b = Lengh(d);

        if(a != b || a==0 || b==0)
        {
            cout << "输入的二进制数不正确,或者是两数长度不匹配" << endl;
            continue;
        }
        c=ClearAllSpace(c);
        d=ClearAllSpace(d);
        a = a - 1;
        mul(c,d,a);
        cout<<"\n================="<<endl;
    }
    while(true);
    return 0;
}

#include<iostream>
#include <cstring>
#include <stdio.h>
#include<ctype.h>
#define N 1000
using namespace std;
int Lengh(char a[])
{
    int i = 0, j = 0;
 
    while(a[i] != '\0')
    {
        if(a[i] != ' ')
        {
            if(a[i] != '0' && a[i] != '1')
                return 0;
 
            j++;
        }
 
        i++;
    }
 
    return j;
}
 
void mul(char int1[],char int2[],int n)
{
    char *sum = new char[2*(n+1)];
    char *temp = new char[2*(n+1)];
    printf("%*s\n",2*(n+1),int1);
    printf("a");
    printf("%*s\n",2*n+1,int2);
    printf("%*s",n+1,"×");
    for(int i = 0; i < n+2; i++)
        printf("-");
    printf("\n");
 
    for(int i = 0; i < 2*(n+1); i++) {
        sum[i] = '0';
        temp[i] = '0';
    }
    for(int i = n; i > -1; i--) {
        if(int2[i]=='1')
        {
            for(int k = 0; k < 2*(n+1); k++)
            {
                temp[k] = '0';
            }
            for(int j = n; j > -1; j--)
            {
                temp[j+i+1] = int1[j];
            }
            bool carry = false;
            for(int i = (2*n + 1); i > -1; i--)
            {
                if(!carry)
                {
                    if((sum[i]^temp[i])==0)
                    {
                        if((sum[i]=='1')&&(temp[i]=='1'))
                        {
                            carry = true;
                        }
                        sum[i] = '0';
                    }
                    else
                    {
                        sum[i] = '1';
                    }
                } else
                {
                    if((sum[i]^temp[i])==0)
                    {
                        carry = ((sum[i]=='1')&&(temp[i]=='1')) ? true : false;
                        sum[i] = '1';
                    }
                    else
                    {
                        sum[i] = '0';
                        carry = true;
                    }
                }
            }
            printf("%*s\n",n+2+i,int1);
        }
        else
        {
            printf("%*s",i+1,"");
            for(int k = 0; k < n+1; k++)
                printf("0");
            printf("\n");
 
        }
 
    }
    for(int i = 0; i < (n+1); i++)
    {
        int1[i] = sum[i];
    }
    for(int i = (n+1); i < 2*(n+1); i++)
    {
        int2[i-n-1] = sum[i];
    }
    for(int i = 0; i < 2*(n+1); i++)
        printf("-");
    printf("\n");
    for(int i=0; i<2*(n+1); i++)
        cout<<sum[i];
    cout<<endl;
}
 
std::string ClearAllSpace(string str)
{
    int index = 0;
    if (!str.empty())
    {
        while ((index = str.find(' ', index)) != std::string::npos) {
            str.erase(index, 1);
        }
    }
    return str;
}
int main()
{
    char *c = new char[N];
    char *d = new char[N];
 
    do
    {
        int a = 0, b = 0;
        cout << "请输入两个二进制乘数,可以加空格,输入quit退出程序" << endl;
        cout << "A=";
        cin.getline(c,N,'\n');
 
        if(strcmp(c, "quit") == 0)
            break;
 
        cout << "B=";
        cin.getline(d,N,'\n');
        if(strcmp(d, "quit") == 0)
            break;
 
        a = Lengh(c);
        b = Lengh(d);
 
        if(a != b || a==0 || b==0)
        {
            cout << "输入的二进制数不正确,或者是两数长度不匹配" << endl;
            continue;
        }
        strcpy(c,ClearAllSpace(c).c_str());
        strcpy(d,ClearAllSpace(d).c_str());
        cout<<"c="<<c<<",d="<<d<<endl;
        a = a - 1;
        mul(c,d,a);
        cout<<"\n================="<<endl;
    }
    while(true);
    return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632