C语言的自学之路(习题篇)

阅读: 评论:0

C语言的自学之路(习题篇)

C语言的自学之路(习题篇)

C语言的自学之路(习题篇)

  • 练习01
  • 练习02
  • 练习03
  • 练习04
  • 练习05

练习01

编写程序可以把字母格式的电话号码翻译成数值格式: Enter phone number :
您输入的电话号为:CALLATT 2255288
其中字母对应的数字为(2=AaBbCc,3=DdEeFf,4=GgHhIi,5=JjKkLl,6=MmNnOo,7=PpRrSs,8=TtUuVv,9=WwXxYyz)

#include <stdio.h>int main(void)
{printf("Enter phone number:");char ch;scanf_s("%c", &ch);while(ch != 'n'){if (ch >= 65 && ch <= 90) {switch (ch){case 'A':case 'a':case 'B':case 'b':case 'C':case 'c':printf("2");scanf_s("%c", &ch);continue;case 'D':case 'd':case 'E':case 'e':case 'F':case 'f':printf("3");scanf_s("%c", &ch);continue;case 'G':case 'g':case 'H':case 'h':case 'I':case 'i':printf("4");scanf_s("%c", &ch);continue;case 'J':case 'j':case 'K':case 'k':case 'L':case 'l':printf("5");scanf_s("%c", &ch);continue;case 'M':case 'm':case 'N':case 'n':case 'O':case 'o':printf("6");scanf_s("%c", &ch);continue;case 'P':case 'p':case 'R':case 'r':case 'S':case 's':printf("7");scanf_s("%c", &ch);continue;case 'T':case 't':case 'U':case 'u':case 'V': case 'v':printf("8");scanf_s("%c", &ch);continue;case 'W':case 'w':case 'X':case 'x':case 'Y':case 'y':case 'Z':case 'z':printf("9");scanf_s("%c", &ch);continue;default:break;}}else{printf("%c", ch);scanf_s("%c", &ch);continue;}}return 0;
}

感觉有点繁琐,不知道大神有没有更简单的方法。
本题目来源《C语言程序设计现代方法 (第2版)》

练习02

在十字拼字游戏中,玩家利用小卡片组成单词,每个卡片包含字母和面值。面值根据字母的稀缺程度的不同而不同。(面值有:1——AEILNORSTU,2——DG,3——BCMP,4——FHVWY,5——K,8——JX,10——QZ。)编写程序通过对单词中字母的面值求和来计算单词的值:
Enter a word:pitfall
Scrabble value:12

#include <stdio.h>
#include <ctype.h>int main(void) {printf("Enter a word:");char ch;scanf_s("%c", &ch);ch = toupper(ch);int sum=0,num;while (ch !='n'){switch (ch){case 'A':case 'E':case 'I':case 'L':case 'N':case 'O':case 'R':case 'S':case 'T':case 'U':num = 1;sum += num;scanf_s("%c", &ch);ch = toupper(ch);continue;case 'D':case 'G':num = 2;sum += num;scanf_s("%c", &ch);ch = toupper(ch);continue;case 'B':case 'C':case 'M':case'P':num = 3;sum += num;scanf_s("%c", &ch);ch = toupper(ch);continue;case 'F':case 'H':case 'V':case 'W':case 'Y':num = 4;sum += num;scanf_s("%c", &ch);ch = toupper(ch);continue;case 'K':num = 5;sum += num;scanf_s("%c", &ch);ch = toupper(ch);continue;case 'J':case 'X':num = 6;sum += num;scanf_s("%c", &ch);ch = toupper(ch);continue;case 'Q':case 'Z':num = 10;sum += num;scanf_s("%c", &ch);ch = toupper(ch);continue;default:break;}}printf("%d", sum);return 0;
}

感觉就是和上次差不多的,只不过用toupper函数来简化大小写问题。

练习03

编写程序要求用户输入12小时制的时间,然后用24小时显示该时间
Enter a 12-hour time:9:11 PM
Equivalent 24-hour time:21:11

#include <stdio.h>
#include <ctype.h>int main(void) {printf("Enter a 12-hour time:");char time;int m, n;scanf_s("%d:%d %c", &m, &n, &time);switch (toupper(time)){case 'A':printf("Equivalent 24-hour time:%d:%d", m,n);case 'P':m += 12;printf("Equivalent 24-hour time:%d:%d", m, n);default:break;printf("Miss");}return 0;
}

这样子简约了一些,把能放括号里面和能省略判断的提取出来了。

练习04

编写程序统计句子中的元音字母(a、e、i、o、u)的个数:
Enter a sentence: And that’s the way it is.
Your sentence contains 6 vowels.

#include <stdio.h>
#include <ctype.h>int main(void) {printf("Enter a sentence:");char ch;int sum=0;while ((ch=getchar())!='n'){switch (toupper(ch)){case 'A': case 'E':case 'I':case 'O':case 'U':sum += 1;default:break;}}printf("%d", sum);return 0;
}

练习05

编写一个程序,根据用户输入的英文名和姓显示姓氏,其后跟一个逗号,然后显示名的首字母,最后加一个点。
Enter a first and last name:Lloyd Fosdick
Fosdick, L.

#include <stdio.h>
#include <ctype.h>int main(void) {printf("Enter a first and last name:");char ch;char first_ch;int i = 0;int j = 0;while ((ch = getchar()) != 'n'){if (i == 0) {first_ch = ch;i++;}if (ch == ' ') {j++;continue;}if (j != 0) {printf("%c",ch);}}printf(",%c.", first_ch);return 0;
}

本文发布于:2024-01-29 05:49:00,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170647854513140.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:VUE游戏九宫格
标签:之路   习题   语言
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23