C语言高手请进!!!100分跪求一篇课程设计!!!急急急!!!可追加分!!!

2024年11月17日 21:33
有3个网友回答
网友(1):

哇哇,分好多,给你一个
barycenter.c
#include
#include
#include
#include

//#define NDEBUG
#include

#include "barycenter.h"

struct barycenter cal_barycenter(struct point **, const int);
static struct barycenter cal_tri(struct point *, struct point *, struct point *);
static struct barycenter new_barycenter(const struct barycenter *, const struct barycenter *);
static struct point ** convert_list_to_arr(struct point_list *, const int);

void delete_point_arr(struct point **, const int);

int write_file(const char *);
int copy_file(const char *, const char *);
int read_data(struct point ***, const char *);
int fexist(const char *);

/*
Main function
*/
int
main (int argc, char *argv[])
{
const char DATA_FILE_NAME[] = "data.bin";
const char TMP_FILE_NAME[] = "tmp.bin";
int ans, rval = -1;
int flag = fexist(DATA_FILE_NAME);

printf("Data file %s.\n", flag ? "exists" : "does not exist");
printf("Do you want to%senter data?(n):", flag ? " re-" : " ");

ans = getchar();

if(ans != '\n')
while(getchar() != '\n');

if(ans == 'y' || ans == 'Y')
{
if(!write_file(TMP_FILE_NAME))
return rval;
if(!copy_file(TMP_FILE_NAME, DATA_FILE_NAME))
return rval;
}
else if (!flag)
{
printf("Exit.\n");
return rval;
}

printf("Reading data from file...\n");

struct point ** point_arr = NULL;
int count;
struct barycenter result;

count = read_data(&point_arr, DATA_FILE_NAME);
if (count < 3)
{
printf("Not valid data.\n");
}
else
{
result = cal_barycenter(point_arr, count);
printf("Wait for the result...\n");
printf("The cg of Multi tri-angle is (%lf, %lf).\n", result.point.x, result.point.y);
rval = 0;
}

delete_point_arr(point_arr, count);
printf("Exit.\n");
return rval;
}

/*
Calculate the area and weight point of tri-angle.
a, b, c is the point.
return structure.
{
point;
weight;
}
*/
static struct barycenter cal_tri(struct point * a, struct point * b, struct point * c)
{
struct barycenter result;
memset(&result, 0, sizeof(struct barycenter));

result.area = (a -> x) * ((b -> y) - (c -> y)) + (b -> x) * ((c -> y) - (a -> y)) + (c -> x) * ((a -> y) - (b -> y));
result.point.x = (a -> x + b -> x + c -> x) / 3.0;
result.point.y = (a -> y + b -> y + c -> y) / 3.0;
return result;
}

/*
cal barycenter of multi tri-angle
*/
struct barycenter cal_barycenter(struct point * point_arr[], const int count)
{
int sub_mtri_count = (count + 1) / 2;
struct point ** sub_mtri = (struct point **)malloc(sizeof(struct point *) * sub_mtri_count);

assert(sub_mtri);

struct barycenter wpoint, sub_mtri_wpoint, tmp_wpoint;

memset(&wpoint, 0, sizeof(struct barycenter));
memset(&sub_mtri_wpoint, 0, sizeof(struct barycenter));
memset(&tmp_wpoint, 0, sizeof(struct barycenter));

int i,j;
for (i = 0, j = 0; i < count; i += 2, j++)
sub_mtri[j] = point_arr[i];

for (i = 0; i < count - 2; i += 2)
{
tmp_wpoint = cal_tri(point_arr[i], point_arr[i + 1], point_arr[i + 2]);
wpoint = new_barycenter(&wpoint, &tmp_wpoint);
}

if (i == count - 2)
{
tmp_wpoint = cal_tri(point_arr[i], point_arr[i + 1], point_arr[0]);
wpoint = new_barycenter(&wpoint, &tmp_wpoint);
}

if(sub_mtri_count >= 3)
{
sub_mtri_wpoint = cal_barycenter(sub_mtri, sub_mtri_count);
wpoint = new_barycenter(&wpoint, &sub_mtri_wpoint);
}

free(sub_mtri);

return wpoint;
}

/*
Cal new weight point and weight(area).
Cal point a and point b and return the
result.
*/
static struct barycenter new_barycenter(const struct barycenter * a, const struct barycenter * b)
{
struct barycenter result;
memset(&result, 0, sizeof(struct barycenter));

result.area = a -> area + b -> area;
result.point.x = (a -> area * a -> point.x + b -> area * b -> point.x) / result.area;
result.point.y = (a -> area * a -> point.y + b -> area * b -> point.y) / result.area;

return result;
}

