面向對象程序設計寒假做業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;
}
相關文章
相關標籤/搜索