C++函數基礎-預處理,條件編譯

預處理指令

在這裏插入圖片描述

文件包含

在這裏插入圖片描述
在這裏插入圖片描述

條件編譯

在這裏插入圖片描述
在這裏插入圖片描述

#include<iostream>
using namespace std;
int main()
{
    extern int x; //外部變量
    cout << "外部變量x=" << x << endl;
    return 0;
}
int x = 8;

在這裏插入圖片描述

在這裏插入圖片描述

#include<iostream>
#include "printdiamond.h"
using namespace std;
int main()
{
	int row;
	cout << "請輸入菱形的行數(奇數):";
	cin >> row;
	if (row % 2 != 0)
		printdiamond(row);
	else
		cout << "應該輸入一個奇數行數,不能是偶數" << endl;
	return 0;
}

#include <iostream>
using  namespace  std;
void printdiamond(int n)
{
	int i, j, k;
	for (i = 1; i <= (n + 1) / 2; i++)  //上半部分
	{
		for (j = 1; j <= (n + 1) / 2 - i; j++)
			cout << " ";
		for (k = 1; k <= 2 * i - 1; k++)
			cout << "*";
		cout << endl;
	}
	for (i = 1; i <= (n - 1) / 2; i++)   //下半部分
	{
		for (j = 1; j <= i; j++)
			cout << " ";
		for (k = n - 2 * i; k >= 1; k--)
			cout << "*";
		cout << endl;
	}
}

在這裏插入圖片描述

#include<iostream>
using namespace std;
#define HELLO //定義標識符
int main()
{
    int x = 8, y = 10;
    //條件編譯
#ifdef HELLO
    cout << x << "+" << y << "=" << x + y << endl;
#else
    cout << x << "*" << y << "=" << x * y << endl;
#endif
    //條件編譯
#if x>y
    cout << x << ">" << y << endl;
#else
    cout << x << "<" << y << endl;
#endif

    return 0;
}

在這裏插入圖片描述

發佈了12 篇原創文章 · 獲贊 1 · 訪問量 713
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章