一、在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");