//楼上的,重量和金额怎么能是整形的,还有输入顺序应切合实际,哈哈
/*
//if-else写法
#include
void main()
{
int grade; //等级
float weight; //重量
float money; //付款
float pay; //应付款
printf("请输入购买信息:\n");
printf("苹果等级:");
scanf("%d",&grade);
printf("苹果重量:");
scanf("%f",&weight);
//数据验证
if(weight<=0 || grade<1 || grade>4)
{
printf("Data Error!\n");
return;
}
//计算应付款
if(grade==1) //一级苹果
pay=5.5*weight;
else if(grade==2) //二级苹果
pay=4.2*weight;
else if(grade==3) //三级苹果
pay=3*weight;
else //四级苹果
pay=2.5*weight;
//显示结果
printf("\n你所购买的是%d级苹果:\n",grade);
printf("实际重量:%g\n",weight);
printf("应付金额:%g\n",pay);
printf("交付金额:");
scanf("%f",&money);
printf("\n应找零钱:%g\n",money-pay);
printf("谢谢惠顾,再见!\n");
}
*/
/*
//switch写法
#include
void main()
{
int grade; //等级
float weight; //重量
float money; //付款
float pay; //应付款
printf("请输入购买信息:\n");
printf("苹果等级:");
scanf("%d",&grade);
printf("苹果重量:");
scanf("%f",&weight);
//数据验证
if(weight<=0 || grade<1 || grade>4)
{
printf("Data Error!\n");
return;
}
//计算应付款
switch(grade)
{
case 1: //一级苹果
pay=5.5*weight;break;
case 2: //二级苹果
pay=4.2*weight;break;
case 3: //三级苹果
pay=3*weight;break;
default: //四级苹果
pay=2.5*weight;
}
//显示结果
printf("\n你所购买的是%d级苹果:\n",grade);
printf("实际重量:%g\n",weight);
printf("应付金额:%g\n",pay);
printf("交付金额:");
scanf("%f",&money);
printf("\n应找零钱:%g\n",money-pay);
printf("谢谢惠顾,再见!\n");
}
*/
/*
//字符串题目
#include
#include
void main()
{
char s1[100],s2[100];
puts("请输入两个字符串:");
gets(s1);
gets(s2);
strcat(s1,s2);
printf("连接后的结果:%s\n",s1);
int i,j;
char t;
i=0;
j=strlen(s1)-1;
while(i<=j)
{
t=s1[i];
s1[i]=s1[j];
s1[j]=t;
i++;
j--;
}
printf("反向后的结果:%s\n",s1);
printf("连接后的长度:%d\n",strlen(s1));
}
*/
//字符统计
#include
void charc(char s[100])
{
int letters=0,numbers=0,spaces=0,others=0;
int i=0;
while(s[i]!='\0')
{
if(s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z')
letters++;
else if(s[i]>='0' && s[i]<='9')
numbers++;
else if(s[i]==' ')
spaces++;
else
others++;
i++;
}
printf("\n字母:%d个,数字:%d个,空格:%d个,其他:%d个\n",letters,numbers,spaces,others);
}
void main()
{
char str[100];
puts("请输入字符串:");
gets(str);
charc(str);
}
用 if
#include
main()
{
int i;
int w;
int ch;
int mon;
int re;
printf("等级: ");
scanf("%d",&i);
printf("\n");
printf("重量: ");
scanf("%d",&w);
printf("\n");
if (i == 1)
ch = 5.5 * w;
else if(i == 2)
ch = 4.2 * w;
else if(i == 3)
ch = 3.0 * w;
else if(i == 4)
ch = 2.5 * w;
else
{
printf("无此等级的苹果\n");
return 0;
}
printf("您选择苹果级别: %d 级\n",i);
printf("您购买苹果重量: %d kg\n",w);
printf("您应付金额为: %d 元\n",ch);
printf("顾客所付金额: ");
scanf("%d",&mon);
printf("\n");
if (mon < ch)
{
printf("Data Error\n");
return 0;
}
printf("应找您: %d 元\n",mon-ch);
}
用switch
#include
main()
{
int i;
int w;
int ch;
int mon;
int re;
printf("等级: ");
scanf("%d",&i);
printf("\n");
printf("重量: ");
scanf("%d",&w);
printf("\n");
if (i>4 ||i<1)
{
printf("无此等级的苹果\n");
return 0;
}
switch (i)
{
case 1 : ch = 5.5 * w;
case 2 : ch = 4.2 * w;
case 3 : ch = 3.0 * w;
case 4 : ch = 2.5 * w;
}
printf("您选择苹果级别: %d 级\n",i);
printf("您购买苹果重量: %d kg\n",w);
printf("您应付金额为: %d 元\n",ch);
printf("顾客所付金额: ");
scanf("%d",&mon);
printf("\n");
if (mon < ch)
{
printf("Data Error\n");
return 0;
}
printf("应找您: %d 元\n",mon-ch);
}