0 like 0 dislike
2.7k views
下面是一個顏色的結構

struct Color {

   int r;

   int g;

   int b;

};

請使用顏色的結構實作以下函數。

void initColor(struct Color *c, int r, int g, int b);

struct Color averageColor(struct Color c[], int n);

double brightness(struct Color c);

void printColor(struct Color *c);

initColor 函數會將c所指向的 Color 結構初始化成 r, g, b的值。

averageColor 函數會分別計算陣列中 r, g, b, 的平均值,函後回傳一個由 r, g, b, 平均值所構成的 Color 結構。

brightness 會計算 c 的亮度,亮度 = ( r + g + b ) / 3。

printColor 會將 c 所指到的顏色結構以 (r, g, b) brightness 的形式印出。

這次作業請把結構的定義還有函數的定義放在標頭檔中,實作在另一個檔案裡。
[Exercise] Essay (Open question) - asked in 作業 by (18k points)
ID: 19753 - Available when: Unlimited - Due to: Unlimited
| 2.7k views

9 Answers

0 like 0 dislike
Hidden content!
#ifndef * ** ****
***** * * *** *
** * * * * * * ***
** ** * **
**** * * *** ** **


*** struct Color {
* * ** *** ** **** r;
* ** * * * * ** ** g;
* * ** * * b;
**



void * * * ** Color *c, int r, int g, int b);

struct Color * *** * Color c[], int n);

double * * * * Color c);

void * **** * Color *c);



#endif // * * * *


* ***


* ** * ** * * ** * *



void * * Color *c, int r, int g, int b)

{
*** * ** * * * *** ** *** = r;
** ** * * * * = g;
* *** * * ** *** ***** * ** = b;

}


** Color * *** ** * Color c[], int n)

{
** * ** ** *** *** *** **** i;
* * * **** * **** ** ** avg;
** * * ** ** * *** ** **** * * * ** i++){
** ** ** ** * *** * ** **** * ***** * * * += c[i].r;
** *** * **** * ** ***** * *** * * ** * *** * *** += c[i].g;
** * * * * ** * *** ** * * **** * * *** * ** ** += c[i].b;
** ** * * **** * ** * *
** * *** ** * * /= n;
** ** * * *** *** * * /= n;
******* ** * ***** * *** *** ** /= n;
* **** *** ** * * ** ***** avg;

}



double ** ** *** Color c)

{
* ** * ***** *** * * * * * ** **** *
** ** * ** *** ** * * += c.r;
* *** *** ** *** *** *** ****** += c.g;
* * ** * * * *** += c.b;
* ****** ** * * * *** * ****** /=3;
* * * * * * * * **** ** *** **

}



void * * * * Color *c)

{
* * * ** * * *** * temp;
** * * * ** ** * ** * * = ****
* ** **** ** ** ** *** *** * = *** ****
* *** ** ** * ** ** ** * * ** = ** * *
** **** * ****** *** **** * *** **** ** ** * *** * *

}
answered by (-248 points)
edited by
0 like 0 dislike
Hidden content!
#ifndef *** ** *** *
*** ** * *** * *
* * * * *** * ***
* *** * * ***
** * * ** **


*** * struct Color {
****** *** * **** r;
** * ****** ** * g;
*** ** ** * * b;
***



void ** * Color *c, int r, int g, int b);

struct Color * * *** ** Color c[], int n);

double *** *** ** Color c);

void ** * * * * Color *c);



#endif // ** ****


** *


* * ** * * **



void ** ** ** Color *c, int r, int g, int b)

{
*** * ** * *** ** = r;
* * * * ** * * * * = g;
* * *** *** * *** * **** **** = b;

}


**** Color ** *** ** Color c[], int n)

{
* * ******** * * * ** i;
** * *** * **** * avg;
* * ** ***** ***** * * ** i++){
** **** * * * * ** * * * *** * * * **** += c[i].r;
* * ** * * * ** * * *** ** ** * **** ** * += c[i].g;
* * ** *** **** ** ** * * ****** *** ** *** ** * ** += c[i].b;
****** * * ****** *
* **** **** * *** **** * * /= n;
* * * * ** * * * * /= n;
** ** ** ** ** ** * /= n;
* * ** * ** * ** * * avg;

}



double * * * Color c)

