c语言这三个题目怎么写?急求!!!
2个回答
展开全部
//代码及解释如下,如对你有帮助,请采纳
//题目1代码如下
#include<stdio.h>
#include<math.h>
void main()
{
float x1,x2,y1,y2;//设两个点(x1,y1)(x2,y2)
float a,b,c;//设x系数a y系数b 常数c
printf("Enter two points:");//按照题目要求与提示输入两个点
scanf("%f %f %f %f",&x1,&y1,&x2,&y2);//并计算a,b,c
a=(y1-y2);
b=(x2-x1);
c=x1*(y1-y2)+y1*(x2-x1);
if(x1==x2&&y1==y2)//如果两点相等,无法确定一条线
printf("The line cannot be formed.\n");
else//计算该直线方程
{
printf("The line is %5fx + %5fy = %5f\n",a,b,c);
printf("The distance from (0.0000,0.0000) to %5fx+%5fy=%5fis%5f\n",a,b,c,(float)abs(a*0+b*0-c)/sqrt(a*a+b*b));
}
if(a==0||b==0||c==0)//当直线 平行于x轴(a=0)平行于y轴(b=0)过原点(c=//0)时无法构成 三角形
printf("The triangle cannot be formed.\n");
else//计算该三角形面积
{
float px=c/a,py=c/b;
printf("The triangle area formed by the line,x-axis,and y-axis is %f\n",0.5*(px*py));
}
}
//题目2代码如下
#include<stdio.h>
#include<math.h>
void main()
{
float x1,x2,x3,y1,y2,y3;//如题1定义三个点
float dis[3],temp;//dis数组为两点间距离(边长),temp为排序用变量
int i,j;//排序用变量
printf("Enter three points:");//按照题目要求及提示输入三点并求边长
scanf("%f %f %f %f %f %f",&x1,&y1,&x2,&y2,&x3,&y3);
dis[0]=(float)sqrt(pow(x1-x2,2)+pow(y1-y2,2));
dis[1]=(float)sqrt(pow(x1-x3,2)+pow(y1-y3,2));
dis[2]=(float)sqrt(pow(x2-x3,2)+pow(y2-y3,2));
for(i=0;i<2;i++)//把边长由小到大排序,最大的就是最远距离
{
for(j=i+1;j<3;j++)
{
if(dis[j]<dis[i])
{
temp=dis[j];
dis[j]=dis[i];
dis[i]=temp;
}
}
}
printf("The largest distance among them is %f.\n",dis[2]);
if(pow(dis[0],2)+pow(dis[1],2)<pow(dis[2],2))//a^2+b^2<c^2则不能构//成三角形
printf("The triangle cannot be formed.\n");
else//计算三角形面积
{
float area=(float)abs(0.5*(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2));
printf("The area of the triangle formed by (%f,%f) , (%f,%f) and (%f,%f) is %f.\n",x1,y1,x2,y2,x3,y3,area);
}
}
//题目3代码如下
#include<stdio.h>
#include<stdlib.h>
struct Goods//把每个商品看成结构体,拥有名字和售价两个属性
{
char* name;
int value;
};
void main()
{
Goods goods[10];//定义每个商品名字和价格
goods[0].name="Water";goods[0].value=5;
goods[1].name="Cupcake";goods[1].value=20;
goods[2].name="Milk";goods[2].value=15;
goods[3].name="Red Bull";goods[3].value=20;
goods[4].name="M&M";goods[4].value=15;
goods[5].name="Cookie";goods[5].value=15;
goods[6].name="Bread";goods[6].value=12;
goods[7].name="Coke";goods[7].value=9;
goods[8].name="Coffee";goods[8].value=15;
goods[9].name="Candy";goods[9].value=8;
int choice,total=0,putin;//total为当前总投币,choice为顾客选择,puti//n为该次投币数
printf("List of Items of Vending Machine:\n");//将定义好的售货机商//品显示出来
printf("1: %s - %d 2: %s - %d 3: %s - %d",goods[0].name,goods[0].value,goods[1].name,goods[1].value,goods[2].name,goods[2].value);
printf(" 4: %s - %d 5: %s - %d\n",goods[3].name,goods[3].value,goods[4].name,goods[4].value);
printf("6: %s-%d 7: %s - %d 8: %s - %d",goods[5].name,goods[5].value,goods[6].name,goods[6].value,goods[7].name,goods[7].value);
printf(" 9: %s - %d 10:%s- %d\n",goods[8].name,goods[8].value,goods[9].name,goods[9].value);
printf("Selection =>");//顾客输入选择的商品
scanf("%d",&choice);
if(choice<1||choice>10)//错误输入处理
{
printf("Wrong Select!\n");
exit(0);
}
while(1)//顾客投币,投入足够的钱就跳出该循环,否则一直循环
{
if(total<goods[choice-1].value)
{
printf("Put coins:");
scanf("%d",&putin);
total+=putin;
}
else if(total==goods[choice-1].value)
break;
else if(total>goods[choice-1].value)
{
printf("Return: %d\n",total-goods[choice-1].value);
break;
}
}
printf("You get a %s\n",goods[choice-1].name);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询