-2
提出一個新問題,導致我的老人在花費很長時間去編輯它後死亡。 所以我試圖擺脫這個代碼所有這些參數(是的,有些參數是多餘的,但我只是說他們櫃面)的:如何使用Structs?
#include <stdio.h>
int main()
{
//Top coord. of the square
int top_x1 = 0;
int top_y1 = 10;
int top_x2 = 10;
int top_y2 = 10;
//Bottom coord. of the square
int bottom_x1 = 0;
int bottom_y1 = 0;
int bottom_x2 = 10;
int bottom_y2 = 0;
//Left coord. of the square
int left_x1 = 0;
int left_y1 = 0;
int left_x2 = 0;
int left_y2 = 10;
//Right coord. of the square
int right_x1 = 10;
int right_y1 = 0;
int right_x2 = 10;
int right_y2 = 10;
parameter(top_x1, top_y1, top_x2, top_y2, bottom_x1,
bottom_y1, bottom_x2, bottom_y2, left_x1,
left_y1, left_x2, left_y2, right_x1, right_y1,
right_x2, right_y2);
}
parameter (int top_x1,int top_y1,int top_x2,int top_y2,int bottom_x1,
int bottom_y1,int bottom_x2,int bottom_y2,int left_x1,
int left_y1,int left_x2,int left_y2,int right_x1,int right_y1,
int right_x2,int right_y2)
{
int totalParameter, topSide, bottomSide, leftSide, rightSide;
topSide = (top_x2 - top_x1);
bottomSide = (bottom_x2 - bottom_x1);
leftSide = (left_y2 - left_y1);
rightSide = (right_y2 - right_y1);
totalParameter = (topSide + bottomSide + leftSide + rightSide);
printf("%d\n", totalParameter);
}
作品完美,但太多的參數!如果我嘗試使用結構...
#include <stdio.h>>
struct coordinates
{
int x1, y1, x2, y2;
};
int main()
{
struct coordinates top;
struct coordinates bottom;
struct coordinates left;
struct coordinates right;
//Top line of the square
top.x1 = 0;
top.y1 = 10;
top.x2 = 10;
top.y2 = 10;
//Bottom line of the square
bottom.x1 = 0;
bottom.y1 = 0;
bottom.x2 = 10;
bottom.y2 = 0;
//Left line of the square
left.x1 = 0;
left.y1 = 0;
left.x2 = 0;
left.y2 = 10;
//Right line of the square
right.x1 = 10;
right.y1 = 0;
right.x2 = 10;
right.y2 = 10;
parameter(top,bottom,left,right);
}
parameter(top, bottom, left, right)
{
int totalParameter, topSide, bottomSide, leftSide, rightSide;
topSide = (top.x2 - top.x1);
bottomSide = (bottom.x2 - bottom.x1);
leftSide = (left.y2 - left.y1);
rightSide = (right.y2 - right.y1);
totalParameter = topSide + bottomSide + leftSide + rightSide;
printf("%d\n", totalParameter);
}
我希望它打印出:40
不工作,雖然,任何幫助嗎? 錯誤我得到的是:「請給成員‘X1’的東西沒有工會的 結構對於所有的x和y COORDS
爲什麼在'參數'的簽名中沒有類型說明符? – XORcist
首先,在C語言中,您必須在使用它們之前聲明函數。其次,你的第一個變體用類型聲明瞭'parameter'函數的所有參數。在第二個變體中,您突然刪除了參數類型。你爲什麼刪除類型? – AnT