{
*** * ** *** * ***** * **
***** ***** **** * ** += c.r;
** * * ** **** * * * * * ** * += c.g;
*** * ** ** * *** * ***** * ** += c.b;
** * ** * **** * * ** ** /=3;
* * ***** ***** * ** **** *

}



void *** ** * Color *c)

{
*** ***** * *** * * * temp;
***** ** * ***** *** ** ** = * **
* * * * * * **** * ** * * = *
** * * *** * ***** *** *** = ** *
** * * * **** * * ** * * * * * *** * * ** * ** ** *

}
answered by (-248 points)
edited by
0 like 0 dislike
Hidden content!
*** * * ** *


* ***** ** * * ** **



int r,g,b,i;



double bright;



 



struct Color



{


* ** * * ** ** r;


**** **** *** *** g;


* * * * ** * * * b;



};



struct Color c[100];



 



struct Color_Real



{


* * ** *** r;


**** * ** * * g;


** * * **** * *** b;



};



 



void * * Color *c, int r, int g, int b);



struct Color_Real ** * **** ** Color *c, int n);



double ** ** Color *c);



void ** *** ** * Color *c);



 


** ** *



 



 



 



int main()



{


**** ** * *** * * ****** Color_Real cc;


* * ** ** * ********** * * n;


* * * **** *** *** * ** * ** * * * ****


* **** * ***** * ***** ** * * ** *


* * ** * * * * *** *** *


* ** ** * ** ** ** ******* *** ** * * * ** * *** * %d ****** * ** * * *


* * * * * * * * * *** * * * *** ** * * ** ** ** ** ** * r, g, b);


** ** * * *** * ** ** * * ******* ** **** * ** ** * * * = ** *** * * ** ** *


*** ** *** * * *** * * ****** ***** * ** * * ** * * ** * **



 


* * * *** ** **** *** **


** * * ****** * * * * ***** ** * ** * * ** * = * ** * *** * ** * **


* **** **** * * * ***** ** * * * * * * * ** * ** ***


* *** * * ** * * * ** ** *** * *** * ***** * **** *


* * * * ** ** ***** ** ** ** *** **** * ** ****** * **


***** *** * ** ** *** * 0;


****



}



 



void * * * * Color *c, int r, int g, int b)



{


**** * * *** * ** **** ** ** *


* * * **** ** * * * *** *** **** **


*** ** ** ** ** * * * * * ** * **



}



struct Color_Real *** * *** * Color *c, int n)



{


** *** ** * **** * * ** * * *



struct * **** *** * ******* ** cc;


** * ** *** *** ** ***


* * * *** *** *** ***


** ** *** ***** * ** *


* ** ** ** ** ** ** ** ** ** * **


** * ** **** * ***


*** * ** *** * * * * ** * * * * * ***


*** ** * ** ***** * * *** **** ** ** *** * ***


*** * **** * ** * *** * ** * ***** ** * **


** * ** *** * *** * * ****


* * * ** ***** * * ** * **


* ** ** *** * **** ** * * **


*** ** ** * * **** * * ** * *


** *** ** * ** ** * * cc;



}



double ** ******* Color *c)



{


** ** * *** * ** * bright;


** ***** * ***** *** * * ** * ** * *** * * * * *


** ** ** ** ** * * * **** ** ** bright;



}



void * * * * Color *c)



{


* **** * ** * **** **** **** * * * * * ****** *** * * * * ** * * * );



}
answered by (-162 points)
0 like 0 dislike
Hidden content!
#include <stdio.h>



typedef struct Color { int r;int g;int b;}Color;

void initColor(struct Color *c, int r, int g, int b)

