标签:c语言 strcmp
一个产品有两种版本:其一是标准版,价格是$3.5,其二是豪华版,价格是$5.5。编写一个程序,使用学到的知识提示用户输入产品的版本和数量,然后根据输入的产品数量,计算并输出价格。
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
char version[10];
int num = 0;
float standard_price = 3.5;
float deluxe_price = 5.5;
float total=0.0;
printf("Please input product version\n");
scanf("%s",&version);
printf("Please input product num\n");
scanf("%d",&num);
if(strcmp(version,"standard")==0){
total = standard_price*num;
}else if(strcmp(version,"deluxe")==0){
total = deluxe_price*num;
}else{
printf("You chose error version!");
exit(-1);
}
printf("%d 套 %s 的总价格是:%f ",num,version,total);
return 0;
}这里用到字符串比较函数strcmp,他是C语言的标准库函数。使用时需要引入string.h头文件。
说明:
当s1<s2时,返回值<0
当s1=s2时,返回值=0
当s1>s2时,返回值>0
这里用来比较用户输入的是标准版,还是豪华版,分别计算价格。如果两个都不是,提示用户输入错误。并退出系统。
标签:c语言 strcmp
原文地址:http://xtceetg.blog.51cto.com/5086648/1693402