當前位置:首頁 » 編程語言 » c語言結構體程序設計

c語言結構體程序設計

發布時間: 2023-05-23 05:20:03

1. c語言結構體編程

1、struct REC
{int n; char c;};
REC t1,t2;//修改廳畝 struct REC t1,t2;
因為不是所有編譯器都支持你那種寫法

2、
typedef struct REC
{int n=0;char c=』A』;}t1,t2;
REC t1,t2
不知道你什麼意思,不過可以這樣寫
typedef struct
{int n=0;char c=』A』;}
REC;
REC t1,t2
表示將這個結構體重命名為REC,否則使用這類型的時候需加struct(特別是C編譯器)

3、struct
{int n;char c;}REC;
這種方式編譯應該通過的,不過是將REC認為是變數,而不是類型改伏鏈

總之你要定義一個t1或者t2的變數,常用核孫以下幾種寫法
struct
{
......
}t1,t2;

struct REC
{
....
};
struct REC t1,t2;

typedef struct
{
.....
} REC;
REC t1,t2;

希望你能理解原理哈

2. 建立一個結構體類型,編寫c語言程序。

#include <stdio.h>
char *SubjectName[] = {"數學","物理","化學","生物","語文","外語","政治","歷史","體育","美術","音樂"}; //科目名稱列表 可調整順序
#define SUBJECT 2 //科目數 可設定 1 - 11門,超過11門時得添加上行科目名稱列表
#define STUDENTS_MAX 100 //最大可輸入的學生數量 根據實際情況調整

typedef struct student{char name[12]; float core[SUBJECT+1];} Student;//最長姓名只許有五個漢字長度,超長會出錯 改一下這個名字長度或規定名字最長5個字多餘的不輸入也成

Student Stu[STUDENTS_MAX]; //結構體數組 全局變數 雖可以不用全局的,要是你自己寫了很多函數,都要訪問它們,不如就讓它們做全局變數的好

void main()
{
int total,i,j,k;
printf("請輸入學生總數[1-%d]:",STUDENTS_MAX);
scanf("%d",&total);

if(total>STUDENTS_MAX){printf("超出設計總數了");exit(-1);}
else if(total<1){printf("沒有學生統計個屁呀"); exit(-1);}

printf("請輸入學生成績,格式:\n\t姓名 ");
for(j=0;j<SUBJECT;j++) printf("%s ",SubjectName[j]);
printf("\n");
for(i=0;i<total;i++)
{
Stu[i].core[SUBJECT]=0.0;//平均分
printf("No.%-3d ",i+1);
fflush(stdin); //清輸入緩沖區
scanf("%s %f",Stu[i].name,&Stu[i].core[0]); //至少有一門科目的
for(j=1;j<SUBJECT;j++) //其它科目 定義了多少就輸入多少中兄棚
{
scanf("%f",&Stu[i].core[j]);
Stu[i].core[SUBJECT] += Stu[i].core[j];
}
Stu[i].core[SUBJECT] /= SUBJECT; //平均分 只是求了沒有輸出
}

printf("\n不及格情況統計:\n");
for(k=0,i=0;i<total;i++)
{
char putbuf[128],buf1[5];
int flag = 0;
strcpy(putbuf,Stu[i].name); //記下名字
for(j=0;j<SUBJECT;j++)
if(Stu[i].core[j]<60)
{
flag++;
strcat(putbuf," ");
strcat(putbuf,SubjectName[j]);//不及格的科目名稱
strcat(putbuf,":");
sprintf(buf1,"%4.1f",Stu[i].core[j]);strcat(putbuf,buf1);//分數
}
if(flag)
{
printf("%s %d門不及格\n",putbuf,flag); //有不及格則輸出
k++; //統計不及格人數
}
}
if(!k)
printf("沒有不及格的學生");
else
printf("有不及格科目的學生個數:%d",k);
}
//常看塵困到有要這類程序的,今天寫一個,給大家參考,別天天上來要程序啊,仿照這個自己編吧
//程序在VC7.1下編譯通過,測試運行賣則正常

3. C語言實驗 結構體與文件程序設計

//main.c文件
#include<stdio.h>
#include<stdlib.h>

#define BufLen 20
#define KeChengCount 3