{
* * * * ** ** ** *** ***
* * ** **** ** * *** *** *** * ***
*** ** *** ** * *** ***** ***
** * * ** ** * *** **** **
* ** *** **** * *

void averageColor(Color *c[], int n)

{
* ******* * * ****** ** *** ** ** averageColor;
* ** ** *** ** * ** * i=0;
** **** * * ** ***** * ** ** *

    {
*** * ***** * * ** *** * * ** ** * * * *** * *** **** * * * ** ** * *
** **** ******** * * * * ** * * * * * **** * ******* ** * * * **
*** * **** * * * * ******* ** ** **** * ** **** ** **
* * *** * * * * * **** * ** *** * **
* *** ***** * * * * **** * ** *
* * * ** *** ** * ** ** * * * * ***
* * * * * ** ** * ** **** *** * * **
*** *** ******* * * ** * * * ** * *
** ** ** ** ** ***** *** *** ** **
** * * ***** ** * * ** ** * *** ****
** ** *** * * * **



double brightness(Color c)

{
** ** * * *** * * bright;
* ** * *** * ***** * **** **
*** ** * ** * * * bright;

    };

void printColor(struct Color *c)

{
**** *** **** ** *** * * ** * ** * ***
* * ** * **** * **
answered by (-122 points)
0 like 0 dislike
Hidden content!
#include <stdio.h>

#include ** ** *** ***** * * *

int main(void)

{
* *** * * * ** * *** f,k,c[2];
** *** * ** ** ** ** * r1,g1,b1,r2,g2,b2;
** * ** **** * * ** ** *** * * ** f:r=%d",&r1);
* * * ** *** ** * * *** * * * * f:g=%d",&g1);
**** * **** ** * * * *** * * * * f:b=%d",&b1);
***** ** * *** * * * * * * ** ** *******
**** *** **** ** * * *** * * * ** * ** ** **
* * *** **** ** *** * ** ** ** k:g=%d",&g2);
* ** * * *** ** ** * * *** * ** * ** *** * *****
** *** * ** **** ** * ** ****** * * ** *
** * * **** * ** * **** *
* * * * *** ******** *** ***
* ** * * * ** * * * * ** *******
* * *** * **** *** * ** * * *** f:r=%d g=%d b=%d ",f.r,f.g,f.b);
* * * * * * ** *** * *** **** * ***
* **** *** ******** **** * *** * * *
** *** * ** * * * * ** *** ** * k:r=%d g=%d b=%d",k.r,k.g,k.b);
* * * *** ** * *** * * ** *** * * *
**** ** *** ** * *** *** * * *****
* * ** **** ** ***** ******* ** ** *** averageColor:r=%d g=%d **** * * * ** **
* * * *** * * ** ** * ** * ***** *


** * ** * * **** * **** 0;
* * * ** * *** *
answered by (-122 points)
0 like 0 dislike
Hidden content!
** ******* ** ** *
* * ** * ** **

int r,g,b,i;

double bright;



struct Color

{
* **** * * * ** * r;
* * ** * ** ** * * g;
********* ***** b;

};



Color c[100];



struct Color_Real

{
**** * * ** *** * ** ** *** r;
*** * ** * * ** g;
** *** * *** * b;

};



void initColor(struct Color *c, int r, int g, int b);

struct Color_Real averageColor(struct Color *c, int n);

double brightness(struct Color *c);

void printColor(struct Color *c);



 



 



int main()

{   
** *** *** * ******* Color_Real cc;
* * ** * ** ** ** *** * * n;
** *** ** * **** ** * ***** * * * * * ** *
* ** ** ** * * * * (i=0;i<n;i++)
** * **** * *** **** *
** ** *** * *** * * ** * * * * * ** ** * * %d * *** ** * * * * ***** ** *
* * * * * * ******* ** ******* *** **** * *** *** ** ** r, g, b);
***** **** ***** ** ****** ** * ** ** ******* = *** * ** **** ** ** *
* * ***** ** * * ** ** * * **** * * ******** * * * * ** **
* * **** *** *** * ** * ** * * * ***
* *** *** ** **** **
** ** * **** *** * * *** * ** * ** = averageColor(&c[0],n);
** * ** ** * ** ** ** ** * * * ***** * ** * * * * * ****
* * ** * * *** * ** ****** * ** ** ** * ** * ** *
*** * *** * * * ** **** * * ***** ** * * ** * * **
* **** * ** * ** * ****** * 0; * *** ** ** ** *
* ** * * * * * * *

}



void initColor(struct Color *c, int r, int g, int b)

{
***** *** * * * **** ***** * ** ** **
*** ***** **** * **** ** ** * ** *
* *** ** * *** * * * * *

}

struct Color_Real averageColor(struct Color *c, int n)

{
* * * ****** * *** * * i,sum1,sum2,sum3;
* * * ***** ** * * * cc;
* **** ** * ** ** * **
*** ** ** * ***** ** * * *
** * * **** ** ** *
**** * * * * ** ** * **** * ***
** * * ***** * *
* ** *** ** ** * * * * ** ** ** * *** * ***
* ** *** * * ** **** * * ** * * * **** * ***
*** * ********* ** *** * * ** **** * ****
****** * * **** **
** * *** ***** * * * ** ******* **
** *** * *** ** * ** *
* * * *** * *** * *** ****** * ***** * **
** ** * ** ** * * * cc;

}

double brightness(struct Color *c)

{
* * **** *** ** ** * bright;
**** * * ** * ***** * * * ** * * * * * ***** *** ** * *
**** ** * * * ***** * * ***** bright;

}

void printColor(struct Color *c)

{
* * ** *** *** * ** ** * * ****** ** * ** * * *** * ***** * * * ** brightness(c) );

}
answered by (-124 points)
0 like 0 dislike
Hidden content!
* *


*** * * ***** * *
* ***** * * ******
** ***** ** * * * * **


** Color{
* * * ** * *** r;
** * * *** * **** g;
*** ** * * ***** * b;

};



void *** * * Color *c, int r, int g, int b){
* * ** **** ** ** * * = r;
****** * *** ** * ** * = g;
* * ** *** * ** * ** = b;

}


** Color **** *** ** Color c[], int n){
****** **** ** * ** Color avg;
** ***** * * * i, ar, ag, ab;
* * ** * * ** s = 0, sr = 0, sg = 0, sb = 0;
* ** * ****** * * = 0; i * **** n; i++){
* ** * * ** * * * * ****** * **
******* * *** *
* ** * ** = sr / n;
*** ** *** * * ** **** = 0; i **** n; i++){
** ** * * * * * ** *** * * **
*** * * * * * *
*** ** ** ***** ** ** = sg / n;
*** * ** * * **** = 0; i ** n; i++){
** * **** * * ****** * ** * * *** *
*** * *** * * *****
*** * ** ** * ** = sb / n;


* ** * * * * = ar;
*** * * *** * * ****** = ag;
** * ** * ** = ab;


*** ***** * * *** * * ** **



};


**** * **** ** * Color c){
* **** * ** ***** (c.r + c.g +c.b) / 3;

}



