题目:
1.利用main函数的外部传参实现简易计算器功能。
2.使用指针的方式,打印杨辉三角的前十行
3.使用指针实现strcmp和strcat函数的功能
代码:
#include<stdio.h>
#include<string.h>
int main(int argc, const char *argv[])
{
/******第一题*****/
#if 0
int a,b;
a=*argv[1]-'0';
b=*argv[3]-'0';
switch(*argv[2])
{
case '+':printf("%d\n",a+b);break;
case '-':printf("%d\n",a-b);break;
case '/':printf("%f\n",(float)a/(float)b);break;
case '%':printf("%d\n",a%b);break;
}
#endif
/*****第二题*****/
#if 0
int a[10][10]={0};
int (*p)[10]=a;
for(int i=0;i<10;i++) *(*(p+i))=1;
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++) *(*(p+i)+j)=*(*(p+i-1)+j)+*(*(p+i-1)+j-1);
}
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(*(*(p+i)+j))printf("%-4d",*(*(p+i)+j));
}
printf("\n");
}
#endif
/********第三题********/
#if 0
char s1[32]="hello";
char s2[32]="world";
char *p1=s1;
char *p2=s2;
while(*p1) p1++;
while(*p2)
{
*p1=*p2;
p1++;
p2++;
}
*p1=*p2;
puts(s1);
#endif
#if 1
char s1[32]="hello";
char s2[32]="hallo";
char *p1=s1;
char *p2=s2;
while(*p1==*p2&&*p1!=0&&*p2!=0)
{
p1++;
p2++;
}
printf("%d\n",*p1-*p2);
#endif
return 0;
}
运行结果:
结果一:
ubuntu@ubuntu:~$ gcc day5.c -o day5 -lm
ubuntu@ubuntu:~$ ./day5 1 + 2
3
ubuntu@ubuntu:~$ ./day5 1 - 2
-1
ubuntu@ubuntu:~$ ./day5 1 / 2
0.500000
ubuntu@ubuntu:~$ ./day5 1 % 2
1
结果二:
ubuntu@ubuntu:~$ gcc day5.c -o day5 -lm
ubuntu@ubuntu:~$ ./day5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
结果三:
ubuntu@ubuntu:~$ gcc day5.c -o day5 -lm
ubuntu@ubuntu:~$ ./day5
helloworld
ubuntu@ubuntu:~$ gcc day5.c -o day5 -lm
ubuntu@ubuntu:~$ ./day5
4
C语言实现简易计算器与杨辉三角及字符串操作
该代码示例展示了如何使用C语言实现一个简单的命令行计算器,支持加减乘除操作,以及通过指针打印杨辉三角的前十行。此外,还使用指针实现了strcmp和strcat函数的功能,将两个字符串拼接并比较。
2173

被折叠的 条评论
为什么被折叠?



