C作业
1.问题描述:输入10个学生5门课的成绩,分别用函数实现下列功能
1、计算每个学生的平均分
2、计算每门课的平均分
3、找出所有50个分数中最高的分数所对应的学生和课程
```c
#include <stdio.h>
#include <math.h>
void aver_stu(int t[][5]); //定义学生平均分函数
void aver_course(int t[][5]); //定义课程平均分函数
void high(int t[][5]); //定义最高分函数
int main()
{
int stu[10][5];
int i, j;
for (i = 0; i < 10; i++)
for (j = 0; j < 5; j++)
scanf("%d", &stu[i][j]); //输入10个学生各5门课的成绩
aver_stu(stu); //调用学生平均分函数
aver_course(stu); //调用课程平均分函数
high(stu); //调用最高分函数
return 0;
}
//学生平均分函数
void aver_stu(int t[][5])
{
int i, j;
float k, ave;
for (i = 0; i < 10; i++) {
for (j = 0, k = 0.0; j < 5; j++)
k += t[i][j];
ave = k / 5;
printf("No.%d student average is %f\n", i + 1, ave);
}
}
//课程平均分函数
void aver_course(int t[][5])
{
int i, j;
float k, ave;
for (j = 0; j < 5; j++) {
for (i = 0, k = 0.0; i < 10; i++)
k += t[i][j];
ave = k / 10;
printf("No.%d course average is %f\n", j + 1, ave);
}
}
//最高分函数
void high(int t[][5])
{
int i, j, h, stu, cour;
for (i = 0, h = 0, stu = 0, cour = 0; i < 10; i++) {
for (j = 0; j < 5; j++)
if (t[i][j] > h) {
h = t[i][j];
stu = i + 1;
cour = j + 1;
}
}
printf("The highest score is %d, from No.%d student & No.%d course\n", h, stu, cour);
}
## 用一个函数来实现将一行字符串中最长的单词输出。此行字符串从主函数传递给该函数
```c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
//写一个函数,输入一行字符,将此字符串中最长的单词输出
#include <stdio.h>
#include <string.h>
#include"test1.c"
int main() {
void longword(char a[]);
char str[80];
gets(str);//输入字符串
longword(str);//调用获取最大单词的函数
return 0;
}
void longword(char a[]) {
//定义字符串t1来存储最长的单词,t2来存储a中的单词
char t1[30], t2[30];
t1[0] = '\0';
int k = strlen(a), i, j = 0;
for (i = 0; i < k; i++)
{
j = 0;
while ((a[i] >= 'A' && a[i] <= 'Z') || (a[i] >= 'a' && a[i] <= 'z'))
{
t2[j++] = a[i++];
}
t2[j] = '\0';
if (strlen(t1) < strlen(t2))
{
strcpy(t1, t2);
}
}
puts(t1);
}
写一个函数,用起泡法对输入的10个字符按由小到大顺序排列
#include <stdio.h>
#include <string.h>
void stob(char s[]); //定义排序函数
int main()
{
char str[11];
gets(str); //输入10个字符
stob(str); //调用排序函数
puts(str); //输出排序后的字符
system("pause");
return 0;
}
//排序函数
void stob(char s[])
{
int i, j;
char temp;
for (i = 0; i < 9; i++)
for (j = 0; j < 9 - i; j++)
if (s[j] > s[j + 1]) {
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
用递归将一个整数n转换成字符串
#include<stdio.h>
void convert(int n) {
int i;
if ((i = n / 10) != 0)
convert(i);
putchar(n % 10 + '0');
}
int main()
{
int num;
scanf("%d", &num);
if (num < 0)
{
printf("-");
num = -num;
}
convert(num);
printf("\n");
return 0;
}
编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果
#include <stdio.h>
#include <string.h>
int lett = 0, dig = 0, spac = 0, oth = 0;
void main()
{
void count(char a[]);
char a[100];
puts("input string:");
gets(a);
count(a);
printf("letters = %d\n", lett);
printf("digtal = %d\n", dig);
printf("space = %d\n", spac);
printf("others = %d\n", oth);
}
void count(char a[])
{
int i;
for (i = 0; a[i] != '\0'; i++)
{
if ((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z'))
{
lett++;
}
else if (a[i] <= '9' && a[i] >= '0')
{
dig++;
}
else if (a[i] == ' ')
{
spac++;
}
else
oth++; ;
}
}
求两个整数的最大公约数和最小公倍数。
#include <stdio.h>
int abb(int a, int b)
{
int i = 1, j, k, m = 1;
while (i <= a && i <= b)
{
j = a % i;
k = b % i;
if (j == 0 && k == 0)
m = i;
i++;
}
return m;
}
int bbb(int a, int b)
{
int i, j, k;
i = a > b ? a : b;
while (1)
{
j = i % a;
k = i % b;
if (j == 0 && k == 0)
{
return i;
break;
}
i++;
}
}
int main()
{
int a, b;
printf("请输入两个整数:");
scanf_s("%d%d", &a, &b);
printf("最大公约数为%d\n", abb(a, b));
printf("最小公倍数为%d\n", bbb(a, b));
return 0;
}
4万+

被折叠的 条评论
为什么被折叠?



