C语言上机作业集.docx
- 文档编号:29745172
- 上传时间:2023-07-26
- 格式:DOCX
- 页数:15
- 大小:28.85KB
C语言上机作业集.docx
《C语言上机作业集.docx》由会员分享,可在线阅读,更多相关《C语言上机作业集.docx(15页珍藏版)》请在冰豆网上搜索。
C语言上机作业集
一:
1.求圆周长,圆柱底面积,圆柱体积。
用scanf()函数输入圆的半径r和圆柱的高h,用printf()函数输出计算结果,输出时要有文字说明,取小数点后两位数字。
#include
#definePI3.1415
voidmain()
{
floatr,h,c,s,v;
printf("Pleaseinputtheradiusandheightofacylinder:
");
scanf("%f%f",&r,&h);
c=2*PI*r;
s=PI*r*r;
v=s*h;
printf("圆的周长C=%7.2f,S=%7.2f,V=%7.2f\n",c,s,v);
}
2.已知5位整数N,将组成这个整数的5个数码作为一个1位整数分别赋值5个变量n1、n2、n3、n4、n5。
比如59312分解后n1=5、n2=9、n3=3、n4=1、n5=2。
N从键盘输入。
#include
voidmain()
{
longN;
intn1,n2,n3,n4,n5;
printf("pleaseinputanintegerwithfivedigits:
");
scanf("%d",&N);
n1=N/10000;
n2=N/1000%10;
n3=N/100%10;
n4=N%100/10;
n5=N%10;
printf("thenumberN=%ld\n",N);
printf("n1=%d,n2=%d,n3=%d,n4=%d,n5=%d\n",n1,n2,n3,n4,n5);
}
二:
1.编程判断输入整数的正负性和奇偶性。
#include
voidmain()
{
inta;
printf("pleaseinputaninteger:
");
scanf("%d",&a);
if(a>0)
printf("thenumberispositive!
\n");
elseif(a<0)
printf("thenumberisnegative!
\n");
else
printf("thenumberisneitherpositivenornegative!
\n");
if(a/2==0)
printf("thenumberiseven!
\n");
else
printf("thenumberisodd!
\n");
}
2.有3个整数a、b、c,由键盘输入,输出其中最大的数。
#include
voidmain()
{
inta,b,c,max;
printf("pleaseinputthreeintegers:
");
scanf("%d%d%d",&a,&b,&c);
max=a;
if(max
max=b;
if(max max=c; printf("themaximumnumberismax=%d\n",max); } 3.分别使用if语句和switch语句,以10分为一段,分别输出实际成绩和所在分数段。 提示: 100~90为优,89~80为良,79~70为中,69~60为及格,<60为不及格 #include voidmain() { intscore; printf("pleaseinputthescore: "); scanf("%d",&score); if(score>=90) printf("成绩等级为优"); elseif(score>=80&&score<=89) printf("成绩等级为良"); elseif(score>=70&&score<=79) printf("成绩等级为中"); elseif(score>=60&&score<=69) printf("成绩等级为及格"); else printf("成绩等级为不及格"); } #include voidmain() { intscore,grade; printf("pleaseinputthescore: "); scanf("%d",&score); grade=score/10; switch(grade) { case10: case9: printf("成绩等级为优");break; case8: printf("成绩等级为良");break; case7: printf("成绩等级为中");break; case6: printf("成绩等级为及格");break; default: printf("成绩等级为不及格"); } } 三: 1.打印具有abcd=(ab+cd)2性质的全部四位数。 (while、dowhile循环结构练习) #include voidmain() { intn=1000,a,b; while(n<10000) { a=n/100; b=n%100; if(n==(a+b)*(a+b)) printf("%d\n",n); n++; } } #include voidmain() { intn=1000,a,b; do { a=n/100; b=n%100; if(n==(a+b)*(a+b)) printf("%d\n",n); n++; }while(n<10000); } 2.输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其中各位数字立方和等于该数本身。 例如153=13+53+33。 #include voidmain() { intn=100,a,b,c; while(n<1000) { a=n/100; b=n%100/10; c=n%10; if(n==a*a*a+b*b*b+c*c*c) printf("%d\n",n); n++; } } 3. 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少(求10000以内的数)? (提示: 如果一个数的平方根的平方等于该数,这就说明此数是完全平方数。 ) #include #include voidmain() { intn=1,a,b; while(n<10000) { a=sqrt(n+100); b=sqrt(n+268); if((n+100==a*a)&&(n+268==b*b)) printf("%d\n",n); n++; } } 四: 1.猴子吃桃问题。 猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。 第2天早上又将剩下的桃子吃了一半,又多吃一个。 以后每天早上都吃了前一天剩下的一半另加一个。 到第10天早上再想吃的时候,就只剩下1个桃子了。 求第1天共摘了多少桃子。 #include #include voidmain() { intday=10,total=1; for(day=10;day>=1;day--) total=(total+1)*2; printf("total=%d\n",total); } 2.搬砖问题: 有36块砖,由36人搬: 一男搬4块,1女搬3块,两个小孩抬一块,一次全部搬完。 问男、女、小孩人数各若干? #include #include voidmain() { intman,woman,child; for(man=0;man<9;man++) for(woman=0;woman<12;woman++) { child=36-man-woman; if((4*man+3*woman+child/2==36)&&(child%2==0)) printf("man=%d,woman=%d,child=%d\n",man,woman,child); } } 3.编程实现打印下列图形。 五: //1.一个一维数组中存放8个整数,编程将数组中的值按逆序排列。 #include voidmain() { inta[8],t,i,j; for(i=0;i<=7;i++) { printf("pleaseinputnumber%d: ",i+1); scanf("%d",&a[i]); } for(i=0,j=7;i { t=a[i]; a[i]=a[j]; a[j]=t; } printf("theresultarrayis: \n"); for(i=0;i<=7;i++) printf("%d",a[i]); } //2.将10个实数存储在一个数组中,设计一个程序输出这个数组的最大值和最小值。 #include voidmain() { floata[10],max,min; inti; for(i=0;i<=9;i++) { printf("pleaseinputnumber%d: ",i+1); scanf("%f",&a[i]); } max=a[0]; min=a[0]; for(i=1;i<=9;i++) {if(a[i] elseif(a[i]>max)max=a[i]; } printf("themaximumnumberis: %f\ntheminimumnumberis: %f\n",max,min); } //3.不用strcat()函数,实现将两个字符串连接。 #include #include voidmain() { charstr1[30],str2[30],str[60]; intlen1,len2,i; printf("pleaseinputstring1: "); gets(str1); printf("pleaseinputstring1: "); gets(str2); len1=strlen(str1); len2=strlen(str2); for(i=0;i str[i]=str1[i]; for(i=len1;i str[i]=str2[i-len1]; str[len1+len2]='\0'; puts(str); } //4.不用strcpy()函数,删除一个字符串中指定位置上的字符。 #include #include voidmain() { charstr[30]; intlen,i,loc; printf("pleaseinputstring: "); gets(str); len=strlen(str); printf("pleaseinputthelocationforthecharactertobedeleted: "); scanf("%d",&loc); for(i=loc-1;i str[i]=str[i+1]; str[len-1]='\0'; puts(str); } //5.编写程序,实现将字符串str中下标值为偶数的元素由小到大排序,其他元素不变。 #include #include voidmain() { charstr[30],min,t; intlen,i,j,loc; printf("pleaseinputstring: "); gets(str); len=strlen(str); for(i=0;i { min=str[i]; loc=i; for(j=i+2;j if(str[j] { min=str[j]; loc=j; } t=str[i]; str[i]=str[loc]; str[loc]=t; } puts(str); } 六: 1. 编写函数,实现对下列分段函数的计算。 #include void main() { floatfun(floatx); floatx; scanf("%f",&x); printf("fun=%6.2f\n",fun(x)); } floatfun(floatx) { if(x<0) return(x*x+x-2); else return(x*x-x+2); } 2.编写程序,计算圆柱的体积。 (提示: 圆柱的体积=底面积×高) 要求: (1)编写函数volumn,完成计算圆柱的体积; (2)编写主程序,要求用户输入圆柱的半径和高,通过调用函数volumn计算出圆柱的体积,并输出。 #include #definePI3.1416 voidmain() { inth,r; floatv; floatvolume(int,int); printf("inputh,r: "); scanf("%d,%d",&h,&r); v=volume(h,r); printf("v=%f\n",v); } floatvolume(inth,intr) { return(PI*r*r*h); } 3. 写一个函数,用来求两个整数的最小公倍数,用主函数调用这个函数,并输出结果。 两个整数由键盘输入。 #include voidmain() { intx,y,f(intx1,intx2); printf("Pleaseinputtwonumber: "); scanf("%d%d",&x,&y); printf("Thisnumberis%d\n",f(x,y)); } intf(intx1,intx2) { inti; for(i=1;x1*i%x2! =0;i++); return(x1*i); } 4.编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述统计的结果。 #include intn1=0,n2=0,n3=0,n4=0; voidmain() { voidf(charb[]); chara[81]; intx; printf("Pleaseinputastring: "); gets(a); f(a); printf("n1=%d\nn2=%d\nn3=%d\nn4=%d\n",n1,n2,n3,n4); } voidf(charb[]) { inti; for(i=0;b[i]! =’\0’;i++) if((65<=b[i]&&b[i]<=90)||(97<=b[i]&&b[i]<=122)) ++n1; elseif(48<=b[i]&&b[i]<=57) ++n2; elseif(b[i]=='') ++n3; else ++n4; } 七: 1.输入一个字符串,判断该字符串是否是“回文”(顺读和倒数都一样的字符串称为“回文”,如level),用指针实现。 参考答案1 #include #include voidmain() { charstring[40],*p,*q; printf("pleaseinputastring: "); gets(string); p=string; q=p+strlen(string)-1; while(p { if(*p==*q) p++,q--; else break; } if(p>=q) printf("该字符串是回文"); else printf("该字符串不是回文"); } 参考答案2 #include voidmain() { chara[100]; char*p,*q; printf("输入一个字符串: "); gets(a); p=a;q=a; while(*q)q++;q--; while(*p==*q&&p<=q) { p++;q--; } if(p>q) printf("%s是回文\n",a); elseprintf("%s不是回文\n",a); } 2.将一个字符串输入到一个数组中,然后从第1个字母开始间隔地输出该字符串。 例如,输入字符串abcdefghijklmnop,输出字符串acegikmo),用指针实现。 参考答案1 #include #include voidmain() { charstr[40],*p; printf("pleaseinputastring: "); gets(str); for(p=str;p printf("%c",*p); } 参考答案2 #include #include voidmain() { charstr[40],str1[20],*p; intj=0; printf("pleaseinputastring: "); gets(str); for(p=str;p str1[j++]=*p; str1[j]='\0'; puts(str1); } 3.输入两个字符串,不用系统提供的函数strcmp(),比较两个字符串是否相等,用指针实现。 参考答案1 #include #include voidmain() { charstr1[40],str2[40],*p,*q; printf("pleaseinputthefirststring: "); gets(str1); printf("pleaseinputthesecondstring: "); gets(str2); p=str1; q=str2; while(*p! ='\0'||*q! ='\0') { if(*p==*q) p++,q++; elseif(*p<*q) {printf("第一个字符串小于第二个字符串");break;} else {printf("第一个字符串大于第二个字符串");break;} } printf("两个字符串相等"); } 参考答案2 #include voidmain() { chars1[80],s2[80],*p1=s1,*p2=s2; inti,m; printf("Inputastrings1: "); gets(s1); printf("Inputastrings2: "); gets(s2); for(i=0;i<80;i++) { if(*(p1+i)>*(p2+i)) {m=1;break;} if(*(p1+i)==*(p2+i)) {m=0;continue;} if(*(p1+i)<*(p2+i)) {m=-1;break;} } if(m>0) printf("字符串s1大于字符串s2\n"); if(m==0) printf("字符串s1等于字符串s2\n"); if(m<0) printf("字符串s1小于字符串s2\n"); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 上机 作业