1、
使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
例如:命令行参数输入:test.exe -a 1 2
执行1+2输出3
2、 写冒泡排序可以排序多个字符串。
例如:命令行参数输入:test.exe -a 1 2
执行1+2输出3
2、 写冒泡排序可以排序多个字符串。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#pragma warning(disable:4996)
//写冒泡排序可以排序多个字符串
void sortStr(char *str[], int size)
{
for (int i = 0; i < size; i++){
for (int j = 0; j < size - i - 1; j++){
if (strcmp(str[j], str[j + 1])){
char *tmp = NULL;
tmp = str[j];
str[j] = str[j + 1];
str[j + 1] = tmp;
}
}
}
}
/*第一个参数:
"- a"选项执行加法
"- s"选项执行减法
"- m"选项执行乘法
"- d"选项执行除法
后面两个参数为操作数。
*/
int main(int argc, char *argv[])
{
char *str[] = { "aesq", "ophg", "aqls", "edsjn" };
int size = sizeof(str) / sizeof(str[0]);
sortStr(str, size);
argv数组存放命令参数
//int result = 0;
//int num1 = atoi(argv[2]);//将字符串转为int
//int num2 = atoi(argv[3]);
//if (!strcmp(argv[1], "-a"))
//{
// result = num1 + num2;
//}
//else if (!strcmp(argv[1], "-s"))
//{
// result = num1 - num2;
//}
//else if (!strcmp(argv[1], "-m"))
//{
// result = num1 * num2;
//}
//else if (!strcmp(argv[1], "-d"))
//{
// result = num1 / num2;
//}
//else{
//}
//printf("%d\n", result);
system("pause");
return 0;
}
main函数参数运行结果: