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;yearfor(month=0,subtot=0;monthsubtot+=*(*(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;monthfor(year=0,subtot=0;yearsubtot+=*(*(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;rowprintf("Enter%dnumbersforrow%d\n",COLS,row+1);
store(stuff[row],COLS);
}
printf("arraycontents:
\n");
showarr2(ROWS,COLS,stuff);
for(row=0;rowprintf("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;iprintf("Entervalue#%d:
",i+1);
scanf("%lf",&ar[i]);
}
}
doubleaverage2d(introws,intcols,doublear[rows][cols]){
intr,c;
doublesum=0.0;
for(r=0;rfor(c=0;csum+=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;rfor(c=0;cif(maxmax=ar[r][c];
returnmax;
}
voidshowarr2(introws,intcols,doublear[rows][cols]){
introw,col;
for(row=0;rowfor(col=0;colprintf("%g",ar[row][col]);
putchar('\n');
}
}
doubleaverage(constdoublear[],intn){
inti;
doublesum=0.0;
for(i=0;isum+=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;iarr[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;topfor(search=top+1;searchif(array[search]>array[top]){
temp=array[search];
array[search]=array[top];
array[top]=temp;
}
}
/*print.c--printsanarray*/
voidprint(constintarray[],intlimit){
intindex;
for(index=0;indexprintf("%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;itotal+=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(countprintf("Enterthenexttitle.\n");
}
if(count>0)
{
printf("Hereisthelistofyourbooks:
\n");
for(index=0;indexprintf("%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]=