C语言程序设计精髓(MOOC第10周 )题

编程入门 行业动态 更新时间:2024-10-25 18:27:37

C语言程序设计<a href=https://www.elefans.com/category/jswz/34/1740656.html style=精髓(MOOC第10周 )题"/>

C语言程序设计精髓(MOOC第10周 )题

第10周编程题在线测试

NOTE:

  • 指针警示: 永远清楚自己在操作哪块内存;永远清楚自己的操作是否合理、合法。
  • 对于 char *ptr = “hello”,指的是将hello这个字符串常量的首地址赋值给ptr指针变量,那么我们知道,字符串常量是存储在常量存储区中的,故只能对其进行读操作,无法进行写操作,也就无法通过 *ptr = ‘w’ 来修改字符串使其从"hello"变成"wello"。但我们可以修改指针变量ptr的值,另其指向别的字符类型的数据。
  • 同样,对于 char str[] = “hello”,str作为数组名是一个地址常量,故不能修改,但能修改字符串"hello"。

1. 数字字符串转换为整型数

题目内容:
从键盘输入一串字符(假设字符数少于8个),以回车表示输入结束,编程将其中的数字部分转换为整型数并以整型的形式输出。

函数原型: int Myatoi(char str[]);

其中,形参数组str[]对应用户输入的字符串,函数返回值为转换后的整型数。

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int Myatoi(char str[]);
int main()
{char str[8];printf("Input a string:");scanf("%7s", str);printf("%d\n", Myatoi(str));return 0;
}
int Myatoi(char str[])
{char num[8];char *p = num;char *q = str;while(*q){if(isdigit(*q)){*p = *q;p++;}q++;}return atoi(num);
}

2. 查找子串

题目内容:
用字符数组作函数参数,编程实现在从键盘输入的字符串(假设长度小于80)中查找与指定的子串,并输出该子串在字符串中首次出现的位置,如果该字符不存在,则输出"Not found!"。

函数原型: int SearchString(char s[], char d[])

函数功能: 在字符数组s中查找子串d,返回d在s中首次出现的位置,若找不到,则返回-1。

#include<stdio.h>
#include<string.h>
#define MAX_LEN 80
int SearchString(char s[], char d[]);
int main()
{char s[MAX_LEN + 1],d[MAX_LEN + 1];printf("Input a string:");gets(s);printf("Input another string:");gets(d);if(SearchString(s,d) != -1)printf("Searching results:%d\n",SearchString(s,d));elseprintf("Not found!\n");return 0;
}int SearchString(char s[], char d[])
{int slen, dlen, pos;int i=0, j=0, find;slen = strlen(s);dlen = strlen(d);for(i = 0;i < slen; i++){pos = i;for(j = 0;j < dlen; j++){if(s[pos + j] != d[j])break;}if(j == dlen)return (pos + 1);}return -1;
}

3. 统计连续重复字符

题目内容:
输入一串字符(字符数小于80),以回车表示输入结束,编程计算并输出这串字符中连续重复次数最多的字符和重复次数。如果重复次数最多的字符有两个,则输出最后出现的那一个。

函数原型: int CountRepeatStr(char str[], int *tag);

#include<stdio.h>
#define STR_LEN 80
int CountRepeatStr(char str[], int *tag);
int main()
{char str[STR_LEN + 1];int ret,tag;printf("Input a string:\n");gets(str);ret = CountRepeatStr(str,&tag);printf("%c:%d\n",str[tag],ret);return 0;
}int CountRepeatStr(char str[], int *tag)
{int count = 1;int max = count;*tag = 0;int i = 0;while(str[i] != '\0'){if(str[i] == str[i + 1]){count++;if(count >= max){*tag = i + 1;max = count;}}else{count = 1;}i++;}return max;
}

4. 凯撒密码

题目内容:
凯撒密码是罗马扩张时期朱利斯•凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令,其原理很简单,就是通过将字母表中的字母移动一定位置而实现加密。例如,每个字母按字母表顺序向后移3位,如a加密后变成d,b加密后变成e,……x加密后变成a,y加密后变成b,z加密后变成c。请编写一个程序,将用户从键盘输入的文本字符串(只包含a~z的字符且长度小于100)进行加密后输出。

