面向对象程序设计寒假做业1 解题报告

做业描述 详情
这个做业属于哪一个课程 2020年面向对象程序设计
这个做业要求在哪里 面向对象程序设计寒假做业1
这个做业的目标 1.了解c语言的缺点与c++的优势<br>2.了解c/c++的编译过程<br>3.查看c++编译器的版本<br>4.用命令行编译一份c/c++代码<br>5.编写一个程序,实现简单的中文编程
做业正文 面向对象程序设计寒假做业1 解题报告
其余参考文献 C语言和C++的区别<br>c/c++编译过程

问答题

1.c++和c相比的优势html

  • c++有了stl
  • c++支持&
  • c++支持函数重载
  • 分配/释放内存空间用new/delete,更简洁方便
  • c++做用域有局部,类,名字空间,而c只有局部,全局

2.c/c++编译过程c++

1.预处理git

  • 展开宏定义并删除#define语句
  • 处理条件编译指令 如#if
  • 处理#include
  • 输出注释语句
  • 添加行号和文件名标识

2.编译阶段github

  • 编译阶段,编译阶段时整个过程当中比较复杂的部分,编译器会将预处理以后的文件的内容,通过词法分析,获得所须要的Tokens,而后做对于的语法解析,语义解析,最后产生 .s结尾的汇编文件。

3.汇编阶段 4.链接阶段编程

实践题

1.查看本身的c++编译器版本app

首先安装mingw,为了方便直接将dev-cpp自带的添加到环境变量里,而后win + r,输入cmd回车,调出控制台,输入g++ -v指令查看编译器版本。函数

2.使用命令行编译代码编码

进入控制台cd指令进入cpp目录spa

调用指令g++ fliename.cpp,默认生成a.exe.net

这里为了支持c++11用了-std=c++11, -o file能够指定exe的文件名

编译成功就有了demo.exe生成

编程题

题目描述

中国文化博大精深,从仓颉造字开始,汉字一直流传到了今天。咱们在感叹汉字的源远流长时,也不由感慨,为何没有一门使用汉字编程的语言? 汉字真的不能编程吗?最近文言文编程火了一把,吾有一數。曰三。名之曰「甲」。这朴实无华的变量定义无疑不是几千年来中华文化的发展中一朵奇葩。 今天小王同窗想,文言文能编程那白话文呢?他找到了你,让你帮帮他。

编程要求

编写一个程序,输入知足如下语法要求的一段文字,输出运行后的结果。 变量定义:整数 钱包 等于 零 运算(加法):钱包 增长 四 运算(减法):钱包 减小 四 输出:看看 钱包

样例

输入:

整数 钱包 等于 零 钱包 增长 四 钱包 减小 三 看看 钱包

输出:

思路

1.编码问题

window10 (简体中文)的活动代码页为936,而本人使用st3,所以须要下载ConvertToUTF8插件来支持gbk编码

2.汉字与阿拉伯数字间的转换

考虑到数据范围只有0~99,所以采用map容器,生成一个一一对照的映射表

0~10能够手动输入

11~19分解为“十” + “*”

10的倍数分解为“*” + “十”

其他能够分解为 “*” + “*”

std::string base_num[11] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
	std::map<std::string, int>chs2num;
	std::map<int, std::string>num2chs;
	void make_chs2num_table() {
		for (int i = 0; i <= 10; ++i) 
			chs2num[base_num[i]] = i; // deal with 0 ~ 10
		for (int i = 1; i <= 9; ++i) {
			std::string a =  base_num[10];
			a += base_num[i];
			chs2num[a] = 10 + i;
		}// deal with 11 ~ 19
		for (int i = 2; i <= 9; ++i) {
			std:: string a = base_num[i];
			a += base_num[10];
			chs2num[a] = i * 10;
		}// deal with multiples of 10
		for (int i = 2; i <= 9; ++i) {
			std::string a = base_num[i];
			a += base_num[10];
			int num = i * 10;
			for (int j = 1; j <= 9; ++j) 
				chs2num[a + base_num[j]] = num + j;
		}
		return ;
	}

在有汉字转阿拉伯数字映射表格的基础上,能够经过遍历该表格,作一个逆映射,便可获得阿拉伯数字转汉字的表格

void make_num2chs_table() {
		make_chs2num_table();
		for (auto it : chs2num) {
			num2chs[it.second] = it.first;
		}// deal with num to chs
		return ;
	}

3.异常抛出

目前只想到了7种可能出现状况

void exception(int types) {
	if (types == 1)
		puts("请正确输入指令");
	else if (types == 2)
		puts("请勿重复定义相同的变量名");
	else if (types == 3)
		puts("请输入正确的数字");
	else if (types == 4)
		puts("这玩意还没被定义过");
	else if (types == 5)
		puts("请不要用关键词做为变量名");
	else if (types == 6) 
		puts("相加的数字过大");
	else if (types == 7)
		puts("相减的数字过大");
}

4.输入单条语句

以空格为标识,进行分割,并初步判断语句是否合法

// s 为传入的语句
	std::string tmp[5] = "";
	int cnt = 0;
	for (int i = 0; i < s.length(); ++i) {
		if (s[i] != ' ') {
			while(s[i] != ' ' && i < s.length()) {
				tmp[cnt] += s[i];
				i++;
			}
			cnt++;
		}
	}
	if (cnt <= 1) {
		exception(1);
		return ;
	}

5.定义变量

对变量是否重复定义,以及是否以关键词为变量名作出判断

