程序设计文档格式.docx

上传人:b****6 文档编号:18097646 上传时间:2022-12-13 格式:DOCX 页数:14 大小:17.94KB
下载 相关 举报
程序设计文档格式.docx_第1页
第1页 / 共14页
程序设计文档格式.docx_第2页
第2页 / 共14页
程序设计文档格式.docx_第3页
第3页 / 共14页
程序设计文档格式.docx_第4页
第4页 / 共14页
程序设计文档格式.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

程序设计文档格式.docx

《程序设计文档格式.docx》由会员分享,可在线阅读,更多相关《程序设计文档格式.docx(14页珍藏版)》请在冰豆网上搜索。

程序设计文档格式.docx

(习题5.7)

应准备以下测试数据:

● 

 

要处理的数为1位正整数;

要处理的数为2位正整数;

要处理的数为3位正整数;

除此之外,程序还应当对不合法的输入作必要的处理。

例如:

输入负数;

输入的数超过3位(如1234)。

•#include<

math.h>

•voidmain()

•{

•intnum,indiv,ten,hundred,place;

•printf("

请输入一个整数(0-999):

"

);

•scanf("

%d"

num);

•if(num>

99)place=3;

•elseif(num>

9)place=2;

•elseplace=1;

位数:

%d\n"

place);

每位数字为:

•hundred=num/100;

•ten=(num-hundred*100)/10;

•indiv=(num-hundred*100-ten*10);

•switch(place)

•{case3:

printf("

%d,%d,%d\n"

hundred,ten,indiv);

反序数字为:

%d%d%d\n"

indiv,ten,hundred);

break;

•case2:

%d,%d\n"

ten,indiv);

•printf("

%d%d\n"

indiv,ten);

•case1:

indiv);

•}

•}

3.求输入的十个整数中正数的个数及其平均值.

Voidmain()

{inti,num=0,a;

intsum=0;

for(i=0;

i<

10;

i++)

{scanf("

a);

if(a<

=0)

continue;

num++;

sum+=a;

}

%dplusinteger'

ssum:

num,sum);

Meanvalue:

%6.2f\n"

sum/num);

•4.打印出以下图案。

•*****

•*****

•*****