函数原型: void Caesar(char c[]);

函数功能: 计算凯撒密码

#include<stdio.h>
#define MAX 100
void Caesar(char c[]);
int main()
{char str[MAX + 1];printf("Input a string:");gets(str);Caesar(str);return 0;
}void Caesar(char c[])
{char *p ;p = &c[0];while(*p){if(*p == 'x')*p = 'a';else if(*p == 'y')*p = 'b';else if(*p == 'z')*p = 'c';else*p = *p + 3;p++;}puts(c);
}

第10周练兵区编程题

1. 有趣的“回文”检测

题目内容:
英文中有很多的回文词,回文词的拼法十分有趣,无论是从前往后拼读,还是从后往前拼读,他们的拼法和词义都不变。例如:dad(爸爸),mum(妈妈),noon(中午),eve(前夕),eye(眼睛),pop(流行),deed(行为),level(水平)等。简单地说,“回文”就是指顺读和倒读都一样的字符串。现在请你编程输入一个单词,判断它是否是回文。

提示:

(1)设置两个指针pStart和pEnd,让pStart指向字符串首部,让pEnd指向字符串尾部。

(2)利用循环从字符串两边对指针所指字符进行比较,当对应的两字符相等且两指针未超越对方时,使指针pStart向前移动一个字符位置(加1),使指针pEnd向后移动一个字符位置(减1),一旦发现两字符不等或两指针已互相超越(不可能是回文),则立即停止循环。

(3)根据退出循环时两指针的位置,判断字符串是否为回文。

#include<stdio.h>
#include<string.h>
#define N 100
int main()
{char str[N + 1];int str_len;char *pstart,*pend;printf("Input string:");gets(str);str_len = strlen(str);pstart = &str[0];pend = &str[str_len - 1];while(pstart < pend){if(*pstart == *pend){pstart++;pend--;}elsebreak;}if(pstart >= pend)printf("Yes!\n");elseprintf("No!\n");return 0;
}

2. 学生成绩管理系统V1.0

题目内容:
某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,用一维数组作函数参数编程实现如下学生成绩管理:

(1)录入每个学生的学号和考试成绩;

(2)计算课程的总分和平均分;

(3)按成绩由高到低排出名次表;

(4)按学号由小到大排出成绩表;

(5)按学号查询学生排名及其考试成绩;

(6)按优秀(90~ 100)、良好(80~ 89)、中等(70~ 79)、及格(60~ 69)、不及格(0~ 59)5个类别,统计每个类别的人数以及所占的百分比;

(7)输出每个学生的学号、考试成绩。

