//================================第六章 循环结构-II==================================/*判断某字符串是否是前后对称的。(要求用break语句)*/
#include<stdio.h>
main()
{
char ch;
char c[100];
int i=0;
int j;
int k;
int flag;
while((ch=getchar())!='\n')
{
if(ch>='a'&&ch<='z')
{ch=ch-32;} c[i]=ch;
i++;
}
k=i-1;
for(j=0;j<=k;j++,k--)
if(c[j]!=c[k])
{
flag=0;
break;
}if(flag)
printf("对称");
else
printf("不对称");
}
-----------------------------------------------------------------------------------------
/*输入一个串字符,统计其中小写字母的个数。(要求用continue语句)*/
#include<stdio.h>
main()
{
char ch;
int i=0;
while((ch=getchar())!='\n')
{
if(ch>='a'&&ch<='z')
{i++;}
else
{continue;}
} printf("小写字母的个数为:%d\n",i);}
//================================第七章 数组========================================== ---------------------------------------------------------------------------------------
/*求一组数中最大和最小值(smart)*/
#include<stdio.h>
main()
{
int a[5],min,max;
int i;
printf("请输入5个整型的数字:\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
min=a[0];
max=a[0];
for(i=0;i<5;i++)
min=min>a[i]?a[i]:min;
max=max>a[i]?max:a[i];printf("最大的数为:%d\n",max);
printf("最小的数为:%d\n",min);}
---------------------------------------------------------------------------------------
/*输入10个数,存放在一个数组中,查找某个数,如果找不到就提示
“找不到”,如果找到了就提示:“找到了”并给出在数组中的位置
*/
#include <stdio.h>
main()
{
int ten[10],somenum;
int i;
int flag=0;
printf("please input ten integer number:\n");
for (i=0;i<10;i++)
scanf("%d",&ten[i]);
printf("please input a need search number:\n");
scanf("%d",&somenum);
for(i=0;i<10;i++)
{
if(ten[i]==somenum)
break;
}
if(i<10)
printf("找到了,在数组的第%d个位置。\n",i+1);
else
printf("对不起,没有找到");}
-------------------------------------------------------------------------------------------
/*编写C程序实现冒泡排序算法,按照降序排列一组数。*/
#include<stdio.h>
main()
{
int a[5]={4,12,85,7,8};
int i,j,temp;
printf("Please input five Integer numbers:\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);for(i=0;i<5;i++)
{
for(j=0;j<5-i-1;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
printf("%d\t",a[i]);
}
-------------------------------------------------------------------------------------------
/*如果一个数组中保存的元素是有序的(由大到小),
向这个数组中插入一个数,使得插入后的数组元素依然保持有序*/
#include<stdio.h>
main()
{
int a[6]={56,45,34,23,12};
int search_num;
int i,j;
printf("please input an search number:\n");
scanf("%d",&search_num);for(i=0;i<5;i++)
if(a[i]<search_num)
break;
for(j=5;j>i;j--)
a[j]=a[j-1];
a[i]=search_num;printf("插入后的数组是:");
for(i=0;i<6;i++)
printf("%d\t",a[i]);}
---------------------------------------------------------------------------------------------
/*有一递推数列,满足:f(0)=0,f(1)=1,f(2)=2,f(n+1)=f(n)+2f(n-1)f(n-2)(n>=2)。使用数组编写程序,顺序打印出f(0)到f(10)的值。*/
#include<stdio.h>
void main()
{ int i, a[20];
a[0]=0;
a[1]=1;
a[2]=2;
printf("f(0)=%d\n",a[0]);
printf("f(1)=%d\n",a[1]);
printf("f(2)=%d\n",a[2]);for(i=2;i<10;i++)
{
a[i+1]=a[i]+2*a[i-2]*a[i-1];
printf("f(%d)=%d\n",i+1,a[i+1]);
}
}
-------------------------------------------------------------------------------------------
/*请用户输入一个含有12个浮点数的一维数组,请分别计算出数组中所有正数的和以及所有的负数的和*/
#include<stdio.h>
main()
{
int i;
float a[12];
float sum=0.0;
float minus_sum=0.0;
printf("please input 12 float numbers:\n");
for(i=0;i<12;i++)
scanf("%f",&a[i]);
for(i=0;i<12;i++)
{
if(a[i]>0)
sum+=a[i];
else
minus_sum+=a[i];
}
printf("sum and minus_suk=%f,%f\n",sum,minus_sum);
}
--------------------------------------------------------------------------------------------
//================================第八章 指针========================================== ----------------------------------------------------------------------------------------
/*用指针判断某字符串是否对称*/
#include<stdio.h>
main()
{
char str[5]={'m','a','d','a','m'};
char *start,*end;
start=str;
end=str+4;
int flag=1;
for(;start<=end;start++,end--)
if (*start!=*end)
{
flag=0;
break;
}
if(flag)
printf("对称");
else
printf("不对称");}
----------------------------------------------------------------------------------------
/*输入两个字符串,将它们拼接起来,放在一个新的数组中.(不能使用string.h中任何字符串操作函数.)*/
#include<stdio.h>
main()
{
char ch;
char c[100];
char c1[50];
char c2[50];
int i,j,k,l; printf("pleasse input the fist string\n");
i=0;
while((ch=getchar())!='\n')
{
c1[i]=ch;
i++;
} fflush(stdin); printf("pleasse input the second string\n");
j=0;
while((ch=getchar())!='\n')
{
c2[i]=ch;
j++;
}
printf("the lengh of the fist string is:%d\n",i);
printf("the lengh of the second string is:%d\n",j);
for(k=0;k<i;k++)
c[k]=c1[k];for(k=i,l=0;l<j;k++,l++)
c[k]=c2[l];printf("last string:\n");for(l=0;l<=k;l++)
printf("%c",c[l]);
}
----------------------------------------------------------------------------------------
/*输入两个字符串,比较其大小.*/
#include<stdio.h>
#include<string.h>
main()
{ char str1[100];
char str2[100];
char *s1,*s2;printf("please input the first string:\n");
gets(str1);
printf("please input the second string:\n");
gets(str2);
s1=str1;
s2=str2;
while(*s1!='\0'||*s2!='\0')
{
if(*s1==*s2)
printf("equels\n");break;
if(*s1=='\0')
printf("two big\n");break;
if(*s2=='\0')
printf("one big\n");break;
if(*s1>*s2)
printf("one big\n");break;
if(*s1<*s2)
printf("two big\n");break;
if(*s1=='\0')
printf("two big\n");break;
s1++;
s2++;
}}
----------------------------------------------------------------------------------------
//================================第九章 函数==========================================
---------------------------------------------------------------------------------------
//求自然数1~10的平方根和立方
#include<stdio.h>
#include<math.h>
float sqrtme(float x);
float pow(float x);
main()
{
int i;
for(i=0;i<=10;i++)
printf("%d的平方根是:%7.2f,立方是:%7.2f\n",i,sqrtme(i),pow(i));
}
float sqrtme(float x)
{
return(sqrt(x));
}
float pow(float x)
{
return (x*x*x);
}
---------------------------------------------------------------------------------------
//================================第十章 函数(下)==================================== ---------------------------------------------------------------------------------------
//格式化输出的函数
#include<stdio.h>
void format();
main()
{
int i;
for(i=0;i<100;i++)
{
printf("%d",i);
format();
}
}
void format()
{
static int m=0;
m++;
if(m % 10==0)
putchar('\n');
else
putchar(' ');
}
-------------------------------------------------------------------------------------------
//通过函数实现求5个数中的最大值。
#include <stdio.h>
int find_larg(int *);
void main()
{
int arr1[5];
int i, larg_num;
printf("\n 请输入 5 个不同的值,存储在一个数组中 \n");
for (i = 0; i <= 4; i++)
{
scanf("%d", &arr1[i]);
}
larg_num = find_larg(arr1);
printf("\n 最大的数是:%d\n ", larg_num);
}
int find_larg(int *ptr)
{
int high = *ptr,i;
ptr++;
for (i = 1; i <=4; i++)
{
if (high < *ptr)
high = *ptr;
ptr++;
}
return high;
}
-------------------------------------------------------------------------------------------
//判断是否是质数
#include<stdio.h>
int iszhi(long x);
main()
{
int a;
while(1)
{
printf("please input an number:\n");
scanf("%ld",&a);
if(iszhi(a))
printf("是");
else
printf("不是");
}
}
int iszhi(long x)
{
int i;
for(i=2;i<x;i++)
if(x%i==0)
{
return 0;
break;
}
}
-------------------------------------------------------------------------------------------
//分解成质因数(9-5A)
#include<stdio.h>
int iszhi(long x);
main()
{
int i;
int a;
while(1)
{
printf("please input an number:\n");
scanf("%ld",&a);
if(a<2)
break;
else
{
for(i=2;i<=a;i++)
{
if((a%i==0)&&(iszhi(i)))
printf("%d ",i);
}
}
}
}
int iszhi(long x)
{
int i;
for(i=2;i<x;i++)
if(x%i==0)
{
return 0;
break;
}
}-------------------------------------------------------------------------------------------
//编写程序,求10名职工的平均薪水.(9-3A)
#include<stdio.h>
#define N 10
float aver(float x[N]);
void main()
{
int i;
float a[N];
float average;
printf("\nplease input ten employees' salary:\n");
for(i=0;i<N;i++)
scanf("%f",&a[i]);
average=aver(a);
printf("the ten employees' average is :%7.2f",average);
}float aver(float x[N])
{
int i;
float total=0.0;
float avera;
for(i=0;i<N;i++)
total+=x[i];
avera=total/i;
return avera;
}
-------------------------------------------------------------------------------------------
//================================第十一章 字符串==========================================
//================================第十二章 结构============================================