typedef struct tagStuInfor
{
char xh[BufLen]; //學號
char xm[BufLen]; //返高雀姓名
char kchm[KeChengCount][BufLen]; //課程名
float pshchj[KeChengCount]; //平時成績
float kshchj[KeChengCount]; //考試成績
float zpchj[KeChengCount]; //總評成績
float zf;
}StudentInformation;
typedef struct tagBookInfor
{
char shm[BufLen]; //書名
float dj; //單價
}BookInformation;

void shurustu();
void zongfen();
void shuchumaxmin();
void sortbook();
void freeall();

//學生數,書數
int xshsh,shsh;
StudentInformation *ptstu=NULL;
BookInformation *ptbook=NULL;
int main()
{
char in[]="data.txt",out[]="T_data.txt";
int i;
float chjs[6],zf;
FILE *fin,*fout;

printf("250這個數字太不好了,能換成350嗎?\n\n");

shurustu();
zongfen();
shuchumaxmin();

printf("請輸入書數:");
scanf("%d",&shsh);
ptbook=(BookInformation*)calloc(shsh,sizeof(BookInformation));
for(i=0;i<shsh;i++)
{
printf("請輸入第%d本書的書名:",i+1);
scanf("%s",ptbook[i].shm);
printf("請輸入第%d本書的單價:",i+1);
scanf("%f",&ptbook[i].dj);
}
printf("\n");
sortbook();
printf("按書的單價升序排序後的結果:\n書名\t單價\n");
for(i=0;i<shsh;i++)
{
printf("%s\t%.2f\n",ptbook[i].shm,ptbook[i].dj);
}

fin=fopen(in,"r");
fout=fopen(out,"w");
while(!feof(fin))
{
for(i=0;i<6;i++)
{
fscanf(fin,"%f",&chjs[i]);
}
zf=0.2*(chjs[0]+chjs[1]+chjs[2])+0.8*(chjs[3]+chjs[4]+chjs[5]);
printf("%.2f\n",zf);
fprintf(fout,"%.2f\r\n",zf);
}
printf("\n");
fclose(fin);
fclose(fout);

freeall();
system("PAUSE");
return EXIT_SUCCESS;
}
void shurustu()
{
int i;

printf("請輸入學生的個數:");
scanf("%d",&xshsh);
ptstu=(StudentInformation*)calloc(xshsh,sizeof(StudentInformation));
for(i=0;i<xshsh;i++)
{
strcpy(ptstu[i].kchm[0],"C語言");
strcpy(ptstu[i].kchm[1],"高等漏早數學");
strcpy(ptstu[i].kchm[2],"英語");
printf("請念納輸入第%d個學生的學號:",i+1);
scanf("%s",ptstu[i].xh);
printf("請輸入第%d個學生的姓名:",i+1);
scanf("%s",ptstu[i].xm);
printf("請輸入第%d個學生的C語言課程的平時成績和考試成績:",i+1);
scanf("%f %f",&ptstu[i].pshchj[0],&ptstu[i].kshchj[0]);
printf("請輸入第%d個學生的高等數學課程的平時成績和考試成績:",i+1);
scanf("%f %f",&ptstu[i].pshchj[1],&ptstu[i].kshchj[1]);
printf("請輸入第%d個學生的英語課程的平時成績和考試成績:",i+1);
scanf("%f %f",&ptstu[i].pshchj[2],&ptstu[i].kshchj[2]);
}
printf("\n");
}
void zongfen()
{
int i;

for(i=0;i<xshsh;i++)
{
ptstu[i].zpchj[0]=0.2*ptstu[i].pshchj[0]+0.8*ptstu[i].kshchj[0];
ptstu[i].zpchj[1]=0.2*ptstu[i].pshchj[1]+0.8*ptstu[i].kshchj[1];
ptstu[i].zpchj[2]=0.2*ptstu[i].pshchj[2]+0.8*ptstu[i].kshchj[2];
ptstu[i].zf=ptstu[i].zpchj[0]+ptstu[i].zpchj[1]+ptstu[i].zpchj[2];
printf("%s的%s,%s,%s的總評成績是:",ptstu[i].xm,ptstu[i].kchm[0],ptstu[i].kchm[1],ptstu[i].kchm[2]);
printf("%.2f,%.2f,%.2f\n",ptstu[i].zpchj[0],ptstu[i].zpchj[1],ptstu[i].zpchj[2]);
printf("%s的總分是:%.2f\n",ptstu[i].xm,ptstu[i].zf);
}
printf("\n");
}
void shuchumaxmin()
{
int i,min,max;

min=max=0;
for(i=1;i<xshsh;i++)
{
if(ptstu[i].zf<ptstu[min].zf)
{
min=i;
}
if(ptstu[i].zf>ptstu[max].zf)
{
max=i;
}
}
printf("總分最高的學生的信息:\n學號\t姓名\t");
printf("%s(平時)\t%s(平時)\t%s(平時)",ptstu[max].kchm[0],ptstu[max].kchm[1],ptstu[max].kchm[2]);
printf("\t%s(考試)\t%s(考試)\t%s(考試)\t總分\n",ptstu[max].kchm[0],ptstu[max].kchm[1],ptstu[max].kchm[2]);
printf("%s\t%s\t",ptstu[max].xh,ptstu[max].xm);
printf("%.2f\t%.2f\t%.2f\t",ptstu[max].pshchj[0],ptstu[max].pshchj[1],ptstu[max].pshchj[2]);
printf("%.2f\t%.2f\t%.2f\t%.2f\n\n",ptstu[max].kshchj[0],ptstu[max].kshchj[1],ptstu[max].kshchj[2],ptstu[max].zf);
printf("總分最低的學生的信息:\n學號\t姓名\t");
printf("%s(平時)\t%s(平時)\t%s(平時)",ptstu[min].kchm[0],ptstu[min].kchm[1],ptstu[min].kchm[2]);
printf("\t%s(考試)\t%s(考試)\t%s(考試)\t總分\n",ptstu[min].kchm[0],ptstu[min].kchm[1],ptstu[min].kchm[2]);
printf("%s\t%s\t",ptstu[min].xh,ptstu[min].xm);
printf("%.2f\t%.2f\t%.2f\t",ptstu[min].pshchj[0],ptstu[min].pshchj[1],ptstu[min].pshchj[2]);
printf("%.2f\t%.2f\t%.2f\t%.2f\n\n",ptstu[min].kshchj[0],ptstu[min].kshchj[1],ptstu[min].kshchj[2],ptstu[min].zf);
}
void sortbook()
{
int i,j,min;
BookInformation t;

for(i=0;i<shsh-1;i++)
{
min=i;
for(j=i+1;j<shsh;j++)
{
if(ptbook[j].dj<ptbook[min].dj)
{
min=j;
}
}
t=ptbook[i];
ptbook[i]=ptbook[min];
ptbook[min]=t;
}
}
void freeall()
{
if(NULL!=ptstu)
{
free(ptstu);
}
if(NULL!=ptbook)
{
free(ptbook);
}
}
//data.txt文件
50 65 70 88 97 80
66 87 95 46 88 97
77 82 65 58 91 58
78 87 91 48 66 70

