C补强阶段作业

上传人:沈*** 文档编号:46212318 上传时间:2021-12-11 格式:DOC 页数:15 大小:84KB
收藏 版权申诉 举报 下载
C补强阶段作业_第1页
第1页 / 共15页
C补强阶段作业_第2页
第2页 / 共15页
C补强阶段作业_第3页
第3页 / 共15页
资源描述:

《C补强阶段作业》由会员分享,可在线阅读,更多相关《C补强阶段作业(15页珍藏版)》请在装配图网上搜索。

1、编写一个函数。函数的三个参数是一个字符和两个整数。字符参数是需要输出的字符。第1个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数。编写一个调用该函数的程序。要求:输入字符为回车时,作为未输入字符处理;输入行列值时若不是数字,直接结束程序。编程考点:循环基本语法;break和continue#include <stdio.h>void chLineRow(char ch, int c, int r);int main(void) char ch;int col, row;printf("Enter a character (# to quit): &q

2、uot;);while ( (ch = getchar() != '#') if (ch = 'n')continue;printf("Enter number of columns and number of rows: ");if (scanf("%d %d", &col, &row) != 2)break;chLineRow(ch, col, row);printf("nEnter next character (# to quit): ");printf("Bye!n&

3、quot;);return 0;void chLineRow(char ch, int c, int r) int col, row;for (row = 0; row < r; row+) for (col = 0; col < c; col+)putchar(ch);putchar('n');return;编写一个函数计算double类型数的某个整数次幂(不得调用系统的函数pow), 注意0的任何次幂和任何数值的0次幂以及负数次幂并编写测试程序编程考点:if-else的嵌套#include <stdio.h>double power(double a

4、, int b); /* ANSI prototype */int main(void) double x, xpow;int n;printf("Enter a number and the integer power");printf(" to whichnthe number will be raised. Enter q");printf(" to quit.n");while (scanf("%lf%d", &x, &n) = 2) xpow = power(x, n); /* funct

5、ion call */printf("%.3g to the power %d is %.5gn", x, n, xpow);printf("Enter next pair of numbers or q to quit.n");printf("Hope you enjoyed this power trip - bye!n");return 0;double power(double a, int b) /* function definition */double pow = 1;int i;if (b = 0) /if (a =

6、 0)/printf("0 to the 0 undefined; using 1 as the valuen");pow = 1.0; else if (a = 0)pow = 0.0;else if (b > 0)for (i = 1; i <= b; i+)pow *= a;else/* b < 0 */pow = 1.0 / power(a, -b);return pow; /* return the value of pow */写一个计算降水量的程序,给定一个数组,记录的每年每月的降水量const float rainYRSMONTHS = 1

7、0.2, 8.1, 6.8, 4.2, 2.1, 1.8, 0.2, 0.3, 1.1, 2.3, 6.1, 7.4, 9.2, 9.8, 4.4, 3.3, 2.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 5.2, 6.6, 5.5, 3.8, 2.8, 1.6, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 4.2, 4.3, 4.3, 4.3, 3.0, 2.0, 1.0, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6, 8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.2 ;

8、使用指针而不是使用下标进行计算,每年的降水总值、每年平均值,以及每月平均量编程考点:如何用指针定位二维数组的元素#include <stdio.h>#define MONTHS 12 /* number of months in a year */#define YRS 5 /* number of years of data */int main(void) /* initializing rainfall data for 1990 - 1994 */const float rain512 = 10.2, 8.1, 6.8, 4.2, 2.1, 1.8, 0.2,0.3, 1.

9、1, 2.3, 6.1, 7.4 , 9.2, 9.8, 4.4, 3.3, 2.2, 0.8, 0.4,0.0, 0.6, 1.7, 4.3, 5.2 , 6.6, 5.5, 3.8, 2.8, 1.6, 0.2, 0.0,0.0, 0.0, 1.3, 2.6, 4.2 , 4.3, 4.3, 4.3, 3.0, 2.0, 1.0, 0.2,0.2, 0.4, 2.4, 3.5, 6.6 , 8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2,0.9, 0.3, 0.9, 1.4, 7.2 ;int year, month;float subtot, total;printf

