C语言程序设计.. 函数设计。请问下,下面的代码哪里错了?真恼人~
作者&投稿:星帘 (若有异议请与网页底部的电邮联系)
代码无错,只是one=(double)up/(double)down;中的(double)是多余的。
scanf定义输入格式为%d,%d,%d,则输入时必须严格按照格式输入,应该输入1,3,6,逗号是半角,逗号后面没有空格。
错误太多了,1,函数定义有问题!函数的参数不能写在外面。所有的函数定义均有问题,自己改一下吧,另外,调用函数时,该函数要先定义,或者是先声明!
void update(PEMP company[],int id,int age,char *name,double salary)
2,#include "s6.h"这句不要的吧。
3,void readin(company)函数中的update(comapny...)这里的comapny写错了,应该是company,还有这里的汪海洋后面的括号写反了。
4.typedef struct employee定义的字和你后面用到的不一致,应该是*PEMP;
我帮你改了如下:
#include<stdio.h>
#include <stdlib.h>
#define num 10
typedef struct employee
{
int age;
char *name;
double salary;
}*PEMP;
//#include "s6.h"
void update(PEMP company[],int id,int age,char *name,double salary)
{
PEMP emp;
if((emp=(PEMP)malloc(sizeof(struct employee)))==NULL)
return;
emp->age=age;
emp->name=name;
emp->salary=salary;
company[id]=emp;
};
double total(PEMP company[])
{
int i,age1,age2;
double sum=0;
for(; ;)
{
printf("输入年龄范围:");
scanf("%d%d",&age1,&age2);
if(age1>age2)
{i=age1;
age1=age2;
age2=i;
}
if((age1>=0)&&(age1<=100)&&(age2>=0)&&(age2<=100)&&(age2>age1))
break;
}
for(i=0;i<num;i++)
if(company[i]!=NULL && (company[i]->age)>=age1 &&(company[i]->age)<=age2)
sum+=(company[i]->salary);
return (sum);
}
double mean(PEMP company[])
{
int i;
char ch;
int age=0;
int n=0;
double sum=0;
printf("求某一年龄段以上的职工总工资和平均工资。\n");
printf("默认是全体职工平均工资,要求改变计算方式输入Y/y\n");
getchar();
ch=getchar();
if(ch=='Y'||ch=='y')
{
printf("age:");
scanf("%d",&age);
}
for(i=0;i<num;i++)
if(company[i]!=NULL&&(company[i]->age)>=age)
{
sum+=(company[i]->salary);
++n;
}
printf("age=%d,sum=%8.2f\n",age,sum);
sum=sum/n;
return (sum);
}
void readin(PEMP company[])
{
update(company,2,22,"章兰花",3000.0);
update(company,1,18,"李艳",2502.5);
update(company,0,28,"赵杰",3558.98);
update(company,3,33,"张学习",3058.8);
update(company,4,52,"周五蓝",2158.14);
update(company,5,38,"汪海洋",2558.8);
update(company,6,24,"李胜利",3128.68);
update(company,7,34,"黎花",2958.98);
update(company,8,20,"刘平平",1958.8);
update(company,9,18,"潘玉峰",1558.98);
}
void main()
{
PEMP company[num];
double x;
int i;
readin(company);
for(i=0;i<num;i++)
printf("%s\t%d\t%1f\n",company[i]->name,company[i]->age,company[i]->salary);
x=total(company);
printf("total=%8.2f\n",x);
printf("mean=%g\n",mean(company));
}
问题比较多,大致修改如下:(VC编译通过,希望对你有帮助)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define num 10
typedef struct employee
{
int age;
char name[128];
double salary;
}PEMP;
void update(PEMP *company, int id,int age,char *name,double salary);
double mean(PEMP *company);
void readin(PEMP *company);
double total(PEMP *company);
void main()
{
PEMP company[num];
double x;
int i;
readin(company);
for(i=0;i<num;i++)
printf("%s\t%d\t%1f\n",company[i].name,company[i].age,company[i].salary);
x=total(company);
printf("total=%8.2f\n",x);
printf("mean=%g\n", mean(company));
}
void update(PEMP *company, int id,int age,char *name,double salary)
{
company[id].age=age;
strcpy(company[id].name, name);
company[id].salary=salary;
}
double total(PEMP *company)
{
int i,age1,age2;
double sum=0;
for(; ;)
{
printf("输入年龄范围(以空格隔开,例如18 50):");
scanf("%d %d",&age1,&age2);
if(age1>age2)
{
i=age1;
age1=age2;
age2=i;
}
if((age1>=0)&&(age1<=100)&&(age2>=0)&&(age2<=100)&&(age2>age1))
break;
}
for(i=0;i<num;i++)
if((company[i].age >= age1) && (company[i].age <= age2))
sum+=(company[i].salary);
return (sum);
}
double mean(PEMP *company)
{
int i;
char ch;
int age=0;
int n=0;
double sum=0;
printf("求某一年龄段以上的职工总工资和平均工资。\n");
printf("默认是全体职工平均工资,要求改变计算方式输入Y/y\n");
getchar();
ch=getchar();
if(ch=='Y'||ch=='y')
{
printf("age:");
scanf("%d",&age);
}
for(i=0;i<num;i++)
if(company[i].age >= age)
{
sum+=(company[i].salary);
++n;
}
printf("age=%d,sum=%8.2f\n",age,sum);
sum=sum/n;
return (sum);
}
void readin(PEMP *company)
{
update(company,2,22,"章兰花",3000.0);
update(company,1,18,"李艳",2502.5);
update(company,0,28,"赵杰",3558.98);
update(company,3,33,"张学习",3058.8);
update(company,4,52,"周五蓝",2158.14);
update(company,5,38,"汪海洋",2558.8);
update(company,6,24,"李胜利",3128.68);
update(company,7,34,"黎花",2958.98);
update(company,8,20,"刘平平",1958.8);
update(company,9,18,"潘玉峰",1558.98);
}
太长了,也没有描述你的出错信息,
- -起码也要说明你题目要求干嘛啊
康印枸橼: void fun(int m, int n) {for(int i = m; i <= n;++i) {if(!(i%5) && (i%3))printf("%d ", i);} }
琼海市17529887750: C语言 函数程序设计 - ?
康印枸橼: #include "stdafx.h"#include "string.h" void main(void) { char src[100]={0}; char cmd[10]={0}; char cmd1[]="out -2"; char cmd2[]="out +6"; char output[7]={0}; char *p=NULL; int len=0; int i; printf("please input str:"); gets(src); len=strlen(src)...
琼海市17529887750: c语言程序设计 函数 急!!! - ?
康印枸橼: #include <stdio.h> float Money(float I,float S,int Y); //括号里是参数 你要求的 返回值M是float型 void main() {int y;float i,s,m;printf ("----请依次输入利率 投资额 年份----\n"); scanf("%f %f %d",&i,&s,&y); //依次读取数值 并放入i,s,ym=...
琼海市17529887750: c语言程序设计,(编写一个函数怎么编...)? - ?
康印枸橼: atoi(digitStr) {int a=0,i; char b[6]="32767"; for(i=0;i<4&&digitStr[i]!='\0';i++) {if(i>4)printf("cuo wu"); a+=(digitStr[i]-'0')*pow(10,i);//10的I次方 if(i==4) {if(*digitStr++>*b++)printf("shu tai da!!!")} } }
琼海市17529887750: C语言 函数设计 - ?
康印枸橼: # include <stdio.h> int inNumber(int a) { printf("input a:");scanf("%d",&a);while(a<=0){printf("\ninput error\n");printf("input a again:");scanf("%d",&a);}return a; } void main () {int a;printf("%d\n",inNumber(a)); }
琼海市17529887750: C 程序设计函数设计 - ?
康印枸橼: #include <stdio.h> void f(int *a, int num) { int i,j,t; for(i=0;i<num-1;i++) { if(i%2!=0) for(j=i+1;j<num;j++) if(a[i]<a[j])t=a[i],a[i]=a[j],a[j]=t; else for(j=i+1;j<num;j++) if(a[i]>a[j])t=a[i],a[i]=a[j],a[j]=t; } } void main() { int n[]={1,2,3,4,5,6,7,8,9}; int i; f(n,9); for(i=0;i<9;i++) printf("%2d",n[i]); }
琼海市17529887750: C语言函数程序设计练习,求大神 - ?
康印枸橼: #include int prime(int x){ int i; if (x for(i=2;i*i if(x%i==0) return 0; return 1; } int main() { int n; scanf("%d",&n); if (prime(n)==1) printf("%d是素数\n",n); else printf("%d不是素数\n",n); }
琼海市17529887750: C语言程序设计,一个简单的分段函数 - ?
康印枸橼: #include <stdio.h> #include <math.h> void main() { float x; double y; printf("Please input the value of x:"); scanf("%f",&x); if(x>=-10&&x<=4) { y=fabs(x-2); printf("y=%.2f\n",y); } else if(x>=5&&x<=7) { y=x+10; printf("y=%.2f\n",y); } else if(x>=8...
琼海市17529887750: C语言 编程 函数 - ?
康印枸橼: #include <stdio.h> double fun (int x[]) { double sum = 0.0;/// int c=0, i=0; while (x[i] != 0) { if (x[i] > 0) { sum += x[i]; c++; } i++; } sum /=c;/// return sum; } int main(void ) { int x[1000]; int i=0; printf( "\nPlease enter some data (end with 0): " ); do { scanf("...
琼海市17529887750: C语言程序 编程 函数 - ?
康印枸橼: int sum = 0.0; => double sum = 0.0; sum \= c; => sum /= c; //这除号错了吗? printf("%f\n", fun (x)); => printf("%.6f\n", fun(x));