當前位置:首頁 » 編程語言 » 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)這么大的內存空間,開銷太大,第二種方式則只需要開辟四個位元組的內存,用來存放地址;

熱點內容
tomcat上傳超時 發布:2025-02-09 01:41:42 瀏覽:483
androidactivity豎屏 發布:2025-02-09 01:41:40 瀏覽:377
家庭配置怎麼合理 發布:2025-02-09 01:36:14 瀏覽:807
頭條軍事源碼 發布:2025-02-09 01:31:53 瀏覽:997
androidintent視頻 發布:2025-02-09 01:31:43 瀏覽:858
歐姆龍plc密碼如何設置 發布:2025-02-09 01:24:31 瀏覽:687
php支持jpeg 發布:2025-02-09 01:24:22 瀏覽:803
反編譯去注冊碼 發布:2025-02-09 01:19:48 瀏覽:887
安卓如何查找舊密碼 發布:2025-02-09 01:17:21 瀏覽:418
hadoop雲存儲 發布:2025-02-09 01:02:49 瀏覽:453