C补强阶段作业之欧阳术创编.docx

上传人:b****3 文档编号:3886869 上传时间:2023-05-06 格式:DOCX 页数:20 大小:21.43KB
下载 相关 举报
C补强阶段作业之欧阳术创编.docx_第1页
第1页 / 共20页
C补强阶段作业之欧阳术创编.docx_第2页
第2页 / 共20页
C补强阶段作业之欧阳术创编.docx_第3页
第3页 / 共20页
C补强阶段作业之欧阳术创编.docx_第4页
第4页 / 共20页
C补强阶段作业之欧阳术创编.docx_第5页
第5页 / 共20页
C补强阶段作业之欧阳术创编.docx_第6页
第6页 / 共20页
C补强阶段作业之欧阳术创编.docx_第7页
第7页 / 共20页
C补强阶段作业之欧阳术创编.docx_第8页
第8页 / 共20页
C补强阶段作业之欧阳术创编.docx_第9页
第9页 / 共20页
C补强阶段作业之欧阳术创编.docx_第10页
第10页 / 共20页
C补强阶段作业之欧阳术创编.docx_第11页
第11页 / 共20页
C补强阶段作业之欧阳术创编.docx_第12页
第12页 / 共20页
C补强阶段作业之欧阳术创编.docx_第13页
第13页 / 共20页
C补强阶段作业之欧阳术创编.docx_第14页
第14页 / 共20页
C补强阶段作业之欧阳术创编.docx_第15页
第15页 / 共20页
C补强阶段作业之欧阳术创编.docx_第16页
第16页 / 共20页
C补强阶段作业之欧阳术创编.docx_第17页
第17页 / 共20页
C补强阶段作业之欧阳术创编.docx_第18页
第18页 / 共20页
C补强阶段作业之欧阳术创编.docx_第19页
第19页 / 共20页
C补强阶段作业之欧阳术创编.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C补强阶段作业之欧阳术创编.docx

《C补强阶段作业之欧阳术创编.docx》由会员分享,可在线阅读,更多相关《C补强阶段作业之欧阳术创编.docx(20页珍藏版)》请在冰点文库上搜索。

C补强阶段作业之欧阳术创编.docx

C补强阶段作业之欧阳术创编

编写一个函数。

函数的三个参数是一个字符和两个整数。

字符参数是需要输出的字符。

第1个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数。

编写一个调用该函数的程序。

要求:

输入字符为回车时,作为未输入字符处理;输入行列值时若不是数字,直接结束程序。

时间:

2021.02.02

创作:

欧阳术

编程考点:

循环基本语法;break和continue

#include

voidchLineRow(charch,intc,intr);

