1.写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。
#include<stdio.h>
//欧几里得辗转相除法 的 递归实现
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
//公式法 lcm = a * b / gcd(a,b)
int lcm(int a, int b)
{
if (a * b == 0)
return 0;
return (a * b) / gcd(a, b);
}
int main()
{
int a, b;
printf("请输入a和b:");
scanf_s("%d %d", &a, &b);
//最大公约数
int gcd_result = gcd(a, b);
printf("%d 和 %d 的最大公约数为:%d\n", a, b, gcd_result);
//最小公倍数
int lcm_result = lcm(a, b);
printf("%d 和 %d 的最小公倍数为:%d\n", a, b, lcm_result);
return 0;
}
2.求方程ax²+bx+c=0的根,用3个函数分别求当:b²-4ac大于0、等于0和小于0的根并输出结果。从主函数输入a,b,c的值。
#include<stdio.h>
#include<math.h>
float disc; //判别式disc=b^2-4ac
float x1, x2; // 两个根
float p, q; //两个虚根
void greater_than_zero(int a, int b)
{
x1 = (-b + sqrt(disc)) / 2 * a;
x2 = (-b - sqrt(disc)) / 2 * a;
}
void equal_to_zero(int a, int b)
{
x1 = x2 = -b / 2 * a;
}
void less_than_zero(int a, int b)
{
p = -b / 2 * a;
q = sqrt(-disc) / 2 * a;
}
int main()
{
int a, b, c;
printf("请输入a,b,c:");
scanf_s("%d %d %d", &a, &b, &c);
disc = b * b - 4 * a * c; //计算判别式
if (disc > 0) {
greater_than_zero(a, b);
printf("disc>0的两个根为:x1=%.2f,x2=%.2f\n", x1, x2);
}
else if (disc == 0) {
equal_to_zero(a, b);
printf("disc=0的两个根为:x1=%.2f,x2=%.2f\n", x1, x2);
}
else {
less_than_zero(a, b);
printf("disc<0的两个根为:x1=%.2f,x2=%.2f\n", p + q, p - q);
}
return 0;
}
3.写一个判素数的函数,在主函数输入一个整数,输出是否为素数的信息。
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
bool IsPrime(int value)
{
for (int i = 2; i < sqrt(value); i++) {
if (value % i == 0)
return false;
}
return true;
}
int main()
{
bool flag;
int value;
printf("请输入value:");
scanf_s("%d", &value);
flag = IsPrime(value);
if (flag)
printf("%d 是素数。\n", value);
else
printf("%d 不是素数。\n", value);
return 0;
}
4.写一个函数,使给定的一个3x3的二维整型数组转置,即行列互换。
#include<stdio.h>
#define ROW 3
#define COL 3
void PrintArray(int ar[ROW][COL])
{
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
printf("%d ", ar[i][j]);
}
printf("\n");
}
}
void ReverseArray(int ar[ROW][COL])
{
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < i; j++) {
int tmp = ar[i][j];
ar[i][j] = ar[j][i];
ar[j][i] = tmp;
}
}
}
int main()
{
int ar[ROW][COL] = { 1,2,3,4,5,6,7,8,9 };
PrintArray(ar);
printf("\n");
ReverseArray(ar);
PrintArray(ar);
return 0;
}
5.写一个函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void ReverseString(char str[])
{
int start = 0;
int end = strlen(str) - 1;
while (start < end) {
char tmp = str[start];
str[start] = str[end];
str[end] = tmp;
start++;
end--;
}
}
int main()
{
char str[100] = { 0 };
printf("请输入一个字符串:");
//scanf("%s", str);
gets(str);
printf("原始字符串为:%s\n", str);
ReverseString(str);
printf("反序字符串为:%s\n", str);
return 0;
}
6.写一个字符串,将两个字符串连接。
#include<stdio.h>
void ConcatString(char str[], char str1[], char str2[])
{
int i = 0;
while (str1[i] != '\0') {
str[i] = str1[i];
i++;
}
int j = 0;
while (str2[j] != '\0') {
str[i++] = str2[j++];
}
str[i] = '\0';
}
int main()
{
char str1[100] = { 0 };
char str2[100] = { 0 };
char str[200];
printf("请输入第一个字符串:");
gets(str1);
printf("请输入第二个字符串:");
gets(str2);
ConcatString(str, str1, str2);
printf("新的字符串为:%s", str);
return 0;
}
7.写一个函数,将一个字符串中的首元音字母复制到另一个字符串中,然后输出。
#include<stdio.h>
void Copy(char dest[], char str[])
{
int i = 0;
int j = 0;
while (str[i] != '\0') {
if (str[i] == 'a' || str[i] == 'A' || str[i] == 'e' || str[i] == 'E' || str[i] == 'i' ||
str[i] == 'I' || str[i] == 'o' || str[i] == 'O' || str[i] == 'u' || str[i] == 'U') {
dest[j++] = str[i];
}
i++;
}
//要给原始字符串加上'\0'
dest[j] = '\0';
}
int main()
{
char str[256] = { 0 };
char dest[256] = { 0 };
printf("请输入一个字符串:");
gets(str);
printf("原始字符串为:%s\n",str);
Copy(dest, str);
printf("元音字符串为:%s\n", dest);
return 0;
}
8.写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字空一个空格。如输入1990,应输出“1 9 9 0”。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void OutString(char digits[])
{
int i = 0;
while (digits[i] != '\0') {
printf("%c", digits[i]);
if (digits[i + 1] == '\0')
break;
printf(" ");
i++;
}
}
int main()
{
char digits[5] = { 0 };
printf("请输入一个4位数字字符串:");
scanf("%s", digits);
OutString(digits);
printf("\n");
return 0;
}
9.编写一个函数,由实参传来一个字符串,统计此字符中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。
#include<stdio.h>
int letter = 0, number = 0, space = 0, other = 0;
void CountString(char str[]) {
int i = 0;
while (str[i] != '\0') {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A') && str[i] <= 'Z') {
letter++;
}
else if (str[i] >= '0' && str[i] <= '9') {
number++;
}
else if (str[i] == ' ') {
space++;
}
else {
other++;
}
i++;
}
}
int main() {
char str[256] = { 0 };
printf("请输入一个字符串:");
gets(str);
printf("原始字符串为:%s\n", str);
CountString(str);
printf("字母=%d,数字=%d,空格=%d,其他=%d", letter, number, space, other);
return 0;
}
10.写一个函数,输入一行字符,将此字符串中最长的单词输出。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
void FindLongWord(char str[], char word[])
{
int i, j;
i = j = 0;
int len = 0;
while (str[i] != '\0') {
j = i;
while (str[j] != ' ' && str[j] != '\0')
j++;
len = j - i;
if (len > strlen(word)) {
strncpy(word, str + i, len); //拷贝临时最长单词
}
j++;
i = j;
}
}
int main()
{
char line[256] = "Please input the text content or web address to be translated";
char word[256] = { 0 };
FindLongWord(line, word);
printf("最长的单词为:%s\n", word);
return 0;
}
11.写一个函数,用“起泡法”对输入的10个字符按有小到大顺序排列。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void BubbleSort(char str[])
{
int n = strlen(str);
for (int i = 0; i < n - 1; i++) { //控制排序的趟数
for (int j = 0; j < n - 1 - i; j++) {
if (str[j] > str[j + 1]) {
//交换字符
char tmp = str[j];
str[j] = str[j + 1];
str[j + 1] = tmp;
}
}
}
}
int main()
{
char str[11] = { 0 };
printf("请输入10个字符:");
for (int i = 0; i < 10; i++)
scanf("%c", &str[i]);
printf("原始字符序列为:%s\n", str);
//冒泡排序
BubbleSort(str);
printf("排序后字符序列为:%s\n", str);
return 0;
}
12.用牛顿迭代法求根。方程为ax³+bx²+cx+d=0,系数a,b,c,d的值依次为1,2,3,4,由主函数输入。求x在1附近的一个实根。求出根后有主函数输出。
#include<stdio.h>
#include<math.h>
double Root(int a, int b, int c, int d, double x)
{
double x0;
double f, f1;
do {
x0 = x;
f = a * pow(x0, 3) + b * pow(x0, 2) + c * x0 + d;
f1 = 3 * a * pow(x0, 2) + 2 * b * x0 + c;
x = x0 - f / f1;
} while (fabs(x - x0) >= 1e-3);
return x;
}
int main()
{
int a, b, c, d;
double x;
printf("请输入系数a b c d x:");
scanf_s("%d %d %d %d %lf", &a, &b, &c, &d, &x);
double res = Root(a, b, c, d, x);
printf("root=%lf\n", res);
return 0;
}
*13.用递归方法求n阶勒让徳多项式的值,递归公式为
Pn(x)=1(n=0);x(n=1);((2n-1)x-pn-1(x)-(n-1)xPn-2(x))/n(n>=1)
#include<stdio.h>
float Poly(int n, int x)
{
if (n == 0)
return 1;
else if (n == 1)
return x;
return ((2 * n - 1) * x - Poly(n - 1, x) - (n - 1) * Poly(n - 2, x)) / n;
}
int main()
{
int n, x;
printf("请输入n和x的值:");
scanf_s("%d %d", &n, &x);
float result = Poly(n, x);
printf("%d阶勒让徳多项式的值为:%f\n", n, result);
return 0;
}
14.输入10个学生5门课程成绩,分别用函数实现下列功能:
(1)计算每个学生的平均分
(2)计算每门课的平均分
(3)找出所有50个分数中最高的分数所对应的学生和课程
(4)计算平均分方差
σ=1/n ∑xi² - (∑xi / n)²
其中,xi为某一学生的平均分。
#include<stdio.h>
#define M 10 //学生数
#define N 5 //课程数
float avg_stu[M]; //每一位学生的平均分
float avg_course[N]; //每一门课程的平均分
float highest; //最高分
float variance; //平均分方差
int stu_index, course_index; //最高分的学生和课程下标
//计算每个学生的平均分
void avg_score_stu(float score[M][N]) {
float sum = 0.0;
for (int i = 0; i < M; i++) {
sum = 0.0;
for (int j = 0; j < N; j++) {
sum += score[i][j];
}
avg_stu[i] = sum / N;
}
}
//计算每门课程的平均分
void avg_score_course(float score[M][N])
{
float sum = 0.0;
for (int i = 0; i < N; i++) {
sum = 0.0;
for (int j = 0; j < M; j++) {
sum += score[j][i];
}
avg_course[i] = sum / M;
}
}
//找最高分
float highest_score(float score[M][N])
{
float highest = 0.0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (score[i][j] > highest) {
highest = score[i][j];
stu_index = i;
course_index = j;
}
}
}
return highest;
}
//平均分的方差
float variance_avg_score()
{
float sum_avg_square = 0.0; //平均分平方和
float sum_avg_score = 0.0; //平均分和
for (int i = 0; i < M; i++) {
sum_avg_square += avg_stu[i] * avg_stu[i];
sum_avg_score += avg_stu[i];
}
return (sum_avg_square / M - (sum_avg_score / M) * (sum_avg_score / M));
}
void print_result(float score[M][N])
{
printf("No cour1 cour2 cour3 cour4 cour5 avg\n");
for (int i = 0; i < M; i++) {
printf("N%-5d", i + 1);
for (int j = 0; j < N; j++) {
printf("%-8.1f", score[i][j]);
}
printf("%-9.1f\n", avg_stu[i]);
}
//显示每门课的平均分
printf("\n%-5s ", "avg");
for (int i = 0; i < N; i++) {
printf("%-8.1f", avg_course[i]);
}
printf("\n");
//最高分
printf("最高分为:%.1f分,是第%d位同学的第%d门课程。\n", highest, stu_index + 1, course_index + 1);
printf("平均方差为:%.1f\n", variance);
}
int main()
{
float score[M][N] = {
{1,2,3,4,5},
{2,3,4,5,6},
{3,4,5,6,7},
{4,5,6,7,8},
{5,6,7,8,9},
{6,7,10,9,1},
{7,8,9,1,2},
{8,9,1,2,3},
{9,1,2,3,4},
{1,3,5,7,9},
};
avg_score_stu(score);
avg_score_course(score);
highest = highest_score(score);
variance = variance_avg_score(score);
print_result(score);
return 0;
}
15.写几个函数:
(1)输入10个职工的姓名和职工号
(2)按职工号由小到大顺序排序,姓名顺序也随之调整
(3)要求输入一个职工号,用折半查找法找出该职工的姓名,从主函数输入要查找的职工号,输出该职工姓名。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#define N 3
#define NAME_SIZE 10
void Input(int id[], char name[][NAME_SIZE])
{
for (int i = 0; i < N; i++) {
printf("输入职工号:");
scanf_s("%d", &id[i]);
getchar(); //用于忽略回车符号
printf("输入职工的姓名:");
gets(name[i]);
}
}
void Output(int id[], char name[][NAME_SIZE])
{
for (int i = 0; i < N; i++) {
printf("[%d]:[%s]\n", id[i], name[i]);
}
}
void Sort(int id[], char name[][NAME_SIZE])
{
char tmp_name[NAME_SIZE];
for (int i = 0; i < N - 1; i++) {
for (int j = 0; j < N - i - 1; j++) {
if (id[j] > id[j + 1]) {
int tmp_id = id[j];
strcpy(tmp_name, name[j]);
id[j] = id[j + 1];
strcpy(name[j], name[j + 1]);
id[j + 1] = tmp_id;
strcpy(name[j + 1], tmp_name);
}
}
}
}
void Search(int id[], char name[][NAME_SIZE], int key)
{
int low = 0, mid;
int high = N - 1;
while (low <= high) {
mid = (low + high) / 2;
if (key == id[mid])
break;
if (key < id[mid])
high = mid - 1;
else
low = mid + 1;
}
if (low <= high)
printf("职工号为%d的职工姓名为:%s\n", key, name[mid]);
else
printf("要查找的职工号为%d的职工不存在.\n", key);
}
int main()
{
int id[N]; //职工号
char name[N][NAME_SIZE]; //职工号对应的职工姓名
Input(id, name);
Output(id, name);
Sort(id, name);
printf("\n");
Output(id, name);
int no;
while (1) {
printf("请输入要查找的职工号:");
scanf("%d", &no);
Search(id, name, no);
}
return 0;
}
16.写一个函数,输入一个十六进制数,输出相应的十进制数。
#include<stdio.h>
unsigned int HextoDec(char hex[])
{
unsigned int result = 0;
int i = 0;
while (hex[i] != '\0') {
if (hex[i] >= 'a' && hex[i] <= 'z')
result = result * 16 + hex[i] - 'a' + 10;
else if (hex[i] >= 'A' && hex[i] <= 'Z')
result = result * 16 + hex[i] - 'A' + 10;
else
result = result * 16 + hex[i] - '0';
i++;
}
return result;
}
int main()
{
char hex[9] = { 0 };
printf("请输入一个十六进制的数:");
scanf_s("%s", hex, 9);
unsigned int result = HextoDec(hex);
printf("0x%s ==> %u\n", hex, result);
return 0;
}
17.用递归法将一个整数n转换成字符串。例如:输入483,应输出对应字符串“483”。n的位数不确定,可以是任意位数的整数。
#include<stdio.h>
void Convert(int val)
{
if ((val / 10) != 0)
Convert(val / 10);
printf("%c", val % 10 + '0');
}
int main()
{
int num;
printf("请输入一个需要转换的整数:");
scanf_s("%d", &num);
printf("转换后的字符串为:");
Convert(num);
return 0;
}
18.给出年、月、日,计算该日是该年的第几天。
#include<stdio.h>
#include<stdbool.h>
bool IsLeap(int year) {
return ((year % 4 == 0 && year % 100 == 0) || (year % 400 == 0));
}
int GetDayByYM(int year, int month)
{
int days[13] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && IsLeap(year))
return days[0];
return days[month];
}
int GetDayByYMD(int year, int month, int day)
{
int days = 0;
for (int i = 1; i < month; i++) {
days += GetDayByYM(year, i);
}
days += day;
return days;
}
int main()
{
int year, month, day;
printf("请输入年 月 日:");
scanf_s("%d %d %d", &year, &month, &day);
int days = GetDayByYMD(year, month, day);
printf("%d年%d月%d日是这一年的第%d天.\n", year, month, day, days);
return 0;
}