1.
/**
*编写一个函数,通过输入球的半径,返回球的体积
*/
#include <stdio.h>double sphere_volume(double r) {return r*r*r*4.0/3.0*3.1415926; }int main(int argc,char *argv[]) {double r;printf("输入半径r的值:\n");scanf("%lf",&r);printf("半径为%lf的球体体积为%.3lf\n",r,sphere_volume(r));return 0; }
2.
/**
*编写一个函数,通过输入一个数学字符,返回该数字
*/
#include <stdio.h>char num(char n) {if(n > '0' && n < '9'){return n;}else{return '\0';}}int main(int argc,char *argv[]) {char ch;printf("输入一个数学字符:\n");scanf("%c",&ch);char res = num(ch);if(res == '\0'){printf("输入的不是数字字符!\n");}elseprintf("%c\n",ch);return 0; }
3.
/**
*编写一个函数,输入四个数据分别表示2个点的x,y坐标,返回两点之间的距离
*/
#include <stdio.h> #include <math.h>double distance(int x1,int y1,int x2,int y2) {double x_dis = x2 - x1;double y_dis = y2 - y1;return sqrt(pow(x_dis,2)+pow(y_dis,2)); }int main(int argc,char *argv[]) {printf("分别输入两个点的x与y坐标,用,隔开:\n");int x1,x2,y1,y2;scanf("%d,%d,%d,%d",&x1,&y1,&x2,&y2);double dis = distance(x1,y1,x2,y2);printf("两点之间距离为%.3lf\n",dis);return 0; }
4.
/**
*编写一个函数,通过参数输入一个整型数,返回该数各位上数字的平方和
*/
#include <stdio.h>int square(int n) {int sum = 0;int digit;while(n){digit = n % 10;n /= 10;digit *= digit;sum += digit;}return sum; }int main(int argc,char *argv[]) {int n;printf("输入一个整数:\n");scanf("%d",&n);printf("%d各位上数字的平方和为%d\n",n,square(n));return 0; }
5.
/**
* 编写一个函数,通过参数输入x的值,计算如下的数学函数值
* 当x>5时,f(x) = 4x + 7;否则f(x) = -2x+3。
* 返回结果值
*/
#include <stdio.h>double fx(double x) {if(x > 5)return 4 * x + 7;elsereturn -2 * x + 3; }int main(int argc,char *argv[]) {printf("输入x的值:\n");double x;scanf("%lf",&x);printf("f(x)的结果为%.2lf\n",fx(x));return 0; }
6.
/**
*设计一个函数,用来求出多个数据的平均值
*/
double ave(int n,int arr[]) {int sum = 0;for(int i = 0; i < n; i++){sum += arr[i];}return (double)sum / n; }int main(int argc,char *argv[]) {int n;printf("要输入数据的个数:\n");scanf("%d",&n);int arr[n];for(int i = 0; i < n; i++){scanf("%d",&arr[i]);}printf("数据的平均值为%.3lf\n",ave(n,arr));return 0; }
7.
/**
*设计一个函数,用来查找一个字符串中某个字符的位置
*/
#include <stdio.h>int find_ch(char ch,char str[]) {int i = 0;while(str[i] != '\0'){if(ch == str[i])return i;i++;}return -1; }int main(int argc,char *argv[]) {char str[] = "abcdefghijklmnopqrstuvwxyz";printf("输入一个字符,查找字符在字符串中的位置!\n");char ch = getchar();int res = find_ch(ch,str);if(res != -1)printf("字符%c在字符串中的第%d处\n",ch,res + 1);elseprintf("字符串中没有此字符!\n");return 0; }
8.
/**
*设计一个函数,把一个整型数字转换成对应的字符串格式
*/
#include <stdio.h>void change(int len,int n,char str[]) {int is_flag = 0;if(n < 0){n = -n;is_flag = 1;}str[len-1] = '\0';len--;while(len){int x = n % 10;n /= 10;str[len-1] = x + 48;len--;}if(is_flag){str[0] = '-';}else{int i = 0;while(str[i] != '\0'){str[i] = str[i + 1];i++;}str[i-1] = '\0';} }int main(int argc,char *argv[]) {int n,i = 0;printf("输入一个整数:\n");scanf("%d",&n);int m = n;while(m){m /= 10;i++;}char str[i];change(i,n,str);printf("转换为字符串形式为:%s\n",str);return 0; }
9.
/**
*设计一个函数,统计字符串中大写字母的个数
*/
#include <stdio.h> #include <string.h>int captial(int len,char str[]) {int num = 0;int i = 0;while(str[i]){if(str[i] > 'A' && str[i] < 'Z')num++;i++;}return num; }int main(int argc,char *argv[]) {char str[] = "aBcdEFghijkLmnOpqRStuVwXyz";int res = captial(strlen(str),str);printf("设有以下字符串:\n%s\n",str);printf("字符串中有%d个大写字母\n",res);return 0; }
10.
/**
*设计函数,实现strcmp的功能
*/
#include <stdio.h> #include <string.h>int my_strcmp(char str1[],char str2[]) {int i = 0;while(str1[i] != '\0' && str2[i] != '\0'){if(str1[i] > str2[i])return 1;else if(str1[i] < str2[i])return -1;i++;}if(str1[i] == '\0' & & str2[i] == '\0')return 0;if(str1[i] != '\0')return 1;else return -1; }int main(int argc,char *argv[]) {char str1[] = "I love you";char str2[] = "I love you";printf("字符串12分别为\n%s\n%s\n",str1,str2);printf("%d\n",my_strcmp(str1,str2));return 0; }
11.
/**
*编写函数,用于判断输入的字符是不是个数字,是返回1,不是返回0
*/
#include <stdio.h> #include <string.h>int is_num(char ch) {if(ch >= '0' && ch <= '9')return 1;return 0; }int main(int argc,char *argv[]) {char ch;printf("输入一个字符:\n");scanf("%c",&ch);int res = is_num(ch);if(res){printf("是数字!\n");}else{printf("不是数字!\n");}return 0; }
12.
/**
*设计一个程序,实现一个简单的计算器。要求有菜单函数和加减乘除的函数,主函数调用这些函数实现功能
*/
#include <stdio.h>void print_menu() {printf("\t1.加法\t2.减法\t3.乘法\t4.除法\t0.退出\n"); }double add(double x,double y) {return x + y; }double subtract(double x,double y) {return x - y; }double mul(double x,double y) {return x * y; }double divide(double x,double y) {return x / y; }int main(int argc,char *argv[]) {int n = -1;while(n){print_menu();scanf("%d",&n);if(n == 0)break;double x,y;scanf("%lf%lf",&x,&y);switch(n){case 1:printf("%.3lf+%.3lf=%.3lf",x,y,add(x,y));break;case 2:printf("%.3lf-%.3lf=%.3lf",x,y,subtract(x,y));break;case 3:printf("%.3lf×%.3lf=%.3lf",x,y,mul(x,y));break;case 4:{if(y != 0)printf("%.3lf/%.3lf=%lf",x,y,divide(x,y));}break;}printf("\n");}return 0; }
13.
/**
*设计函数实现冒泡排序
*/
#include <stdio.h>/** *设计函数实现冒泡排序 */void sort(int len,int arr[]) {for(int i = 0; i < len - 1; i++){for(int j = 0; j < len - i - 1; j++){if(arr[j] > arr[j+1]) {int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}} }int main(int argc,char *argv[]) {int arr[15] = {12,23,32,15,16,27,32,9,72,63,82,18,26,37,47};sort(sizeof(arr) / sizeof(arr[0]),arr);for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++){printf("%d ",arr[i]);}printf("\n");return 0; }
14.
/**
* 编写一个函数,实现数组的首位互换
*/
#include <stdio.h>void swap(int len,int arr[]) {for(int i = 0;i < len / 2; i++){int temp = arr[i];arr[i] = arr[len - i - 1];arr[len - i - 1] = temp;} } void print_arr(int len,int arr[]) {for(int i = 0; i < len; i++){printf("%d ",arr[i]);}printf("\n"); }int main(int argc,char *argv[]) {int arr[] = {12,23,34,54,65,78,68,69,98,32};int len = sizeof(arr) / sizeof(arr[0]);printf("原数组是:\n");print_arr(len,arr);swap(len,arr);printf("首位交换的数组是:\n");print_arr(len,arr);return 0; }