当前位置:首页 » 编程语言 » 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!");
}

热点内容
我的世界怎样刷出32k服务器 发布:2024-05-18 14:32:32 浏览:565
c语言程序设计江宝钏 发布:2024-05-18 14:32:22 浏览:780
右击文件夹总是转圈圈 发布:2024-05-18 14:31:10 浏览:695
新建数据库phpmyadmin 发布:2024-05-18 14:22:38 浏览:735
安卓手机设备连接在哪里 发布:2024-05-18 14:08:28 浏览:819
路由器的密码最多是多少位 发布:2024-05-18 13:58:18 浏览:419
扫描服务器名称如何填 发布:2024-05-18 13:36:29 浏览:114
芒果缓存的视频看不了视频怎么下载不了 发布:2024-05-18 13:35:14 浏览:519
c语言发短信 发布:2024-05-18 13:23:08 浏览:834
vb数据库程序 发布:2024-05-18 13:01:57 浏览:113