人人范文网 范文大全

编程题总结

发布时间:2020-03-02 05:16:58 来源:范文大全 收藏本文 下载本文 手机版

C作业汇总

1.short a,b=32767; /*short类型在内存中占2B*/ a=b+1; 问:a的值是多少?并分析原因。

2.有一4位数整数,假设用abcd表示,请把这个4位数的每个数位用表达式表示出来。 3.从键盘输入圆的半径r,计算并输出圆的面积s (要求:半径r定义为float型;圆周率定义为符号常量;面积s保留2位小数) #define PI 3.14159 #include void main() { float r,s; printf(\"请输入半径r:\\n\"); scanf(\"%f\",&r); s=PI*r*r; printf(\"面积是:%.2f\\n\",s); }

4.输入m>=3的正整数,判断m是否素数。画出算法流程图及NS图

5.有一函数:

x1 x y2x1 1x10

3x-11 x10 写一段程序,输入x,输出y值。

要求x,y声明为float类型,y保留2位小数。 #include void main() { float x,y; printf(\"请输入x的值:\\n\"); scanf(\"%f\",&x);

if(x

y=x; else

if(x

y=2*x-1;

else

y=3*x-11;

}

printf(\"y=%.2f\\n\",y);

x3x5x7x9,6.课后习题4.17(P159)利用泰勒级数sinxx计算sinx的3!5!7!9!值。要求最后一项的绝对值小于10,并统计出此时累加了多少项。 #include #include void main() { float x,sinx=0; float term;

/*记录每个项数*/ int n=1,count=0; /*count记录累加了多少项*/

printf(\"请输入x值(弧度):\\n\"); scanf(\"%f\",&x);

term=x/n; while(fabs(term)>1e-5)

/* while循环*/ {

sinx+=term;

count++;

n+=2;

term=-term*x*x/((n-1)*n); }

/* do

/*do while循环*/ {

sinx+=term;

count++;

n+=2;

term=-term*x*x/((n-1)*n); }while(fabs(term)>1e-5);

*/

printf(\"sin(%.2f)=%.4f\\n\",x,sinx); printf(\"一共累加了:%d项。\\n\",count); }

7.用牛顿迭代法求下面方程在1.5附近的根:

2x4x3x60 325

牛顿迭代公式:

x1x0f(x0)f(x0)

#include #include void main() { float x0,x1=1.5; float y1,y2;

/*y1记录f(x0),y2记录f(x0)的导数*/

do {

x0=x1;

y1=2*x0*x0*x0-4*x0*x0+3*x0-6;

y2=6*x0*x0-8*x0+3;

x1=x0-y1/y2; }while(fabs(x1-x0)>1e-5);

printf(\"the root is:%.2f\\n\",x1); }

8.写一函数,输入一个16进制整数,输出相应的10进制数。 例:从键盘输入2a,输出结果是42。

要求:若输入数据不合法,则输出提示信息。如输入了35g,输出“您输入的16进制数不合法!”。

#include #include void main() { char c; int sum=0;

printf(\"请输入一个16进制数字:\\n\");

while((c=getchar())!=\'\\n\') {

if(c>=\'0\' && c

sum=sum*16+c-\'0\';

else

if(c>=\'a\' && c

sum=sum*16+c-87;

else

if(c>=\'A\' && c

sum=sum*16+c-55;

else

{

printf(\"您输入的16进制不合法.\\n\");

exit(0);

} }

printf(\"相应的10进制数是:%d\\n\",sum); } 方法2:用字符串处理的方式 #include #include void main() { char str[20]; int i,sum=0;

printf(\"请输入一个16进制数字:\\n\"); gets(str);

for(i=0;str[i];i++) {

if(str[i]>=\'0\' && str[i]

sum=sum*16+str[i]-\'0\';

else

if(str[i]>=\'a\' && str[i]

sum=sum*16+str[i]-87;

else

if(str[i]>=\'A\' && str[i]

sum=sum*16+str[i]-55;

else

{

printf(\"您输入的16进制不合法.\\n\");

exit(0);

} }

printf(\"相应的10进制数是:%d\\n\",sum); } 方法3:用字符数组及指针处理的方式 #include #include void main()

{ char str[20],*p=str; int sum=0;

printf(\"请输入一个16进制数字:\\n\"); gets(p);

while(*p) {

if(*p>=\'0\' && *p

sum=sum*16+*p-\'0\';

else

if(*p>=\'a\' && *p

sum=sum*16+*p-87;

else

if(*p>=\'A\' && *p

sum=sum*16+*p-55;

else

{

printf(\"您输入的16进制不合法.\\n\");

exit(0);

}

p++; }

printf(\"相应的10进制数是:%d\\n\",sum); } 9.编写一个小函数,其功能是计算两个整数的平均值,该函数要在主函数中调用。

#include void main() { int x,y; float avg; float average(int,int); printf(\"输入x,y的值,用空格分隔两个数:\\n\"); scanf(\"%d%d\",&x,&y);

avg=average(x,y);

printf(\"%d,%d的平均值是:%.2f\\n\",x,y,avg); }

float average(int x,int y)

{ return (x+y)/2.0; }

10.有N(N用宏定义为符号常量)个元素的一维整型数组,该数组中各元素值从键盘随机输入。然后,将这个整型数组中的值逆序存放。例如,原来5个元素的顺序为

8、

1、

4、

6、5,逆序之后各元素的值是

5、

6、

4、

1、8 #define N 5 #include void main() { int a[N]; int i,t;

printf(\"输入%d个整数,用空格或回车分隔:\\n\",N); for(i=0;i

scanf(\"%d\",&a[i]);

printf(\"数组原来的值是:\\n\"); for(i=0;i

printf(\"%d\\t\",a[i]);

for(i=0;i

t=a[i];

a[i]=a[N-1-i];

a[N-1-i]=t; }

printf(\"\\n逆序之后数组的值是:\\n\"); for(i=0;i

printf(\"%d\\t\",a[i]);

printf(\"\\n\"); } 11.有N(N用宏定义为符号常量)个元素的一维整型数组,该数组中各元素值从键盘随机输入。然后,对该数组元素进行由小到大排序(要求,该功能用函数实现),输出数组中各元素值。最后,从键盘随机输入一个整数,并把该整数插入上述数组中(该功能用函数实现),使得插入该整数后的数组仍然有序,输出数组中各元素的值。 #define N 5 #include void main() { int a[N+1];

int i,x; void sort(int array[],int n); void insert(int array[],int n,int x);

printf(\"输入%d个整数,用空格或回车分隔:\\n\",N); for(i=0;i

scanf(\"%d\",&a[i]);

sort(a,N);

/*调用sort对数组进行排序*/

printf(\"\\n升序排序之后数组的值是:\\n\"); for(i=0;i

printf(\"%d

\",a[i]);

printf(\"\\n输入一个x值插入到数组中:\\n\"); scanf(\"%d\",&x);

insert(a,N,x);

printf(\"\\n插入%d之后数组的值是:\\n\",x); for(i=0;i

printf(\"%d

\",a[i]);

printf(\"\\n\"); }

void sort(int array[],int n) /*用选择法对数组array升序排序*/ { int i,j,t,min;

for(i=0;i

min=i;

for(j=i+1;j

if(array[j]

min=j;

if(min!=i)

{

t=array[i];

array[i]=array[min];

array[min]=t;

} } }

void insert(int array[],int n,int x) { int i,pos;

for(i=0;i

pos=i;

for(i=n-1;i>=pos;i--)

array[i+1]=array[i];

array[pos]=x; }

12.有一整型数组,N(N用宏定义为符号常量)个元素,该数组中各元素值从键盘随机输入。从键盘随机输入一个整数x,删除该数组中值与x相同的所有元素(该功能用函数实现),输出数组中各元素的值。 #define N 5 #include void main() { int a[N]; int i,x,n; int delet(int a[],int n,int x);

printf(\"输入%d个整数,用空格或回车分隔:\\n\",N); for(i=0;i

scanf(\"%d\",&a[i]);

printf(\"数组原来的值是:\\n\"); for(i=0;i

printf(\"%d\\t\",a[i]);

printf(\"\\n请输入要删除的值x:\\n\"); scanf(\"%d\",&x);

n=delet(a,N,x);

/*n值是删除与x相同的元素后,数组剩余元素的个数。*/

printf(\"删除%d之后数组的值是:\\n\",x); for(i=0;i

printf(\"%d

\",a[i]);

printf(\"\\n\");

}

int delet(int a[],int n,int x) { int i,j;

for(i=0,j=0;i

if(a[i]!=x)

a[j++]=a[i];

return j; }

13.从键盘随机输入一字符串,将所有ASCII值为偶数的字符输出。例如:输入abc123,输出结果是b2(因为b的ASCII值是98,2的ASCII值是50,其他字符的ASCII值都是奇数)

#include #define N 40 void main() { char str[N]; int i;

printf(\"输入字符串:\\n\"); gets(str);

printf(\"ASCII码是偶数的字符有:\"); for(i=0;str[i];i++)

if(str[i]%2==0) putchar(str[i]);

printf(\"\\n\"); }

14.从键盘输入两个字符串s1,s2,把s2连接到s1的末尾。不能用strcat函数 #include #define N 40 void main() { char str1[N],str2[N]; void mystrcat(char *p1,char *p2);

printf(\"输入两个字符串,输入回车键结束:\\n\"); gets(str1); gets(str2);

mystrcat(str1,str2);

printf(\"连接在一起的字符串是:\\n\"); puts(str1);

}

void mystrcat(char *p1,char *p2) { while(*p1) p1++; while(*p2)

*p1++=*p2++; *p1=\'\\0\'; } 15.从键盘输入一个字符串,把其中最长的单词输出。单词定义为连续的一串英文字母。如输入I am a student.输出结果为student #include void main()

{

char str[80]; int i,start,len=0,lenthest=0,lenstart=0; int word=0;

printf(\"input a string:\\n\"); gets(str);

for(i=0;str[i];i++) {

if(str[i]>=\'a\' && str[i]=\'A\'&&str[i]

if(!word)

{

word=1;

start=i;

}

else

{

len++;

}

else

if(word)

{

word=0;

}

} if(len>lenthest) { lenthest=len; lenstart=start; } len=0;

printf(\"the lenthest word is:\\n\"); for(i=lenstart;i

putchar(str[i]);

printf(\"\\n\"); }

16.有一整型数组,N(N用宏定义为符号常量)个元素,该数组中各元素值从键盘随机输入。编写函数calculate,计算出所有元素的最大值、最小值、平均值,结果在主函数中输出。 #define N 5 #include void main() { int a[N]; int i,max,min; float avg; void calculate(int a[],int *pmax,int *pmin,float *pavg);

printf(\"输入%d个整数,用空格或回车分隔:\\n\",N); for(i=0;i

scanf(\"%d\",&a[i]);

calculate(a,&max,&min,&avg);

printf(\"数组中所有元素的最大值、最小值、平均值分别是:\\n\"); printf(\"最大值max=%d,\\n\",max); if(word) if(len>lenthest) {

lenthest=len;

lenstart=start; }

printf(\"最小值min=%d,\\n\",min); printf(\"平均值avg=%.2f,\\n\",avg);

}

void calculate(int a[],int *pmax,int *pmin,float *pavg) { int i; int sum;

*pmax=*pmin=sum=a[0];

for(i=1;i

if(a[i]>*pmax)

*pmax=a[i];

if(a[i]

*pmin=a[i];

sum=sum+a[i]; }

*pavg=(float)sum/N; }

编程题总结

编程题小结

改错编程题

普通专升本考试编程题总结

c语言编程题

VB编程题(循环部分总结题目

描述性编程总结

c语言编程题答案

C语言期末考试编程题

VB编程题及答案

编程题总结
《编程题总结.doc》
将本文的Word文档下载到电脑,方便编辑。
推荐度:
点击下载文档
相关专题 编程题类型总结
点击下载本文文档