{

inti,j,k;

for(i=0;

=4;

for(j=0;

j<

=3-i;

j++)

printf("

"

."

for(k=0;

k<

=2*i;

k++)

*****"

\n"

•做习题7.11

for(i=0;

i<

5;

{for(j=0;

i;

printf("

for(k=0;

*"

}

5.输入10个数并从中找出最大者.

#include<

{inta[10],i,max;

input10numbers:

a[i]);

max=a[0];

for(i=1;

{if(max<

a[i])max=a[i];

max=%d\n"

max);

6.用选择法(冒泡法)对10个整数排序。

10个整数用scanf函数输入。

冒泡法

{inta[10],i,j,t;

for(j=0;

9;

9-j;

if(a[i]>

a[i+1])

{t=a[i];

a[i]=a[i+1];

a[i+1]=t;

a[i]);

选择法:

inti,j,min,temp,a[10];

pleaseenter10integers:

scanf("

{

min=i;

for(j=i+1;

if(a[min]>

a[j])

min=j;

if(min!

=i)

{temp=a[min];

把值赋给a[i]

a[min]=a[i];

a[i]=temp;

}

printf("

printf("

7.用二分法查找数值

#defineN5

voidmain()

{inthigh,low,m,x,a[N]={-2,5,16,18,43};

pleaseinputx:

x);

low=0;

high=N-1;

while(low<

=high)

m=(low+high)/2;

if(x==a[m])

break;

elseif(x>

a[m])

low=m+1;

elsehigh=m-1;

if(low<

thepositionof%dis%d\n"

x,m+1);

elseprintf("

%dnotfound!

x);

8.有一个3*4的矩阵,要求编程序求出其中值最大的那个元素的值,以及其所在的行号和列号.(见教材139页)

{inti,j,row=0,colum=0,max;

inta[3][4]={{1,2,3,4},{9,8,7,6},{-10,10,-5,2}};

max=a[0][0];

3;

4;

if(a[i][j]>

max)

{max=a[i][j];

row=i;

colum=j;

max=%d,row=%d,colum=%d\n"

max,row,colum);

9将两个字符串连接起来,不要用strcat函数。

charstr1[80],str2[40];

inti=0,j=0;

inputstring1:

%s"

str1);

inputstring2:

str2);

while(str1[i]!

='

\0'

i++;

while(str2[j]!

{str1[i]=str2[j];

i++;

j++;

str1[i]='

;

\nthenewstringis:

%s\n"

10.找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小。

也可能没有鞍点。

(习题7.8)

#defineN4

#defineM5

inti,j,max,maxj,a[N][M],k,flag;

pleaseinputmatrix:

N;

M;

a[i][j]);

for(i=0;

max=a[i][0];

maxj=0;

for(j=0;

if(a[i][j]>

max=a[i][j];

maxj=j;

flag=1;

for(k=0;

if(max>

a[k][maxj])

{

flag=0;

continue;

if(flag)

{printf("

a[%d][%d]=%d\n"

i,maxj,max);

break;

if(!

flag)

Itisnotexist!

8.5写一函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。

string.h>

voidinverse(charstr[])

inti,j,t;

for(i=0,j=strlen(str);

strlen(str)/2;

i++,j--)

{t=str[i];

str[i]=str[j-1];

str[j-1]=t;

charstr[100];

pleaseinputastring:

scanf("

str);

inverse(str);

inversestringis:

例求三个数中最大数和最小数的差值

intdif(intx,inty,intz);

intmax(intx,inty,intz);

intmin(intx,inty,intz);

voidmain()

{inta,b,c,d;

%d%d%d"

c);

d=dif(a,b,c);

Max-Min=%d\n"

d);

intdif(intx,inty,intz)

{returnmax(x,y,z)-min(x,y,z);

intmax(intx,inty,intz)

{intr;

r=x>

y?

x:

y;

return(r>

z?

r:

z);

intmin(intx,inty,intz)

r=x<

return(r<

voidconcatenate(charstring1[],charstring2[],charstring[]);

chars1[100],s2[100],s[100];

s1);

s2);

concatenate(s1,s2,s);

\nThenewstringis%s\n"

s);

voidconcatenate(charstring1[],charstring2[],charstring[])

inti,j;

string1[i]!

=0;

string[i]=string1[i];

for(j=0;

string2[j]!

string[i+j]=string2[j];

string[i+j]='

•给出一个百分制成绩,要求输出成绩等级A、B、C、D、E。

90分以上为A,81-89分为B,70-79分为C,60-69分为D,60分以下为E。

(习题5.6)。

intscore,grade;

inputascore(0-100):

score);

grade=score/10;

switch(grade)

case10:

case9:

grade=A\n"

case8:

grade=B\n"

case7:

grade=C\n"

case6:

grade=D\n"

case5:

case4:

case3:

case2:

case1:

case0:

grade=E\n"

default:

Thescoreisoutofrange!

输入一行字符,分别统计出其中的英文字母、空格、数字和其它字符的个数。

(习题6.2)

charc;

intletter=0,space=0,digit=0,other=0;

请输入一行字符\n"

while((c=getchar())!

\n'

if(c>

a'

&

c<

z'

||c>

A'

Z'

letter++;

elseif(c='

'

space++;

else

0'

9'

digit++;

elseother++;

母数:

%d\n空格数:

%d\n数字数:

%d\n其它:

letter,space,digit,other);

猴子吃桃问题。

猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。

第二天早上又将剩下的桃子吃掉一半,又多吃了一个。

以后每天早上都吃了前一天剩下的一半零一个。

到第10天早上想再吃时,见只剩一个桃子了。

求第一天共摘了多少桃子。

(习题6.10)

intx1,x2,day;

x2=1;

for(day=9;

day>

0;

day--)

x1=(x2+1)*2;

x2=x1;

total=%d\n"

x1);

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

当前位置:首页 > 高等教育 > 艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1