[英]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,您有以下選擇:
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 }
void foo( int *p1, double *p2 ) { ... scanf( "%d", p1 ); ... scanf( "%lf", p2 ); ... }
並稱其為 int bar; double bletch; ... foo( &bar, &bletch );
C ++為您提供了一些不同的選擇:
std::vector<int> foo( void ) { std::vector<int> arr; size_t i = 0; ... std::cin >> arr[i++]; ... return arr; }
並將其稱為: std::vector<int> values = foo();
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.