如何使用Dev-c++來進行多源代碼文件編譯


一、在Dev-c++中創建空白工程

1.新建項目

2.創建空白工程、勾選c語言、創建名稱

3、在項目下面創建子文件

4.c語言程序的文件后綴是.c,頭文件的文件后綴是.h

二、多源代碼編寫

1.主函數一個文件

2.定義函數一個文件或者多個文件

3.頭文件一個文件(把聲明函數原型和固定常量都放在一個頭文件中)

4.引用c標准庫中的頭文件用<> 引用自己定義的頭文件用“ ”。

例子:

//主函數的編寫
#include <stdio.h>
#include "head_files.h"

int main(void)
{
int nights;
double hotel_rate;
int code;

while ((code=menu())!=QUIT)
{
switch (code)
{
case 1:hotel_rate=HOTEL1;
break;
case 2:hotel_rate=HOTEL2;
break;
case 3:hotel_rate=HOTEL3;
break;
case 4:hotel_rate=HOTEL4;
break;
}
nights=getnight();
showprice(nights,hotel_rate);
}
printf("Thank you and goog bye!\n");
return 0;
}
//定義函數內容
#include <stdio.h>
#include "head_files.h"
int menu(void)
{
int code;
int return_value;//定義了一個返回值變量
printf("Enter the number of desired hotel :\n");
printf("(1) Fairfield Arms (2) Hotel Olympic\n");
printf("(3) Chertworthy Pleaza (4) The Stockton\n");
printf("(5) Quit\n");

while ((return_value=scanf("%d",&code))!=1||code>5||code<1)
{
if (return_value!=1)//注意,輸入一個非數字,scanf()會有返回值,但是不會讀取,因此要清空輸入緩存流。
{
int ch;
while ((ch=getchar())!='\n')
{
continue;
}
printf("Please input a number!\n");
}
if (code>5||code<1)
{
printf("Please input a number from 1-5!\n");
}
}
return code;
}

int getnight(void)
{
int nights;
int return_value;
printf("Please input the nights you want to live:\n");
while ((return_value=scanf("%d",&nights))!=1||nights<0)
{
if (return_value!=1)//注意,輸入一個非數字,scanf()會有返回值,但是不會讀取,因此要清空輸入緩存流。
{
int ch;
while ((ch=getchar())!='\n')
{
continue;
}
printf("Please input a number!\n");
}
if (nights<0)
{
printf("Please input a number bigger than 0!\n");
}
}
return nights;
}


void showprice (int night,double hotel_rate)
{
double total;
int n;
double factor;
for (total=1.0,n=1,factor=1.0;n<=night;n++,factor*=DISCOUNT)
{
total+=hotel_rate*factor;
}
printf("You want to stay %d nights,and the total costs are:%f\n",night,total);
return;
}
//定義頭文件
#define HOTEL1 100
#define HOTEL2 150
#define HOTEL3 200
#define HOTEL4 300
#define DISCOUNT 0.95
#define QUIT 5
int menu(void);
int getnight(void);
void showprice(int, double);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM