一、定義結構體類型struct date,它含有年、月、日3個成員;
二、定義struct date類型的變量,並從鍵盤輸入初值;
三、利用循環語句,計算天數;
四、 利用分支語句,判斷閏年。算法
一、定義候選人struct person結構體,含有姓名、得票數兩個成員;
二、 定義struct person結構體數組,人數自定,初始得票數爲0;
三、利用循環輸入候選人名字,該人員票數加0;
四、 輸入的候選人不是規定候選人時,至關於廢票,不累計。數組
一、 定義結構體類型,其餘成員有編號、姓名、職業和班級(或職務);
二、定義該結構體類型的數組,可有若干數組元素;
三、利用循環語句輸入每條記錄的數據,若該記錄的職業爲「s」,則該記錄是學生的,輸入該學生的班級;若該記錄的職業爲「t」,則該記錄是教師的,輸入該教師的職務;
四、根據記錄的職業來判斷,使用哪一個printf()語句輸出記錄。函數
一、定義結構體類型,其成員有人員編號(地址);
二、定義該結構體類型的數組,可有n個數組元素;
三、n我的圍成一圈,能夠考慮用結構體數組元素中的「下一成員編號」來實現,可是最後一個元素的「下一人員編號」指向第一個。spa
#include<stdio.h> main() { struct date { int year; int month; int days; }; struct date a; int i,days; printf("輸入年,月,日:"); scanf("%d %d %d",&a.year,&a.month,&a.days); for(i=1;i<a.month;i++) { if (i==1||i==3||i==5||i==7||i==8||i==10) days+=31; else if(i==4||i==6||i==9||i==11) days+=30; else if(a.year%400==0||(a.year%4==0&&a.year %100!=0)) days+=29; else days+=28; } days+=a.days-1; printf("%d年%d月%d日是該年的第%d天",a.year,a.month,a.days,days); }
.設計
days=days+a.day,從1月1日開始算起,因此後面應該-1,不然輸出的結果爲87。指針
#include "stdio.h" #include <string.h> struct person { char name[20]; int count; }a[6]={"zhang",0,"li",0,"wang",0,"zhao",0,"liu",0,"zhu",0}; main() { int i, j; char abc[20]; for(i=1;i<=10;i++) { printf("輸入候選人名字: "); scanf("%s",abc); for(j=0;j<6;j++) if(strcmp(abc,a[j].name)==0) a[j].count++; /*若第j個候選人名字與輸入的名字相同,第j位候選人票數加1*/ } for(j=0;j<6;j++) printf("%s:%d\n",a[j].name,a[j].count); }
本題運用了strcmp()函數,功能:用來比較兩個字符串,基本形式爲strcmp(str1,str2),若str1=str2,則返回零;若str1<str2,則返回負數;若str1>str2,則返回正數。 code
#include<stdio.h> #include<stdlib.h>//包含頭文件stdlib.h struct { int number; char name[30]; char job; union { int classes; char position[10]; }category; }person[2]; main() { int i; for(i=0;i<2;i++) { scanf("%s%d%s",&person[i].name,&person[i].number,&person[i].job); if(person[i].job=='s') { scanf("%d",&person[i].category.classes); } else if(person[i].job=='t') { scanf("%s",&person[i].category.position); } else{ printf("input error!"); abort();//若輸入錯誤,則退出程序 } } printf("\n"); printf("編號\t\t姓名\t\t職位\t\t班級\職務\n"); for(i=0;i<2;i++) { if(person[i].job=='s') printf("%d\t\t%s\t\t%c\t\t%d\n",person[i].number,person[i].name,person[i].job,person[i].category.classes); else printf("%d\t\t%s\t\t%c\t\t%s\n",person[i].number,person[i].name,person[i].job,person[i].category.position); } }
製做表格咱們能夠用\t\t來輸出,若是用空格的話會致使數據不整齊。orm
#include<stdio.h> #define N 10 struct child { int no; int next; }; struct child link[N]; main() { int i,n,m,s,count,h; printf("輸入圍圈入數,出圈報數,開始報數位置:"); scanf("%d %d %d",&n,&m,&s); for(i=1;i<=n;i++) { if(i==n) link[i].next=1; else link[i].next=i+1; link[i].no=i; } count=0; if(s==1)h=n;else h=s-1; printf("出圈順序爲:"); while(count<n-1)//出圈人數<圍圈人數-1 { i=0; while(i!=m) { h=link[h].next; if(link[h].no) i++; } printf("%d, ",link[h].no); link[h].no=0; count++; } for(i=1;i<=n;i++) if(link[i].no!=0) printf("%d ",link[i].no); }
字符串比較的時候,用strcmp()函數進行比較。blog
要有邏輯思惟。字符串