⑴ c語言求和
先獲取數組長度,然後用for循環,從數組中獲取值進行累加求和。
#include
#include
int main()
{
int n;
int val;
int * a;
int sun = 0, i;
printf("請輸入數搏唯枝組的長度:");
scanf("%d", &n);
printf(" ");
a = (int *)malloc(n * sizeof(int));//為數組a動態分配內存
for(i = 0; i < n; i++)
{
printf("請輸入數組的第%d個元素的值:", i+1);
scanf("%d", &val);
printf(" ");
a[i] = val;
}
for (i = 0; i < n; i++)
{
sun+=a[i];//sun+=a[i]相當於sun=sun+a[i];
}
printf("sun = %d ",sun);
free(a);//釋放a動態分配的內存
return 0;
}
(1)用c語言編輯計算sun擴展閱讀:
一個數組中的所有元基敏素具有相同的數據類型(在C、C++、Java、pascal中都這樣。但也並非所有涉及數組的地方都這樣,比如在Visual Foxpro中的數組就並沒這樣的要求)。當然,當數據類型為 Variant 時,各個元素能夠包含不同種類的數據(對象、字元串、數值等等)。可以聲明任何基本數據類型的數組,包括用戶自定義類型和對象變數。
如果要用戶輸入的是一個數組,一般是用一個循環,但是在輸入前也需要固定數組的大小。
compact跟變長數組沒有太山答大的關系,也應該用不到變長數組。因為一般的傳數組到函數中就是傳數組的地址和元素的個數的,那隻是一個提示,不是要求。
⑵ 利用日期、經緯度求日出日落時間 C語言程序代碼
#definePI3.1415926
#include<math.h>
#include<iostream>
usingnamespacestd;
intdays_of_month_1[]={31,28,31,30,31,30,31,31,30,31,30,31};
intdays_of_month_2[]={31,29,31,30,31,30,31,31,30,31,30,31};
longdoubleh=-0.833;
//定義全局變數
voidinput_date(intc[]){
inti;
cout<<"Enterthedate(form:20090310):"<<endl;
for(i=0;i<3;i++){
cin>>c[i];
}
}
//輸入日期
voidinput_glat(intc[]){
inti;
cout<<"Enterthedegreeoflatitude(range:0°-60°,form:404040(means40°40′40″)):"<<endl;
for(i=0;i<3;i++){
cin>>c[i];
}
}
//輸入緯度
voidinput_glong(intc[]){
inti;
cout<<"Enterthedegreeoflongitude(westisnegativ,form:404040(means40°40′40″)):"<<endl;
for(i=0;i<3;i++){
cin>>c[i];
}
}
//輸入經度
intleap_year(intyear){
if(((year%400==0)||(year%100!=0)&&(year%4==0)))return1;
elsereturn0;
}
//判斷是否為閏年:若為閏年,返回1;若非閏年,返回0
intdays(intyear,intmonth,intdate){
inti,a=0;
for(i=2000;i<year;i++){
if(leap_year(i))a=a+366;
elsea=a+365;
}
if(leap_year(year)){
for(i=0;i<month-1;i++){
a=a+days_of_month_2[i];
}
}
else{
for(i=0;i<month-1;i++){
a=a+days_of_month_1[i];
}
}
a=a+date;
returna;
}
//求從格林威治時間公元2000年1月1日到計算日天數days
longdoublet_century(intdays,longdoubleUTo){
return((longdouble)days+UTo/360)/36525;
}
//求格林威治時間公元2000年1月1日到計算日的世紀數t
longdoubleL_sun(longdoublet_century){
return(280.460+36000.770*t_century);
}
//求太陽的平黃徑
longdoubleG_sun(longdoublet_century){
return(357.528+35999.050*t_century);
}
//求太陽的平近點角
longdoubleecliptic_longitude(longdoubleL_sun,longdoubleG_sun){
return(L_sun+1.915*sin(G_sun*PI/180)+0.02*sin(2*G_sun*PI/180));
}
//求黃道經度
longdoubleearth_tilt(longdoublet_century){
return(23.4393-0.0130*t_century);
}
//求地球傾角
longdoublesun_deviation(longdoubleearth_tilt,longdoubleecliptic_longitude){
return(180/PI*asin(sin(PI/180*earth_tilt)*sin(PI/180*ecliptic_longitude)));
}
//求太陽偏差
longdoubleGHA(longdoubleUTo,longdoubleG_sun,longdoubleecliptic_longitude){
return(UTo-180-1.915*sin(G_sun*PI/180)-0.02*sin(2*G_sun*PI/180)+2.466*sin(2*ecliptic_longitude*PI/180)-0.053*sin(4*ecliptic_longitude*PI/180));
}
//求格林威治時間的太陽時間角GHA
longdoublee(longdoubleh,longdoubleglat,longdoublesun_deviation){
return180/PI*acos((sin(h*PI/180)-sin(glat*PI/180)*sin(sun_deviation*PI/180))/(cos(glat*PI/180)*cos(sun_deviation*PI/180)));
}
//求修正值e
longdoubleUT_rise(longdoubleUTo,longdoubleGHA,longdoubleglong,longdoublee){
return(UTo-(GHA+glong+e));
}
//求日出時間
longdoubleUT_set(longdoubleUTo,longdoubleGHA,longdoubleglong,longdoublee){
return(UTo-(GHA+glong-e));
}
//求日落時間
longdoubleresult_rise(longdoubleUT,longdoubleUTo,longdoubleglong,longdoubleglat,intyear,intmonth,intdate){
longdoubled;
if(UT>=UTo)d=UT-UTo;
elsed=UTo-UT;
if(d>=0.1){
UTo=UT;
UT=UT_rise(UTo,GHA(UTo,G_sun(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))),glong,e(h,glat,sun_deviation(earth_tilt(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo))))));
result_rise(UT,UTo,glong,glat,year,month,date);
}
returnUT;
}
//判斷並返回結果(日出)
longdoubleresult_set(longdoubleUT,longdoubleUTo,longdoubleglong,longdoubleglat,intyear,intmonth,intdate){
longdoubled;
if(UT>=UTo)d=UT-UTo;
elsed=UTo-UT;
if(d>=0.1){
UTo=UT;
UT=UT_set(UTo,GHA(UTo,G_sun(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))),glong,e(h,glat,sun_deviation(earth_tilt(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo))))));
result_set(UT,UTo,glong,glat,year,month,date);
}
returnUT;
}
//判斷並返回結果(日落)
intZone(longdoubleglong){
if(glong>=0)return(int)((int)(glong/15.0)+1);
elsereturn(int)((int)(glong/15.0)-1);
}
//求時區
voidoutput(longdoublerise,longdoubleset,longdoubleglong){
if((int)(60*(rise/15+Zone(glong)-(int)(rise/15+Zone(glong))))<10)
cout<<"Thetimeatwhichthesunrisesis"<<(int)(rise/15+Zone(glong))<<":0"<<(int)(60*(rise/15+Zone(glong)-(int)(rise/15+Zone(glong))))<<". ";
elsecout<<"Thetimeatwhichthesunrisesis"<<(int)(rise/15+Zone(glong))<<":"<<(int)(60*(rise/15+Zone(glong)-(int)(rise/15+Zone(glong))))<<". ";
if((int)(60*(set/15+Zone(glong)-(int)(set/15+Zone(glong))))<10)
cout<<"Thetimeatwhichthesunsetsis"<<(int)(set/15+Zone(glong))<<":"<<(int)(60*(set/15+Zone(glong)-(int)(set/15+Zone(glong))))<<". ";
elsecout<<"Thetimeatwhichthesunsetsis"<<(int)(set/15+Zone(glong))<<":"<<(int)(60*(set/15+Zone(glong)-(int)(set/15+Zone(glong))))<<". ";
}
//列印結果intmain(){
longdoubleUTo=180.0;
intyear,month,date;
longdoubleglat,glong;
intc[3];
input_date(c);
year=c[0];
month=c[1];
date=c[2];
input_glat(c);
glat=c[0]+c[1]/60+c[2]/3600;
input_glong(c);
glong=c[0]+c[1]/60+c[2]/3600;
longdoublerise,set;
rise=result_rise(UT_rise(UTo,GHA(UTo,G_sun(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))),glong,e(h,glat,sun_deviation(earth_tilt(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))))),UTo,glong,glat,year,month,date);
set=result_set(UT_set(UTo,GHA(UTo,G_sun(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))),glong,e(h,glat,sun_deviation(earth_tilt(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))))),UTo,glong,glat,year,month,date);
output(rise,set,glong);
system("pause");
return0;
}
⑶ 怎樣用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器
用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器的方法:
1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;
⑷ (新手)用c語言求奇偶數求和(循環,分支,數組)
作了一點修改:
#include<stdio.h>
intmain()
{
inta[201],sum,sun,i,n;
scanf("%d",&n);
while(n){
sum=sun=0;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
if(a[i]%2==0)
sum=sum+a[i];
else
sun=sun+a[i];
}
printf("%d%d
",sun,sum);
for(i=n-1;i>=0;i--)
printf("%d",a[i]);
printf("
");
scanf("%d",&n);
}
return0;
}
⑸ 如果想要計算1 2 3…… 100的值。該怎樣用vi編寫C語言程序並運行
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=0,i=1;
while(i<=100)
{
a=a+i;
i++;
}
printf("%d",a);
system("pause");
return o;
}