您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 校园网-C语言例题整理
顺序结构程序设计已知三角形的底边为20,高为10,求其面积。#includestdio.hvoidmain(){floata,h,s;a=20;h=10;s=1.0/2*a*h;/*1.0/2不能写作1/2*/printf(s=%f\n,s);}从键盘输入三角形的底边及高的长度,求其面积。#includestdio.hvoidmain(){floata,h,s;scanf(%f%f,&a,&h);s=1.0/2*a*h;/*1.0/2不能写作1/2*/printf(s=%f\n,s);}变量作用域#includestdio.hintcount;/*count是全局变量*/voidfunc1();/*函数声明*/voidfunc2();voidmain(){count=100;func1();}voidfunc1()/*函数定义*/{inttemp;/*temp是局部变量*/temp=count;func2();printf(countis%d\n,count);/*打印100*/func2();}voidfunc2(){2intcount;/*定义局部变量count*/for(count=1;count20;count++)printf(.);/*打印出.*/printf(\n);}多文件f1.c#includestdio.hexterna;voidmain(){intx=7,y=8,result1,result2;result1=(x+y)*a+max(x,y*a);a-=3;result2=min(a,max(x,y));printf(result1=%d,result2=%d\n,result1,result2);}f2.cexterninta=10;intmax(intx,inty){returnxy?x:y;}intmin(intx,inty){returnxy?x:y;}分段函数#includestdio.hvoidmain(){doublex,y;scanf(%lf,&x);if(x=1)if(x=10)y=3*x-14;elsey=2*x-1;elsey=x;printf(%lf\n,y);}冒泡13#includestdio.h#defineN5voidmain(){inti,j,t,a[N];for(i=0;iN;i++)scanf(%d,&a[i]);for(i=0;iN-1;i++)for(j=0;jN-1-i;j++)if(a[j]a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}for(i=0;iN;i++)printf(%d\t,a[i]);printf(\n);}冒泡2#includestdio.h#defineN5voidsort(inta[N]){inti,j,t;for(i=0;iN-1;i++)for(j=0;jN-1-i;j++)if(a[j]a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}voidmain(){inti,a[N];for(i=0;iN;i++)scanf(%d,&a[i]);sort(a);for(i=0;iN;i++)printf(%d\t,a[i]);4printf(\n);}逆序三位数#includestdio.hvoidmain(){intx,y=0,m;printf(请输入一个三位数:);scanf(%d,&x);while(x){m=x%10;y=10*y+m;x=x/10;}printf(y=%d\n,y);}闰年#includestdio.hintisleap(intyear){if(year%400==0||year%4==0&&year%100!=0)return1;elsereturn0;}voidmain(){intyear,n=0;for(year=1900;year=2010;year++){if(isleap(year)){printf(%d\t,year);n++;if(n%5==0)printf(\n);}}printf(\n);}数组参数15/*编写一函数,该函数的功能是*/#includestdio.h#defineM3#defineN4voidconert(inta[][N],intb[][N]);voidmain(){inti,j;intb[M][N],a[M][N]={1,2,-4,6,-9,11,32,-45,-22,-7,67,91};conert(a,b);for(i=0;iM;i++){for(j=0;jN;j++)printf(%d\t,b[i][j]);printf(\n);}}voidconert(inta[][N],intb[][N]){inti,j;for(i=0;iM;i++){for(j=0;jN;j++){if(a[i][j]0)b[i][j]=-1;elseif(a[i][j]0)b[i][j]=1;elseb[i][j]=0;}}}2#includestdio.h#defineN12doubleadd(doublea[],intn,intm);voidmain(){doublea[N]={1,2,-4,6,-9,11,32,-45,-22,-7,67,91},sum;sum=add(a,3,4);printf(%lf\n,sum);6}doubleadd(doublea[],intn,intm){inti;doubles=0;for(i=n;in+m;i++){s+=a[i];}returns;}素数/*编写判断素数的函数,再调用该函数判断1937是否是素数。*/#includestdio.hintisprime(intn){inti;for(i=2;in;i++)if(n%i==0)return0;elsecontinue;return1;}voidmain(){if(isprime(1937))printf(prime\n);elseprintf(noprime\n);}统计单词#includestdio.hvoidmain(){charstring[1024];inti,num=0,word=0;charc;gets(string);for(i=0;(c=string[i])!='\0';i++)if(c=='')word=0;else{7if(word==0){num++;word=1;}}printf(%d\n,num);}选择排序#includestdio.h#includestring.hvoidnsort(int*p,intn){inti,j,min;for(i=0;in-1;i++){min=i;for(j=i+1;jn;j++){if(p[min]p[j]){min=j;}}if(min!=i){intt;t=p[min];p[min]=p[i];p[i]=t;}}}voidssort(char**p,intn){inti,j,min;for(i=0;in-1;i++){min=i;for(j=i+1;jn;j++){if(strcmp(p[min],p[j])0)min=j;}8if(min!=i){char*q;q=p[min];p[min]=p[i];p[i]=q;}}}voidmain(){inti,a[]={23,6,78,15,88,123};char*name[]={FORTRAN,BASIC,C++,Java,VB,C};nsort(a,6);for(i=0;i6;i++)printf(%d\t,a[i]);printf(\n);ssort(name,6);for(i=0;i6;i++)puts(name[i]);}/*输入一个字符串,将其中连续的数字作为一个整数,依次存放在一个数组中,统计共有多少个整数,并输出这些整数*/#includestdio.hvoidmain(){charstr[80];intnumber[20],i=0,j=0,n=0,flag=0;gets(str);while(str[i]!='\0'){if(str[i]='0'&&str[i]='9'){n=10*n+(str[i]-'0');if(flag==0)flag=1;}else{if(flag){number[j]=n;9j++;n=0;flag=0;}}i++;}if(flag)number[j]=n;for(i=0;i=j;i++)printf(%d\t,number[i]);printf(\n);}最大公约数#includestdio.hvoidmain(){intm,n,r,p;scanf(%d%d,&m,&n);p=m*n;while((r=m%n)!=0){m=n;n=r;}printf(最大公约数是:%d\n,n);/*r=0时的除数是最大公约数*/printf(最小公倍数是:%d\n,p/n);/*两数的积除以最大公约数等于最小公倍数*/}插入#includestdio.hvoidmain(){inta[8]={2,32,45,67,88,91,94};inti,j,x;for(i=0;i7;i++){printf(%d,a[i]);}printf(\n);scanf(%d,&x);for(i=0;i7;i++){if(xa[i])10break;}for(j=7;ji;j--){a[j]=a[j-1];}a[i]=x;for(i=0;i8;i++){printf(%d,a[i]);}printf(\n);}查找#includestdio.hvoidmain(){inta[]={3,6,8,10,12,33,38,56,60};intx,top=0,bottom=8,mid;scanf(%d,&x);while(bottom=top){mid=(top+bottom)/2;if(xa[mid])top=mid+1;elseif(xa[mid])bottom=mid-1;else{printf(%d在数组中找到,下标为%d\n,x,mid);return;}}printf(%d在数组中没找到!\n);}带参数的main#includestdio.hvoidmain(intargc,char*argv[]){inti;printf(argc=%d\n,argc);for(i=0;iargc;i++)11{printf(argv[%d]=%s\n,i,argv[i]);}}共用体的意义#includestdio.hvoidmain(){inti;uniondata{inta;charb;charc[20];}x;scanf(%d,&x.a);printf(%d\n,x.a);scanf(%c,&x.b);printf(%c\n,x.b);scanf(%s,x.c);printf(%s\n,x.c);}删除#includestdio.hvoidmain(){inta[7]={2,32,45,67,88,91,94};intj,x;for(j=0;j7;j++){printf(%d,a[j]);}printf(\n);scanf(%d,&x);for(j=x;j6;j++){a[j]=a[j+1];}for(j=0;j6;j++){printf(%d,a[j]);12}printf(\n);}文件块操作#includestdio.hstructstudent{intnum;charname[20];charsex;intage;floatscore;};structstudents1,stu[4]={{11301,ZhangPing,'F',19,496.5},{11302,WangLi,'F',20,483},{1130
本文标题:校园网-C语言例题整理
链接地址:https://www.777doc.com/doc-4021805 .html