4. c語言 結構體 編程

立一個記錄學生學號和期中·期末成績的系統(可以運行,但是喚做每次會彈出問題框,使因為配沒p1->next沒有指向空指針)
#include<stdio.h>
#include<windows.h>

struct student
{ int num;
float mid;
float end;

struct student *next;};
int n;
struct student *lucifer()
{struct student *head,*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(sizeof(struct student));
scanf("%d",&p1->num);
scanf("%f",&p1->mid);
scanf("%f",&p1->end);
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%d",&p1->num);
scanf("%f",&p1->mid);
scanf("%f",&p1->end);
}
return(head);
}p1->next=NULL;
void main()
{printf("請輸入學生學號和期中期末成培鏈納績\n");
struct student *p;
p=lucifer();
do{printf("學號是%d\t期中成績%f\t期末成績%f\n",p->num,p->mid,p->end);
p=p->next;}
while(p!=NULL);
}這個應該可以額 有什麼可以交流

5. 用c語言結構體指針設計程序

參考代碼:

#include"string.h"
#include<stdio.h>
structstudent
{
charname[20];floats[3];
floataver;
};
main()
{
voidenter(structstudent*,int);
voidcalaver(structstudent*,int);
voiddisplay(structstudent*,int);
structstudenta[5];
enter(a,5);
斗仿calaver(a,5);
display(a,5);
}
voidenter(structstudent*p,intn)
{
inti,j;
空培纖for(i=0;i<n;i++)
{
printf("請輸入學生的姓名 ");
scanf("%s",p->name);
printf("請輸入三門課的成績 ");
for(j=0;j<3;j++)
scanf("%f",&p->s[j]);
p++;
}
}
voidcalaver(structstudent*p,intn)
{
inti,j;
floats;
for(i=0;i<n;i++)
{
s=0.0;
for(j=0;j<3;j++)
s+=p->s[j];
中銀p->aver=s/3;
p++;
}
}
voiddisplay(structstudent*p,intn)
{
inti;
for(i=0;i<n;i++)
{
printf("%-10s%5.1f%5.1f%5.1f%10.1f ",p->name,p->s[0],p->s[1],p->s[2],p->aver);
p++;
}
}

