C/C++ 一个函数中的多个 scanf
C/C++ multiple scanf in one function
如何创建一个从控制台读取一些值然后 return 的函数?我的意思是不是单个 scanf
,另一个函数中的多个 scanf
,然后是 return 值。
例如:
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++ 函数都不能 return 多个值;这并不重要,因为 C 和 C++ 都不支持多重赋值。
对于 C,您有以下选择:
- 如果所有对象都是同一类型并且逻辑上相关(即等级、温度等的列表),传递一个数组作为函数参数,写入数组元素:
void foo( int *arr, size_t arrSize )
{
...
scanf( "%d", &arr[i] );
...
}
这将被称为:int values[N];
...
foo( values, N );
或者,您可以让函数动态分配一块内存来保存其输入,return 指向该分配块的指针:/**
* 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 函数不能 return 数组类型的对象;也就是说,你不能这样做:int foo(void)[N] // illegal syntax
{
int arr[N];
...
return arr;
}
你也不能 return 一个对应于本地数组的指针,因为一旦函数退出,数组就不再存在: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++ 为您提供了一些不同的选择:
- 如果所有项都具有相同的类型并且在逻辑上相关,则使用向量来存储它们并且return向量:
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
</pre>
如何创建一个从控制台读取一些值然后 return 的函数?我的意思是不是单个 scanf
,另一个函数中的多个 scanf
,然后是 return 值。
例如:
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++ 函数都不能 return 多个值;这并不重要,因为 C 和 C++ 都不支持多重赋值。
对于 C,您有以下选择:
- 如果所有对象都是同一类型并且逻辑上相关(即等级、温度等的列表),传递一个数组作为函数参数,写入数组元素:
这将被称为:void foo( int *arr, size_t arrSize ) { ... scanf( "%d", &arr[i] ); ... }
或者,您可以让函数动态分配一块内存来保存其输入,return 指向该分配块的指针: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; }
请注意,C 函数不能 return 数组类型的对象;也就是说,你不能这样做:size_t size = 0, count = 0; int *values = foo( &size, &count ); ... free( values ); // need to release the memory when you're done with it
你也不能 return 一个对应于本地数组的指针,因为一旦函数退出,数组就不再存在: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++ 为您提供了一些不同的选择:
- 如果所有项都具有相同的类型并且在逻辑上相关,则使用向量来存储它们并且return向量:
并将其命名为: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 </pre>