if (tmp[0] == opt[0]) {//define variable
		if (name.count(tmp[1])) {//judge duplication
			exception(2);
			return ;
		}
		else if(tmp[1] == opt[0] || tmp[1] == opt[1] || tmp[1] == opt[2] || tmp[1] == opt[3]) {
			exception(5);
			return ;
		}
		else {
			if (!chs2num.count(tmp[3])) {
				exception(3); 
				return ;
			}// judeg if num is correct
			else {
				int initial_num = chs2num[tmp[3]];
				name[tmp[1]] = initial_num;
			}
		}
	}

6.查看某个变量

保证变量被定义过

else if (tmp[0] == opt[1]) {//look
		if (name.count(tmp[1]))
			std::cout << num2chs[name[tmp[1]]] << std::endl;
		else {
			exception(4);
			return ;
		}
	}

7.加/减处理

首先变量应该被定义过,其次相加/减的结果应该在0~99范围内部,并且输入的数字应该也在范围内

else {
    	if (cnt != 3) {
			exception(1);
			return ;
		}
		if (!name.count(tmp[0])) {
			exception(4);
			return ;
		}
		if (tmp[1] == opt[2]) {//add
			int add = name[tmp[0]];
			if (!chs2num.count(tmp[2])) {
				exception(3);
				return ;
			}
			add += chs2num[tmp[2]];
			if (add >= 100) { //too large
				exception(6);
				return ;
			}
			name[tmp[0]] = add;
		}

		else if (tmp[1] == opt[3]) {//decrease
			int de = name[tmp[0]];
			if (!chs2num.count(tmp[2])) {
				exception(3);
				return ;
			}
			de -= chs2num[tmp[2]];
			if (de < 0) {// too low
				exception(7);
				return ;
			}
			name[tmp[0]] = de;
		}
	}

程序实现结果

完整代码

#include <bits/stdc++.h>

std::string base_num[11] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
std::string opt[4] = {"整数", "看看", "增长", "减小"};
std::map<std::string, int> name; //variable name

namespace mapping_table {
	std::map<std::string, int>chs2num;
	std::map<int, std::string>num2chs;
	void make_chs2num_table() {
		for (int i = 0; i <= 10; ++i) 
			chs2num[base_num[i]] = i; // deal with 0 ~ 10
		for (int i = 1; i <= 9; ++i) {
			std::string a =  base_num[10];
			a += base_num[i];
			chs2num[a] = 10 + i;
		}// deal with 11 ~ 19
		for (int i = 2; i <= 9; ++i) {
			std:: string a = base_num[i];
			a += base_num[10];
			chs2num[a] = i * 10;
		}// deal with multiples of 10
		for (int i = 2; i <= 9; ++i) {
			std::string a = base_num[i];
			a += base_num[10];
			int num = i * 10;
			for (int j = 1; j <= 9; ++j) 
				chs2num[a + base_num[j]] = num + j;
		}
		return ;
	}
	void make_num2chs_table() {
		make_chs2num_table();
		for (auto it : chs2num) {
			num2chs[it.second] = it.first;
		}// deal with num to chs
		return ;
	}
	void Make_table() {
		make_num2chs_table();
		return ;
	}
} 

using namespace mapping_table;

void exception(int types) {
	if (types == 1)
		puts("请正确输入指令");
	else if (types == 2)
		puts("请勿重复定义相同的变量名");
	else if (types == 3)
		puts("请输入正确的数字");
	else if (types == 4)
		puts("这玩意还没被定义过");
	else if (types == 5)
		puts("请不要用关键词做为变量名");
	else if (types == 6) 
		puts("相加的数字过大");
	else if (types == 7)
		puts("相减的数字太小");
}
void analysis_run_line(std::string s) {
	std::string tmp[5] = "";
	int cnt = 0;
	for (int i = 0; i < s.length(); ++i) {
		if (s[i] != ' ') {
			while(s[i] != ' ' && i < s.length()) {
				tmp[cnt] += s[i];
				i++;
			}
			cnt++;
		}
	}
	if (cnt <= 1) {
		exception(1);
		return ;
	}
	if (tmp[0] == opt[0]) {//define variable
		if (name.count(tmp[1])) {//judge duplication
			exception(2);
			return ;
		}
		else if(tmp[1] == opt[0] || tmp[1] == opt[1] || tmp[1] == opt[2] || tmp[1] == opt[3]) {
			exception(5);
			return ;
		}
		else {
			if (!chs2num.count(tmp[3])) {
				exception(3); 
				return ;
			}// judeg if num is correct
			else {
				int initial_num = chs2num[tmp[3]];
				name[tmp[1]] = initial_num;
			}
		}
	}

	else if (tmp[0] == opt[1]) {//look
		if (name.count(tmp[1]))
			std::cout << num2chs[name[tmp[1]]] << std::endl;
		else {
			exception(4);
			return ;
		}
	}

	else {
		if (cnt != 3) {
			exception(1);
			return ;
		}
		if (!name.count(tmp[0])) {
			exception(4);
			return ;
		}
		if (tmp[1] == opt[2]) {//add
			int add = name[tmp[0]];
			if (!chs2num.count(tmp[2])) {
				exception(3);
				return ;
			}
			add += chs2num[tmp[2]];
			if (add >= 100) { //too large
				exception(6);
				return ;
			}
			name[tmp[0]] = add;
		}

		else if (tmp[1] == opt[3]) {//decrease
			int de = name[tmp[0]];
			if (!chs2num.count(tmp[2])) {
				exception(3);
				return ;
			}
			de -= chs2num[tmp[2]];
			if (de < 0) {// too low
				exception(7);
				return ;
			}
			name[tmp[0]] = de;
		}
	}
}
int main() {
	Make_table();
	std::string s;
	while(std::getline(std::cin, s)) {
		analysis_run_line(s);
	}
	return 0;
}
相关文章
相关标签/搜索