当前位置:首页 » 编程语言 » c语言pst

c语言pst

发布时间: 2023-07-10 10:57:24

① C语言这段代码该怎么改。。。编译运行无法显示排序后的结果

voidinputstudent(structstudent**pst1,int&len_1)//参数修改
{
inti;

printf("请输入学生的个数: ");
printf("len=");
scanf("%d",&len_1);//地址传参才可以改变原值
student*pst=(structstudent*)malloc(len_1*sizeof(structstudent));
*pst1=pst;//地址赋值

for(i=0;i<len_1;i++)
{
printf("请输入第%d个学生的信息: ",i+1);
printf("age=");
scanf("%d",&pst[i].age);

printf("name=");
scanf("%s",pst[i].name);

printf("score=");
scanf("%f",&pst[i].score);
}
}

调用处改为:

inputstudent(&parr,len);

错误在于,普通实参传递不能改变传入变量的值,需要使用二级指针和地址传参。

② 一道C语言题目。用结构体。运行环境VC

#include<iostream>
#include <time.h>
#include <iomanip>
using namespace std;

#define NUM 30
#define NUM_START 20000

char names[NUM][20]={
"abc0","abc1","abc2","abc3",
"abc4","abc5","abc6","abc7",
"abc8","abc9","abc10","abc11",
"abc12","abc13","abc14","abc15",
"abc16","abc17","abc18","abc19",
"abc20","abc21","abc22","abc23",
"abc24","abc25","abc26","abc27",
"abc28","abc29"
};

struct Student
{
int Number;
char Name[20];
float scoChn;
float scoMath;
float scoEng;
};

Student stu[NUM];

void Init(Student*pst)
{
srand(time(NULL));
for(int i=0;i<NUM;i++)
{
pst[i].Number=NUM_START+i;
strcpy(pst[i].Name,names[i]);
pst[i].scoChn=51+rand()%50;
pst[i].scoMath=51+rand()%50;
pst[i].scoEng=51+rand()%50;
}
}

void Show(Student*pst)
{
cout<<setw(8)<<"学号"
<<setw(20)<<"姓名"
<<setw(10)<<"语文成绩"
<<setw(10)<<"数学成绩"
<<setw(10)<<"英语成绩"
<<setw(10)<<"总成绩"
<<endl;
for(int i=0;i<NUM;i++)
{
cout<<setw(8)<<pst[i].Number
<<setw(20)<<pst[i].Name
<<setw(10)<<pst[i].scoChn
<<setw(10)<<pst[i].scoMath
<<setw(10)<<pst[i].scoEng
<<setw(10)<<pst[i].scoEng+pst[i].scoChn+pst[i].scoMath
<<endl;
}
}

void ShowAtIndex(Student*pst,int i)
{
if(i==-1)
{
cout<<"查无此人!"<<endl;
return;
}

cout<<setw(8)<<"学号"
<<setw(20)<<"姓名"
<<setw(10)<<"语文成绩"
<<setw(10)<<"数学成绩"
<<setw(10)<<"英语成绩"
<<setw(10)<<"总成绩"
<<endl;

cout<<setw(8)<<pst[i].Number
<<setw(20)<<pst[i].Name
<<setw(10)<<pst[i].scoChn
<<setw(10)<<pst[i].scoMath
<<setw(10)<<pst[i].scoEng
<<setw(10)<<pst[i].scoEng+pst[i].scoChn+pst[i].scoMath
<<endl;
}

void Sort(Student*pst)
{
Student tmp;
int imax;
float max;
for(int i=0;i<NUM-1;i++)
{
max=pst[i].scoChn+pst[i].scoMath+pst[i].scoEng;
imax=i;
for(int j=i+1;j<NUM;j++)
{
if(max<pst[j].scoChn+pst[j].scoMath+pst[j].scoEng)
{
max=pst[j].scoChn+pst[j].scoMath+pst[j].scoEng;
imax=j;
}
}
if(imax!=i)
{
tmp.Number=pst[i].Number;
strcpy(tmp.Name,pst[i].Name);
tmp.scoChn=pst[i].scoChn;
tmp.scoMath=pst[i].scoMath;
tmp.scoEng=pst[i].scoEng;

pst[i].Number=pst[imax].Number;
strcpy(pst[i].Name,pst[imax].Name);
pst[i].scoChn=pst[imax].scoChn;
pst[i].scoMath=pst[imax].scoMath;
pst[i].scoEng=pst[imax].scoEng;

pst[imax].Number=tmp.Number;
strcpy(pst[imax].Name,tmp.Name);
pst[imax].scoChn=tmp.scoChn;
pst[imax].scoMath=tmp.scoMath;
pst[imax].scoEng=tmp.scoEng;
}
}
}

int FindByName(Student*pst,char*na)
{
for(int i=0;i<NUM;i++)
{
if(strcmp(pst[i].Name,na)==0)
return i;
}
return -1;
}

void main()
{
char name[20];
Init(stu);
Sort(stu);
Show(stu);
cout<<endl<<"请输入要查找的学生的名字:";
cin>>name;

int index=FindByName(stu,name);
cout<<"查询结果:"<<endl;
ShowAtIndex(stu,index);
}

③ C语言函数参数传出怎么用

struct Student st;
void function(struct Student st);这种形式就是传递地址;
void fuction2(struct Student * pst);这种形式就是传递结构体的地址;
调用这两个函数的形式如下:
function(st);
fuction2(&st);
一般推荐第二种方式,因为第一种方式需要传递整个结构体,需要开辟sizeof(struct student)这么大的内存空间,开销太大,第二种方式则只需要开辟四个字节的内存,用来存放地址;

热点内容
c语言编译完怎么执行 发布:2025-02-09 00:16:16 浏览:27
AMD平台对应的C编译器 发布:2025-02-09 00:15:35 浏览:68
行李箱的密码锁哪里修 发布:2025-02-08 23:58:14 浏览:531
c语言字母ascii码表 发布:2025-02-08 23:55:49 浏览:838
笔记本电脑一般存储空间 发布:2025-02-08 23:51:15 浏览:835
php网站优化 发布:2025-02-08 23:49:41 浏览:455
php网页打印 发布:2025-02-08 23:40:02 浏览:820
windowssmb无法访问 发布:2025-02-08 23:33:28 浏览:467
python27编译器 发布:2025-02-08 23:29:20 浏览:339
如何运行python代码 发布:2025-02-08 23:28:15 浏览:692