您的位置:首页 > 其它

c实例

2015-07-30 09:27 369 查看
#include<stdio.h>

#include "Header.h"
int maxNum(int a,int b)
{
return a>b?a:b;
}
int minNum(int a,int b)
{
return a<b?a:b;
}
int adds(int a,int b)
{
return a+b;
}
void process(int a,int b,int (*fun)(int,int))
{
int result=0;
result=(*fun)(a,b);
printf("%d",result);
}
int main()
{
int a,b,c;

printf("please enter two number:");
scanf("%d %d",&a,&b);

// int (*p)();

// p=maxNum;

// c=(*p)(a,b);

// printf("%d",c);
process(a, b, adds);//利用指针函数

//根据输入的数的奇偶性作加减法

// int x,y,sum,sub,ac;

// printf("please enter the number to control the action:\n");

// scanf("%d",&ac);

// printf("please enter two numbers:");

// scanf("%d %d",&x,&y);

// if(ac%2==0)

// {

// sum=add(x, y);

// printf("the two numbers sum is %d",sum);

// }else if(ac%2==1){

// sub=subtract(x,y);

// printf("the two numbers sub is %d",sub);

// }

int add(int x,int y)//函数实现add()
{
int sum=0;
sum=x+y;
return sum;
}

int subtract(int x,int y)//函数实现sub()
{
int sub=0;
sub=x-y;
return sub;
}

//等差数列求和

// int seq;

// int a,n,d;

// printf("please enter the first number:");

// scanf("%d",&a);

// printf("please enter the size of the number:");

// scanf("%d",&n);

// printf("please enter the sub:");

// scanf("%d",&d);

// seq=sequence(a, d, n);

// printf("the sum of the sequence is %d\n",seq);

int sequence(int a1,int d,int n)
{
int sum=0;
int an=0;
an=a1+(n-1)*d;
sum=(a1+an)*n/2;
return sum;
}

//求三角形面积

// int a,b,c;

// printf("please the angle of triangle!");

// scanf("%d %d %d",&a,&b,&c);

// area(a, b, c);

void area(int a,int b,int c)
{
float ar=0;
int result=0;
int p=0;
result=isTriangle(a, b, c);
if(result==1)
{
p=(a+b+c)/2;
ar=sqrt(p*(p-a)*(p-b)*(p-c));

printf("the area of trianle is %3.2f\n",ar);
}else{

printf("the numbers you enter are not make of a triangle!\n ");
}
}

//利用指针和数组找出最大值和最小值

// int n,i;

// int a[num]={0};

// printf("please enter the size of the array;\n");

// scanf("%d",&n);

// for(i=0;i<n;++i)

// {

// printf("please enter the %d number:",i+1);

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

// }

// max(a,n);

// min(a,n);

void max(int a[],int n)//函数实现求数组中的最大值max()
{
int *p=NULL;
p=a;
int i=0;
for(i=1;i<n;++i)
{
if(*p<a[i])
{
p=a+i;
}
}

printf("the max number is %d\n",*p);
}

void min(int a[],int n)//函数实现求数组中的最小值min()
{
int *p=NULL;
p=a;
int i=0;
for(i=1;i<n;++i)
{
if(*p>a[i])
{
p=a+i;
// *p=a[i];
}
}

printf("the min number is %d\n",*p);
}

//逆序输出

// int n,i;

// int a[num]={0};

// char ch[30];

// printf("please enter the size of the array;\n");

// scanf("%d",&n);

// for(i=0;i<n;++i)

// {

// printf("please enter the %d number:",i+1);

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

// }

// printf("please enter the string:");

// scanf("%s",ch);

// reverse(a, n);

// reverseString(ch);

// printf("the reverse of array is:\n");

// for(i=0;i<n;++i)

// {

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

// }

// printf("\n");

// printf("the reverse string is %s",ch);

void reverse(int a[],int n)//函数实现逆序reverse()
{
int t=0;
int i=0;
for(i=0;i<n/2;++i)
{
t=a[i];
a[i]=a[n-1-i];
a[n-1-i]=t;
}
}
void reverseString(char a[])//函数实现逆序reverse()
{
int len=(int)strlen(a);
int t=0;
int i=0;
for(i=0;i<len/2;++i)
{
t=a[i];
a[i]=a[len-1-i];
a[len-1-i]=t;
}
}

//字符串复制、连接

// char ch[30];

// char ch1[30];

// char ch2[50];

// char ch3[30];

// printf("please enter the string:");

// scanf("%s",ch);

// copy(ch, ch1);

// printf("the copy result is %s\n",ch1);

// printf("please enter the first string:");

// scanf("%s",ch2);

// printf("please enter the second string:");

// scanf("%s",ch3);

// connection(ch2, ch3);

// printf("the connection string is %s",ch2);

void copy(char ch[],char ch1[])//函数调用字符串复制copy()
{
int i=0;
for(i=0;ch[i]!='\0';++i)
{
ch1[i]=ch[i];
}
ch1[i]='\0';
}
void connection(char ch[],char ch1[])//函数实现字符串链接connection()
{
int chl=(int)strlen(ch);
int i=0,j=0;
ch[chl]=' ';
for(i=chl+1,j=0;ch1[j]!='\0';++i,++j)
{
ch[i]=ch1[j];
}
ch[i]='\0';
}

//俩个数组排序

// int a[4]={1,3,5,7};

// int b[4]={2,4,6,8};

// int c[8]={0};

// int i=0;

// sort1(a,b,c);

// for(i=0;i<8;++i)

// {

// printf("%d",c[i]);

// }

void sort(int a[],int b[],int c[])//函数实现数组排序sort();
{
int i=0;
for(i=0;i<8;++i)
{
if(i%2==0)
{
c[i]=a[i/2];
}else{
c[i]=b[i/2];
}
}
}
void sort1(int a[],int b[],int c[])//函数实现数组排序sort()冒泡排序;
{
int i=0,j=0;
int t;
for(i=0;i<4;++i)
{
c[i]=a[i];
}
for(j=0;j<4;++j)
{
c[i+j]=b[j];
}
for(i=0;i<8;++i)
{
for(j=0;j<7-i;++j)
{
if(c[j]>c[j+1])
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
}
}

}

//数据交换

// int a=0,b=0,x[10],n=0,i=0;

// printf("please enter tow numbers:\n");

// scanf("%d %d",&a,&b);

// printf("a=%d,b=%d\n",a,b);

// if(a<b)

// {

// exchange(&a,&b);

// }

// printf("a=%d,b=%d\n",a,b);

// printf("please enter size of array:\n");

// scanf(" %d",&n);

// printf("please enter the number of array:\n");

// for(i=0;i<n;++i)

// {

// scanf("%d",&x[i]);

// }

// exchanges(x, n);//冒泡排序

// sorts(x, n);//选择排序

// for(i=0;i<n;++i)

// {

// printf("%d ",x[i]);

// }

void exchange(int * x,int *
y)//函数实现俩个数交换exchange()
{
int temp=0;
temp=*x;
*x=*y;
*y=temp;
}

void exchanges(int x[],int n)//函数实现数组交换exchange()
{
int i=0,j=0;
int temp=0;
for(i=0;i<n;++i)
{
for(j=0;j<n-1-i;++j)
{
if(*(x+j)>*(x+j+1))
{

temp=*(x+j);
*(x+j)=*(x+j+1);
*(x+j+1)=temp;
}
}
}
}
void sorts(int x[],int n)//选择排序
{
int i,j,t;
for(i=0;i<n;++i)
{
for(j=i+1;j<n;++j)
{
if(x[j]<x[i])
{
t=x[i];
x[i]=x[j];
x[j]=t;
}
}
}
}

//字符串截取

// char str[30]="I am Chinese";

// int n=0;

// printf("the array is %s\n",str);

// printf("please enter the length of interception:");

// scanf("%d",&n);

// intercept(str,n);

// printf("the array is %s\n",str);

//void intercept(char *str,int n)//函数实现截取字符串intercept()

//{

// int i=0,j=0;

// while(*(str+i)!='\0')i++;

// if(n<=i)

// {

// while(j<n)

// {

// printf("%c",*(str+j));

// j++;

// }

// }else{

// printf("the action failed!\nthe number is longger than the size of array!");

// }

//}

void intercept(char str[],int n)//函数实现截取字符串intercept()
{
int i=0,j=0;
while(str[i]!='\0')i++;
if(n<=i)
{
while(j<n)
{
j++;
}
str[j]='\0';
}else{

printf("the action failed!\nthe number is longger than the size of array!");
}
}

//大小写转换

// char str[20]="hello";

//

// int i=0;

// for(i=0;str[i]!='\0';++i)

// {

// str[i]=str[i]-32;

// }

// printf("%s",str);

//

// changeStr(str);//指针

// printf("%s",str);

void changeStr(char *s)//函数实现小写转换为大写
{
while(*s!='\0')
{
*s=*s-32;
s++;
}
}

//统计数字、大写字母、小写字母、空格、特殊字符的个数

// char str[100];

// printf("please enter the array:");

// gets(str);

// countStr(str);

// puts(str);

void countStr(char str[])
{
int i=0,j=0,k=0,l=0,t=0;
while(*str!='\0')
{
if(*str>='a'&&*str<='z')
{
i++;
}else if(*str>='A'&&*str<='Z')
{
j++;
}else if(*str>='0'&&*str<='9')
{
k++;
}else if(*str==' '){
l++;
}else{
t++;
}
str++;
}

printf("a~z:%d\nA~Z:%d\n0~9:%d\nblank:%d\nother:%d",i,j,k,l,t);
}

//小球落地

// float s=100.0;

// int n=10;

// int i=0;

// float h=100.0;

// for(i=1;i<n;++i)

// {

// h=h*0.5;

// s=s+2*h;

// }

//

// printf("第10次落地共经过%f米\n",s);

// printf("第10次反弹高度为%f米",h/2);

//计算圆周率

// float pi=0.0;

// float n=1.0;

// float s=1.0;

// float t=1.0;

//

// while(fabs(t)>1e-6)

// {

// pi=pi+t;

// n=n+2;

// s=-s;

// t=s/n;

// }

// pi=pi*4.0;

// printf("%f",pi);

return 0;

}

//小球落地

float s=100.0;

int n=10;

int i=0;

float h=100.0;

for(i=1;i<n;++i)

{

h=h*0.5;

s=s+2*h;

}

printf("第10次落地共经过%f米\n",s);

printf("第10次反弹高度为%f米",h/2);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: