當前位置:首頁 » 編程語言 » 用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);
}