#include<stdio.h>
#include<string.h>
void Input(long id[],float score[],int n);
float TotalScore(long id[],float score[],int n);
float AveScore(long id[],float score[],int n);
void score_dsort(long id[],float score[],int n);
void id_asort(long id[],float score[],int n);
void find_id(long id[],float score[],int n);
void analysis(long id[],float score[],int n);
int main()
{int n,m,ret;printf("Input student number(n<30):\n");scanf("%d",&n);long id[n];float score[n];do{printf("Management for Students' scores\n");printf("1.Input record\n");printf("2.Caculate total and average score of course\n");printf("3.Sort in descending order by score\n");printf("4.Sort in ascending order by number\n");printf("5.Search by number\n");printf("6.Statistic analysis\n");printf("7.List record\n");printf("0.Exit\n");printf("Please Input your choice:\n");ret = scanf(" %d",&m);switch(m){case 1:Input(id,score,n);break;case 2:printf("sum=%.0f,aver=%.2f\n",TotalScore(id,score,n),AveScore(id,score,n));break;case 3:printf("Sort in descending order by score:\n");score_dsort(id,score,n);break;case 4:printf("Sort in ascending order by number:\n");id_asort(id,score,n);break;case 5:find_id(id,score,n);break;case 6:analysis(id,score,n);break;case 7:id_asort(id,score,n);break;default:break;}if(ret != 1 || m < 1 || m > 7)break;}while(1);if(ret != 1 || m < 0 || m > 7)printf("Input error!\n");if(m == 0)printf("End of program!\n");return 0;
}void Input(long id[],float score[],int n)
{printf("Input student's ID, name and score:\n");for(int i = 0; i < n; i++)scanf("%ld%f",&id[i],&score[i]);
}float TotalScore(long id[],float score[],int n)
{float sum = 0;for(int i = 0; i < n; i++)sum += score[i];return sum;
}float AveScore(long id[],float score[],int n)
{float sum = 0;for(int i = 0; i < n; i++)sum += score[i];return sum/n;
}void score_dsort(long id[],float score[],int n)
{int i,j;for(i = 0;i < n ; i++){for(j = i + 1; j < n ; j++){if(score[j] > score[i]){float ex_score;long ex_id;ex_score = score[i];score[i] = score[j];score[j] = ex_score;ex_id = id[i];id[i] = id[j];id[j] = ex_id;}}}for(i = 0;i < n; i++)printf("%ld\t%.0f\n",id[i],score[i]);
}void id_asort(long id[],float score[],int n)
{int i,j;for(i = 0;i < n ; i++){for(j = i + 1; j < n ; j++){if(id[j] < id[i]){float ex_score;long ex_id;ex_score = score[i];score[i] = score[j];score[j] = ex_score;ex_id = id[i];id[i] = id[j];id[j] = ex_id;}}}for(i = 0;i < n; i++)printf("%ld\t%.0f\n",id[i],score[i]);
}void find_id(long id[],float score[],int n)
{long num;printf("Input the number you want to search:\n");scanf("%ld",&num);for(int i = 0;i < n; i++){if(num == id[i]){printf("%ld\t%.0f\n",id[i],score[i]);return;}}printf("Not found!\n");
}void analysis(long id[],float score[],int n)
{float a1,b1,c1,d1,e1,f1;int a2,b2,c2,d2,e2,f2;a2 = b2 = c2 = d2 = e2 = f2 = 0;a1 = b1 = c1 = d1 = e1 = f1 = 0;for(int i = 0; i < n; i++){switch((int)(score[i]/10)){case 10:a2++;break;case 9:b2++;break;case 8:c2++;break;case 7:d2++;break;case 6:e2++;break;default:f2++;break;}}a1 = a2*1.0/n*100;b1 = b2*1.0/n*100;c1 = c2*1.0/n*100;d1 = d2*1.0/n*100;e1 = e2*1.0/n*100;f1 = f2*1.0/n*100;printf("<60\t%d\t%.2f%%\n",f2,f1);printf("%d-%d\t%d\t%.2f%%\n",60,69,e2,e1);printf("%d-%d\t%d\t%.2f%%\n",70,79,d2,d1);printf("%d-%d\t%d\t%.2f%%\n",80,89,c2,c1);printf("%d-%d\t%d\t%.2f%%\n",90,99,b2,b1);printf("%d\t%d\t%.2f%%\n",100,a2,a1);
}

3. 程序改错——1

#include <stdio.h>
#include <string.h>
char* MyStrcat(char *dest, char *source);
int main(void)
{char *first, *second, *result;char dest[81], src[81];first = dest;second = src;printf("Input the first string:\n");gets(dest);printf("Input the second string:\n");gets(src);result = MyStrcat(first, second);printf("The result is : %s\n", result);return 0;
}
char* MyStrcat(char *dest, char *source)
{int i = 0, p = 0;while (*(dest+i)!='\0')   i++;for (; *(source+p)!='\0'; i++, p++){*(dest+i) = *(source+p);}*(dest+i) = '\0';return dest;
}

4. 程序改错——2

#include<stdio.h>
#define  ARR_SIZE  5
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{int  a[ARR_SIZE][ARR_SIZE];YH(a, ARR_SIZE);PrintYH(a, ARR_SIZE);return 0;
}
void YH(int a[][ARR_SIZE], int n)
{int  i, j ;for (i=1; i<n; i++){a[i][1] = 1;a[i][i] = 1;}for (i=3; i<n; i++){for (j=2; j<=i-1; j++){a[i][j] = a[i-1][j-1] + a[i-1][j];}}
}
void PrintYH(int a[][ARR_SIZE], int n)
{int i , j ;for (i=1; i<n; i++){for (j=1; j<=i; j++){printf("%4d", a[i][j]);}printf("\n");}
}

