繁体   English   中英

C / C ++多重扫描功能合而为一

[英]C/C++ multiple scanf in one function

如何制作一个从控制台读取一些值然后返回它们的函数? 我的意思是在另一个函数中不是单个scanf ,不是多个scanf ,然后返回值。

例如:

int main(){
    write();
}

int write(void){
    int a,b;
    printf("Enter an int");
    scanf("%d",&a);
    printf("Enter another int");
    scanf("%d",&b)
    return a,b;
}

我知道C不是C++ 我只想要一个C++的例子。 这是我制作的完整程序。 我问这个问题是因为我想优化代码。 我在程序中实现了上述功能,但是我没有想到仅使用指针就可以使它起作用。

#include <stdio.h>
#include <stdlib.h>
#define MAX 32

void read(int*x,int*y);

void write(int m[][MAX],int x,int y);

void display(int sir[][MAX],int x,int y);

int main(){
    int x,y,m,n,a,b,i,k;
    int matrice1[MAX][MAX]={0},matrice2[MAX][MAX]={0},result[MAX][MAX]={0},result2[MAX][MAX]={0};
    read(&x,&y); //this is what i want to not use direct addreses if is possible
    write(matrice1,x,y);
    m=x;
    n=y;
    read(&x,&y);
    write(matrice2,x,y);
    system("cls");
    printf("Prima matrice: \n");
    for(i=0;i<m;i++){
        for(k=0;k<n;k++){
            printf(" %d",matrice1[i][k]);
        }
        printf("\n");
    }
    display(matrice1,m,n);
    printf("\nA doua matrice: \n");
    for(i=0;i<x;i++){
        for(k=0;k<y;k++){
            printf(" %d",matrice2[i][k]);
        }
        printf("\n");
    }
    display(matrice2,x,y);
    printf("\nSuma matricelor: \n");
    if(x>m){
        a=x;
    }
    else
        a=m;
    if(y>n){
        b=y;
    }
    else
        b=n;
    for(i=0;i<a;i++){
        for(k=0;k<b;k++){
            result[i][k]=matrice1[i][k]+matrice2[i][k];
            printf(" %d",result[i][k]);
            if(matrice1[i][k]%2!=0 && matrice2[i][k]%2!=0){
                result2[i][k]=matrice1[i][k]+matrice2[i][k];
            }
        }
        printf("\n");
    }
    printf("\nSuma matricelor impare<doar daca ambele sunt impare>: \n");
    for(i=0;i<a;i++){
        for(k=0;k<b;k++){
            printf(" %d",result2[i][k]);
        }
        printf("\n");
    }
    return 0;
}

void write(int m[][MAX],int x,int y){
    int i,k;
    for(i=0;i<x;i++){
        for(k=0;k<y;k++){
            printf("elementul de pe linia %d, coloana %d: ",i,k);
            scanf("%d",&m[i][k]);
        }
    }
}

void read(int*x,int*y){
    printf("\nIntroduceti numarul de randuri a matricei: ");
    scanf("%d",x);
    printf("Introduceti numarul de coloane a matricei: ");
    scanf("%d",y);
    printf("\nIntroduceti elementele matricei.\n");
}

void display(int sir[][MAX],int x,int y){
    int i,k;
    for(i=0;i<x;i++){
        for(k=0;k<y;k++){
            if(sir[i][k]%2==0)
                printf("numar par, pozitia %d,%d: %d\n",i,k,sir[i][k]);
        }
    }
}

请注意,C和C ++是不同的语言,因此代码也将不同。

对于C,我建议采用以下方法:

int readInts(int intArr[], size_t maxInts)
{
    int i = 0;
    while (i < maxInts && scanf("%d", &intArr[i]) == 1) { i++; }
    return i;
}

这样称呼它:

int myInts[100];
numInts = readInts(myInts, sizeof(myInts) / sizeof(myInts[0]);


在C ++中,应尽可能避免手动进行内存管理,因此我可以这样做:

std::vector<int> readInts()
{
    int x;
    std::vector<int> result;
    while (std::cin >> x)
    {
        result.push_back(x);
    }

    return result;
}

C和C ++函数都不能返回多个值。 没关系,因为C和C ++都不支持多重赋值。

使用C,您有以下选择:

  1. 如果所有对象都是同一类型并且在逻辑上相关(例如,等级,温度等的列表),则将一个数组作为函数参数传递,并写入该数组的元素:
     void foo( int *arr, size_t arrSize ) { ... scanf( "%d", &arr[i] ); ... } 
    这将被称为:
     int values[N]; ... foo( values, N ); 
    或者,您可以让函数动态分配一个内存块来保存其输入,然后将指针返回到该分配的块:
     /** * Stores inputs to a dynamically-allocated block of memory. * * Outputs: * * arrSize - Number of array elements allocated * arrCount - Number of array elements assigned */ int *foo( size_t *arrSize, size_t *arrCount ) { *arrSize = INITIAL_SIZE; // some size that covers most of your use cases *arrCount = 0; /** * Allocate the array */ int *arr = malloc( sizeof *arr * *arrSize ); if ( !arr ) // allocation failed, handle as appropriate int val; while ( scanf( "%d", &val ) == 1 ) { /** * If we've filled the array, extend it by doubling its size */ if ( *arrCount == *arrSize ) { int *tmp = realloc( arr, sizeof *arr * (*arrSize * 2) ); if ( tmp ) { arr = tmp; *arrSize *= 2; } else { // failed to extend the array, handle as appropriate } } arr[(*arrCount)++] = val; } return arr; } 
    这将被称为:
     size_t size = 0, count = 0; int *values = foo( &size, &count ); ... free( values ); // need to release the memory when you're done with it 
    请注意,C函数不能返回数组类型的对象。 也就是说,您不能执行以下操作:
     int foo(void)[N] // illegal syntax { int arr[N]; ... return arr; } 
    您也不能返回与本地数组相对应的指针,因为一旦函数退出,该数组就不再存在:
     int *foo( void ) { int arr[N]; ... return arr; // pointer will be invalid after function exits } 
  2. 如果对象具有不同的类型,或者在逻辑上不属于同一组,则在参数列表中使用多个指针:
     void foo( int *p1, double *p2 ) { ... scanf( "%d", p1 ); ... scanf( "%lf", p2 ); ... } 
    并称其为
     int bar; double bletch; ... foo( &bar, &bletch ); 

C ++为您提供了一些不同的选择:

  1. 如果所有项目都具有相同的类型并且在逻辑上相关,则使用向量存储它们并返回向量:
     std::vector<int> foo( void ) { std::vector<int> arr; size_t i = 0; ... std::cin >> arr[i++]; ... return arr; } 
    并将其称为:
     std::vector<int> values = foo(); 
  2. 如果项目具有不同的类型或在逻辑上不相关,则对这些项目使用多个引用
     void foo( int& bar, double& bletch ) { ... std::cin >> bar; ... std::cin >> bletch; } 
    并称其为
     int x; double y; ... foo( x, y ); // no & operator since we're using references in the called function 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM