当前位置:首页 » 编程语言 » 用c语言计算商品价格总价
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

用c语言计算商品价格总价

发布时间: 2022-08-24 14:24:05

‘壹’ 用c语言来编写:商品销售统计程序

#include<iostream>
#include<cstring>
#include<fstream>
#include<stdlib.h>//system("cls")//清屏
#include<conio.h>//getche()
using namespace std;
//全局变量
int i=0;//已录入商品总个数
char ch;//cin>>ch
int n;//case(n)
char code[10];
char name[10];
char unit[10];
int amount;
float unitprice;
float total=0;//总价
ofstream f1("./test.txt");
ofstream f2("./sell.txt");//构建输出流,没有文件就建立

class Goods
{
private:
char code[10];//代码
char name[10];//名称
char unit[10];//单位
int amount;//总数
float unitprice;//单价
public:
Goods();
Goods(char co[10],char na[10],char un[10],int am,float unpr);//构造函数
void f_write();// 录入
void f_change();//改变
void f_delete();//删除
void display();//显示全部商品信息
void s_buy();//买入
};
//构造函数
Goods::Goods(){}
Goods::Goods(char co[10],char na[10],char un[10],int am,float unpr)
{
strcpy(code,co);
strcpy(name,na);
strcpy(unit,un);
amount=am;
unitprice=unpr;
}
Goods *g[50];
//商品信息录入
void Goods::f_write()
{
cout<<"请输入第"<<i+1<<"件商品代码:"<<endl;
cin>>code;
cout<<"请输入第"<<i+1<<"件商品名称:"<<endl;
cin>>name;
cout<<"请输入第"<<i+1<<"件商品计量单位:"<<endl;
cin>>unit;
cout<<"请输入第"<<i+1<<"件商品总数:"<<endl;
cin>>amount;
cout<<"请输入第"<<i+1<<"件商品单价:"<<endl;
cin>>unitprice;
g[i]=new Goods(code,name,unit,amount,unitprice);
i++;
cout<<"信息录入成功!(继续录入按y,返回上一层按n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_write();
}
}
//改变商品信息
void Goods::f_change()
{
cout<<"请输入要改变的商品代码:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
cout<<"商品信息如下:"<<endl;
cout<<"代码 名称 单价 总数 单位"<<endl;
cout<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<"\t"<<g[h]->unit<<endl;
char newco,newna,newun;
int newam;
float newunpr;
cout<<"你想要修改:1、代码;2、名称;3、单价;4、总数;5、单位。"<<endl;
cin>>n;
switch(n)
{
case 1:
cout<<"请输入修改后的商品代码:";
cin>>newco;
g[h]->code[10]=newco;
cout<<"修改成功!"<<endl;
break;
case 2:
cout<<"请输入修改后的商品名称:";
cin>>newna;
g[h]->name[10]=newna;
cout<<"修改成功!"<<endl;
break;
case 3:
cout<<"请输入商品单价:";
cin>>newunpr;
g[h]->unitprice=newunpr;
cout<<"修改成功!"<<endl;
break;
case 4:
cout<<"请输入修改后的商品总数:";
cin>>newam;
g[h]->amount=newam;
cout<<"修改成功!"<<endl;
break;
case 5:
cout<<"请输入修改后的商品单位:";
cin>>newun;
g[h]->unit[10]=newun;
cout<<"修改成功!"<<endl;
break;
}
break;
}//if
}//for循环
cout<<"是否继续修改?(y/n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_change();
}
}
//删除信息
void Goods::f_delete()
{
cout<<"请输入要删除的商品代码:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
for(int k=h;k<i;k++)
{
g[k]=g[k+1];
i--;
}
}
}
cout<<"删除成功!"<<endl;
cout<<"是否继续删除?(y/n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_delete();
}
}
//打印信息
void Goods::display()
{
system("cls");
cout<<" "<<endl;
cout<<"-----------全部商品信息如下-------------------"<<endl;
cout<<" "<<endl;
cout<<"代码 名称 单价 总数 单位"<<endl;
f1<<" "<<endl;
f1<<"---------------全部商品信息如下--------------"<<endl;
f1<<" "<<endl;
f1<<"代码 名称 单价 总数 单位"<<endl;
if(i==0)
{
cout<<"系统未曾录入任何商品信息,或记录被删除!";
}
for(int k=0;k<i;k++)
{
cout<<g[k]->code<<"\t"<<g[k]->name<<"\t"<<g[k]->unitprice
<<"\t"<<g[k]->amount<<"\t"<<g[k]->unit<<endl;
f1<<g[k]->code<<"\t"<<g[k]->name<<"\t"<<g[k]->unitprice
<<"\t"<<g[k]->amount<<"\t"<<g[k]->unit<<endl;
}
cout<<endl;
}
//买入
void Goods::s_buy()
{
float price=0;//单个商品价格
cout<<"请输入想要买的商品代码:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
cout<<"请输入想要购买的商品数量:";
cin>>amount;//当前要购买的数量
price=amount*g[h]->unitprice;
g[h]->amount=g[h]->amount-amount;
cout<<endl;
cout<<"代码 名称 单价 数量 小计"<<endl;
cout<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<g[h]->unit<<"\t"<<price<<endl;
f2<<"代码 名称 单价 数量 小计"<<endl;
f2<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<g[h]->unit<<"\t"<<price<<endl;
total=total+price;
break;
}
}
cout<<"按1继续购买,按2结束。"<<endl;
cin>>n;
if(n==2)
{
cout<<endl;
cout<<"购买结束,总计:"<<total<<"元!"<<endl;
}
else
{
s_buy();
}
}

