结构体定义:
struct point {
int x ;
int y ;
}; 可在分号前加变量名表列。
typedef struct point {
int x ;
int y ;
}myPoint;
struct size {
int width ;
int height;
};
typedef struct size mySize;
struct rect {
myPoint point;
mySize size;
};
typedef struct rect myRect;
结构体内存占用 :简单表示是各个成员变量所占空间之和,实际上是 最大成员变量(类型)所占空间的最小整数倍。
结构体元素引用 结构体名 . 结构元素名。
// 实心矩形无坐标
//void juxing ( myRect rect )
//{
// for ( int i = 0 ; i < rect . size . height; i ++ ) {
// for ( int j = 0 ; j < rect . size . width ; j ++ ) {
// printf( " *");
// }
// printf( " \n");
// }
//}
// 空心矩形无坐标
//void juxing ( myRect rect )
//{
// for ( int i = 0 ; i < rect . size . height; i ++ ) {
// for ( int j = 0 ; j < rect . size . width ; j ++ ) {
if (i == 0 || i == rect . size . height-1 || j == 0 || j == rect . size . width-1) {
printf( " *");
}
else
printf( " ");
// int x = i == 0 || i == rect . size . height-1 || j == 0 || j == rect . size . width-1;
// printf( x ? "*" : " ");
// }
// printf( " \n");
// }
//}
// 空心矩形有坐标
void jx(myRect rect)
{
for ( int i = 0 ; i < rect . point . y ; i ++ ) {
printf( " \n" ) ;
}
for ( int i = 0 ; i < rect . size . height ; i ++ ) {
for ( int j = 0 ; j < rect . point . x + rect . size . width ; j ++ ) {
if ( j < rect . point . x ) {
printf( " " ) ;
}
else
{
int a = i == 0 || i == rect . size . height -1 || j == rect . point . x || j == rect . point . x + rect . size . width -1 ;
printf( a ? "*" : " " ) ;
}
}
printf( "\n" );
}
}
// 空心矩形套空心矩形
void juxings ( myRect rect )
{
for ( int i = 0 ; i < rect . point . y ; i ++ ) {
printf( " \n" ) ;
}
for ( int i = 0 ; i < rect . size . height; i ++ ) {
for ( int j = 0 ; j < rect . size . width + rect . point . x; j ++ ) {
if (j < rect . point .x) {
printf( " ");
}
else
{
int a = i == 0 || i == rect . size . height-1 || j == rect . point . x || j == rect . size . width + rect . point . x -1 || ((i == 2 || i == rect . size . height -3 ) && ( j != rect . point . x + 1 && j != rect . size . width + rect . point . x -2)) || (( j == rect . point . x + 2 || j == rect . size . width + rect . point . x - 3 ) && ( i != 1 && i != rect . size . height -2)) ;
printf( a ? "*" : " ");
}
}
printf( " \n");
}
}
void bubbleSort ( myRect array[] , int count )
{
for ( int i = 0 ; i < count -1 ; i ++ ) {
for ( int j = 0 ; j < count -i-1 ; j ++ ) {
if ( array[j] . size . width > array[j+1] . size . width) {
myRect temp = array[j];
array[j] = array[j+1];
array[j+1] = temp ;
}
}
}
}
// 矩形内套五角星
void juxing ( myRect rect )
{
for ( int i = 0 ; i < rect . size . height; i ++ ) {
for ( int j = 0 ; j < rect . size . width ; j ++ ) {
int x = i == 0 || i == rect . size . height-1 || j == 0 || j == rect . size . width-1 || i == rect . size . height /4 || i == 2 * j - rect . size . height + 1 || i == (-2) * j + rect . size . height -1 || i== 0.75* j + rect.size.height / 4 || i == (-0.75) * j + rect . size . height -1;
printf( x ? " *" : " ");
}
printf( " \n");
}
}
// 矩形内套五角星
void juxing ( myRect rect )
{
for ( int i = 0 ; i < rect . size . height; i ++ ) {
for ( int j = 0 ; j < rect . size . width ; j ++ ) {
int x = i == 0 || i == rect . size . height-1 || j == 0 || j == rect . size . width-1 || i == rect . size . height /3 || i == 2 * j - rect . size . height + 1 || i == (-2) * j + rect . size . height -1 || i== (int ) (0.67* j + rect.size.height / 3) || i == (int ) ((-0.67) * j + rect . size . height );
printf( x ? " *" : " ");
}
printf( " \n");
}
}
// printf( " %d \n" , (int )sizeof( struct myS ));
// struct point p1 = { 3, 5} ;
// printf( " x = %d\n y = %d \n " , p1.x , p1.y);
// myPoint p1 = {0,0};
// mySize s1 = {21,21} ;
// myRect r1 = {p1,s1};
// printf ( " 起始点坐标为 : %d ,%d \n 宽 : %d 高 : %d \n" , r1.point.x ,r1. point . y , r1 . size . width , r1 . size . height );
// juxing(r1);
// jx(r1);
// juxings(r1);
// myRect r2 = { { 0, 0 } , { 7 , 7 } } ;
// myRect r3 = { { 0, 0 } , { 5 , 5 } } ;
// myRect r[3] = { r1 , r2 , r3 } ;
// bubbleSort ( r , 3);
// for ( int i = 0 ; i < 3 ; i ++ ) {
// juxings(r[i] );
// printf( "\n") ;
// }
1、定义一个结构体变量(包含年月日),计算该日在本年中为第几天?要求写一个days函数。参数是此结构体类型的变量,返回值是整数。
2、模拟n个人参加选举的过程,四个候选人A、B、C、D。若选举某人直接输入其编号,最后按获得票数从高到低排序并输出候选人编号和票数。
// 作业一:
struct dayday {
int year ;
int month ;
int day ;
};
typedef struct dayday myday;
int days ( myday n);
// 作业二 :
void xuan(int n ) ;
int days ( myday n)
{
int sum = n . day ;
switch (n . month - 1 ) {
case 11:
sum += 30 ;
case 10:
sum += 31 ;
case 9:
sum += 30 ;
case 8:
sum += 31 ;
case 7:
sum += 31 ;
case 6:
sum += 30 ;
case 5:
sum += 31 ;
case 4:
sum += 30 ;
case 3:
sum += 31 ;
case 2:
if (( n . year %4 == 0 && n . year % 100 != 0 )|| n . year % 400 == 0 ) {
sum += 29 ;
}
else sum += 28;
case 1:
sum += 31 ;
}
return sum ;
}
void xuan(int n )
{
char array[4] = { 'a' , 'b' , 'c' , 'd' } ;
char ab ;
int piao[4] = {0} ;
int m = 0 ;
for ( int i = 0 ; i < n ; i ++ ) {
printf( " 请输入被选举人的编号 : " );
scanf ( "%c" , & ab) ;
getchar();
switch (ab) {
case 'a':
piao[0] ++ ;
break;
case 'b':
piao[1] ++ ;
break;
case 'c':
piao[2] ++ ;
break;
case 'd':
piao[3] ++ ;
break;
default:
m ++ ;
break;
}
}
for ( int i = 0 ; i < 3 ; i ++ ) {
for ( int j = 0 ; j < 3-i ; j ++ ) {
if (piao[j] < piao[j+1] ) {
int temp = piao[j] ;
piao[j] = piao[j+1] ;
piao[j+1] = temp ;
char tt = array[j] ;
array[j] = array[j+1] ;
array[j+1] = tt ;
}
}
}
printf( " 选举结果为 : \n " ) ;
for ( int i = 0 ; i < 4 ; i ++ ) {
printf( " %c : %d \n " , array[i] , piao[i] ) ;
}
printf( " %d 人弃权 \n" , m ) ;
}