当前位置:首页 » 编程语言 » c语言结构体程序

c语言结构体程序

发布时间: 2022-12-31 07:27:34

1. 用c语言(结构体)编写程序

正好有个现成的:#include"stdio.h"
#define yes 1
#define no 0typedef struct date
{
int year;
int month;
int day;
}DATE;
long timedef(DATE d1,DATE d2);
int leap(int year);
int main()
{
DATE date1,date2;
date1.year=2008;
date1.month=11;
date1.day=1;
date2.year=2009;
date2.month=12;
date2.day=1; printf("%ld\n",timedef(date1,date2));}
int leap(int year)
{
if(year%400==0 ||(year%4==0&&year%100!=0))
return yes;
else
return no;
}
long timedef(DATE d1,DATE d2)
{
int i;
long day=0,dayt1=0,dayt2=0;
int d[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
for(i=d1.year;i<d2.year;i++)
if(leap(i)==no) day+=365;
else day+=366; for(i=1;i<d1.month;++i) dayt1+=d[leap(d1.year)][i];
dayt1+=d1.day; for(i=1;i<d2.month;++i) dayt2+=d[leap(d2.year)][i];
dayt2+=d2.day; return day+dayt2-dayt1;
}

2. C语言结构体简单编程

引用指针变量前没有对赋予确定的值。其实指针变量p中不是空的,而是存放了不可预测的值。这种编写方式是很危险的,可能破坏系统的工作环境。
改成下面的样式就可以了
void main()
{
//printf("%d",mode(2));
struct student m;
struct student *hf;
hf=&m;
hf->num=10;
printf("%d",hf->num);
}

3. 【重金悬赏】C语言结构体编程

修改如下:

#include <stdio.h>
struct dat
{
int year;
int month;
int day;
};
struct STUDENT
{
int studentID;
char studentName[10];
char studentSex[4];
struct dat timeOfEnter;
int scoreComputer;
int scoreEnglish;
int scoreMath;
int scoreMusic;
};
struct STUDENT stu[30]={{1,"令狐冲","男",{1999,12,20},90,83,72,82},
{2,"林平之","男",{1999,07,06},78,92,88,78},
{3,"岳灵珊","女",{1999,07,06},89,72,98,66},
{4,"任莹莹","女",{1999,07,06},78,95,87,90}
};
void main()
{
struct STUDENT *pt;
float sum[4] ={0.0},average[4] = {0.0};
int i;
char *name[]={"score of Computer","score of English",
"score of Math","score of Music"};
pt = stu;
printf("Name\t\t%s\t%s\t\t%s\t\t%s\n","Computer","English","Math","Music");
for (pt=stu; pt<stu+4; pt++) {
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\n",pt->studentName,pt->scoreComputer,pt->scoreEnglish,pt->scoreMath,pt->scoreMusic);
}
printf("\n\nName\t\tTotal\t\tAverage\n");
for (pt=stu; pt<stu+4; pt++) {
printf("%s\t\t%d\t\t%g\n",pt->studentName,pt->scoreComputer+pt->scoreEnglish+pt->scoreMath+pt->scoreMusic,(pt->scoreComputer+pt->scoreEnglish+pt->scoreMath+pt->scoreMusic)/4.0);
}
putchar('\n');
for(pt=stu;pt<stu+30;pt++)
{
sum[0] = sum[0] + pt->scoreComputer;
sum[1] = sum[1] + pt->scoreEnglish;
sum[2] = sum[2] + pt->scoreMath;
sum[3] = sum[3] + pt->scoreMusic;
}
printf("Total:\n");
for (i = 0; i<4; i++) {
printf("%20s:%g\n",name[i],sum[i]);
}
printf("\nAverage:\n");
for(i=0;i<4;i++)
{
average[i] = sum[i]/4;
printf("%20s : %4.2f\n" ,name[i],*(average+i));
}

}

4. C语言结构体的程序

第一段代码我觉得应该选d;
第一个printf不用说打印的就是a的元素;说说b,b=a;把a赋给b;gender和score不用说;name是个指针,所以复制给b的也是个指针,a的name和b的name指向同一块类存地址,然后调用f();函数把b的name指针传递进去,在f()函数中开辟一块空间并把这块空间的地址赋给b的name指针,f()函数结束后,在f();中开辟的空间并未消失了,f()函数中b。name指针是局部的,f();结束后b的name指针在栈中被释放了,所以b。name和a的name指正还是一样的,
第二段代码就选a;
在struct stu中name是个字符数组,在创建stu的一个对象后,就有空间储存字符,b=a;你懂的
然后调用f()函数,把qian写入b的name的那块空间,空间中的字符串就变为了qian,

5. C语言结构体编程

#include<stdio.h>

#include<stdlib.h>

typedef struct

{

char num[10];

int s;


}strec;

int N;

int fun(strec*a,strec*b)

{

int i,j=0;

strec max;

for(i=0;i<N;i++)

{

printf("输入姓名:");

scanf("%s",a[i].num);

printf("输入分数:");

scanf("%d",&a[i].s);

}

max=a[0];

for(i=0;i<N;i++)

if(max.s<a[i].s)

max=a[i];

for(i=0;i<N;i++)

if(max.s==a[i].s)

{ b[j]=a[i];

j++;

}

for(i=0;i<j;i++)

{

printf("%s ",b[i].num);

printf("%d",b[i].s);

printf(" ");

}


return j;





}

void main()

{

strec *a,*b;

int n;

printf("input N:");

scanf("%d",&N);

a=(strec*)malloc(N*sizeof(strec));

b=(strec*)malloc(N*sizeof(strec));

n=fun(a,b);

printf("最高分有%d人 ",n);


}

采纳时多给点分!桥这么多代码不容易啊!

6. C语言程序 结构体、共同体

#include<stdio.h>
structDate
{
intyear;
intmonth;
intday;
};
structPerson//加上typedefme就是变量类型,去掉就是实例变量
{
charname[10];
charsex;
structDatebirthday;
charID[19];
charemail[60];
}me;
intmain()
{
printf("请输入各人信息<姓名、性别<‘m’或‘f’>、出生年月日、身份证号、电子邮箱>");
scanf("%s%c%d%d%d%s%s",me.name,me.sex,&me.birthday.year,&me.birthday.month,&me.birthday.day,me.ID,me.email);
printf("姓名:%s 性别:%c 出生日期:%d年%d月%d日 身份证号:%s 电子邮箱:%s ",me.name,me.sex,me.birthday.year,me.birthday.month,me.birthday.day,me.ID,me.email);
return0;
}

7. 写一个关于结构体的 C语言程序

以前写过的程序,你自己改下用吧
#include "stdio.h"

int get_week(int y,int m,int d)
{
int a; /* 用来保存计算得到的星期几的整数*/
if((m==1)||(m==2))/*如果是一月或二月进行换算*/
{
m+=12;
y--;
}
a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; /*基姆拉尔森计算公式,得到的星期几的整数*/
return a;
}

void PrintWeek(int weekday)
{
switch(weekday)
{
case 6 : printf("%4s","Sun"); break;
case 0 : printf("%4s","Mon"); break;
case 1 : printf("%4s","Tue"); break;
case 2 : printf("%4s","Wed"); break;
case 3 : printf("%4s","Thu"); break;
case 4 : printf("%4s","Fri"); break;
case 5 : printf("%4s","Sat"); break;
}
}
main()
{
int year,month,day;
printf("Input the date:(yyyy-mm-dd)");
scanf("%d-%d-%d",&year,&month,&day);
if((year<1)||(month<1||month>12)||(day<1||day>31))
{
printf("Invalid date format.\n");
/*break;*/
}
printf("The week is");
PrintWeek(get_week(year,month,day));
printf("\n");
getch();
}

8. 用c语言结构体编写

//很明显这是结构体定义
structstudentNode{
charname[30];
charsno[30];
};

//比较字符数组的函数,对于结构体中的字符数组,直接用串比较可能会错误处理一些特殊字符。
boolcompare(constchar*str1,constchar*str2){
for(inti=0;i<30;i++){
if(str1[i]!=str2[i]){
if(str1[i]>str2[i]){
returntrue;
}else{
returnfalse;
}
}
}
returntrue;
}

//排序函数,传入结构体数组指针,长度,第三个参数是是否为降序
voidsortWithSno(studentNode*stu,intlen,boolisdesc){
studentNodetemp;
for(inti=0;i<len;i++){
for(intj=0;j<len;j++){
if(isdesc){
if(compare(&(*(stu+i)->sno),&(*(stu+j)->sno))){
temp=*(stu+j);
*(stu+j)=*(stu+i);
*(stu+i)=temp;
}
}else{
if(!compare(&(*(stu+i)->sno),&(*(stu+j)->sno))){
temp=*(stu+j);
*(stu+j)=*(stu+i);
*(stu+i)=temp;
}
}
}
}
}

//同上,可以将两个函数合并,中间再加一层结构体成员的枚举选择器
voidsortWithName(studentNode*stu,intlen,boolisdesc){
studentNodetemp;
for(inti=0;i<len;i++){
for(intj=0;j<len;j++){
if(isdesc){
if(compare(&(*(stu+i)->name),&(*(stu+j)->name))){
temp=*(stu+j);
*(stu+j)=*(stu+i);
*(stu+i)=temp;
}
}else{
if(!compare(&(*(stu+i)->name),&(*(stu+j)->name))){
temp=*(stu+j);
*(stu+j)=*(stu+i);
*(stu+i)=temp;
}
}
}
}
}

//很明显这是个打印函数
voidprint(studentNodestu[],intlen){
for(inti=0;i<len;i++){
printf("%d:name:%ssno:%s ",i,stu[i].name,stu[i].sno);
}
}

注意这程序中使用了结构体直接复制,老TC应该会挂掉,用最新的GCC能编译

9. 分析c语言程序---结构体程序

union是用来定义联合体的关键字,union维护足够的空间来置放多个资料成员中的最大的“一种”,而不是为每一个资料成员配置空间,在union中所有的资料成员共用一个空间,同时间只能储存其中一个成员的资料。
可以得出该共用体在内存中占用4个字节。

k.a[0]=10000; k.a[1]=15000; 为k的int a[2]成员赋值,即该内存的内容是[00111010] [10011000] [00100111] [00010000]将这4个字节换算成符点数就是0.001161.

scanf ("%s",k.d); //k.d[]="abcd"
因为scanf把4个输入压栈后再出栈对4个字符变量赋制值,则内存结构为:
[01100100] [01100011] [01100010] [01100001]
d[3] d[2] d[1] d[0]
c(4个字节)
b(前两个字节)
a[1](前两个字节) a[0](后两个字节)
将这4个字节换算成符点数c就是:16777999408082104400000.000000.

至于上面的数字是怎么得来的,那要考虑浮点数在计算机中的储存结构问题了.在存储1个浮点数的32位内存中,
第一位是符号位,下来的8位是指数位,后面的23位是尾数位.
[01100100] [01100011] [01100010] [01100001]
换算成浮点数的算法是:
第一位为0:正数
下来的8位是:[11001000],减去[01111111]为[1001001].所以指数为73
再下来的23位是1100011[01100010] [01100001].所以尾数为
(1.11000110110001001100001)B = 2^0+2^(-1)+2^(-1)+2^(-6)+2^(-7)+2^(-9)+2^(-10)+2^(-14)+2^(-17)+2^(-18)+2^(-23).
最后结果=底数^指数
可能你还会问为什么位数一样,结果还是有差距,那是因为浮点数的计算精度问题.浮点数的精度是有限的.

热点内容
90岁老年人助听器如何配置 发布:2025-07-02 00:59:16 浏览:815
配置ip代理服务器加速软件 发布:2025-07-02 00:58:32 浏览:693
linux链接库 发布:2025-07-02 00:53:06 浏览:676
数据库的划分的 发布:2025-07-02 00:43:19 浏览:655
补码源码和 发布:2025-07-02 00:37:25 浏览:979
centos7mysql远程访问 发布:2025-07-02 00:35:58 浏览:712
有线认证服务器地址错误 发布:2025-07-02 00:33:22 浏览:278
本田思域2021款买哪个配置 发布:2025-07-02 00:31:43 浏览:326
安卓十二系统什么时候更新 发布:2025-07-02 00:12:28 浏览:346
shell脚本需要编译链接 发布:2025-07-02 00:04:20 浏览:475