c++學習筆記=>;萬丈高樓平地起=>;ex3:運算式練習

距離上一篇 c++學習筆記=>萬丈高樓平地起=>ex2:參數輸入、printf() 、 scanf() 已經一年多,利用春節這幾天空間再把一些以前的教學資料整理一下。這一篇要練習的是程式語言的基本元素,運算式與運算子的應用。

常見的C++運算子列表如下:

運算子名稱 運算子符號 符號分類
加 Addition + 數學運算
減 Substration 數學運算
乘 Multiplication * 數學運算
除 Division / 數學運算
餘數 Modulus % 數學運算
等於 Equality == 大小關係
大於等於 Greater than or equal to >= 大小關係
大於 Greater than > 大小關係
小於等於 Less than or equal to <= 大小關係
小於 Less than < 大小關係
不等於 Not equal != 大小關係
位元且運算 Bitwise AND & 位元運算
位元互斥或運算 Bitwise exclusive OR ^ 位元運算
位元或運算 Bitwise inclusive OR | 位元運算
位元左移 Left shift << 位元運算
位元右移 Right shift >> 位元運算
一位補數 One’s Complement ~ 位元運算
加指定 Addition Assignment += 變數指定
減指定 Subtraction Assignment -= 變數指定
乘指定 Multiplication Assignment *= 變數指定
除指定 Division Assignment /= 變數指定
餘數指定 Modulus Assignment %= 變數指定
位元且指定 Bitwise AND Assignment &= 變數指定
位元互斥或指定 Bitwise exclusive OR Assignment ^= 變數指定
位元或指定 Bitwise inclusive OR Assignment |= 變數指定
位元左移指定 Left shift Assignment <<= 變數指定
位元右移指定 Right shift Assignment >>= 變數指定
遞減 Postfix decrement  
遞加 Postfix increment ++  
正 Unary positive + 正負標記
負 Unary negative 正負標記
非 Logical Negation ! 邏輯運算
且 Logical AND && 邏輯運算
或 Logical OR || 邏輯運算
取址 Address-of & 記憶體位置運算
指向 Indirection * 記憶體位置運算
物件成員存取 Member access . or –> 物件操作
物件成員指標存取 Pointer-to-member .* or –>* 物件操作
條件運算 Conditional ?  
範圍解析 Scope resolution :: 宣告物件成員
     

為了讓學生有練習的環境,改用免費的Microsoft Visual C++ 2010 Express 作為教學軟體,一樣我們新增一個"win32主控台應用程式"專案,名稱設定為ex3

打開ex3.cpp在程式進入點中輸入以下程式碼:

// ex3.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	//運算式練習
	int x = 10 ;
	int y = 2 ;
	int z = 0 ;
	bool w = true ;
	string myname = argv[1] ;
	cout << myname << "的c++練習範例:簡單運算子練習!\n" ;
	cout << "宣告變數 int x = 10;" <<  "\n";
                cout << "宣告變數 int y = 2;" <<  "\n";
                cout << "宣告變數 int z = 0;" <<  "\n";
                cout << "宣告變數 bool w = true ;" <<  "\n";
	cout << "加 x+y=" << (x + y) << "\n";
	cout << "減 x-y=" << (x- y) << "\n";
	cout << "乘 x*y=" << (x * y) << "\n";
	cout << "除 x/y=" << (x / y) << "\n";
	cout << "餘數 x%y=" << (x % y) << "\n";
                cout << "非! x=" << (! x) << "\n" ;
	cout << "非! z=" << (! z) << "\n" ;
	cout << "等於 x==y ="<< (x==y) << "\n" ;
	cout << "不等於 x!=y ="<< (x!=y) << "\n" ;
	cout << "等於 (x==w) ="<< (x==w) << "\n" ;
	cout << "位元運且運算 (2&16)=" << (2&16)<<"\n";
                cout << "位元互斥或運算 (2^16)=" << (2^16)<<"\n";
	cout << "位元運或運算 (2|16)=" << (2|16)<<"\n";
	x+=3;
                cout << "加指定 (x+=3) ="<< x << "\n" ;
	x-=3;
                cout << "減指定 (x-=3) ="<< x << "\n" ;
                x*=3;
                cout << "乘指定 (x*=3) ="<< x << "\n" ;
                x/=3;
                cout << "除指定 (x/=3) ="<< x << "\n" ;

	return 0;
}

再來打開stdafx.h,在這個標頭檔加入以下的程式碼

// stdafx.h : 可在此標頭檔中包含標準的系統 Include 檔,
// 或是經常使用卻很少變更的
// 專案專用 Include 檔案
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <iostream> 
#include <string>


// TODO: 在此參考您的程式所需要的其他標頭

執行建置方案,為了方便看到執行的結果,在…\Visual Studio 2010\Projects\ex3\Debug 方案目錄中加入一個批次檔do.bat內容如下:

ECHO OFF

::代表在目錄中找到.exe類型的檔案然後傳入字串參數"楊煥謀" 
for %%e in (*.exe) do %%e "楊煥謀" 
pause

只要執行do.bat可以看到程式的執行結果

image