#include<iostream> #include<cctype> #include<array> using namespace std; #define MaxSize 10 typedef int Status; typedef double ElemType; typedef struct{ array<ElemType,MaxSize>data; //也可以new 一个 int top; }Stack; Status InitStack(Stack *s) { s->top=-1; return 1; } Status push(Stack *s, ElemType num){ if(s->top==MaxSize-1){ exit(EXIT_FAILURE); } s->top++; s->data[s->top] = num; return EXIT_SUCCESS; } Status pop(Stack *s,ElemType *e){ if(s->top==-1){ return EXIT_FAILURE; } *e = s->data[s->top]; s->top--; return EXIT_SUCCESS; } int main(){ Stack s; int i=0,j=0; ElemType k,e,sum=0,averager = 0; //下面的语句这样写似乎有点蠢,啊哈哈哈 if(InitStack(&s)==1){ for (j=1;j<=MaxSize;j++){ if(k!=-1){ cin>>k; push(&s,k); sum += k; }}} printf("%.2f\n",sum); averager = sum/(s.top+1); for(i=0;i<MaxSize;i++){ pop(&s,&e); if(averager<e){ printf(" %.2f -> ",e); } } printf("\n The SUM/NUM = %.2f",averager); return 0; }
#include <iostream> #include <climits> using namespace std; const int Max = 10; bool isdigit(double i); int main() { double donation[Max]; int count = 0; int num = 0; double sum = 0.0; double average = 0.0; cout << "请输入最多10个数值(输入非数字时将结束输入):" << endl; int i = 0; cin >> donation[i]; while (isdigit(donation[i])) { ++count; sum += donation[i]; ++i; if (i < Max) cin >> donation[i]; else break; } if (0 == count) average = 0; else average = sum /count; for (int j = 0; j< i; j++) { if (donation[j] > average) ++ num } cout << "Average: " << average << endl; cout << "输入数字中有 " << num << "个大于平均值"; return 0; } bool isdigit(double i) { if (i <= INT_MAX && i>= INT_MIN) return true; else return false; }