6. C語言程序設計,結構體

#include<stdio.h>
#include<stdlib.h>
typedefstructScoreTable{
intnum;
intscore;
}st;
intmain(){

sta[1024];
intnum,score,rank;
int中察t;
scanf("%d",&t);
inttnum,tscore;
while(t--){

scanf("%d",&圓含num);
rank=1;
inti=0;
while(1){
scanf("%d%d",&tnum,&tscore);
if(tnum==0&&tscore==0)break;
a[i].num=tnum;a[i].score=tscore;i++;
if(tnum==num)score=tscore;
}
intn=i;
for(i=0;i<n;i++){
if(a[i].score>score)rank++;
}
printf("%d ",rank);
}
//system("pause");
return0;
}


此題目的解答已經放在橘培笑了我的網路空間,

http://hi..com/vrzwlwidgwbetzq/item/6d6f8e5326c22c23db163548

還有一些被採納的比較有趣的題目,

http://hi..com/vrzwlwidgwbetzq

希望對你有所幫助~~~~~~

7. C語言編程:定義一個汽車結構體

按照題目要求編寫的C語言汽車結構體程序如下

#include<stdio.h>

struct car{

float petrol;

}c1={0};

struct car refuel(struct car c){

c.petrol=c.petrol+2;

return c;

}

int main(){

int i;

for(i=0;i<5;i++){

c1=refuel(c1);

printf("%.2f ",c1.petrol);

}

return 0;

}

8. c語言設計菜單程序的結構體(只是最簡單的菜單)(c語言菜單選擇程序設計)

實現步驟:

1,在LV的編輯菜單中選擇『運行時賣掘菜單』菜單

2,在出現的對話框中選擇菜單御配洞類型為自定義,然後編輯自己的菜單

3,在程序框圖中中添加事件結構,選擇添加結構,事件類鎮枯型為:本VI-菜單選擇(用戶)

4,編輯條件結構處理各個菜單標識符對應的事件

9. C語言結構體程序設計問題

teacher2=teacher1;
strcpy(teacher2.cheng,"教授");

10. C語言結構體編程

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct telephone
{
char name[10];
char telno[20];
};
void search(struct telephone b[], char *x, int n);
int main()
{
int i,n;
struct telephone b[100];
char nane[100];

for(i=0;;i++)
{
printf("Please input name:");
gets(b[i].name);
if(b[i].name[0]=='#')
break;
printf("Please input telephone:");
gets(b[i].telno);
}
n=i;
printf("Please input you want to find name:");
gets(nane);
search(b,&nane[0],n);
return 0;
}
void search(struct telephone b[],char *x,int n)
{
int i;
int find=0;
for(i=0;i<n;i++)
{
if(strcmp(x,b[i].name)==0)
{
printf("the telephone is %s\n",b[i].telno);
find=1;
}
}
if(find==0)
printf("Not found!");
}

熱點內容
linuxshellif 發布:2024-05-04 17:09:47 瀏覽:15
演算法精英挑戰賽 發布:2024-05-04 17:09:08 瀏覽:738
河南電力公眾號綁定密碼是多少 發布:2024-05-04 17:08:55 瀏覽:331
手機上怎麼打開壓縮文件 發布:2024-05-04 17:03:57 瀏覽:171
word加密文件如何解密 發布:2024-05-04 17:02:57 瀏覽:289
php源碼本地測試 發布:2024-05-04 16:57:17 瀏覽:800
c語言編譯exe 發布:2024-05-04 16:57:16 瀏覽:974
國密演算法獲取 發布:2024-05-04 16:38:24 瀏覽:70
腳本精靈荒野亂斗 發布:2024-05-04 16:28:33 瀏覽:520
剛到的筆記本怎麼看配置 發布:2024-05-04 16:26:58 瀏覽:4