5. 出售金鱼

题目内容:
买买提将养的一缸金鱼分五次出售:第一次卖出全部的一半加二分之一条;第二次卖出余下的三分之一加三分之一条;第三次卖出余下的四分之一加四分之一条;第四次卖出余下的五分之一加五分之一条;最后卖出剩下的11条。问原来鱼缸中共有几条鱼?

#include<stdio.h>
int main()
{float num = 11;for(int i = 5;i >=2; i--){num = (num + 1.0/i)/(1 - 1.0/i);}printf("There are %d fishes at first.\n",(int)num);return 0;
}

6. 找最值

题目内容:
从键盘任意输入10个整数,用指针变量作函数参数编程计算最大值和最小值,并返回它们所在数组中的位置。

函数原型: int FindMax(int num[], int n, int *pMaxPos);//函数返回最大值,pMaxPos返回最大值所在的下标

int FindMin(int num[], int n, int *pMinPos);//函数返回最小值,pMaxPos返回最小值所在的下标

#include<stdio.h>
int FindMax(int num[], int n, int *pMaxPos);
int FindMin(int num[], int n, int *pMinPos);
int main()
{int a[10] = {0};int pMaxPos,pMinPos,max,min;pMaxPos = 0;pMinPos = 0;printf("Input 10 numbers:\n");for(int i = 0; i < 10; i++)scanf("%d",&a[i]);max = FindMax(a,10,&pMaxPos);min = FindMin(a,10,&pMinPos);printf("Max=%d,Position=%d,Min=%d,Position=%d\n",max,pMaxPos,min,pMinPos);return 0;
}int FindMax(int num[], int n, int *pMaxPos)
{int max = num[0];for(int i = 0;i < n; i++){if(num[i] > max){max = num[i];*pMaxPos = i;}}return max;
}int FindMin(int num[], int n, int *pMinPos)
{int min = num[0];for(int i = 0;i < n; i++){if(num[i] < min){min = num[i];*pMinPos = i;}}return min;
}

7. 杨辉三角形

题目内容:
编程打印具有如下形式的杨辉三角形,其中输出数据的行数n从键盘输入,并且n<=10。

#include<stdio.h>
int main()
{int n;printf("Input n (n<=10):\n");scanf("%d",&n);int a[n][n];for(int i = 0;i < n; i++){a[i][0] = 1;a[i][i] = 1;}for(int i = 2;i < n; i++){for(int j = 1;j < i;j++){a[i][j] = a[i - 1][j - 1] + a[i - 1][j];}}for(int i = 0;i < n; i++){for(int j = 0; j <= i; j++){printf("%4d",a[i][j]);}printf("\n");}return 0;
}

8. 颠倒句子中的单词顺序

题目内容:
从键盘输入一个句子(假设字符数小于100个),句子中的单词之间用空格分隔,句子必须以一个标点符号作为结尾,句子开头和末尾标点符号前均没有空格,以回车表示输入结束,请编程颠倒句中的单词顺序并输出。

函数原型: int Inverse(char str1[], char str2[][N])

#include<stdio.h>
#define N 100
int Inverse(char str1[], char str2[][N]);
int main()
{char str1[N],str2[N][N];printf("Input a sentence:");gets(str1);int num = Inverse(str1,str2);for(;num > 0; num--){printf("%s ",str2[num]);}printf("%s%c\n",str2[0],str1[0]);return 0;
}int Inverse(char str1[], char str2[][N])
{int num = 0,i;int j = 0;int str1_len = strlen(str1);for(i = 0; i < str1_len - 1; i++){if(str1[i] != ' '){str2[num][j++] = str1[i];}else{num++;j = 0;}}str1[0] = str1[str1_len - 1];return num;
}

更多推荐

C语言程序设计精髓(MOOC第10周 )题

本文发布于:2024-02-12 08:23:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1686946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:精髓   语言程序设计   MOOC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!