10、(" YEAR RAINFALL (inches)n");for (year = 0, total = 0; year < YRS; year+) /* for each year, sum rainfall for each month */for (month = 0, subtot = 0; month < MONTHS; month+)subtot += *(*(rain + year) + month);printf("%5d %15.1fn", 1990 + year, subtot);total += subtot; /* to

11、tal for all years */printf("nThe yearly average is %.1f inches.nn", total/YRS);printf("MONTHLY AVERAGES:nn");printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");printf(" Nov Decn");for (month = 0; month < MONTHS; month+) /* for each month, sum rainfall over

12、 years */for (year = 0, subtot =0; year < YRS; year+)subtot += *(*(rain + year) + month);printf("%4.1f ", subtot/YRS);printf("n");return 0;编写一个程序,提示用户输入3个数据集,每个数据集包括5个double值。程序应当实现下列所有功能:A 把输入信息存储到一个3*5的数组中B 计算出每个数集(包含5个值)的平均值C 计算所有数值的平均数D 找出这15个数中的最大值打印出结果每个任务需要用一个单独的函数来实现。对

13、于任务B需要编写计算并返回一维数组平均值的函数,循环三次调用该函数来实现任务B。对于任务C、D,函数应当把整个数组做为参数,并却完成任务B、C和D的函数应该向他的调用函数返回答案(推荐使用变长数组,参考程序为变长数组实现)编程考点:循环和二维数组的熟练应用,以及数组作为参数的传递#include <stdio.h>#define ROWS 3#define COLS 5 void store(double ar, int n);double average2d(int rows, int cols, double arrowscols);double max2d(int rows,

14、 int cols, double arrowscols);void showarr2(int rows, int cols, double arrowscols);double average(const double ar, int n);int main(void) double stuffROWSCOLS;int row;for (row = 0; row < ROWS; row+) printf("Enter %d numbers for row %dn", COLS, row + 1);store(stuffrow, COLS);printf("

15、array contents:n");showarr2(ROWS, COLS, stuff);for (row = 0; row < ROWS; row+)printf("average value of row %d = %gn", row + 1, average(stuffrow,COLS);printf("average value of all rows = %gn", average2d(ROWS, COLS, stuff);printf("largest value = %gn", max2d(ROWS,

16、 COLS, stuff);printf("Bye!n");return 0;void store(double ar, int n) int i;for (i = 0; i < n; i+) printf("Enter value #%d: ", i + 1);scanf("%lf", &ari);double average2d(int rows, int cols, double arrowscols) int r, c;double sum = 0.0;for (r = 0; r < rows; r+)fo

17、r (c = 0; c < cols; c+)sum += arrc;if (rows * cols > 0)return sum / (rows * cols);elsereturn 0.0;double max2d(int rows, int cols, double arrowscols) int r, c;double max = ar00;for (r = 0; r < rows; r+)for (c = 0; c < cols; c+)if (max < arrc)max = arrc;return max;void showarr2(int rows

18、, int cols, double arrowscols) int row, col;for (row = 0; row < rows; row+) for (col = 0; col < cols; col+)printf("%g ", arrowcol);putchar('n');double average(const double ar, int n) int i;double sum = 0.0;for (i = 0; i < n; i+)sum += ari;if (n > 0)return sum / n;elsere

19、turn 0.0;编写产生100个1-10的随机数的程序,并以降序排列rand()为C的标准随机数产生函数编程考点:一维数组实现选择排序,嵌套循环的基本使用#include <stdio.h>#include <stdlib.h>void print(const int array, int limit);void sort(int array, int limit);#define SIZE 100int main(void) int i;int arrSIZE;for (i = 0; i < SIZE; i+)arri = rand() % 10 + 1;pu

