2013-03-07 90 views
-1

我必須完成C.C函數返回兩個值

編寫要求用戶輸入兩個正整數的函數分配如下,讀取這兩個數字,說A和B,以及一直詢問他們,直到用戶輸入兩個這樣的號碼。該函數將這兩個數字返回到它被調用的地方。

我在這裏有點困惑。我將如何要求用戶從一個函數中輸入兩個值?你不能只從main()函數做到這一點嗎?截至目前,我有以下功能代碼。它工作正常,但當然我需要在外部功能。

#include <stdio.h> 

int main() { 

int a(2); // initialize just as some positive number so as not to set off negative alert. 
int b(2); 
printf("Enter two positive numbers: \nFirst: "); 
do { 
    if (a <= 0 || b <= 0) { // negative alert 
     printf("Woops. Those are negative. Try again. \nFirst: "); 
    } 
    scanf(" %d", &a); 
    printf("Second: "); 
    scanf(" %d", &b); 
    printf("\n"); 
} while (a <= 0 || b <= 0); 

return(0); 
} 
+1

在這裏混合C&C++,它看起來像...... – 2013-03-07 17:52:59

+0

函數是一個函數。沒有理由你不能在你想要的任何功能上做到這一點。 – 2013-03-07 17:53:39

+0

爲什麼你不能把這個代碼放在一個不同的函數中?我不確定我是否理解這個問題... – 2013-03-07 17:53:45

回答

1

一個把戲,沒有提到任何一個是另一種方式返回多個值從一個函數是通過指針作爲參數。做這個工作的一個常見功能是scanf函數:

int x,y; 
scanf("%d %d", &x, &y); 

您可以排序查看此碼作爲scanf函數returninf兩個值並將其分配給x和y。

+0

嗯。有趣的點 – codedude 2013-03-07 18:10:41

+0

你的函數聲明可能是'int myfunc(int * a,int * b){...}'。你的電話可能看起來像:if(!myfunc(&i,&j)){... fail ...} else {... success ...} – paulsm4 2013-03-07 20:47:08

1

main的唯一特別之處在於它是您的應用程序的入口點。只要你想,你可以打電話給你想要的任何東西。一旦指令指針在你的入口點碰到第一條指令,它就是從這裏開始的一系列操作碼。除了跳躍之外,你還有「功能」的事實並沒有什麼特別之處。你也可以將它們全部內聯。

強加於代碼成另一種方法僅有差別在傳遞和返回信息:

/* this signature will change if you need to pass/return information */ 
void work() 
{ 
    int a = 2; /* did you really mean C++? */ 
    int b = 2; 
    printf("Enter two positive numbers: \nFirst: "); 
    do { 
     if (a <= 0 || b <= 0) { /* negative alert */ 
      printf("Woops. Those are negative. Try again. \nFirst: "); 
     } 

     scanf(" %d", &a); 
     printf("Second: "); 
     scanf(" %d", &b); 
     printf("\n"); 
    } while (a <= 0 || b <= 0); 
} 

調用像這樣:

int main(int argc, char **argv) 
{ 
    work(); /* assuming it is defined or declared above us */ 

    return 0; 
} 

1.對於「無所謂」的合理的定義和「每當」。

+1

該任務讓我「將兩個號碼都返回到它被呼叫的地方。」 void函數不返回任何東西嗎? – codedude 2013-03-07 17:58:13

+0

我不一定會爲你完成你的任務,但你是對的。目前我沒有返回任何東西。如果你願意,你當然可以改變「工作」的簽名來這樣做。你似乎正處在正確的軌道上,看到你如何意識到「無效」並不能達到你想要的。 – user7116 2013-03-07 18:00:20

+0

哈哈,我沒想到你會爲我做我的功課。 :)感謝您的幫助,但! – codedude 2013-03-07 18:01:38

2

cc++中的函數(方法在oop中)只能返回一個值。使用的同時擁有值的結構,並從你的函數返回它

#include<stdio.h> 
#include<stdlib.h> 

typedef struct two_ints { 
    int a, b; 
} two_ints_t; 

two_ints_t read_two_ints(); 

two_ints_t read_two_ints() { 
    two_ints_t two_ints; 
    two_ints.a = 0; 
    two_ints.b = 0; 
    char tmp[32] = ""; 
    printf("Enter two positive numbers: \nFirst: "); 
    do { 
     scanf(" %s", tmp); 
     two_ints.a = atoi(tmp); 
     printf("Second: "); 
     scanf(" %s", tmp); 
     two_ints.b = atoi(tmp); 
     printf("\n"); 
     if (two_ints.a <= 0 || two_ints.b <= 0) { // negative alert 
      printf("Woops. Those are negative. Try again. \nFirst: "); 
     } 
    } while (two_ints.a <= 0 || two_ints.b <= 0); 

    return two_ints; 
} 

int main() { 
    two_ints_t two_ints = read_two_ints(); 
    printf("a=%i, b=%i\n", two_ints.a, two_ints.b); 
    return 0; 
}