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

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

练习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;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值