20、ts("initial array");print(arr, SIZE);sort(arr, SIZE);puts("nsorted array");print(arr, SIZE);return 0;/* sort.c - sorts an integer array in decreasing order */void sort(int array, int limit) int top, search, temp;for (top = 0; top < limit -1; top+)for (search = top + 1; search

21、< limit; search+)if (arraysearch > arraytop) temp = arraysearch;arraysearch = arraytop;arraytop = temp;/* print.c - prints an array */void print(const int array, int limit) int index;for (index = 0; index < limit; index+) printf("%2d ", arrayindex);if (index % 10 = 9)putchar('

22、n');if (index % 10 != 0)putchar('n');设计一个简单的结构用来存储 月份的全称、缩写、月号(1-12)、天数写一个简单的程序 输出从一月开始到用户选择的月份的总天数(2月假设28天)注意,用户输入不区分大小写编程考点:C库字符及字符串函数的初步使用;结构的初步使用#include <stdio.h>#include <string.h>#include <ctype.h>struct month char name10;char abbrev4;int days;int monumb;const st

23、ruct month months12 = "January", "Jan", 31, 1 , "February","Feb", 28, 2 , "March", "Mar", 31, 3 , "April", "Apr", 30, 4 , "May", "May", 31, 5 , "June", "Jun", 30, 6 , "July&qu

24、ot;, "Jul", 31, 7 , "August", "Aug", 31, 8 , "September","Sep", 30, 9 , "October", "Oct", 31, 10 , "November","Nov", 30, 11 , "December", "Dec", 31, 12 ;int days(char * m);int main(void) cha

25、r input20;int daytotal;printf("Enter the name of a month: ");while (gets(input) != NULL && input0 != '0') daytotal = days(input);if (daytotal > 0)printf("There are %d days through %s.n", daytotal, input);elseprintf("%s is not valid input.n", input);pr

26、intf("Next month (empty line to quit): ");puts("bye");return 0;int days(char * m) int total = 0;int mon_num = 0;int i;if (m0 = '0')total = -1;else m0 = toupper(m0);for (i = 1; mi != '0' i+)mi = tolower(mi);for (i = 0; i < 12; i+)if (strcmp(m, monthsi.name) = 0)

27、 mon_num = monthsi.monumb;break;if (mon_num = 0)total = -1;elsefor (i = 0; i < mon_num; i+)total +=monthsi.days;return total;修改下面的代码使他首先按照输入的顺序输出图书的信息,然后按照书名的字母升序输出图书的信息,就后按照value的值升序输出图书的信息,排序的工作由子函数完成。注意,移动数据量很大的单元来实现数组的排序,不可取。可采用辅助的指针数组来实现编程考点:指针数组;结构成员的两种引用方法(-> .)#include <stdio.h>#

28、define MAXTITL 40#define MAXAUTL 40#define MAXBKS 100 /* maximum number of books */struct book /* set up book template */ char titleMAXTITL; char authorMAXAUTL; float value;int main(void) struct book libraryMAXBKS; /* array of book structures */ int count = 0; int index; printf("Please enter th

29、e book title.n"); printf("Press enter at the start of a line to stop.n"); while (count < MAXBKS && gets(librarycount.title) != NULL && librarycount.title0 != '0') printf("Now enter the author.n"); gets(librarycount.author); printf("Now enter t

30、he value.n"); scanf("%f", &librarycount+.value); while (getchar() != 'n') continue; /* clear input line */ if (count < MAXBKS) printf("Enter the next title.n"); if (count > 0) printf("Here is the list of your books:n"); for (index = 0; index <

31、count; index+) printf("%s by %s: $%.2fn", libraryindex.title, libraryindex.author, libraryindex.value); else printf("No books? Too bad.n"); return 0;参考答案代码#include <stdio.h>#include <string.h>#define MAXTITL 40#define MAXAUTL 40#define MAXBKS 100 /* maximum number of

32、books */struct book /* set up book template */char titleMAXTITL;char authorMAXAUTL;float value;void sortt(struct book * pb, int n);void sortv(struct book * pb, int n);int main(void) struct book libraryMAXBKS; /* array of book structures */struct book * pbkMAXBKS; /* pointers for sorting */int count

33、= 0;int index;printf("Please enter the book title.n");printf("Press enter at the start of a line to stop.n");while (count < MAXBKS && gets(librarycount.title) != NULL&& librarycount.title0 != '0') printf("Now enter the author.n");gets(libraryc

34、ount.author);printf("Now enter the value.n");scanf("%f", &librarycount.value);pbkcount = &librarycount;count+;while (getchar() != 'n')continue; /* clear input line */if (count < MAXBKS)printf("Enter the next title.n");printf("Here is the list of

35、your books:n");for (index = 0; index < count; index+)printf("%s by %s: $%.2fn", libraryindex.title,libraryindex.author, libraryindex.value);printf("Here is the list of your books sorted by title:n");sortt(pbk, count);for (index = 0; index < count; index+)printf("%

36、s by %s: $%.2fn", pbkindex->title, pbkindex->author,pbkindex->value);sortv(pbk, count);printf("Here is the list of your books sorted by value:n");for (index = 0; index < count; index+)printf("%s by %s: $%.2fn", pbkindex->title, pbkindex->author,pbkindex->

37、;value);return 0;void sortt(struct book * pb, int n) int top, search;struct book * temp;for (top = 0; top < n -1; top+)for (search = top + 1; search < n; search+)if (strcmp(pbsearch->title, pbtop->title) < 0) temp = pbsearch;pbsearch = pbtop;pbtop = temp;void sortv(struct book * pb, i

38、nt n) int top, search;struct book * temp;for (top = 0; top < n -1; top+)for (search = top + 1; search < n; search+)if (pbsearch->value < pbtop->value) temp = pbsearch;pbsearch = pbtop;pbtop = temp;编写程序满足下列要求a) 定义一个name结构,它含有两个成员:一个字符串用于存放名字,另一个字符串用于存放姓氏b) 定义一个student结构,它含有3个成员:name结构,