void ** ** * Color *c){
* * ** ** **** ** * *** * *** * * * ** *** ** * * *** * * **

}


* **


* * *


* * ** ** ** *
* * ********* **** **



int main(){
** * ****** ** * * Color s={1, 1, 1};
**** * * * ** ** Color ** 1, 1},{2, 4, 6},{3, 9, 27}};
** * * * * * ** * Color a;
***** * * * * * * ** n;


* * * * * ** * ** * * *** *** * * ** * **
*** ****** **** * = * ** ** 3);
*** ** *** **** ** * * ** %d *** * ** a.r, a.g, a.b);
** * * *** * ** * *
**** ** * *** * ** * * *** ** ** ***** *
** ** ** * * * **** **** ** * 0, 0, 0);
** ****** * * * * * * * ****
* ** ******* *** *** **** *** * *** ****


* * 0;

}
answered by (-105 points)
0 like 0 dislike
Hidden content!
標頭檔:



#ifndef color_RGB

#define color_RGB

#include <stdio.h>

struct Color {


**** * ***** * * * r;


* * **** **** * g;


**** * * *** ** b;



};



void initColor(struct Color *c, int r, int g, int b)

{
*** * ** **** *** * * **
** * ** **** * *** ** ** * = r;


** * * * * * * * = g;
** ** * * *** * *
* *** * ** **** ** * * * * = b;
** * * ** *****

}



struct Color averageColor(struct Color c[], int n)

