天天看點

面向對象程式設計作業(4)

第四次作業

GitHub:pullself

基本資訊

在主函數中加入了指令行判斷。以及一個簡陋的菜單。

代碼行數 調試bug 編碼時間
1032行 暫無 2h

程式總共由5個檔案組成:

  • main.cpp

    :主函數,程式的入口。
  • elevator.h

    :定義了電梯類,包含兩個電梯成員的封閉類以及兩個派生類。
  • elevator.cpp

    :電梯類方法的實作。
  • ask.h

    :定義了請求類。
  • ask.cpp

    :請求類方法的實作。

編碼過程

簡介

  1. 再稍微了解指令行之後,大緻覺得指令行的處理就是對字元串的處理,通過字元串來引導程式的運作。
  2. 在實作作業給出的兩種輸入之外,加入了一個循環的系統來引導提示輸入,確定程式的輸入輸出目錄合理。

問題

1.指令行的處理是一定要放在main函數中嗎?說實話這是個疑問。

bug修複

具體代碼與效果

功能介紹

  1. 在直接隻輸入input位址,會在input位址下生成三個output檔案來存儲。
  2. 直接輸入input位址以及任意(3以下)output位址,按照給的位址輸入輸出,未給出的output位址預設input目錄。
  3. 輸入任意其他内容,會給出相應提示,并且進入引導狀态。
  4. 具有五個功能:1.input位址輸入。2.output位址輸入。3.run:程式運作。4.view:檢視四個檔案位址。5.end:中止程式。

具體代碼

if (argc == 2)
	{
		if (strcmp(argv[1], "run") == 0)
		{
			cout << "   錯誤:缺少input檔案位址" << endl;
			cout << "   需要更多幫助請輸入help" << endl;
			error = 1;
		}
		else if (strcmp(argv[1], "help") == 0)
		{
			cout << "   *:\\****\\input.txt:輸入input檔案位址" << endl;
			cout << "   *:\\****\\outputi.txt:輸入output檔案位址,i=1,2,3,若不給出預設為input檔案目錄" << endl;
			cout << "   run:運作程式" << endl;
			cout << "   view:檢視檔案位址" << endl;
			cout << "   end:中止程式" << endl;
			error = 1;
		}
		else if (strcmp(argv[1], "view") == 0)
		{
			cout << "   input:缺失" << endl;
			cout << "   output1:目前目錄" << endl;
			cout << "   output2:目前目錄" << endl;
			cout << "   output3:目前目錄" << endl;
			error = 1;
		}
		else if (strcmp(argv[1], "end") == 0)
		{
			cout << "   運作終止" << endl;
			return -1;
		}
		else if (strstr(argv[1], "input") != NULL)
		{
			address_change(address[1], argv[1]);
			for (int i = 2; i < 5; i++)
			{
				address_copy(address[i], address[1], i - 1);
			}
			run_fun();
			cout << "   運作結束" << endl;
			return 0;
		}
		else if (strstr(argv[1], "output") != NULL)
		{
			cout << "   錯誤:缺少input檔案位址" << endl;
			out_change(argv[1]);
			cout << "   output位址已存儲" << endl;
			cout << "   需要更多幫助請輸入help" << endl;
			error = 1;
		}
		else
		{
			cout << "   錯誤:非法輸入" << endl;
			cout << "   需要更多幫助請輸入help" << endl;
			error = 1;
		}
	}
	else
	{
		if (argc == 1)
		{
			strcpy_s(address[1], "input.txt");
			strcpy_s(address[2], "output1.txt");
			strcpy_s(address[3], "output2.txt");
			strcpy_s(address[4], "output3.txt");
			run_fun();
			cout << "   運作結束" << endl;
			return 0;
		}
		else if (argc > 2)
		{
			if (strstr(argv[1], "input") == NULL)
			{
				cout << "   錯誤:非法輸入" << endl;
				cout << "   需要更多幫助請輸入help" << endl;
				error = 1;
			}
			else
			{
				address_change(address[1], argv[1]);
				for (int i = 2; i < 5; i++)
				{
					address_copy(address[i], address[1], i - 1);
				}
				for (int i = 2; i < argc; i++)
				{
					out_change(argv[i]);
				}
				run_fun();
				cout << "   運作結束" << endl;
				return 0;
			}
		}
	}
	while (error == 1)
	{
		cin >> order;
		if (strcmp(order, "run") == 0)
		{
			if (address[1][0] == '\0')
			{
				cout << "   錯誤:缺少input檔案位址" << endl;
				cout << "   需要更多幫助請輸入help" << endl;
			}
			else
			{
				for (int i = 2; i < 5; i++)
				{
					if (address[i][0] == '\0') address_copy(address[i], address[1], i - 1);
				}
				run_fun();
				cout << "   運作結束" << endl;
				return 0;
			}
		}
		else if (strcmp(order, "help") == 0)
		{
			cout << "   *:\\****\\input.txt:輸入input檔案位址" << endl;
			cout << "   *:\\****\\outputi.txt:輸入output檔案位址,i=1,2,3,若不給出預設為input檔案目錄" << endl;
			cout << "   run:運作程式" << endl;
			cout << "   view:檢視檔案位址" << endl;
			cout << "   end:中止程式" << endl;
		}
		else if (strcmp(order, "view") == 0)
		{
			if (address[1][0] == '\0') cout << "   input:缺失" << endl;
			else cout << "   input:" << address[1] << endl;
			for (int i = 2; i < 5; i++)
			{
				if (address[i][0] == '\0') cout << "   output" << i - 1 << ":目前目錄" << endl;
				else cout << "   output" << i - 1 << ":" << address[i] << endl;
			}
		}
		else if (strcmp(order, "end") == 0)
		{
			cout << "   運作終止" << endl;
			return -1;
		}
		else if (strstr(order, "input") != NULL)
		{
			address_change(address[1], order);
			for (int i = 2; i < 5; i++)
			{
				if (address[i][0] == '\0')
				{
					address_copy(address[i], address[1], i - 1);
				}
			}
			cout << "   已擷取input位址,程式可運作" << endl;
		}
		else if (strstr(order, "output") != NULL)
		{
			out_change(order);
			cout << "   output位址已存儲" << endl;
			if (address[1][0] == '\0') cout << "   警告:缺少input檔案位址" << endl;
		}
		else
		{
			cout << "   錯誤:非法輸入" << endl;
			cout << "   需要更多幫助請輸入help" << endl;
		}
	}
           
  1. run_fun:程式運作的入口。
  2. address_change:處理位址函數。
  3. address_copy:output複制函數。
  4. out_change:output處理函數。

實作效果

面向對象程式設計作業(4)
面向對象程式設計作業(4)