全局变量的应用:
#include<stdio.h>
double cash;
int main(void)
{
? ? ?int choice;
? ? ?double value;
? ? ?void income (number),expend (number);
? ? ?cash=0;
? ? ?printf ("Enter operate choice(0--end,1--income,2--expend):");
? ? ?scanf ("%d",&choice");
? ? ?while (choice!=0){
? ? ? ? ? ? if (choice==1 || choice==2){
? ? ? ? ? ? ? ? ? ?printf ("Enter cash value:");
? ? ? ? ? ? ? ? ? ?scanf ("%lf",&value);
? ? ? ? ? ? if (choice==1){
? ? ? ? ? ? ? ? ? ?income(value);
? ? ? ?? }?else{
? ? ? ? ? ? ? ? ? ?expend(value);
? ? ? ? ? ? ?}? ??
? ? ? ? ? ?printf ("Current cash=%.2f\n",&cash);
? ? ? ?}?
? ? ? ? ? ?printf ("Enter operate choice(0--end,1--income,2--expend):");
? ? ? ? ? ?scanf ("%d",&choice);
?}
return 0;
}
void income(double number){
? ? ? ? ? ?cash=cash+number;
}
void expend(double number){
? ? ? ? ? ?cash=cash-number;
}
数组的应用:
?
?
/*输出大于平均数的数*/
#include<stdio.h>
int main()
{
int i,n;
double average,sum;
int a[10];
printf("Enter n:");
scanf("%d",&n);
if(n>=1 && n<=10){
printf("Enter %d integers:",n);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sum=0;
for(i=1;i<=n;i++){
sum+=a[i];
}
average=sum/n;
printf("average=%.2f\n",average);
printf(">average:");
for(i=1;i<=n;i++){
if(a[i]>average){
printf("%d",a[i]);
}
}
printf("\n");
}
return 0;
}
?
?
|