39、一个存放3个浮点数的grade数组,以及一个存放这3个数平均值的变量c) 使用main()函数声明一个具有CSIZE(CSIZE=4)个student结构的数组,用来作为一个班级的数组,并随意初始化这些结构的名字部分,即:初始化一个班级数组里面学生的姓名。使用函数来执行d,e,f,g所描述的任务d) 请求输入学生的分数,以交互地获取每个学生的成绩。将分数放到相应结构的grade数组成员中。可以自主选择在main()或一个函数中实现这个循环e) 为每个结构计算平均分,并把平均值赋给合适的成员f) 输出每个结构的信息g) 输出每门课程的班级平均分编程考点:结构数组的使用#include <s

40、tdio.h>#include <string.h>#define LEN 14#define CSIZE 4#define SCORES 3struct name char firstLEN;char lastLEN;struct student struct name person;float scoresSCORES;float mean;void get_scores(struct student ar, int lim);void find_means(struct student ar, int lim);void show_class(const struct

41、student ar, int lim);void show_ave(const struct student ar, int lim);int main(void) struct student classCSIZE = "Flip", "Snide" , "Clare", "Voyans" , "Bingo", "Higgs" , "Fawn", "Hunter" ;get_scores(class, CSIZE);find_mea

42、ns(class, CSIZE);show_class(class, CSIZE);show_ave(class, CSIZE);return 0;void get_scores(struct student ar, int lim) int i, j;for (i = 0; i < lim; i+) printf("Please enter %d scores for %s %s:n", SCORES,ari.person.first, ari.person.last);for (j = 0; j < SCORES; j+) while (scanf(&quo

43、t;%f", &ari.scoresj) != 1) scanf("%*s");puts("Please use numeric input.");void find_means(struct student ar, int lim) int i, j;float sum;for (i = 0; i < lim; i+) for (sum = 0, j = 0; j < SCORES; j+)sum += ari.scoresj;ari.mean = sum / SCORES;void show_class(const st

44、ruct student ar, int lim) int i, j;char wholename2*LEN;for (i = 0; i < lim; i+) strcpy(wholename, ari.person.first);strcat(wholename, " ");strcat(wholename, ari.person.last);printf("%27s: ", wholename);for (j = 0; j < SCORES; j+)printf("%6.1f ", ari.scoresj);print

45、f(" Average = %5.2fn", ari.mean);void show_ave(const struct student ar, int lim) int i, j;float total;printf("n%27s: ", "QUIZ AVERAGES");for (j = 0; j < SCORES; j+) for (total = 0, i = 0; i < lim; i+)total += ari.scoresj;printf("%6.2f ", total / lim);put

46、char('n');编写一个程序,用指向函数的指针数组执行菜单功能(程序中不得使用if-else语句和switch-case语句)。例如实现如下菜单编程考点:函数指针和函数指针数组#include <stdio.h>#include <math.h>double twice(double x);double half(double x);double thrice(double x);void showmenu(void);#define NUM 4int main(void) double (*pfNUM)(double) = twice, half,

47、 thrice, sqrt ;double val;double ans;int sel;printf("Enter a number (negative to quit): ");while (scanf("%lf", &val) && val >= 0) showmenu();while (scanf("%d", &sel) && sel >= 0 && sel <= 3) ans = (*pfsel)(val);printf("answe

48、r = %fn", ans);showmenu();printf("Enter next number (negative to quit): ");puts("bye");return 0;void showmenu(void) puts("Enter one of the following choices:");puts("0) double the value 1) halve the value");puts("2) triple the value 3) squareroot the value");puts("4) next number");double twice(double x) return 2.0 * x;double half(double x) return x / 2.0;double thrice(double x) return 3.0 * x;

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!