|
10鱼币
本帖最后由 忘我- 于 2014-8-23 18:46 编辑
刚学c不久,正在练习,
期间写了个小例子1,主要是运算矩形,圆,正方形的面积。
然后在main里面写了相关的调用,在图代码2,
之后又写了一个打印数组的小例子,在图代码1.
问题是,两个单独运行都不报错,单独就是注释掉其中一个。
一旦一起打开,就会因为代码1中的int len就是算数组长度报错,导致编译不过,
好郁闷,求教各位大神帮忙指点谢谢!!!!
main文件- #include <stdio.h>
- #include "MathShape.h"
- #include "Arrays.h"
- #include "Sort.h"
- int main(void)
- {
-
- //代码一
- int arr[] = {1, 4, 7 -1, 12, 8, 32, -2, 8, 9, 66, 55, 44, 77, 45, 32};
- int len = sizeof(arr)/sizeof(int);
- printIntArray(arr, len);
- printf("-----starting quickSorting-----\n");
- quickSortMine(arr, len, 0, len - 1);
- printf("-----ending quickSorting-----\n");
- printIntArray(arr, len);
- //代码二
- struct Shape sh;
- struct Shape* shape = &sh;
- shape -> type = Rectangle;
- printf("pls insert a value for 'rectangle-width'\n");
- scanf("%d",&shape -> shapeData.rectangle.width);
- printf("pls insert a value for 'rectangle-height'\n");
- scanf("%d",&shape -> shapeData.rectangle.height);
- double result = calculate(shape);
- printf("-----now the result of calculating Rectangle is %lf-----\n", result);
-
- // shape - circle
- shape -> type = Circle;
- printf("pls insert a value for 'circle-radius'\n");
- scanf("%d", &shape -> shapeData.circle.radius);
- // shape - circle : result
- printf("-----now the result of calculating Circle is %lf-----\n", calculate(shape));
-
- // shape - square
- shape -> type = Square;
- printf("pls insert a value for 'square-sides'\n");
- scanf("%d", &shape -> shapeData.square.sides);
- // shape - sqaure : result
- printf("-----now the result of calculating Square is %lf-----\n", calculate(shape));
-
- //other
- //
- return 0;
- }
复制代码
Array.h
- #ifndef ARRAYS_H
- #define ARRAYS_H
- void printIntArray(int arr[], int len);
- int isValid(int arr[], int len);
- #endif
复制代码
Arrays.c
- #include <stdio.h>
- #include "Arrays.h"
- int isValid(int arr[], int len)
- {
- return arr != NULL && len > 0;
- }
-
- void printIntArray(int arr[], int len)
- {
- if(!isValid(arr, len))
- {
- printf("Invalid Array Parameter!!!!");
- return;
- }
-
-
- int *arrP = arr;
- for(int i = 0; i < len; i++, arrP++)
- {
- if(i == 0) printf("[ ");
- printf("%d", *arrP);
- if(i < len - 1) printf(", ");
- if(i == len -1) printf("]\n");
- }
- }
复制代码
MathShape.h
- #ifndef MATH_SHAPE_H
- #define MATH_SHAPE_H
- #define PI 3.14
- #define NULL_INT -1
- enum ShapeType{ Rectangle, Circle, Square};
- struct Shape{
- enum ShapeType type;
- union{
- struct Rectangle{int width; int height;} rectangle;
- struct Circle{int radius;} circle;
- struct Square{int sides;} square;
- } shapeData;
- };
- double calculate(struct Shape* shape);
- double calculateCircle(int radius);
- double calculateSquare(int sides);
- double calculateRectangle(int width, int height);
- #endif
复制代码
MathShape.c
- #include "MathShape.h"
- double calculateRectangle(int width, int height)
- {
- return width * height;
- }
- double calculateCircle(int radius)
- {
- return PI * radius * radius;
- }
- double calculateSquare(int sides)
- {
- return sides * sides;
- }
- double calculate(struct Shape* shape)
- {
- const enum ShapeType type = shape -> type;
- if(type == Rectangle) return calculateRectangle(shape -> shapeData.rectangle.width, shape -> shapeData.rectangle.height);
- else if(type == Circle) return calculateCircle(shape -> shapeData.circle.radius);
- else if(type == Square) return calculateSquare(shape -> shapeData.square.sides);
- else return NULL_INT;
- }
复制代码
|
|