19.04.10--第七次作业 封装函数

1./*编译器:VC6.0*/

#include<stdio.h>

#define ERROR 404//错误时返回的值

//函数声明

int caculation(char*,int,int);//计算函数(返回404时为无用)

//主函数

int main()

{

int a,b,c;

char name[5];

printf("请输入 指令 确认输入并按下回车后再次输入 数字 数字");

gets(name);

scanf("%d%d",&a,&b);

c=caculation(name,a,b);

printf("计算结果为%d\n",c);

return 0;

}

//函数定义

int caculation(char *name,int a,int b)

{

int i=0,x=0,c;

char add[]="add",sub[]="sub",mux[]="mux",dive[]="dive";

while(!name[i])

{

if(name[i]==add[i]&&(x==1||x==0))

  x=1;

else if(name[i]==sub[i]&&(x==2||x==0))

  x=2;

else if(name[i]==mux[i]&&(x==3||x==0))

  x=3;

else if(name[i]==dive[i]&&(x==4||x==0))

  x=4;

else

{

 x=0;

 break;

}

}

switch(x)

{

  case 1:c=a+b;break;

  case 2:c=a-b;break;

  case 3:c=a*b;break;

  case 4:c=a/b;break;

  default:c=ERROR;

}

return c;

}

2.

/*编译器:VC6.0*/

#include<stdio.h>

//函数声明

int my_strcmp(char*,char*);

//主函数

int main()

{

char s1[]="hello world",s2[]="hello abc";

int flag=my_strcmp(s1,s2);

if(flag==0)

  printf("%s等于%s\n",s1,s2);

else if(flag>0)

  printf("%s大于%s\n",s1,s2);

else

  printf("%s小于%s\n",s1,s2);

return 0;

}

int my_strcmp(char *s1,char *s2)

{

int i=0,j=0,x;

while(!s1[i])i++;

while(!s2[j])j++;

if(i==j)

  x=0;

else if(i>j)

  x=1;

else

  x=-1;

return x;

}

posted @ 2019-04-10 20:22  拓荒的路上  阅读(127)  评论(0编辑  收藏  举报