⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 name.cpp

📁 c语言教程源码
💻 CPP
字号:
//这个程序在本书所带软盘中。文件名为NAME.CPP
//这个程序将汉语拼音姓名按标准英文方式输出。

#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>

#define size 41

void main(void)
{
	int process(char*, int);
	void output(char*);						//定义输出子程序

	char name[size];

	cout << "这个程序将你的汉语拼音姓名按标准英文方式输出。" << endl;
	cout << "请按中文拼音输入你的名字:";

	while (gets(name) != NULL) {
		if (process(name, strlen(name))) 	//调用规范处理名字的子程序
			output(name);					//调用输出子程序
		else								//用户输入有问题
			cout << "你的输入有问题,请按标准中文拼音方法输入你的名字。" << endl;
		cout << "请按中文拼音输入你的名字(按下Ctrl+z停止程序运行):";
	}
}

/************子程序process()************************/
int process(char *array, int len)
{
	char *text, *last_name;
	int pos = -1;
	int j = 0;

	text = new char(len+1);		//给一个临时字符串变量动态分配字节

	//找到姓名中空格的位置
	for (int i = 0; i < len; i++) {
		if (*(array+i) == ' ') {
			pos = i;
			break;
		}
	}
	if (i == len)				//说明姓名中没有空格
		return pos;				//返回假值-1
	else {						//将名和最后的空格拷贝到临时字符串text
		for (int i = pos+1; i < len; i++) {
			*(text+j) = *(array+i);
			j++;
		}
		*(text+j++) = ' ';		//在名的最后加上一个空格
		*(text+j) = '\0';		//在空格后面加上字符串终止符

		last_name = new char(len-pos+1);

		for (i = 0; i < pos; i++)
			*(last_name+i) = *(array+i);	//把姓拷贝到临时字符串last_name中
		*(last_name+i) = '\0';				//在其末尾加上字符串终止符

		strcat(text, last_name);			//把姓连接到临时字符串text的最后

		*(text) = toupper(*(text));			//将名的第一个字母转换成大写

		for (i = 1; i < len; i++)
			if (*(text+i-1) == ' ')			//说明上一个字母是姓的第一个字母
				*(text+i) = toupper(*(text+i));
			else							//是姓名中的其他字母,应当是小写
				*(text+i) = tolower(*(text+i));

		strcpy(array, text);				//将规范化了的姓名text拷贝到字符串array中

		return pos;							//返回真值
	}
}

/***********子程序output()****************************/
void output(char *array)
{
	puts("你的名字的英文规范书写方式为:");
	puts(array);
}

/*下面是这个程序运行后的一个典型输出结果:
这个程序将你的汉语拼音姓名按标准英文方式输出.
请按中文拼音输入你的名字:Gao Yongqiang
你的名字的英文规范书写方式为:
Yongqiang Gao
请按中文拼音输入你的名字(按下Ctrl+z停止程序运行):
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -