20201216-成信大-C语言程序设计-20201学期《C语言程序设计B》平时自主学习-命令行
P116
编写一程序P116.C实现以下功能
从命令行输入两个实数,格式为:P116 数1 数2,输出“(数1 + 数2) ÷ 2”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atof。
(2)当命令行格式不正确(参数个数不为3)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回9
(4)编程可用素材:printf(" usage: P116 num1 num2\n")、printf(" (… + …) / 2 = …\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P116.exe所在的文件夹,考生的程序位置可不必如此;图1中的P116 100.123 5200119.789、P116 100.123是从命令行输入的内容。
E:\Debug>P116 100.123 5200119.789
(100.123 + 5200119.789) / 2 = 2600109.956
E:\Debug>P116 100.123
usage: P116 num1 num2
E:\Debug>
解题结果代码
/*
编写一程序P116.C实现以下功能
从命令行输入两个实数,格式为:P116 数1 数2,输出“(数1 + 数2) ÷ 2”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atof。
(2)当命令行格式不正确(参数个数不为3)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回9
(4)编程可用素材:printf(" usage: P116 num1 num2\n")、printf(" (… + …) / 2 = …\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P116.exe所在的文件夹,考生的程序位置可不必如此;
图1中的P116 100.123 5200119.789、P116 100.123是从命令行输入的内容。
E:\Debug>P116 100.123 5200119.789
(100.123 + 5200119.789) / 2 = 2600109.956
E:\Debug>P116 100.123
usage: P116 num1 num2
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double num1, num2, result; // 注意,精度上使用double,因为atof返回的是double
if (argc!=3)
{
printf(" usage: P116 num1 num2\n");
exit(9);
}
num1 = atof(argv[1]);
num2 = atof(argv[2]);
result = (num1 + num2) / 2;
printf(" (%.3lf + %.3lf) / 2 = %.3lf\n", num1, num2, result);
return 0;
}
P117
编写一程序P117.C实现以下功能
从命令行输入两个实数,格式为:P117 数1 数2,输出“(数1 - 数2) × 3”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atof。
(2)当命令行格式不正确(参数个数不为3)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回76
(4)编程可用素材:printf(" usage: P117 num1 num2\n")、printf(" (… - …) * 3 = …\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P117.exe所在的文件夹,考生的程序位置可不必如此;图1中的P117 100.123 52001.789、P117 100.123是从命令行输入的内容。
E:\Debug>P117 100.123 52001.789
(100.123 - 52001.789) * 3 = -155704.998
E:\Debug>P117 100.123
usage: P117 num1 num2
E:\Debug>
解题代码
/*
编写一程序P117.C实现以下功能
从命令行输入两个实数,格式为:P117 数1 数2,输出“(数1 - 数2) × 3”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atof。
(2)当命令行格式不正确(参数个数不为3)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回76
(4)编程可用素材:
printf(" usage: P117 num1 num2\n");
printf(" (… - …) * 3 = …\n"…);
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P117.exe所在的文件夹,
考生的程序位置可不必如此;图1中的P117 100.123 52001.789、P117 100.123是从命令行输入的内容。
E:\Debug>P117 100.123 52001.789
(100.123 - 52001.789) * 3 = -155704.998
E:\Debug>P117 100.123
usage: P117 num1 num2
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
double num1, num2, result;
if (argc!=3)
{
printf(" usage: P117 num1 num2\n");
exit(76);
}
num1 = atof(argv[1]);
num2 = atof(argv[2]);
result = (num1 - num2) * 3;
printf(" (%.3lf - %.3lf) * 3 = %.3lf\n", num1, num2, result);
return 0;
}
P118
编写一程序P118.C实现以下功能
从命令行输入两个实数,格式为:P118 数1 数2,输出“(数12 - 数22) ÷ 6”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atof。
(2)当命令行格式不正确(参数个数不为3)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回103
(4)编程可用素材:printf(" usage: P118 num1 num2\n")、printf(" (…*… - …*…) / 6 = …\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P118.exe所在的文件夹,考生的程序位置可不必如此;图1中的P118 100.123 5001.789、P118 100.123是从命令行输入的内容。
E:\Debug>P118 100.123 5001.789
(100.123*100.123 - 5001.789*5001.789) / 6 = -4167978.098
E:\Debug>P118 100.123
usage: P118 num1 num2
E:\Debug>
/*
编写一程序P118.C实现以下功能
从命令行输入两个实数,格式为:P118 数1 数2,输出“(数1^2 - 数2^2) ÷ 6”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atof。
(2)当命令行格式不正确(参数个数不为3)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回103
(4)编程可用素材:printf(" usage: P118 num1 num2\n")、printf(" (…*… - …*…) / 6 = …\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P118.exe所在的文件夹,考生的程序位置可不必如此;
图1中的P118 100.123 5001.789、P118 100.123是从命令行输入的内容。
E:\Debug>P118 100.123 5001.789
(100.123*100.123 - 5001.789*5001.789) / 6 = -4167978.098
E:\Debug>P118 100.123
usage: P118 num1 num2
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
double num1, num2, result;
if(argc!=3)
{
printf(" usage: P118 num1 num2\n");
exit(103);
}
num1 = atof(argv[1]);
num2 = atof(argv[2]);
result = (num1*num1 - num2*num2) / 6;
printf(" (%.3lf*%.3lf - %.3lf*%.3lf) / 6 = %.3lf\n", num1, num1, num2, num2, result);
return 0;
}
P119
编写一程序P119.C实现以下功能
从命令行输入三个数,格式为:P119 数1 数2 数3,输出“数1 + (数2 + 数3) ÷ 2”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atoi, atof。
(2)当命令行格式不正确(参数个数不为4)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回8
(4)编程可用素材:printf(" usage: P119 num1 num2 num3\n")、printf(" … + (… + …) / 2 = …\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P119.exe所在的文件夹,考生的程序位置可不必如此;图1中的P119 101 100.123 5200119.789、P119 100.123是从命令行输入的内容。
E:\Debug>P119 101 100.123 5200119.789
101 + (100.123 + 5200119.789) / 2 = 2600210.956
E:\Debug>P119 100.123
usage: P119 num1 num2 num3
E:\Debug>
解题代码
/*
编写一程序P119.C实现以下功能
从命令行输入三个数,格式为:P119 数1 数2 数3,输出“数1 + (数2 + 数3) ÷ 2”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atoi, atof。
(2)当命令行格式不正确(参数个数不为4)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回8
(4)编程可用素材:printf(" usage: P119 num1 num2 num3\n")、printf(" … + (… + …) / 2 = …\n"…。
程序的运行效果应类似地如图1所示,
图1中的E:\Debug>为命令行提示符,表示程序P119.exe所在的文件夹,考生的程序位置可不必如此;
图1中的P119 101 100.123 5200119.789、P119 100.123是从命令行输入的内容。
E:\Debug>P119 101 100.123 5200119.789
101 + (100.123 + 5200119.789) / 2 = 2600210.956
E:\Debug>P119 100.123
usage: P119 num1 num2 num3
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int num1;
double num2, num3, result;
if(argc!=4)
{
printf(" usage: P119 num1 num2 num3\n");
exit(8);
}
num1 = atoi(argv[1]);
num2 = atof(argv[2]);
num3 = atof(argv[3]);
result = num1 + (num2 + num3) / 2;
//P119 101 100.123 5200119.789
//P119 数1 数2 数3,输出“数1 + (数2 + 数3) ÷ 2"
printf(" %d + (%.3lf + %.3lf) / 2 = %.3lf\n", num1, num2, num3, result);
return 0;
}
P120
编写一程序P120.C实现以下功能
从命令行输入三个数,格式为:P120 数1 数2 数3,输出“数1 + (数2 - 数3) × 3”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atoi, atof。
(2)当命令行格式不正确(参数个数不为4)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回23
(4)编程可用素材:printf(" usage: P120 num1 num2 num3\n")、printf(" … + (… - …) * 3 = …\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P120.exe所在的文件夹,考生的程序位置可不必如此;图1中的P120 104 100.123 52001.789、P120 100.123是从命令行输入的内容。
E:\Debug>P120 104 100.123 52001.789
104 + (100.123 - 52001.789) * 3 = -155600.998
E:\Debug>P120 100.123
usage: P120 num1 num2 num3
E:\Debug>
解题代码
/*
编写一程序P120.C实现以下功能
从命令行输入三个数,格式为:P120 数1 数2 数3,输出“数1 + (数2 - 数3) × 3”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atoi, atof。
(2)当命令行格式不正确(参数个数不为4)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回23
(4)编程可用素材:printf(" usage: P120 num1 num2 num3\n")、printf(" … + (… - …) * 3 = …\n"…。
程序的运行效果应类似地如图1所示,
图1中的E:\Debug>为命令行提示符,表示程序P120.exe所在的文件夹,考生的程序位置可不必如此;
图1中的P120 104 100.123 52001.789、P120 100.123是从命令行输入的内容。
E:\Debug>P120 104 100.123 52001.789
104 + (100.123 - 52001.789) * 3 = -155600.998
E:\Debug>P120 100.123
usage: P120 num1 num2 num3
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int num1;
double num2, num3, result;
if(argc!=4)
{
printf(" usage: P120 num1 num2 num3\n");
exit(23);
}
num1 = atoi(argv[1]);
num2 = atof(argv[2]);
num3 = atof(argv[3]);
result = num1 + (num2 - num3) * 3;
//P120 104 100.123 52001.789
//P120 数1 数2 数3,输出“数1 + (数2 - 数3) × 3”
printf(" %d + (%.3lf - %.3lf) * 3 = %.3lf\n", num1, num2, num3, result);
return 0;
}
P121
编写一程序P121.C实现以下功能
从命令行输入三个数,格式为:P121 数1 数2 数3,输出“数1 + (数22 - 数32) ÷ 6”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atoi, atof。
(2)当命令行格式不正确(参数个数不为4)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回97
(4)编程可用素材:printf(" usage: P121 num1 num2 num3\n")、printf(" … + (…*… - …*…) / 6 = …\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P121.exe所在的文件夹,考生的程序位置可不必如此;图1中的P121 278 100.123 5001.789、P121 100.123是从命令行输入的内容。
E:\Debug>P121 278 100.123 5001.789
278 + (100.123*100.123 - 5001.789*5001.789) / 6 = -4167700.098
E:\Debug>P121 100.123
usage: P121 num1 num2 num3
E:\Debug>
解题代码
/*
编写一程序P121.C实现以下功能
从命令行输入三个数,格式为:P121 数1 数2 数3,输出“数1 + (数22 - 数32) ÷ 6”之值且保留3位小数。
提示与注意事项:
(1)库函数提示:atoi, atof。
(2)当命令行格式不正确(参数个数不为4)时,应报错。
(3)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回97
(4)编程可用素材:printf(" usage: P121 num1 num2 num3\n")、printf(" … + (…*… - …*…) / 6 = …\n"…。
程序的运行效果应类似地如图1所示,
图1中的E:\Debug>为命令行提示符,表示程序P121.exe所在的文件夹,考生的程序位置可不必如此;
图1中的P121 278 100.123 5001.789、P121 100.123是从命令行输入的内容。
E:\Debug>P121 278 100.123 5001.789
278 + (100.123*100.123 - 5001.789*5001.789) / 6 = -4167700.098
E:\Debug>P121 100.123
usage: P121 num1 num2 num3
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int num1;
double num2, num3, result;
if(argc!=4)
{
printf(" usage: P121 num1 num2 num3\n");
exit(97);
}
num1 = atoi(argv[1]);
num2 = atof(argv[2]);
num3 = atof(argv[3]);
result = num1 + (num2*num2 - num3*num3) / 6;
//P119 101 100.123 5200119.789
//P119 数1 数2 数3,输出“数1 + (数2^2 - 数3^2) ÷ 6"
printf(" %d + (%.3lf * %.3lf - %.3lf * %.3lf) / 6 = %.3lf\n", num1, num2, num2, num3, num3, result);
return 0;
}
P792
编写一程序P792.C实现以下功能
将命令行输入的三个字符串按从小到大的顺序排序后输出。注意事项:
(1)命令行格式为:P792 str1 str2 str3,当命令行格式不正确(参数个数不为4)时,应报错。
(2)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回1
(3)编程可用素材:printf(" error, usage: P792 str1 str2 str3\n")、printf(" output: %s %s %s\n"…。
程序的运行效果应类似地如图1所示,图1中的E:\Debug>为命令行提示符,表示程序P792.exe所在的文件夹,考生的程序位置可不必如此;图1中的P792 BbcdM89 bbcdM Axyz、P792 BbcdM89 bbcdM Axyz wmv和P792 BbcdM89 bbcdM是从命令行输入的内容。
E:\Debug>P792 BbcdM89 bbcdM Axyz
output: Axyz BbcdM89 bbcdM
E:\Debug>P792 BbcdM89 bbcdM Axyz wmv
error, usage: P792 str1 str2 str3
E:\Debug>P792 BbcdM89 bbcdM
error, usage: P792 str1 str2 str3
E:\Debug>
解题代码
/*
编写一程序P792.C实现以下功能
将命令行输入的三个字符串按从小到大的顺序排序后输出。注意事项:
(1)命令行格式为:P792 str1 str2 str3,当命令行格式不正确(参数个数不为4)时,应报错。
(2)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①正常运行结束时,返回0 ②命令行格式不对返回1
(3)编程可用素材:printf(" error, usage: P792 str1 str2 str3\n")、printf(" output: %s %s %s\n"…。
程序的运行效果应类似地如图1所示,
图1中的E:\Debug>为命令行提示符,表示程序P792.exe所在的文件夹,考生的程序位置可不必如此;
图1中的P792 BbcdM89 bbcdM Axyz、P792 BbcdM89 bbcdM Axyz wmv和P792 BbcdM89 bbcdM是从命令行输入的内容。
E:\Debug>P792 BbcdM89 bbcdM Axyz
output: Axyz BbcdM89 bbcdM
E:\Debug>P792 BbcdM89 bbcdM Axyz wmv
error, usage: P792 str1 str2 str3
E:\Debug>P792 BbcdM89 bbcdM
error, usage: P792 str1 str2 str3
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *s1, *s2, *s3, *tmp;
if(argc!=4)
{
printf(" error, usage: P792 str1 str2 str3\n");
exit(1);
}
s1 = argv[1];
s2 = argv[2];
s3 = argv[3];
//从小到大的顺序
if (strcmp(s1,s2)>0)
{
tmp = s1;
s1 = s2;
s2 = tmp;
}
if (strcmp(s1,s3)>0)
{
tmp = s1;
s1 = s3;
s3 = tmp;
}
if (strcmp(s2,s3)>0)
{
tmp = s2;
s2 = s3;
s3 = tmp;
}
printf(" output: %s %s %s\n", s1, s2, s3);
return 0;
}