//类外函数
//第一部分操作显示
void f_screen()
{
system("cls");
Goods g;
cout<<"按相应键操作:"<<endl;
cout<<"0.录入信息 1.更改信息 2.删除信息 3.返回上一层"<<endl;
cin>>n;
switch(n)
{
case 0:
g.f_write();
if(ch=='n'||ch=='N')
f_screen();
break;
case 1:
g.f_change();
if(ch=='n'||ch=='N')
f_screen();
break;
case 2:
g.f_delete();
if(ch=='n'||ch=='N')
f_screen();
break;
}
}
//第二部分操作显示
void s_screen()
{
Goods g;
g.display();
cout<<endl;
g.s_buy();
}
//初始屏幕显示
void screen()
{
system("cls");
cout<<" "<<endl;
cout<<"-----------------商品销售统计系统---------------"<<endl;
cout<<" "<<endl;
f2<<" "<<endl;
f2<<"----------------商品销售统计系统--------------"<<endl;
f2<<" "<<endl;
cout<<"更改商品信息请按1,进行销售统计请按-1。"<<endl;
cin>>ch;
if(ch=='1')
{
f_screen();
if(n==3)
screen();
}
else if(ch=='-1')
{
s_screen();
}
else
{
cout<<"输入错误,系统重新启动!";
screen();
}
}
int main()
{
screen();
return 0;
}

‘贰’ 请问c语言如何实现输入一个商品的名字和购买数量,自动计算出总价,商品有苹果和梨子,单价分别是10和

#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
structgoods
{
chargoodsName[20];
intgoodNum;
};
intpriceCount(structgoodsinputGoods)
{
if(!strcmp(inputGoods.goodsName,"苹果"))
returninputGoods.goodNum*10;
if(!strcmp(inputGoods.goodsName,"梨子"))
returninputGoods.goodNum*11;
else
return-1;
}

intmain()
{
structgoodsinputGoods={};
while(1)
{
printf("请输入商品名字和购买数量 ");
scanf("%s%d",inputGoods.goodsName,&inputGoods.goodNum);
if(priceCount(inputGoods)!=-1)
printf("购买%s的总价是:%d ",inputGoods.goodsName,priceCount(inputGoods));
else
//printf("我头像,惊喜")
printf("商品名字输入有问题 ");
}
system("pause");
return0;
}

运行结果:

‘叁’ 已知商品价格和数量,C语言编程求总价

参考代码:

#include<stdio.h>
intmain(){
doublex;
scanf("%lf",&x);
printf("%lf ",x*30);
return0;
}

‘肆’ C语言简单问题,求商品总价。。在线急等大神解答

没大问题呀,就是
d=d+a[i]*b[i];}

后面多了一个“}”,去掉就可以运行了。

d=(d*100+0.5)/100的本意是对的,但是会结果不对,至少得改为:
d=(int)(d*100+0.5)/100.0f,结果才可能理解,否则输出时对整数会多出一个从0.005舍入得到的.01来。因为在“%10.2f“格式时printf会自动对小数点后第3位进行舍入操作,所以这一步应该去掉为好。
输入时注意输入格式,品名 数量 单价之间都用空格分开,不能用别的。

没说总的项数,可以用输入的数量或单价作为结束标志,比如输入-1时就结束循环:
do {...} while(a[i]<0 || b[i] <0)。

其实这里的a和b都不需要用数组,因为就输入时用一次,直接设为普通变量就够了;c保存输入的品名,在程序中完全没有用到,根本不用输入的。就是说,最后代码可以这样写:
int main()
{
int i=0,a;
float b,d=0;
while(1)
{
scanf("%f%d",&b,&a);
if ( a == -1 ) break;
d=d+a*b;
}
printf("%10.2f",d);
return 0;
}

‘伍’ c语言 商品价格计算系统 急急急!

