#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 256//这是一句预定义,定义BUFSIZE的值是100,是缓冲空间的大小,作为数组的。
int main(int argc, char *argv[]) //第一个int argc,是记录你输入在命令行上的字符串个数;
//第二个*argv[]是个指针数组,存放输入在命令行上的命令(字符串)。
//argv[]是一个字符数组.
//argv[0]:指向程序的全路径名
//argv[1]:指向在DOS命令行中执行程序名后的第一个字符串。
//argv[2]:指向第二个字符串。
{
char prefix[BUFSIZE];//定义一个大小为BUFSIZE的字符型数组
char next[BUFSIZE];
int i;
float sum = 0.0;
for (i = 1; i < argc; i++) {
float x = atof(argv[i]);//atof:将……转成float型;atoi:字符串转成int型;atol:将……转成long型
sum += x;
if (i == 1) {
sprintf(prefix, "%.4g", x);// 用格式控制,数字转换成字符串
} else {
sprintf(next, " + %.4g", x);//%g为自动选择合适的表示法
strcat(prefix, next);//strcat的用法:strcat(字符串1,字符串2)
// strcat是一个函数.是字符串连接的意思. 起作用是连接两个字符数组中的字符串.
//把字符串2接到字符串1的后面.结果放在字符串1中.
//这个函数调用后得到一个函数值:字符串1的地址
printf("%s = %.4g\n", prefix, sum);
}
}
return 0;
}
/*
@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 1e20 -1e20 3.14
1e+20 + -1e+20 = 0
1e+20 + -1e+20 + 3.14 = 3.14
@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out -1e20 3.14
-1e+20 + 3.14 = -1e+20
@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out -1e20 3.14 1e20
-1e+20 + 3.14 = -1e+20
-1e+20 + 3.14 + 1e+20 = 0
*/