⑴ 某物體的重量m為5kg,編寫一個c語言程序求它的重力G,已知重力G=mg(其中g=9.8H/kg)
#include<stdio.h>
int main()
{float a=5.0;
printf("G=%5.2f",a*9.8);
return 0;
}
⑵ c語言編程題
分析關鍵點:
1、根據字元的輸入,檢測sizeof執行的類型,這個要分類,所以需要用到分支結構,多個分類的還是switch會比較清楚一些
2、根據輸入的數量進行計算,考慮基礎單位是位元組,所以數值可能比較大,計算記過應該使用long等大一些的類型。
3、根據總的位元組數進行換算。這里最多就到兆位元組,所以就圖個省事,算窮舉一下了。其實也可以利用遞歸的思路,一直到更高的單位的。
代碼和測試結果如圖,我這里是C++環境就沒有貼出來引用的庫,所以還請根據自己的環境引用庫,比如stdio等:
目前測試與用例一致。注意我這里每一個輸出有個' ',如果是網路教室之類的需要注意下系統要求有沒有要求換行。
⑶ 誰能幫我把這個用c語言遍成程序
#include "stdio.h"
void main()
{
double apple=2.5, pear=1.8, banana=2.0, orange=1.6;
double a, b, c, d;
printf("請輸蘋果、鴨梨、香蕉、桔子的重量, 每輸入一個數敲一次回車。\n");
scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
double total, money;
total = a*apple + b*pear + c*banana + d*orange;
printf("應付款: %lf\n", total);
printf("請輸入顧客實付款數:\n");
scanf("%lf",&money);
printf("應找錢數: %lf\n", money-total);
}
程序通過調試運行,完全正確。
樓主覺得好就把我列為最佳答案,謝謝。
⑷ 如何用C語言編寫計算球體重量的程序
計算不就這么寫么,代碼如下:#include
#include
void
main()
{
double
r,v,pi=3.1415926;
printf("請輸入圓的半徑:\n");
scanf("%lf\n",
&r);
v=4.0/3*r*r*r*pi;
printf("%f\n",v);
}如果寫成4/3的話,而4/3的值是1,4不是實型而是整型與定義的不一樣
⑸ 用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語言砝碼稱重
#include<stdio.h>
struct fama
{
int weight; int num;
}fama1[10];
int count(struct fama a[10],int k,int n)
{
int temp=0,m=0,p;
int w[10000]={0};
for(int i=k;i<n;i++)
{
for(int j=0;j<a[i].num;j++)
{
p=1; temp+=a[i].weight;
for(int x=0;x<m;x++)
{
if(temp==w[x])
p=0;
}
if(p) { w[m]=temp; m++; }
}
} return m; }
void main()
{ int n=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&fama1[i].weight,&fama1[i].num);
int maxnum=0;
for(i=0;i<n;i++)
maxnum+=count(fama1,i,n);
printf("%d\n",maxnum);
}
給分吧,速度
⑺ 如何用C語言編寫計算球體重量的程序
樓上用的數據類型都太小,應該用double
void main()
{
int n;
double result;
result=1.0;
n=100;
for(int i=1;i<=n;++i)
{
result*=(double)i;
}
cout <<result;
}
⑻ C語言程序設計
樓上的寫反了
#include <stdio.h>
#define LB_TO_KG 0.453592
void main()
{
float weight;
printf("輸入重量單位(KG):");
scanf("%f",&weight);
printf("等於%.3f磅\n",weight/LB_TO_KG);
}
⑼ 求c語言程序 輸入磅的重量數,換算成千克單位 程序中要有輸入輸出提示句
按照題目要求編寫的磅轉換為千克的C語言程序如下
#include<stdio.h>
int main(){
float lb,kg;
printf("請輸入磅的重量數:");
scanf("%f",&lb);
kg=lb*453.6/1000;
printf("%f磅等於%f千克",lb,kg);
return 0;
}
⑽ 利用hx711轉換模塊的稱重感測器並用lcd顯示重量的C語言程序
寬頻錯誤711意思為:在此計算機上的配置錯誤阻止此連接。
錯誤詳情:
這個問題較為糾結。