本科时候没有好好学习C语言,现在通过做一下基础题,重新拾起来。
我一直相信,只要想学习,什么时候都不晚。
第1题
int root(double a, double b, double c, double* x1, double* x2);
功 能:求一元二次方程ax2+bx+c=0的根
参 数:a, b, c为方程的系数; x1, x2为方程的两个根
返回值:0,成功;<0,不成功.
分 值:
#include#include int root(double a,double b,double c,double *x1,double *x2) { double d; d=b*b-4*a*c; if(d>=0){ *x1=(-b+sqrt(d))/(2*a); *x2=(-b-sqrt(d))/(2*a); return 1; } else return 0; } void main() { double a,b,c,x1,x2; int flag; printf("input a、b、c:\n"); scanf("%lf%lf%lf",&a,&b,&c); flag=root(a,b,c,&x1,&x2);//这里要加地址符&x1,&x2,不能直接用x1,x2 if(flag==1) printf("x1=%.2f,x2=%.2f\n",x1,x2); else printf("no real roots\n"); }
第2题
int letters(char* str);
功 能:求出字符串中英文字母的个数
参 数:str为一个字符串
返回值:>=0, 英文字母的个数;<0,不成功.
分 值:
#include//#include int letters(char* str){ int n; for(n=0;*str!='\0';str++) n++; return n; } void main() { //调用letters()函数 char str[]="hello world"; int n=letters(str); printf("-----------\n"); printf("the string's length is %d\n",n); }
第3题
int digits(char* str);
功 能:求出字符串中数字字符的个数
参 数:str为一个字符串
返回值:>=0, 数字字符的个数;<0,不成功.
分 值:
#include//#include int digits(char *str){ int n; for(n=0;*str!='\0';str++) { if((*str>47) && (*str<58)) n++; } return n; } void main() { //调用digits()函数 char str[]="123456abcc"; int n=digits(str); printf("-----------\n"); printf("the number of numbers in the string is %d\n",n); }
第4题
double highest(double* sheet, unsigned int n);
功 能:求出成绩单中的最高分
参 数:sheet为含有成绩的数组;n为数组中元素的个数。
返回值:成绩单中的最高分.
分 值:
//第4题 double highest(double *sheet,unsigned int n){ unsigned int i; double highest=0; for(i=0;i第5题
double lowest(double* sheet, unsigned int n);
功 能:求出成绩单中的最低分
参 数:sheet为含有成绩的数组;n为数组中元素的个数.
返回值:成绩单中的最低分.
分 值://第5题 double lowest(double *sheet,unsigned int n){ unsigned int i; double lowest=*sheet; for(i=0;i*sheet) lowest=*sheet; sheet++; } return lowest; } 第6题
double average(double* sheet, unsigned int n);
功 能:求出成绩单的平均成绩
参 数:sheet为含有成绩的数组;n为数组中元素的个数.
返回值:成绩单的平均成绩.
分 值://第6题 double average(double *sheet,unsigned int n){ unsigned int i; double sum=0; for(i=0;i第7题
double average1(double* sheet, unsigned int n);
功 能:求出成绩单中去掉最高分和最低分的平均成绩
参 数:sheet为含有成绩的数组;n为数组中元素的个数.
返回值:成绩单中去掉最高分和最低分的平均成绩.
分 值://第七题 double average1(double* sheet, unsigned int n){ unsigned int i; double highest=*sheet; double lowest=*sheet; double sum=0; for(i=0;i*sheet) lowest=*sheet; if(highest<*sheet) highest=*sheet; sheet++; } double avg=(sum-highest-lowest)/n; return avg; } 第8题
int greater_equal(double* sheet, unsigned int n, double score);
功 能:求出成绩单中大于等于某分值的学生人数
参 数:sheet为含有成绩的数组;n为数组中元素的个数;score为给定的分值。
返回值:>=0, 成绩单中大于等于某分值的学生人数;<0,不成功.
分 值:int greater_equal(double *sheet,unsigned int n,double score){ int count=0; unsigned int i; for( i=0;i=score){ count++; } sheet++; } return count; } 第9题
int less_equal(double* sheet, unsigned int n, double score);
功 能:求出成绩单中小于等于某分值的学生人数
参 数:sheet为含有成绩的数组;n为数组中元素的个数;score为给定的分值。
返回值:>=0, 成绩单中小于等于某分值的学生人数;<0,不成功.
分 值://第9题 int less_equal(double *sheet,unsigned int n,double score){ int count=0; unsigned int i; for( i=0;i //测试程序 #include//#include //将highest()、lowest()、average()、agerage1()、greater_equal()、less_equal()代码考到此处 void main() { //调用highest()、lowest()、average()、agerage1()、greater_equal()、less_equal() double sheet[]={60.2,70,80,100,40,30,50.5,80,99.9,77}; unsigned int n= 10 ;//数组中10个数 double largest=highest(sheet,n); double smallest=lowest(sheet,n); double avg=average(sheet,n); double avg1=average1(sheet,n); int count=greater_equal(sheet,n,60); int count1=less_equal(sheet,n,60); printf("-----------\n"); printf("the largest number is %.2f\n",largest); printf("the smallest number is %.2f\n",smallest); printf("the average number is %.2f\n",avg); printf("the average1 number is %.2f\n",avg1); printf("the greater_equal's number is %d\n",count); printf("the less_equal's number is %d\n",count1); } 时间关系,就写到这里,会陆续更新的......