#includeintmain(){int n,i,d;double y=0.0;printf("please enter your ideal number\n");scanf("%d",&n);for(i=1;i<=n;i++){
d=i*i;
y+=1.0/d;}printf("The result is %f\n",y);}
#includeintmain(){int i,n;double A=1.0;printf("Enter your ideal number:\n");scanf("%d",&n);for(i=2;i<=n;i++)//目标是为了A的值与i的值下标对应相当。
A=1.0/(1+A);printf("The result is:%f\n",A);}
字符串类:
1.字符大VS小:
#includevoidfun(char*s,int*a,int*b){while(*s){if(*s>='A'&&*s<='Z')++*a;//留意这里万万不能用*a++,为确保万一可以接纳*a+=1来替换。if(*s>='a'&&*s<='z')++*b;
s++;}}intmain(){char s[100];int upper=0,lower=0;printf("please enter a string:\n");gets(s);fun(s,&upper,&lower);printf("upper=%d lower=%d\n",upper,lower);return0;}
2.统计行式单词数:
#include#includeintLINE_WORDS(char*s){int i,count=0;for(i=0;s[i]!='\0';i++)if((s[i]!=' ')&&((s[i+1]==' ')||(s[i+1]=='\0')))//这里万万不能写成s[i++]如许的情势,s[i++]如许的情势在表现的照旧是s[i],i++先用后增!
count++;return count;}intmain(){char words[80];printf("Enter a string:\n");gets(words);//留意这里万万不能用scanf("%s",&words)!printf("The number of word is %d\n",LINE_WORDS(words));return0;}
3.搜索串中元音数:
#includevoidfun(char*s,int num[5])//这个fun()函数太美丽了!{//第一步:分配输出数组。int k,i=5;for(k=0;k<i;k++)
num[k]=0;//第二步:给输出数组分配标识变量。for(;*s;s++){
i=-1;switch(*s){case'A':case'a':{
i=0;break;}case'E':case'e':{
i=1;break;}case'I':case'i':{
i=2;break;}case'O':case'o':{
i=3;break;}case'U':case'u':{
i=4;break;}}//统计各变量计数下的值。if(i>=0)
num[i]++;}}intmain(){char s[80];int num[5],i;printf("please enter a string:\n");gets(s);fun(s,num);for(i=0;i<5;i++)printf("%d\n",num[i]);}
4.行式英文——首字母:
#include#includevoidupfst(char*p){int k=0;for(;*p;p++)//这种写法多用于处理处罚字符串标题。if(k){if(*p==' ')
k=0;}else{if(*p!=' '){
k=1;*p=toupper(*p);}}}//这个fun()函数非常美丽,值得学习。intmain(){char chrstr[81];printf("please enter an English text line:\n");gets(chrstr);printf("\nBefore changing :\n%s",chrstr);upfst(chrstr);printf("\nAfter changing :\n%s\n",chrstr);}
5.串值求和:
//这是一道好题。#include#include#include//存储着isdight函数。#define N 9//接纳两个方法的利益在与优化与简便。intstring(char*s){int num=0;while(*s)if(isdigit(*s))//isdight函数:查抄参数是否为十进制的数组。{
num=num*10+*s-'0';//这是本题的灵魂所在,字符与数值之间的转换。
s++;}return num;}intfun(char*a,char*b)//一个方法套一个方法,克制了步伐的拖沓。{returnstring(a)+string(b);}intmain(){char s1[N],s2[N];do{printf("Input string1:");gets(s1);}while(strlen(s1)>N);//这里接纳do...while是由于标题条件所限,必须先获取,才华判断。do{printf("Input string2:");gets(s2);}while(strlen(s2)>N);printf("The result is:%d\n",fun(s1,s2));return0;}
数值生存:
#includeintmain(){float num=0.0;printf("Enter num:");scanf("%f",&num);printf("The original date is:%lf\n",num);
num=((num*1000)+5)/10.0;
num=(int)(num);
num=float(num)/100.0;printf("The result :%f\n",num);return0;}
与矩阵及数组有关的一系列转化和求值:
1.边和均值:
#include#include#define N 5doublefun(int w[][N]){int i,j,k=0;double sum=0.0;for(i=0;i<N;i++)for(j=0;j<N;j++)if(i==0||j==0||i==N-1||j==N-1){
sum+=w[i][j];
k++;}return sum/k;}intmain(){int a[N][N],m,i,j;double s;printf("*** The array ***\n");for(i=0;i<N;i++){for(j=0;j<N;j++){
a[i][j]=rand()%20;printf("%4d",a[i][j]);}printf("\n");}
s=fun(a);printf("***The result***\n");printf("The average sum is:%lf\n",s);return0;}
2,阵部倍值:
#include#include#define N 5voidfun(int a[][N],int m){int i,j;//for(j=0;j<N;j++)for(i=0;i<=j;i++)//类比于平面函数y=-x其在y>-x的线性规划标题。
a[i][j]*=m;//}intmain(){int a[N][N],m,i,j;printf(" *** The array ***\n");//一个二维数组的构建遍历与配值。for(i=0;i<N;i++){for(j=0;j<N;j++){
a[i][j]=rand()%20;printf(" %4d ",a[i][j]);}printf("\n");}//
m=rand()%20;printf(" m= %d\n",m);fun(a,m);printf(" *** THE RESULT ***\n");for(i=0;i<N;i++){for(j=0;j<N;j++)printf(" %4d ",a[i][j]);printf("\n");}return0;}
3,美满数组:
#include#include#define N 20voidfun(int*a){int i,x,n=0;
x=rand()%20;while(n<N){for(i=0;i<n;i++)if(x==a[i])break;if(i==n){
a[n]=x;
n++;}
x=rand()%20;}}intmain(){int x[N]={0},i;fun(x);printf("The result :\n");for(i=0;i<N;i++){printf("%4d",x[i]);if((i+1)%5==0)printf("\n");}return0;}