background image

  我举个例子来说明面向过程的编程方式

:

  用户需求:老板让我写个通用计算器。

  最终用户就是老板,我作为程序员,任务就是写一个计算器程序。

OK,很

简单,以下就是用

C 语言完成的计算器:

  假定程序的文件名为:

main.c。

  

int main(int argc, char *argv[]){

  

//变量初始化

  

int nNum1,nNum2;

  

char cOpr;

  

int nResult;

  

nNum1 = nNum2 = 0;

  

cOpr = 0;

  

nResult = 0;

  

//输入数据

  

printf("Please input the first number:rn");

  

scanf("%d",&nNum1);

  

printf("Please input the operator:rn");

  

scanf("%s",&cOpr);

  

printf("Please input the second number:rn");

  

scanf("%d",&nNum2); 

  

//计算结果 

  

if( cOpr == ’+’ ){

  

nResult = nNum1 + nNum2;

  

}else if( cOpr == ’-’ ){

  

nResult = nNum1 - nNum2;

  

}else{

  

printf("Unknown operator!");

  

return -1;