c11结构体
1.
面試問題:typedef define 區別是什么?
typedef自定義類型
/C和C++的區別:
//C:空的結構體,大小不確定,報錯
//c++:空的結構體,大小為1個字節
typedef int *PP;//==》由這種變量提升為類型
2.聯合體
union sample {short i;//2char ch;//1float f;//4union MyUnion{char ch;}; }s;3.結構體的大小
struct A {char a;//1int b;//4 };//8 struct B {char a;//1+1short b;//2int c;//4 };//8 //向前對齊 struct C {char a;//1+3int b;//4short c;//2double d;//8 };//24 struct D {char a;//1char b;//1short c;//2int d;//4 };//8 struct E {char a;//1+1short c;//2char b;//1+3int d;//4 };//12 //向后對齊 struct F {int a;//4char c;//1//int d;//4struct F *p;//4 }; typedef struct A {char a;int b;//struct A bb;//在當前結構體內,不能自引用 }Aa; // .:是否為變量 ->:是否為指針 struct B {int i;//不能進行賦值struct A Aaa;struct A *p; }Bb;4.用聯合體判斷大小端??
bool IsLittle() {union MyUnion{short a;char ch;}uu;uu.a = 0x1234;if(uu.ch == 0x34){return true;}return false; }5.輸入年月日,得到今天是今年的第幾天
typedef struct Day {int year;int month;int day; }Day;int Today(Day * pdays) {int today = 0;int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31};if((pdays->year % 4 == 0 && pdays->year %100 != 0)|| pdays->year % 400 == 0){arr[1] = 29;}//1,1switch(pdays->month) {case 12://12today += arr[10];case 11://today += arr[9];case 10://today += arr[8];case 9://today += arr[7];case 8://today += arr[6];case 7://today += arr[5];case 6://today += arr[4];case 5://today += arr[3];case 4://today += arr[2];case 3://today += arr[1];case 2://today += arr[0];case 1:today += 0;break;}today += pdays->day;return today; }6.//3、在第一個字符串中 ,刪除第二個字符串中所有的字符。
//函數原型:char *DeleteChars(char *str1,char *str2);
//參數str1:“Welcome hello” 參數str2:“come” 結果輸出:“Wl hello”
7. 第四題4、篩選法求100以內的素數。即2的倍數大于1的全部置0,3的倍數大于1的全部置0,4的倍數大于1的全部置0…,最后找出不為0的數字
void Screen() {int arr[N+1]={};int i;for(i=1;i<=N;i++)//將所有數賦值為1{arr[i]=1;}//int i,count =2;int count =2;while(count<=N/2) //顯然:count不會超過N/2,必能使留下的數全為素數。{for( i=count+1;i<=N;i++){if(arr[i]==1&&i%count==0)//判斷是否為素數,如果否將這個值賦值為0arr[i]=0;}for(i=count+1;i<=N;i++){if(arr[i]==1)//如果是素數,將i的值賦給count,{count=i;break;//結束for循環}}}for(i=2;i<N;i++){if(arr[i]==1){printf("%3d",i);}} } void Prime(int n)//法二 {int *p=(int *)malloc(n*sizeof(int));assert(p!=NULL);for(int i=0;i<n;i++){p[i]=1;//亮燈}for(int j=2;j<n;j++)//2 -n之間的素數{for(int num=2;num<j;num++)//3-n之間能否整除判斷j是否為素數{if(j%num==0)//不是素數{p[j]=0;}}}for(int k=2;k<n;k++){if(p[k]==1){printf("%d ",k);}}printf ("\n"); } //棧上的內容函數運行完銷毀總結
- 上一篇: layui之在table的编辑的按钮的思
- 下一篇: leetcood学习笔记-2-两数相加