好文档 - 专业文书写作范文服务资料分享网站

C补强阶段作业

天下 分享 时间: 加入收藏 我要投稿 点赞

-------------------------------------------------------------------------------

}

}

putchar('\\n');

double average(const double ar[], int n) { }

for (i = 0; i < n; i++)

sum += ar[i]; return sum / n; return 0.0; if (n > 0) else int i;

double sum = 0.0;

编写产生100个1-10的随机数的程序,并以降序排列 rand()为C的标准随机数产生函数

编程考点:一维数组实现选择排序,嵌套循环的基本使用

#include #include

void print(const int array[], int limit); void sort(int array[], int limit);

#define SIZE 100 int main(void) { }

/* sort.c -- sorts an integer array in decreasing order */

return 0;

for (i = 0; i < SIZE; i++)

arr[i] = rand() % 10 + 1; puts(\); print(arr, SIZE); sort(arr, SIZE);

puts(\); print(arr, SIZE); int i;

int arr[SIZE];

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

void sort(int array[], int limit) { }

/* print.c -- prints an array */

void print(const int array[], int limit) { }

for (index = 0; index < limit; index++) { }

if (index % 10 != 0)

putchar('\\n');

printf(\, array[index]); if (index % 10 == 9)

putchar('\\n');

int index;

for (top = 0; top < limit -1; top++)

for (search = top + 1; search < limit; search++)

if (array[search] > array[top]) { }

temp = array[search]; array[search] = array[top]; array[top] = temp;

int top, search, temp;

设计一个简单的结构用来存储 月份的全称、缩写、月号(1-12)、天数

写一个简单的程序 输出从一月开始到用户选择的月份的总天数(2月假设28天) 注意,用户输入不区分大小写

编程考点:C库字符及字符串函数的初步使用;结构的初步使用

#include #include #include

struct month { };

char name[10]; char abbrev[4]; int days; int monumb;

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

const struct month months[12] = { { \, \, 31, 1 }, { \,

int days(char * m); int main(void) { }

int days(char * m) {

int total = 0; int mon_num = 0; int i;

if (m[0] == '\\0')

total = -1;

m[0] = toupper(m[0]);

for (i = 1; m[i] != '\\0'; i++)

m[i] = tolower(m[i]);

if (strcmp(m, months[i].name) == 0) { }

total = -1;

mon_num = months[i].monumb; break;

for (i = 0; i < 12; i++) else { return 0;

printf(\);

while (gets(input) != NULL && input[0] != '\\0') { }

puts(\);

daytotal = days(input); if (daytotal > 0)

printf(\, daytotal, input); printf(\, input); else

printf(\); char input[20]; int daytotal;

\, 28, 2 }, { \, \, 31, 3 }, { \, \, 30, \, \, 31, 5 }, { \, \, 30, 6 },

{ \, \, 31, 7 }, { \, \, 31, 8 }, { \,

\, 30, 9 }, { \, \, 31, 10 }, { \, \, 30, 11 }, { \, \, 31, 12 } };

4 }, {

if (mon_num == 0)

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

}

}

else

for (i = 0; i < mon_num; i++)

total +=months[i].days;

return total;

修改下面的代码使他首先按照输入的顺序输出图书的信息,然后按照书名的字母升序输出图书的信息,就后按照value的值升序输出图书的信息,排序的工作由子函数完成。

注意,移动数据量很大的单元来实现数组的排序,不可取。可采用辅助的指针数组来实现

编程考点:指针数组;结构成员的两种引用方法(-> .)

#include #define MAXTITL 40 #define MAXAUTL 40

#define MAXBKS 100 /* maximum number of books */

struct book { /* set up book template */ char title[MAXTITL]; char author[MAXAUTL]; float value; };

int main(void) {

struct book library[MAXBKS]; /* array of book structures */ int count = 0; int index;

printf(\);

printf(\); while (count < MAXBKS && gets(library[count].title) != NULL && library[count].title[0] != '\\0') {

printf(\); gets(library[count].author); printf(\); scanf(\, &library[count++].value); while (getchar() != '\\n')

continue; /* clear input line */ if (count < MAXBKS)

printf(\);

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

}

if (count > 0) {

printf(\); for (index = 0; index < count; index++)

printf(\, library[index].title, library[index].author, library[index].value); } else

printf(\);

return 0; }

参考答案代码

#include #include #define MAXTITL 40 #define MAXAUTL 40

#define MAXBKS 100 /* maximum number of books */ struct book { /* set up book template */ };

void sortt(struct book * pb[], int n); void sortv(struct book * pb[], int n);

int main(void) {

printf(\);

printf(\); while (count < MAXBKS && gets(library[count].title) != NULL

&& library[count].title[0] != '\\0') { printf(\); gets(library[count].author); printf(\); scanf(\, &library[count].value); pbk[count] = &library[count];

struct book library[MAXBKS]; /* array of book structures */ struct book * pbk[MAXBKS]; /* pointers for sorting */ int count = 0; int index;

char title[MAXTITL]; char author[MAXAUTL]; float value;

-------------------------------------------------------------------------------

03otr6v1h26d7jn4l8uv58u602x74s012pr
领取福利

微信扫码领取福利

微信扫码分享