/*
write ori data to file file_name.
The ori data is a seril of point
which form a shape multi-tri-angle.
*/
int write_file(const char * file_name)
{
FILE * fp = NULL;
fp = fopen(file_name, "wb");

if (fp == NULL)
{
perror("Can not write file.");
return 0;
}

int line = 0;

do
{
int i;
printf("How many lines does it have:");
scanf("%d", &line);
while(getchar() != '\n');

if(line >= 3)
{
struct point * head = (struct point *)malloc(sizeof(struct point) * line);
assert(head);
printf("Now enter the ZuoBiao.\n");
for (i = 0; i < line; i++)
{
printf("NO. %d:", i + 1);
scanf("%lf,%lf", &((head + i) -> x), &((head + i) -> y));
while(getchar() != '\n');
}

int len;
for (i = 0; i < line; i++)
{
len = fprintf(fp, "(%.20lf,%.20lf)\n", (head + i) -> x, (head + i) -> y);
if (len == -1)
{
free(head);
perror("Error writing file.");
return 0;
}
}

free(head);
}
else
printf("Not enough lines. Min is 3.\n");

}while (line < 3);

fflush(fp);
fclose(fp);

return 1;
}

/*
Copy source file to dest file.
*/
int copy_file(const char * src, const char * dst)
{
FILE * fp_src = NULL;
FILE * fp_dst = NULL;
int ch, rval = 1;

/*
Open file
*/
fp_src = fopen(src, "rb");
if(fp_src == NULL)
goto ERROR;
fp_dst = fopen(dst, "wb");
if(fp_dst == NULL)
goto ERROR;

/*
Copy data
*/
while ((ch = fgetc(fp_src)) != EOF)
{
if (fputc(ch, fp_dst) == EOF)
goto ERROR;
}

fflush(fp_dst);
goto EXIT;

ERROR:
rval = 0;
EXIT:
if (fp_src != NULL)
fclose(fp_src);
if (fp_dst != NULL)
fclose(fp_dst);
return rval;
}

/*
read ori data from file
return count of points and point array
In this function, due to unpredictable count of points, I used
a structure of chain.
*/
int read_data(struct point *** p_point_arr, const char * file_name)
{
FILE * fp = fopen(file_name, "rb");

if (fp == NULL)
{
perror("Can not open data file.");
return 0;
}

int count = 0;
int flag1 = 0, flag2 = 0;

struct point_list * plist = NULL;
struct point_list * head = NULL;

struct point ptemp;

do
{
flag1 = fscanf(fp, "(%lf,%lf)", &(ptemp.x), &(ptemp.y));
if (flag1 != EOF)
{
fprintf(stdout, "%lf,%lf\n", ptemp.x, ptemp.y);

do
{
flag2 = fgetc(fp);
}while(flag2 != '\n' && flag2 != EOF);

if (count == 0)
{
head = (struct point_list *)malloc(sizeof(struct point_list));
assert(head);

plist = head;
}
else
{
plist -> next = (struct point_list *)malloc(sizeof(struct point_list));
plist = plist -> next;

assert(plist);
}

plist -> next = NULL;
plist -> point = ptemp;

count++;
}

}while(flag1 != EOF && flag2 != EOF);

fclose(fp);

*p_point_arr = convert_list_to_arr(head, count);

return count;
}

/*
convert chain point list to point array
*/
static struct point ** convert_list_to_arr(struct point_list * plist, const int count)
{
struct point ** arr = (struct point **)malloc(sizeof(struct point *) * count);
assert(arr);

struct point_list * tmp = plist;

int i;
for (i = 0; i < count; i++)
{
arr[i] = (struct point *)malloc(sizeof(struct point));
*arr[i] = tmp -> point;
tmp = tmp -> next;
}

/*
free the ori chain point_list
*/
for (i = 0; i < count; i++)
{
tmp = plist -> next;
free(plist);
plist = tmp;
}

return arr;
}

/*
delete point array
*/
void delete_point_arr(struct point ** point_arr, const int count)
{
if (point_arr != NULL)
{
int i;
for (i = 0; i < count; i++)
if (point_arr[i] != NULL)
free(point_arr[i]);
free(point_arr);
}
}

/*
test if file exist.
*/
int fexist(const char * file_name)
{
FILE * fp = fopen(file_name, "rb");

if (fp == NULL)
return 0;
else
{
fclose(fp);
return 1;
}
}

barycenter.h

struct point
{
double x;
double y;
};

struct point_list
{
struct point point;
struct point_list * next;
};

struct barycenter
{
struct point point;
double area;
};

网友(2):

做个标记,我加你QQ了 ,发到你邮箱去了~~~

网友(3):

#include
main()
{
int num,indiv,ten,hundred,place;
scanf ("%d",&num);
hundred=(int)num/100;
ten=(int)(num-hundred*100)/10;
indiv=num-hundred*100-ten*10;
if(num>99) place=3;
else if(num>9) place=2;
else if(num>=0) place=1;
printf("__> %d\n",place); /*__> 无实际意义,只是为了指示%d更清楚*/
swith(place)
{
case 3 :printf("%d %d %d\n",hundred,ten,indiv);break;
case 2 :printf("%d %d\n",ten,indiv);break;
case 1 :printf("%d\n",indiv);break;
}

}