intmain(void){

charch;

intcol,row;

printf("Enteracharacter(#toquit):

");

while((ch=getchar())!

='#'){

if(ch=='\n')

continue;

printf("Enternumberofcolumnsandnumberofrows:

");

if(scanf("%d%d",&col,&row)!

=2)

break;

chLineRow(ch,col,row);

printf("\nEnternextcharacter(#toquit):

");

}

printf("Bye!

\n");

return0;

}

voidchLineRow(charch,intc,intr){

intcol,row;

for(row=0;row

for(col=0;col

putchar(ch);

putchar('\n');

}

return;

}

编写一个函数计算double类型数的某个整数次幂(不得调用系统的函数pow),注意0的任何次幂和任何数值的0次幂以及负数次幂

并编写测试程序

编程考点:

if-else的嵌套

#include

doublepower(doublea,intb);/*ANSIprototype*/

intmain(void){

doublex,xpow;

intn;

printf("Enteranumberandtheintegerpower");

printf("towhich\nthenumberwillberaised.Enterq");

printf("toquit.\n");

while(scanf("%lf%d",&x,&n)==2){

xpow=power(x,n);/*functioncall*/

printf("%.3gtothepower%dis%.5g\n",x,n,xpow);

printf("Enternextpairofnumbersorqtoquit.\n");

}

printf("Hopeyouenjoyedthispowertrip--bye!

\n");

return0;

}

doublepower(doublea,intb)/*functiondefinition*/

{

doublepow=1;

inti;

if(b==0){

//if(a==0)

//printf("0tothe0undefined;using1asthevalue\n");

pow=1.0;

}elseif(a==0)

pow=0.0;

elseif(b>0)

for(i=1;i<=b;i++)

pow*=a;

else

/*b<0*/

pow=1.0/power(a,-b);

returnpow;/*returnthevalueofpow*/

}

写一个计算降水量的程序,给定一个数组,记录的每年每月的降水量

constfloatrain[YRS][MONTHS]={

{10.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}

};

使用指针而不是使用下标进行计算,每年的降水总值、每年平均值,以及每月平均量

编程考点:

如何用指针定位二维数组的元素

#include

#defineMONTHS12/*numberofmonthsinayear*/

#defineYRS5/*numberofyearsofdata*/

intmain(void){

/*initializingrainfalldatafor1990-1994*/

constfloatrain[5][12]={{10.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}};

intyear,month;

floatsubtot,total;

printf("YEARRAINFALL(inches)\n");

for(year=0,total=0;year

for(month=0,subtot=0;month

subtot+=*(*(rain+year)+month);

printf("%5d%15.1f\n",1990+year,subtot);

total+=subtot;/*totalforallyears*/

}

printf("\nTheyearlyaverageis%.1finches.\n\n",total/YRS);

printf("MONTHLYAVERAGES:

\n\n");

printf("JanFebMarAprMayJunJulAugSepOct");

printf("NovDec\n");

for(month=0;month

for(year=0,subtot=0;year

subtot+=*(*(rain+year)+month);

printf("%4.1f",subtot/YRS);

}

printf("\n");

return0;

}

编写一个程序,提示用户输入3个数据集,每个数据集包括5个double值。

程序应当实现下列所有功能:

A.把输入信息存储到一个3*5的数组中

B.计算出每个数集(包含5个值)的平均值

C.计算所有数值的平均数

D.找出这15个数中的最大值

打印出结果

每个任务需要用一个单独的函数来实现。

对于任务B需要编写计算并返回一维数组平均值的函数,循环三次调用该函数来实现任务B。

对于任务C、D,函数应当把整个数组做为参数,并却完成任务B、C和D的函数应该向他的调用函数返回答案

(推荐使用变长数组,参考程序为变长数组实现)

编程考点:

循环和二维数组的熟练应用,以及数组作为参数的传递

#include

#defineROWS3

#defineCOLS5

voidstore(doublear[],intn);

doubleaverage2d(introws,intcols,doublear[rows][cols]);

doublemax2d(introws,intcols,doublear[rows][cols]);

voidshowarr2(introws,intcols,doublear[rows][cols]);

doubleaverage(constdoublear[],intn);

intmain(void){

doublestuff[ROWS][COLS];

introw;

for(row=0;row

printf("Enter%dnumbersforrow%d\n",COLS,row+1);

store(stuff[row],COLS);

}

printf("arraycontents:

\n");

showarr2(ROWS,COLS,stuff);

for(row=0;row

printf("averagevalueofrow%d=%g\n",row+1,average(stuff[row],

COLS));

printf("averagevalueofallrows=%g\n",average2d(ROWS,COLS,stuff));

printf("largestvalue=%g\n",max2d(ROWS,COLS,stuff));

printf("Bye!

\n");

return0;

}

voidstore(doublear[],intn){

inti;

for(i=0;i

printf("Entervalue#%d:

",i+1);

scanf("%lf",&ar[i]);

}

}

doubleaverage2d(introws,intcols,doublear[rows][cols]){

intr,c;

doublesum=0.0;

for(r=0;r

for(c=0;c

sum+=ar[r][c];

if(rows*cols>0)

returnsum/(rows*cols);

else

return0.0;

}

doublemax2d(introws,intcols,doublear[rows][cols]){

intr,c;

doublemax=ar[0][0];

for(r=0;r

for(c=0;c

if(max

max=ar[r][c];

returnmax;

}

voidshowarr2(introws,intcols,doublear[rows][cols]){

introw,col;

for(row=0;row

for(col=0;col

printf("%g",ar[row][col]);

putchar('\n');

}

}

doubleaverage(constdoublear[],intn){

inti;

doublesum=0.0;

for(i=0;i

sum+=ar[i];

if(n>0)

returnsum/n;

else

return0.0;

}

编写产生100个1-10的随机数的程序,并以降序排列

rand()为C的标准随机数产生函数

编程考点:

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

#include

#include

voidprint(constintarray[],intlimit);

voidsort(intarray[],intlimit);

#defineSIZE100

intmain(void){

inti;

intarr[SIZE];

for(i=0;i

arr[i]=rand()%10+1;

puts("initialarray");

print(arr,SIZE);

sort(arr,SIZE);

puts("\nsortedarray");

print(arr,SIZE);

return0;

}

/*sort.c--sortsanintegerarrayindecreasingorder*/

voidsort(intarray[],intlimit){

inttop,search,temp;

for(top=0;top

for(search=top+1;search

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

temp=array[search];

array[search]=array[top];

array[top]=temp;

}

}

/*print.c--printsanarray*/

voidprint(constintarray[],intlimit){

intindex;

for(index=0;index

printf("%2d",array[index]);

if(index%10==9)

putchar('\n');

}

if(index%10!

=0)

putchar('\n');

}

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

写一个简单的程序输出从一月开始到用户选择的月份的总天数(2月假设28天)

注意,用户输入不区分大小写

编程考点:

C库字符及字符串函数的初步使用;结构的初步使用

#include

#include

#include

structmonth{

charname[10];

charabbrev[4];

intdays;

intmonumb;

};

conststructmonthmonths[12]={{"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","Jul",31,7},{"August","Aug",31,8},{"September",

"Sep",30,9},{"October","Oct",31,10},{"November",

"Nov",30,11},{"December","Dec",31,12}};

intdays(char*m);

intmain(void){

charinput[20];

intdaytotal;

printf("Enterthenameofamonth:

");

while(gets(input)!

=NULL&&input[0]!

='\0'){

daytotal=days(input);

if(daytotal>0)

printf("Thereare%ddaysthrough%s.\n",daytotal,input);

else

printf("%sisnotvalidinput.\n",input);

printf("Nextmonth(emptylinetoquit):

");

}

puts("bye");

return0;

}

intdays(char*m){

inttotal=0;

intmon_num=0;

inti;

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

total=-1;

else{

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

for(i=1;m[i]!

='\0';i++)

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

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

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

mon_num=months[i].monumb;

break;

}

if(mon_num==0)

total=-1;

else

for(i=0;i

total+=months[i].days;

}

returntotal;

}

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

注意,移动数据量很大的单元来实现数组的排序,不可取。

可采用辅助的指针数组来实现

编程考点:

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

#include

#defineMAXTITL40

#defineMAXAUTL40

#defineMAXBKS100/*maximumnumberofbooks*/

structbook{/*setupbooktemplate*/

chartitle[MAXTITL];

charauthor[MAXAUTL];

floatvalue;

};

intmain(void)

{

structbooklibrary[MAXBKS];/*arrayofbookstructures*/

intcount=0;

intindex;

printf("Pleaseenterthebooktitle.\n");

printf("Press[enter]atthestartofalinetostop.\n");

while(count

=NULL

&&library[count].title[0]!

='\0')

{

printf("Nowentertheauthor.\n");

gets(library[count].author);

printf("Nowenterthevalue.\n");

scanf("%f",&library[count++].value);

while(getchar()!

='\n')

continue;/*clearinputline*/

if(count

printf("Enterthenexttitle.\n");

}

if(count>0)

{

printf("Hereisthelistofyourbooks:

\n");

for(index=0;index

printf("%sby%s:

$%.2f\n",library[index].title,

library[index].author,library[index].value);

}

else

printf("Nobooks?

Toobad.\n");

return0;

}

参考答案代码

#include

#include

#defineMAXTITL40

#defineMAXAUTL40

#defineMAXBKS100/*maximumnumberofbooks*/

structbook{/*setupbooktemplate*/

chartitle[MAXTITL];

charauthor[MAXAUTL];

floatvalue;

};

voidsortt(structbook*pb[],intn);

voidsortv(structbook*pb[],intn);

intmain(void){

structbooklibrary[MAXBKS];/*arrayofbookstructures*/

structbook*pbk[MAXBKS];/*pointersforsorting*/

intcount=0;

intindex;

printf("Pleaseenterthebooktitle.\n");

printf("Press[enter]atthestartofalinetostop.\n");

while(count

=NULL

&&library[count].title[0]!

='\0'){

printf("Nowentertheauthor.\n");

gets(library[count].author);

printf("Nowenterthevalue.\n");

scanf("%f",&library[count].value);

pbk[count]=

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2