{
* ** ****** *** ***** * *
* **** * * * * ***** Color avg;
* * * **** ****
* *** ** * * ** * i,sr=0,sg=0,sb=0;
** * * *** * ** * * *
***** ** * * ** * (i = 0;i < n;i++){
* * * * **** *** *** ** * * ** ** * * *** ** *
* * *** ** * * ** **** * * * *** * ** += c[i].r;
* * * * * * * * * ** ** * ***** *** * * ** *** *
** * * ** * * * ** * * ** * **** ** * * += c[i].g;
**** * ** *** * ***** ** ** ** * * *
* * * ** *** * ** * *** * ********** * ** ** * * += c[i].b;
** *** * ** * ** * * * * ***** ** *** *
** **** * * * * ** *
** **** ** * * ****** *
** * ** * ***** * * = sr/n;
* * * * *** * ** **
* ** **** * * *** = sg/n;
* * **** *** * *
* * * ** * *** * * = sb/n;
* * ** ** * ** **
* * * **** ***** ** * * avg;



};



double brightness(struct Color c)

{


** ***** ** * * *** * bright;
* * *** * ** ** ** *** * *
** ****** * * ** * *** **** = (c.r + c.g + c.b) / 3;
** **** * ** ** *** **
****** ** *** * ** ** *** bright;
* ** * **** ** *** ****

}



void printColor(struct Color *c)

{
* ** ** ** *** * *** * *
* **** ** * * ** * * * ** * * * ("(%d,%d,%d) * ** * **** ** ** ** * * ** *
* ** * ** * * ** ** ** *

}



#endif



 



主程式:



#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include * ** * * *** ***** *



int main(void)

{
* * ** ** * * *** *
*** * ** * * * * i,n;
** * * ** ** **

// * **** * * * * * * *** * ** * * * *** ** *****
* * * ***** ** * * *
* ******** * ** *** * * ***** ** ** ***** **
** * ** * * ****
** *** * * *** * *** ** * Color c[n];
* **** * * * * * ******
** *** ** *** ** * (i = 0;i < n;i++){
**** * ** *** * * * *** ***** ** * * * *

// * * *** * * ********* ** * ** * ***** **** * * *** * ** * * * * * * *
* ** * ** **** *********** ****** ** ** *
* ***** ** * *** **** ** * * * *** * *** %d ** * * * * * ** ** * * * **** ** *
**** * *** * * ** *** ** * *** ** ***
** * ******* * * *
* **** *** * ** *
* * ***** * * *** ***** Color ave;
* *** * * ** * ** * *
* *** * *** ** = averageColor(c,n);
* ** ** * *** ** * * ****
** *** **** ** **** * ** * ** * *


* ** **** **** * * ** * * 0;



}
answered by (-157 points)
0 like 0 dislike
Hidden content!
#include <stdio.h>


***** *** ** * ****** color
**** * * ** * ******
** * * ******** * ** ** ***** ** r;
* **** ** ****** ***** * ** g;
** * * ** * * * * * * * * b;
*** * ** * * * **
** * * ******* * main(void)

   {
** **** ** * ** * * **** * * x;
* ** * **** * ***** * color c ;
**** * *** ** ** * * * ** * * *** ** **** **
* * * * * ***** *** ** * *** * *** *** * ** **
**** ** ****** *** ** ** *** * * ** *** *
* * * * ** = (c.r+c.g+c.b) / 3;
*** * ** *** * * * * ******** * *** * * **** * * ** ** *
* * ** *** * * *** ** 0;

   }
answered by (43 points)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.197.95
©2016-2026

Related questions

0 like 0 dislike
3 answers
[Exercise] Essay (Open question) - asked Dec 21, 2016 in 作業 by Shun-Po (18k points)
ID: 19755 - Available when: Unlimited - Due to: Unlimited
| 1.3k views
0 like 0 dislike
20 answers
[Exercise] Essay (Open question) - asked Dec 21, 2016 in 作業 by Shun-Po (18k points)
ID: 19748 - Available when: Unlimited - Due to: Unlimited
| 4.3k views
0 like 0 dislike
0 answers
[Exercise] Essay (Open question) - asked Dec 28, 2016 in 作業 by Shun-Po (18k points)
ID: 20140 - Available when: Unlimited - Due to: Unlimited
| 813 views
0 like 0 dislike
10 answers
[Exercise] Essay (Open question) - asked Dec 7, 2016 in 作業 by Shun-Po (18k points)
ID: 18876 - Available when: Unlimited - Due to: Unlimited
| 2.4k views
0 like 0 dislike
0 answers
[Exercise] Essay (Open question) - asked Dec 28, 2016 in 作業 by Shun-Po (18k points)
ID: 20139 - Available when: Unlimited - Due to: Unlimited
| 793 views
12,783 questions
183,442 answers
172,219 comments
4,824 users