资源描述
1.程序Cmody021.c输出如下所示图形:
*
***
*****
*******
*********
#include<stdio.h>
void main()
{
int i,j;for(i=1;i<=5;i++)
{
for(j=1;j<=5-i;j++)printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
}
2. 程序Cmody032.c的功能是:输出201-300之间的所有素数,记录總個数。
#include<stdio.h>
#include<math.h>
void main()
{
int num;
printf("\n");
num=fun();
printf("\nThe total of prime is %d",num);
getch();
}
int fun()
{
int m,i,k,n=0;
for(m=201;m<=300;m++)
{
k=sqrt(m+1);
for(i=2;i<=k;i++)
/**/if(m/i==0)/**/
break;
/**/if(i==k)/**/
{
printf("%-4d",m);
n++;
if(n%10==0)printf("\n");
}
}
return n;
}
3.
程序Cmody041.c,其功能是记录输入字符串中小写英文字母的個数。
如 输入:abcdEFGHIJK123
输出:4
#include<stdio.h>
#include<string.h>
main()
{
char str1[128];
/**/int i,len,sum=0;/**/
gets(str1);
len=strlen(str1);
for(i=0;i<len;i++)
{
/**/if(str1[i]>='a'&&str1[i]<='z')/**/
sum++;
}
printf("%d\n",sum);
getch();
}
4.程序Cmody051.c,其功能是记录输入字符串中大写英文字母的個数。
如 输入:abcDEFGH123
输出:5
#include<stdio.h>
#include<string.h>
main()
{
/**/char str1[128]/**/
int i,len,sum=0;
printf("Please input a string:\n");
scanf("%s",str1);
len=strlen(str1);
for(i=0;i<len;i++)
{
if(str1[i]>='A'&&str1[i]<='Z')
/**/sum++;/**/
}
printf("%d\n",sum);
getch();
}
5.程序Cmody061.c,其功能是将字符串中'0'-'8'的数字字符变為比它大1的数字字符,将'9'变為'0'。
如 输入:abc12cd56EF89GH4
输出:abc23cd67EF90GH5
#include<stdio.h>
#include<string.h>
main()
{
char str1[128],str2[128];
int i,len;
gets(str1);
len=strlen(str1);
/**/for(i=0;i<len;i++)/**/
{
if(str1[i]>='0'&&str1[i]<='8')
str2[i]=str1[i]+1;
else if(str1[i]=='9')
str2[i]='0';
else str2[i]=str1[i];
}
/**/str2[i]='\n';/**/
puts(str2);
getch();
}
6.程序Cmody091.C,其功能是计算1至100之间的奇数之和,偶数之和。
#include<stdio.h>
void main()
{
int b,i;
/**/int a=c=0; /**/
/**/for(i=0,i<=100,i+=2)/**/
{
a+=i;
b=i+1;
c+=b;
}
printf("total of even numbers:%d\n",a);
printf("total of odd numbers:%d\n",c-101);
getch();
}
7.程序Cmody101.c,其功能是计算如下所示的数學体現式:
#include<stdio.h>
#define F(x) (x*x-2.3*x+5.6)/(x+8.2)
void main()
{
float a=6.0,b=3.0,c;
/**/float s;/**/
printf("\nPlease input c: ");
scanf("%f",/**/&c/**/);
/**/ s=F(a)+F(b)-F(c)); /**/
printf("\ns=%.2f\n,s");
getch();
}
8.程序Cmody111.C,输出如下所示图形:
@
@@
@@@
@@@@
@@@@@
@@@@@@
#include<stdio.h>
#include<conio.h>
void main()
{
/**/int i,j; /**/
/**/for(i=6;i>=1;i--)/**/
{
printf("@");
for(j=1;j<=6-i;j++)
printf("@");
printf(/**/"\r"/**/);
}
getch();
}
9.打開Cprog011.C,完毕其中的函数fun1,该函数的数學体現式是:
#include <math.h>
#include <stdio.h>
double fun1(double x)
{
Return((1+ sin(x)+ exp( x))/(x+1));
}
void main()
{
clrscr();
printf("fun1(0.76)=%8.3lf\n",fun1(0.76));
printf("fun1(3.00)=%8.3lf\n",fun1(3.00));
printf("fun1(3.76)=%8.3lf\n",fun1(3.76));
}
打開Cprog021.C,完毕其中的函数fun1,该函数的数學体現式是:
例如:fun1(0.76)= 3.582
fun1(3.00)= 5.369
fun1(3.76)= 8.931
#include <math.h>
#include <stdio.h>
double fun1(double x)
{
Return((exp( x)+ fabs(x-6))/(x+1.3));
}
void main()
{
clrscr();
printf("fun1(0.76)=%8.3lf\n",fun1(0.76));
printf("fun1(3.00)=%8.3lf\n",fun1(3.00));
printf("fun1(3.76)=%8.3lf\n",fun1(3.76));
}
打開Cprog031.C,完毕其中的函数fun1,该函数的数學体現式是:
例如:fun1(0.76)=1.200
fun1(3.00)=10.000
fun1(3.76)=8.520
--------Cprog031.C--------------------------------------------------------------------------------
#include <math.h>
#include <stdio.h>
double fun1(double x)
{
If(x<3) x=1.2;
Else if(x=3) x=10;
Else x=2*x+1;
Return (x);
}
void main()
{
clrscr();
printf("fun1(0.76)=%8.3lf\n",fun1(0.76));
printf("fun1(3.00)=%8.3lf\n",fun1(3.00));
printf("fun1(3.76)=%8.3lf\n",fun1(3.76));
}
打開程序Cprog041.C,完毕其中fun()函数,使其计算:
如 输入:12 输出f(12.000)=10.387
输入:32.25 输出f(32.250)=12.935
输入:0.113 输出f(0.113)=1568
---------Cprog041.C-------------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
double f(float x)
{
/**/
If(x<=0) return(0); x=0;
Else rerurn((sqrt(x)+3.2)/(sin(x)+2)) x=(sqrt(x)+3.2)/(sin(x)+2);
Return(x);
/**/
}
void main()
{
float x;
double y;
printf("Please input a number:\n");
scanf("%f",&x);
y=f(x);
printf("f(%.3f)=%.3f\n",x,y);
getch();
}
1.打開程序Cprog051.C,完毕其中的f()函数,使其计算:
如 输入:0.4 输出:f(0.40)=0.82
输入: 1.5 输出:f(1.50)=1.24
输入: 7.80 输出:f(780.00)=-1.00
-------------Cprog051.C--------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
double f(float x)
{
/**/
If(x>=-700&&x<=700) x=(sqrt(5.8+fabs(x))/(cos(x)+2.1));
Else x=-1;
Return (x);
/**/
}
void main()
{
float x;
double y;
printf("please input a number :\n");
scanf("%f",&x);
y=f(x);
printf("f(%0.2f)=%0.2f\n",x,y);
getchar();
}
1. 打開程序Cprog061.C,完毕其中的f()函数,使其计算:
如 输入:0.8 输出:f(0.80)=0.96
输入: 4.5 输出;f(4.50)=107.05
输入;725 输出;f(725.00)=-1.00
----------Cprog061.C-------------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
double f(float x)
{
/**/
If(x<=300&&x>=-300) return((x*x*x)/log10(fabs(x)+2.6));
Else return(-1);
/**/
}
void main()
{
float x;
double y;
printf("Please iuput a number:\n");
scanf("%f",&x);
y=f(x);
printf("f(%0.2f)=%0.2f\n",x,y);
getch();
}
1. 打開程序Cprog071.C,完毕其中的f(x)的函数,使對其输入的一种月工资数额,求应交税款。设应交税款的计算公式如下:
例如 输入:1825 输出:f(1825)=11.25
输入:2700 输出:f(2700)=85.00
输入:5655 输出:f(5655)=483.25
-----------Cprog071.C-----------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
double f(float x)
{
/* */
If(x<=1600) x=0;
Else if(x>1600&&x<=2100) x=(x-1600)*5%;
Else if(x>2100&&x<=3100) x=(x-1600)*10%-25;
Else x=(x-1600)*15%-125;
Return (x);
/**/
}
void main()
{
float x;
double y;
clrscr();
printf("Please input a number:\n");
scanf("%f",&x);
y=f(x);
printf("f(%.2f)=%.2f\n",x,y);
getch();
}
打開程序Cprog081.C,完毕其中的f(x)函数,使其计算:
如 输入:-1.2 输出:f(-1.200)=0.241
输入:6 输出:f(6.000)=19.879
--------------Cprog081.C---------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
double f(float x)
{
If(x<=0) return ((x+2)*exp(x));
Else return((x+2)*log(2*x));
}
void main()
{
float x;
double y;
printf("Please input a number:\n");
scanf("%f",&x);
y=f(x);
printf("f(%.3f)=%.3f\n", x,y);
getch ();
}
1. 打開程序CPROG091.C,完毕其中的f()函数,使其返回方程的两個根中较大的根,求根公式為
,其中假设:且
-------------CPROG091.C-------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
double f(float a,float b,float c)
{
/**/
Double x1,x2;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
X2=(-b-sqrt(b*b-4*a*c))/(2*a);
If(x1>x2) return (x1);
Else return(x2);
/**/
}
void main()
{
float x;
printf("The bigger root is %.2f\n",f(1,5,6));
getch( );
}
打開考生文献夹中的Cprog111.c,完毕其中的函数fun,该体現式是:
例如:當時,函数的值為4.724444。
该函数返回数组a中的次大数(即仅次于最大数的数)。
------------------------Cprog111.c------------------------------------------------------------------
#include <stdio.h>
void main()
{
double x;
int n;
double fun (double x, int n);
printf ("Please enter x,n:");
scanf ("%lf%d",&x,&n);
printf ("fun=%lf\n",fun(x,n));
getch();
}
double fun (double x, int n)
{
/**/double y
If(n==0) y=1;
Else if(n==1) y=x;
Else if(n>1) y=(2n-1)*x-fun(x,n-1)-(n-1)fun(x,n-2)/n
Return (y)
/**/
}
补充程序Ccon0112.C,其功能是求下列级数的部分和。
例如:當m=100,x=2時,ex=7.389057
-------Ccon0112.C---------------------------------------------------------------------------
#include<stdio.h>
main()
{
int i,m;
float x,s,tem;
scanf("%d,%f",&m,&x);
/**/ tem=1;s=1; /**/
for(i=1;/**/ i<=m /**/;i++)
{
tem*=x/i;
s+=tem;
}
printf("e**%.2f=%f\n",x,s);
getch();
}
1.补充程序Ccon091.C,输入一种3行3列的整型数组,求其最大值和最小值。
如输入:1 2 3
4 5 6
7 8 9
输出:max=9
min=1
-------Ccon091.C---------------------------------------------------------------------------
#include"stdio.h"
#define ROW 3
#define COL 3
void main()
{
int a[ROW][COL],i,j,max,min;
for(i=0;i<ROW;i++)
for(j=0;j<COL;j++)
scanf("%d",&a[i][j]);
/**/ max=min=a[0][0]; /**/
for(i=0;i<ROW;i++)
for(j=0;j<COL;j++)
{
if(/**/ a[i][j]>max /**/)
max=a[i][j];
if(a[i][j]<min)
/**/ min=a[i][j]; /**/
}
printf("max=%d\n",max);
printf("min=%d\n",min);
}
程序Cmody011.c的功能是:從字符串数组str1中取出ACSII码值為偶数且下標為偶数的字符依次寄存到字符串t中。
例如,若str1所指的字符串為:4AZ18c?Ge9a0z!
则t所指的字符為:4Z8z
注意:数组下標從0開始。
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str1[100],t[200];
int i,j;
/**/i=0;j=0;/**/
strcpy(str1,"4AZ18c?Ge9a0z!");
for(i=0;i<strlen(str1);i++)
{
/**/if((str1[i]%2==0)&&(i%2==0))/**/
{
t[j]=str1[i];
j++;
}
}
t[j]='\0';
printf("\nOriginal string:%s\n",str1);
printf("\n Result string:%s\n",t);
}
程序Cmody012.c中,函数fun(int n)的功能是:根据参数n,计算不小于10的最小n個能被3整除的正整数的倒数之和。
#include<string.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
#define M 50
double fun(int n)
{
double y=0.0;
int i,j;
j=0;
for(i=1;;i++)
{
/**/if((i>10)&&(i%3==0))/**/
{
/**/y+=1/i;/**/
j++;
}
if(j==n)break;
}
return y;
}
void main()
{
clrscr();
printf("fun(8)=%8.3lf\n",fun(8));
}
.程序Cmody022.c的功能是求解百元买百鸡問題:
设一只公鸡2元、一只母鸡1元、一只小鸡0.5元。問一百元买一百只鸡,公鸡、母鸡、小鸡数可分别為多少?有多少种分派方案?
#include<stdio.h>
#include<conio.h>
/*double fun();/**/
{
int hen,cock,chicken,n=0;
clrscr();
for(cock=0;cock<=50;cock+=1)
for(hen=0;hen<=100;hen=hen+1)
{
chicken=2*(100-hen-2*cock);
/**/if(cock+hen+chicken==100)/**/
{
n++;
printf("%d-->hen:%d,cock:%d,chicken:%d\n",n,hen,cock,chicken);
if(n==20)getch();
}
}
return n;
}
void main()
{
int num;
num=fun();
printf("\nThere are %d solutions.\n",num);
getch();
}
1.程序Cmody031.c的功能是:從键盘上输入两個整数,及一种运算符(+、-、*、/或%),進行對应的运算後输出运算的成果。
如输入:1+2
将输出:1+2=3
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,result,flag=0;
/**/char ch;/**/
clrscr();
printf("Input an expression:");
scanf("%d%c%d",&m,&ch,&n);
/**/switch (ch)/**/
{
case '+':result=m+n;break;
case '-':result=m-n;break;
case '*':result=m*n;break;
case '%':result=m%n;break;
case '/':result=m/n;break;
default:{printf("Error!\n");flag=1;}
}
if(!flag)printf("%d%c%d=%d\n",m,ch,n,result);
getch();
}
程序Cmody032.c的功能是:输出201-300之间的所有素数,记录總個数。
#include<stdio.h>
#include<math.h>
void main()
{
int num;
printf("\n");
num=fun();
printf("\nThe total of prime is %d",num);
getch();
}
int fun()
{
int m,i,k,n=0;
for(m=201;m<=300;m++)
{
k=sqrt(m+1);
for(i=2;i<=k;i++)
/**/if(m%i==0)/**/
break;
/**/if(i==k)/**/
{
printf("%-4d",m);
n++;
if(n%10==0)printf("\n");
}
}
return n;
}
程序Cmody072.c,其功能是求解百馬百担問題。
有100匹馬,驮100担货,大馬驮3担,中馬驮2担,两匹小馬驮1担,問大、中、小馬数可分别為多少?有多少种处理方案?
#include<stdio.h>
#include<conio.h>
/**/void fun()/**/
{
int large,middle,small,n=0;
clrscr();
for(large=0;large<=33;large++)
for(middle=0;middle<=50;middle++)
{
small=2*(100-3*large-2*middle);
/**/if(large+middle+small=100)/**/
{
n++;
printf("%d-->large:%d,middle:%d,small:%d\n",n,large,middle,small);
}
}
return n;
}
void main()
{
int num;
num=fun();
printf("\nThere are %d solutions.\n",num);
getch();
}
1.程序Cmody081.c,其功能是求一堆零件的總数(100到200之间)。
假如提成4個零件一组的若干组,则多2個零件;若提成7個零件一组,则多3個零件;若提成9個零件一组,则多5個零件。
#include<stdio.h>
void main()
{
int i;
/**/for(i=100;i<200;i++)/**/
if((i-2)%4==0)
if(!((i-3)%7))
if(i%9==5)
printf("%d\n",/**/i/**/);
getch();
}
其功能是互换连個变量的值。
如 输入:Original:a=2 b=3
输出:Result:a=3 b=2
#include<stdio.h>
/**/void swap(int *p1,int *p2)/**/
{
int temp;
temp=*p1;
/**/*p1=*p2;/**/
*p2=temp;
}
void main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("\nOriginal:a=%d b=%d\n",a,b);
swap(&a,&b);
printf("\nResult:a=%d b=%d\n",a,b);
getch();
}
程序Cmody092.C的功能是求满足等式xyz+yzz=520的x,y,z值(其中xyz和yzz分别表达一种三位数)。
#include<stdio.h>
void main()
{
int x,y,z,i,result=520;
for(x=1;x<10;x++)
for(y=1;y<10;y++)
/**/for(z=0;z<10;z++)/**/
{
i=100*x+10*y+z+100*y+10*z+z;
/**/if(i==result) /**/
printf("x=%d,y=%d,x=%d\n",x,y,z);
}
getch();
}
程序Cmody091.C,其功能是计算1至100之间的奇数之和,偶数之和。
#include<stdio.h>
void main()
{
int b,i;
/**/int a=0,c=0; /**/
/**/for(i=0,i<=100,i+=1)/**/
{
a+=i;
b=i+1;
c+=b;
}
printf("total of even numbers:%d\n",a);
printf("total of odd numbers:%d\n",c-101);
getch();
}
程序Cmody102.c,其功能是实現打印出所有的“水仙花数”。所谓“水仙花数”是指一种三位数,其各位数字立方和等于该数自身。例如,153是一种水仙花数,由于153=13+53+33。
void main()
{
int f,s,t,n;
printf("\nThe list is:\n");
for(n=100;n<1000;n++)
{
f=n%10;
s=(n%100)/10;
/**/t=n/100;/**/
/**/if(t*t*t+s*s*s+f*f*f==n)/**/
printf("%d ",n);
}
printf("\n");
getch();
}
程序Cmody062.c,其功能是将程序中的两個字符串"ABC"、"xyz"连接在一起,并输出"ABCxyz"。
#include<stdio.h>
#include<string.h>
void main()
{
char s1[12]="ABC",s2[]="xyz";
char *ps1=s1,*ps2;
/**/ps2=&NULL;/**/
/**/while(*ps1==NULL)/**/
ps1++;
while(*ps2)*(ps1++)=*(ps2++);
printf("%s\n",s1);
getch();
}
程序Cmody052.c,其功能是实現從键盘依次输入M個整数,输出其中所有的偶数。
如 输入:23 62 38 45 26
输出:62 38 26
#include<stdio.h>
#include<math.h>
/**/#define M 5/**/
main()
{
int a[M],i;
for(i=0;i<M;i++)
scanf("%d",&a[i]);
for(i=0;i<M;i++)
/**/if(a[i]%2==0)/**/
printf("%d ",a[i]);
printf("\n");
getch();
}
.程序Cmody042.c,其功能是将從键盘依次输入的M個整数逆序输出。
#include<stdio.h>
#include<math.h>
#define M 8
main()
{
int a[M],i;
printf("Please input 8 numbers:\n");
for(i=0;i<M;i++)
scanf("%d",/**/&a[i]/**/);
printf("Inverge order is:\n");
/**/for(i=M-1;i>=0;i--)/**/
printf("%d ",a[i]);
printf("\n");
getch();
}
程序Cmody032.c的功能是:输出201-300之间的所有素数,记录總個数。
#include<stdio.h>
#include<math.h>
void main()
{
int num;
printf("\n");
num=fun();
printf("\nThe total of prime is %d",num);
getch();
}
int fun()
{
int m,i,k,n=0;
for(m=201;m<=300;m++)
{
k=sqrt(m+1);
for(i=2;i<=k;i++)
/**/if(m%i==0)/**/
break;
/**/if(i>k)/**/
{
printf("%-4d",m);
n++;
if(n%10==0)printf("\n");
}
}
return n;
}
展开阅读全文