内容正文:
《C语言程序设计》
期末复习卷(五)
时间:90分钟 总分:100分
班级 姓名 学号 成绩
一.程序设计题(本大题共10小题,每小题10分,共100分)
1.编写程序,输入一串数字字符串,将该字符串转换成一个整数,要求不能使用字符串转换整数的库函数。
#include<stdio.h>
int strToInt(char *s){
/*******space*******/
int result=0,sign=1,i=0;
if(s[0] == '-'){
sign = -1;
i = 1;
}
for(; s[i] != '\0'; i++){
if(s[i] >= '0' && s[i] <= '9'){
result = result * 10 + (s[i] - '0');
}else{
break;
}
}
return sign * result;
/*******space*******/
}int main(){
char s[100];
scanf("%s", s);
printf("%d
", strToInt(s));
return 0;
}
2.编写程序,输入一个数,判断该数是否是快乐数,快乐数定义:各位数字平方和反复计算最终能变为1的数。
#include<stdio.h>
int isHappy(int n){
/*******space*******/
int seen[1000] = {0};
while(n != 1 && !seen[n]){
seen[n] = 1;
int sum = 0;
int temp = n;
while(temp > 0){
int digit = temp % 10;
sum += digit * digit;
temp /= 10;
}
n = sum;
}
return n == 1;
/*******space*******/
}int main(){
int n;
scanf("%d", &n);
printf("%s
", isHappy(n) ? "是快乐数" : "不是快乐数");
return 0;
}
3.编写程序输入一串字符串,统计该字符串中每个字符出现的次数,例如输入:hello ,输出:h:1 e:1 l:2 o:1
#include<stdio.h>
#include<string.h>
void charCount(char s[]){
/*******space*******/
int count[256]={0};
int i;
for(i=0;s[i]!='\0';i++){
count[(int)s[i]]++;
}
for(i=0;i<256;i++){
if(count[i]>0){
printf("%c:%d ", i, count[i]);
}
}
printf("
");
/*******space*******/
}
int main(){
char s[100];
gets(s);
charCount(s);
return 0;
}
4.编写程序,输入一个整数n,并输入n名同学的单科成绩,要求统计并输出该科目成绩的平均分以及高于该平均分的人数
#include <stdio.h>
int aboveAvg(int score[], int n, float *avg) {
/*******space*******/
int sum = 0;
int count = 0;
int i;
for(i=0; i<n;i++){
sum += score[i];
}
*avg = (float)sum / n;
for(i=0;i<n;i++){
if(score[i]>*avg){
count++;
}
}
return count;
/*******space*******/
}int main(){
int score[100], n, i, count;
float avg;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &score[i]);
count = aboveAvg(score, n, &avg);
printf("平均分=%.2f, 高于平均分的人数=%d
", avg, count);
return 0;
}
5.编写程序,输入一个整数n,并输入对应的n个整数存放于数组a中,利用指针找出数组a中的最大值以及最小值
#include <stdio.h>
void findMinMax(int a[], int n, int *min, int *max) {
/*******space*******/
*min = *a;
*max = *a;
int i;
for(i = 1; i < n; i++){
if(*(a+i) < *min) *min = *(a+i);
if(*(a+i) > *max) *max = *(a+i);
}
/*******space*******/
}
int main()
{
int a[100],n,i,min,max;
scanf("%d", &n);
for(i = 0;i<n; i++) scanf("%d", &a[i]);
findMinMax(a, n, &min, &max);
printf("min=%d max=%d
", min, max);
return 0;
}
6.编写程序,有一有序整数数组a,输入一个整数x,将该整数x插入到数组a中,并使数组仍然有序。例如原数组:1 3 5 7 9,输入整数4插入后数组顺序为:1 3 4 5 7 9
#include<stdio.h>
void insert(int a[],int *n,int x){
/*******space*******/
int i,pos = *n;
for(i=0;i<*n;i++){
if(a[i] > x){
pos = i;
break;
}
}
for(i=*n;i>pos;i--){
a[i] = a[i-1];
}
a[pos] = x;
(*n)++;
/*******space*******/
}
int main(){
int a[100] = {1,3,5,7,9}, n = 5, x, i;
scanf("%d", &x);
insert(a, &n, x);
for(i = 0; i < n; i++) printf("%d ", a[i]);
return 0;
}
7.编写程序,编写函数isPalindromeStr,该函数的功能为判断字符串是否为回文串
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int isPalindromeStr(char s[]){
/*******space*******/
int left = 0;
int right = strlen(s) - 1;
while(left < right){
while(left < right && !isalnum(s[left])) left++;
while(left < right && !isalnum(s[right])) right--;
if(tolower(s[left]) != tolower(s[right])){
return 0;
}
left++;
right--;
}
return 1;
/*******space*******/
}int main(){
char s[100];
gets(s);
if(isPalindromeStr(s)) printf("是回文串
");
else printf("不是回文串
");
return 0;
}
8.编写程序,输入一串英文单词字符串,每个单词之间使用空格隔开,完成功能将每个单词的首字母变大写。例如输入:hello world 输出:Hello World
#include<stdio.h>
#include<string.h>
void capitalize(char s[]){
/*******space*******/
int i;
int newWord = 1;
for(i = 0; s[i] != '\0'; i++){
if(newWord && s[i] >= 'a' && s[i] <= 'z'){
s[i] = s[i] - 32;
newWord = 0;
}
else if(s[i] == ' '){
newWord = 1;
}
else{
newWord = 0;
}
}
/*******space*******/
}
int main(){
char s[200];
gets(s);
capitalize(s);
puts(s);
return 0;
}
9.编写程序,输入一串字符串,统计该字符串中元音字母的个数并输出
#include<stdio.h>
#include<string.h>
int countVowel(char s[]){
/*******space*******/
int count = 0;
int i;
for(i=0;s[i]!='\0';i++){
char c = s[i];
if(c >= 'A' && c <= 'Z'){
c = c + 32;
}
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){ count++;
}
}
return count;
/*******space*******/
}
int main(){
char s[100];
gets(s);
printf("%d
", countVowel(s));
return 0;
}
10.编写程序,向数组a中输入对应n个整数,在该数组中找到第二大的数并输出
#include<stdio.h>
#include<limits.h>
int secondMax(int a[], int n){
/*******space*******/
int first=a[0];
int second=INT_MIN;
int i;
for(i=1;i<n;i++){
if(a[i] > first){
second = first;
first = a[i];
}
else if(a[i]>second &&a[i]!=first){
second = a[i];
}
}
return second;
/*******space*******/
}
int main(){
int a[100], n, i;
scanf("%d", &n);
for(i = 0; i<n;i++) scanf("%d", &a[i]);
printf("%d
",secondMax(a, n));
return 0;
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$
《C语言程序设计》
期末复习卷(五)
时间:90分钟 总分:100分
班级 姓名 学号 成绩
一.程序设计题(本大题共10小题,每小题10分,共100分)
1.编写程序,输入一串数字字符串,将该字符串转换成一个整数,要求不能使用字符串转换整数的库函数。
#include<stdio.h>
int strToInt(char *s){
/*******space*******/
/*******space*******/
}int main(){
char s[100];
scanf("%s", s);
printf("%d
", strToInt(s));
return 0;
}
2.编写程序,输入一个数,判断该数是否是快乐数,快乐数定义:各位数字平方和反复计算最终能变为1的数。
#include<stdio.h>
int isHappy(int n){
/*******space*******/
/*******space*******/
}int main(){
int n;
scanf("%d", &n);
printf("%s
", isHappy(n) ? "是快乐数" : "不是快乐数");
return 0;
}
3.编写程序输入一串字符串,统计该字符串中每个字符出现的次数,例如输入:hello ,输出:h:1 e:1 l:2 o:1
#include<stdio.h>
#include<string.h>
void charCount(char s[]){
/*******space*******/
/*******space*******/
}
int main(){
char s[100];
gets(s);
charCount(s);
return 0;
}
4.编写程序,输入一个整数n,并输入n名同学的单科成绩,要求统计并输出该科目成绩的平均分以及高于该平均分的人数
#include <stdio.h>
int aboveAvg(int score[], int n, float *avg) {
/*******space*******/
/*******space*******/
}int main(){
int score[100], n, i, count;
float avg;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &score[i]);
count = aboveAvg(score, n, &avg);
printf("平均分=%.2f, 高于平均分的人数=%d
", avg, count);
return 0;
}
5.编写程序,输入一个整数n,并输入对应的n个整数存放于数组a中,利用指针找出数组a中的最大值以及最小值
#include <stdio.h>
void findMinMax(int a[], int n, int *min, int *max) {
/*******space*******/
/*******space*******/
}
int main()
{
int a[100],n,i,min,max;
scanf("%d", &n);
for(i = 0;i<n; i++) scanf("%d", &a[i]);
findMinMax(a, n, &min, &max);
printf("min=%d max=%d
", min, max);
return 0;
}
6.编写程序,有一有序整数数组a,输入一个整数x,将该整数x插入到数组a中,并使数组仍然有序。例如原数组:1 3 5 7 9,输入整数4插入后数组顺序为:1 3 4 5 7 9
#include<stdio.h>
void insert(int a[],int *n,int x){
/*******space*******/
/*******space*******/
}
int main(){
int a[100] = {1,3,5,7,9}, n = 5, x, i;
scanf("%d", &x);
insert(a, &n, x);
for(i = 0; i < n; i++) printf("%d ", a[i]);
return 0;
}
7.编写程序,编写函数isPalindromeStr,该函数的功能为判断字符串是否为回文串
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int isPalindromeStr(char s[]){
/*******space*******/
/*******space*******/
}int main(){
char s[100];
gets(s);
if(isPalindromeStr(s)) printf("是回文串
");
else printf("不是回文串
");
return 0;
}
8.编写程序,输入一串英文单词字符串,每个单词之间使用空格隔开,完成功能将每个单词的首字母变大写。例如输入:hello world 输出:Hello World
#include<stdio.h>
#include<string.h>
void capitalize(char s[]){
/*******space*******/
/*******space*******/
}
int main(){
char s[200];
gets(s);
capitalize(s);
puts(s);
return 0;
}
9.编写程序,输入一串字符串,统计该字符串中元音字母的个数并输出
#include<stdio.h>
#include<string.h>
int countVowel(char s[]){
/*******space*******/
/*******space*******/
}
int main(){
char s[100];
gets(s);
printf("%d
", countVowel(s));
return 0;
}
10.编写程序,向数组a中输入对应n个整数,在该数组中找到第二大的数并输出
#include <stdio.h>
#include<limits.h>
int secondMax(int a[], int n){
/*******space*******/
/*******space*******/
}
int main(){
int a[100], n, i;
scanf("%d", &n);
for(i = 0; i<n;i++) scanf("%d", &a[i]);
printf("%d
",secondMax(a, n));
return 0;
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$