#include<stdio.h>//the program is for commodity price calculation and management// by miaohuihui 2014/1/2
#define swap(x, y) { int temp; temp = x; x = y; y = temp;}
#define N 3//N is the number of commoditiesstruct commodity{ int ID; char *name; int univalence; int amount; int totalPrice;} com[N];
//totalPrice calculationvoid inputCom( int id, char *na, int univ, int am){ com[id - 1].ID = id; com[id - 1].name = na; com[id - 1].univalence = univ; com[id - 1].amount = am; com[id - 1].totalPrice = univ * am;}
//sort by bubblevoid bubbleSort(){ int i, j; int array[N], arr[N]; for ( i = 0; i < N; i++) { array[i] = com[i].totalPrice; arr[i] = com[i].ID; }
for ( i = 0; i < N; i++) { for ( j = 1; j < N - i; j++) { if ( array[j - 1] > array[j]) { swap(array[j - 1], array[j]); swap(arr[j - 1], arr[j]); } } }
for ( i = 0; i < N; i++) { printf("%d\t%d\n", arr[i], array[i]); }}
void printTotal(){ int i = 0, total = 0; for ( ; i < N; i++) { total = total + com[i].totalPrice; }
printf("All commodities's total price is %d\n", total);}
main(){ inputCom( 1, "zhao", 10, 5); inputCom( 2, "qian", 20, 3); inputCom( 3, "sun", 30, 1);
bubbleSort();
printTotal();
return 0;}

‘陆’ 怎么用C语言编写一个程序 要求:输入单价和个数后可以计算出这些商品的总价格

#include<stdio.h>
struct sp
{
char name[10]; //名称数自己定

float price;

int num;

float sum;

}sp[5];
float total=0;
int main()
{
int i;

float calc(float sum1,float sum2, float sum3, float sum4, float sum5);
void output();

for(i=0;i++;i<5)

{

printf("请输入第%d种商品的名称,单价,数量:\n",i);

gets(sp[i].name);
scanf("%f",&sp[i].price);

scanf("%d",&sp[i].num);

sp[i].sum=price*num;

output();
printf("%f\n",calc(sp[0].sum,sp[1].sum,sp[2].sum,sp[3].sum,sp[4].sum));

}

float calc(float sum1,float sum2, float sum3, float sum4, float sum5)

{
total=sum1+sum2+sum3+sum4+sum5;

return total;

}
void output()
{
int i;

for(i=0;i++;i<5)

{

puts(sp[i].name);

printf("\n%f\n",sp[i].price);

printf("%d\n",sp[i].num);

printf("%f\n",sp[i].sum);

}

}

‘柒’ 怎么用C语言编写一个程序,完成如下功能:定义一个结构数组,输入5种商品的名称,数量和单价。计算出每种

新鲜出炉,还有些BUG,可以自行修改。

#include<stdio.h>

int main()

{

double rental;//rental销售总额

double pri[5]={2.46,4.95,9.12,4.35,6.66};//pri[]零售价格

int i,num[5],count[5];//num[]名字,sale[]销售数量

for (i=1;i<6;i++)

{

printf ("输入第%d种商品名字和销售量: ",i);

scanf ("%d %d",&num[i],&count[i]);//无法限制用户输入数据类型,可产生bug

printf ("商品名字:%d,价格:%f,销售量:%d ",num[i],pri[i],count[i]);//可注释

rental += pri[i]*count[i];//+=

}

printf ("销售总额是%f ",rental);

}


‘捌’ c语言计算价格

intmain()
{
intprice[100];
intn,m,total=0,i;
scanf("%d%d",&n,&m);
i=0;
while(i<n){
intno,p;
scanf("%d%d",&no,&p);
price[no-1]=p;
i++;
}
i=0;
while(i<m){
intno,cnt;
scanf("%d%d",&no,&cnt);
total+=cnt*price[no-1];
i++;
}
printf("%d ",total);
return0;
}

‘玖’ C语言中如何计算总价

a+=a-=a*a;为连续赋值运算,从右向左计算。
于是原始的表达式等效于:
a-=a*a;
a+=a;
也就是
a=a-a*a;
a=a+a;
例如,a=5;
那么
a=a-a*a=5-5*5=-20;
a=a+a=-20
+
(-20)
=
-40;
最终a为-40,表达式整体值也就是a最终值,一样是-40。

‘拾’ 用c语言怎么循环输入10个商品的价格,计算总价

# include<stdio.h>
int main()
{
int a[10],i,sum=0;
for(i=0;i<10;i++)
{
printf("请依次输入10个商品价格\n");
scanf("%d",&a[i]);
sum+=a[i]
}

printf("总价为%d:\n",sum);
}