⑴ 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;
}