第二个问题,因为C编译器是顺序执行的,用k声铅吵明的话编译器还不知道k是什么呢,所以要用m声明。
满意请采纳。
// 结构体数组person[5]原有五个记录: zhou,wu,li,zheng,wang// 现在,需要在zheng的位置增加1个记录test,那么,zheng和wang就相应往后移动.
// 测试时输入:
// test 33 2000
// 输出6个记录
#include<stdio.h>
#include<string.h> //需要调用strcpy()
//staff[5]与struct staff的名称一样,建议将staff[5]改为其它名称,例如 person[5]
struct staff
{
char name[20]
int age
float salary
}person[5]={{"zhou",22,1500},{"wu",29,1200},{"li",30,900},{"zheng",32,900},{"wang",29,990}}
//原代码struct staff 1[6] //变量名怎么看似是数字"1"? 将其改为其它名称,例如 L
struct staff L[6]
int main()
{
int i //int j //j可以不用
int k
//这些 staff a[3].name 不需要在变量前面加staff
//原代码scanf("%s%d%d",staff a[3].name,staff a[3].age,staff a[3].salary)
//for(i=0i<3i++)
者码 // staff 1[i]=staff[i]
//for(j=3j<=5j++)
// staff 1[j+1]=staff[j]
//手工输入一个记录. 浮点float用%f. &L[3].age和&L[3].salary前面要加&
scanf("%s%d%f",L[3].name,&L[3].age,&L[3].salary)
for(i=0i<3i++)
{
strcpy(L[i].name,person[i].name) //字符串的赋值用函数strcpy()
L[i].age=person[i].age
L[i].salary=person[i].salary
}
for(i=3i<5i++) //i不能等于5,因为person[5]的下标范围是0到4
{
strcpy(L[i+1].name,person[i].name) //字符串的赋值用函数strcpy()
L[i+1].age=person[i].age
昌码 L[i+1].salary=person[i].salary
}
for(k=0k<6k++) //屏幕输出6个记录
{
printf("%s %d %0.2f",L[k].name,L[k].age,L[k].salary) //%0.2f"表示保留2位小数
printf("\n") //每个记录要隔行
}
//原代码printf("%s%d%d",staff 1[k].name,staff 1[k].age,staff 1[k].salary)
耐嫌哪return 0
}
编译了一下,是这一行出错:
{temp = (ps + j) ->score(ps + j) ->score= (ps + j + 1) ->score(ps + j + 1) = temp}
具体一点是:
(ps + j + 1) = temp出错
报告赋值 *** 作类型错误,改成:
(ps + j + 1)->score = temp
可正常编译,
另,不知这一段:
for(i = 0i <4i++)
for(j = 0j <4 - ij++)
if((ps + j) ->score <(ps + j + 1) ->score)
{temp = (ps + j) ->score(ps + j) ->score= (ps + j + 1) ->score(ps + j + 1) = temp}
是否在做冒泡算法,
是的话,似应改成:
for(i = 0i <4i++)
for(j = i+1j <4+1 j++)
if((ps + i) ->score <(ps + j) ->score)
{temp = (ps + i) ->score(ps + i) ->score= (ps + j) ->score(ps + j) = temp}
供参考
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)