内容正文:
《C语言程序设计》
期末复习卷(二)
时间:90分钟 总分:100分
班级 姓名 学号 成绩
一.程序设计题(本大题共10小题,每小题10分,共100分)
1.输入两个正整数m和n,求最大公约数和最小公倍数
#include<stdio.h>
void main(){
int m,n,t,r,gcd,lcm;
/*******space*******/
scanf("%d%d",&m,&n);
int a=m, b=n;
while(b != 0){
r = a % b;
a = b;
b = r;
}
gcd = a;
lcm = m * n / gcd;
/*******space*******/
}
2.输入一串字符串,统计该字符串中字母、数字、空格和其它字符的个数
#include<stdio.h>
#include<string.h>
void main(){
char str[200];
int letters=0,digits=0,spaces=0,others=0,i; /*******space*******/
gets(str);
for(i=0; str[i]; i++){
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) letters++;
else if(str[i]>='0' && str[i]<='9') digits++;
else if(str[i]==' ') spaces++;
else others++;
}
/*******space*******/
}
3.输入一个整数,判断该数是否是回文数
#include<stdio.h>
void main(){
int num,reversed=0,original,remainder;
/*******space*******/
scanf("%d", &num);
original = num;
while(num > 0){
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
/*******space*******/
}
4.输入一个整数n,利用递归函数求n的阶乘
#include<stdio.h>
long factorial(int n)
{
/*******space*******/
if(n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
/*******space*******/
}
void main()
{
int n;
/*******space*******/
scanf("%d", &n);
printf("%d! = %ld
", n, factorial(n));
/*******space*******/
}
5.输入两个字符串,将这两个字符串进行拼接,要求不使用strcat函数
#include<stdio.h>
void main(){
char s1[100],s2[50];
int i,j;
/*******space*******/
gets(s1);
gets(s2);
i = 0;
while(s1[i] != '\0'){
i++;
}
j = 0;
while(s2[j] != '\0'){
s1[i] = s2[j];
i++;
j++;
}
s1[i] = '\0';
/*******space*******/
}
6.编写程序,输入10个整数,求出其中的最大值和最小值及它们的下标
#include<stdio.h>
void main(){
int a[10],i,max,min,imax,imin;
/*******space*******/
for(i = 0; i < 10; i++){
scanf("%d", &a[i]);
}
max = min = a[0];
imax = imin = 0;
for(i = 1; i < 10; i++){
if(a[i] > max){
max = a[i];
imax = i;
}
if(a[i] < min){
min = a[i];
imin = i;
}
}
/*******space*******/
}
7.函数fun的功能是:统计一个整数n(n>0)中包含的偶数数字的个数。
例如:n = 123456,其中偶数数字有2、4、6,共3个,函数返回3。
#include<stdio.h>
int fun(int n){
int count = 0;
/*******space*******/
if(n == 0){
if(0 % 2 == 0) return 1;
}
while(n > 0){
int digit = n % 10;
if(digit % 2 == 0){
count++;
}
n = n / 10;
}
/*******space*******/
return count;
}
int main()
{
int n = 123456;
printf("数字%d中包含%d个偶数数字
", n, fun(n));
return 0;
}
8.函数fun的功能是:判断一个字符串是否是“纯字母串”(只包含大小写字母,不含数字、空格、标点等其他字符)。如果是,返回1;否则返回0
#include<stdio.h>
#include<ctype.h>
int fun(char s[]){
int i = 0;
/*******space*******/
while(s[i] != '\0'){
if(!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))){
return 0;
}
i++;
}
/*******space*******/
return 1;
}
int main(){
char s1[] = "HelloWorld";
char s2[] = "Hello123";
printf("%s : %d
", s1, fun(s1));
printf("%s : %d
", s2, fun(s2));
return 0;
}
9.函数fun的功能是:将字符串s中的所有大写字母转换为小写字母,其他字符 不变。转换后的结果仍存放在原字符串中。
#include<stdio.h>
void fun(char s[]){
int i = 0;
/*******space*******/
while(s[i] != '\0'){
if(s[i] >= 'A' && s[i] <= 'Z'){
s[i] = s[i] + 32;
}
i++;
}
/*******space*******/
}
int main(){
char s[] = "Hello World! C Programming.";
fun(s);
printf("%s
", s);
return 0;
}
10.函数fun的功能是:实现字符串的循环右移k位。例如,字符串"abcdef",k=2, 循环右移2位后变为"efabcd"。要求在原字符串上直接修改,不借助额外数组。 #include<stdio.h>
#include<string.h>
void reverse(char s[], int start, int end){
char temp;
while(start < end){
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
void fun(char s[], int k){
int len = strlen(s);
k = k % len;
/*******space*******/
reverse(s, 0, len - 1);
reverse(s, 0, k - 1);
reverse(s, k, len - 1);
/*******space*******/
}
int main(){
char s[] = "abcdefgh";
int k = 3;
printf("原字符串:%s
", s);
fun(s, k);
printf("循环右移%d位后:%s
", k, s);
return 0;
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$
《C语言程序设计》
期末复习卷(二)
时间:90分钟 总分:100分
班级 姓名 学号 成绩
一.程序设计题(本大题共10小题,每小题10分,共100分)
1.输入两个正整数m和n,求最大公约数和最小公倍数
#include<stdio.h>
void main(){
int m,n,t,r,gcd,lcm;
/*******space*******/
/*******space*******/
}
2.输入一串字符串,统计该字符串中字母、数字、空格和其它字符的个数
#include<stdio.h>
#include<string.h>
void main(){
char str[200];
int letters=0,digits=0,spaces=0,others=0,i; /*******space*******/
/*******space*******/
}
3.输入一个整数,判断该数是否是回文数
#include<stdio.h>
void main(){
int num,reversed=0,original,remainder;
/*******space*******/
/*******space*******/
}
4.输入一个整数n,利用递归函数求n的阶乘
#include<stdio.h>
long factorial(int n)
{
/*******space*******/
/*******space*******/
}
void main()
{
int n;
/*******space*******/
/*******space*******/
}
5.输入两个字符串,将这两个字符串进行拼接,要求不使用strcat函数
#include<stdio.h>
void main(){
char s1[100],s2[50];
int i,j;
/*******space*******/
/*******space*******/
}
6.编写程序,输入10个整数,求出其中的最大值和最小值及它们的下标
#include<stdio.h>
void main(){
int a[10],i,max,min,imax,imin;
/*******space*******/
/*******space*******/
}
7.函数fun的功能是:统计一个整数n(n>0)中包含的偶数数字的个数。
例如:n = 123456,其中偶数数字有2、4、6,共3个,函数返回3。
#include<stdio.h>
int fun(int n){
int count = 0;
/*******space*******/
/*******space*******/
return count;
}
int main()
{
int n = 123456;
printf("数字%d中包含%d个偶数数字
", n, fun(n));
return 0;
}
8.函数fun的功能是:判断一个字符串是否是“纯字母串”(只包含大小写字母,不含数字、空格、标点等其他字符)。如果是,返回1;否则返回0
#include<stdio.h>
#include<ctype.h>
int fun(char s[]){
int i = 0;
/*******space*******/
/*******space*******/
return 1;
}
int main(){
char s1[] = "HelloWorld";
char s2[] = "Hello123";
printf("%s : %d
", s1, fun(s1));
printf("%s : %d
", s2, fun(s2));
return 0;
}
9.函数fun的功能是:将字符串s中的所有大写字母转换为小写字母,其他字符 不变。转换后的结果仍存放在原字符串中。
#include<stdio.h>
void fun(char s[]){
int i = 0;
/*******space*******/
/*******space*******/
}
int main(){
char s[] = "Hello World! C Programming.";
fun(s);
printf("%s
", s);
return 0;
}
10.函数fun的功能是:实现字符串的循环右移k位。例如,字符串"abcdef",k=2, 循环右移2位后变为"efabcd"。要求在原字符串上直接修改,不借助额外数组。 #include<stdio.h>
#include<string.h>
void reverse(char s[], int start, int end){
char temp;
while(start < end){
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
void fun(char s[], int k){
int len = strlen(s);
k = k % len;
/*******space*******/
/*******space*******/
}
int main(){
char s[] = "abcdefgh";
int k = 3;
printf("原字符串:%s
", s);
fun(s, k);
printf("循环右移%d位后:%s
", k, s);
return 0;
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$