初學(xué)C語言常用簡單程序代碼_第1頁
初學(xué)C語言常用簡單程序代碼_第2頁
初學(xué)C語言常用簡單程序代碼_第3頁
初學(xué)C語言常用簡單程序代碼_第4頁
初學(xué)C語言常用簡單程序代碼_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、初學(xué)C語言常用簡單程序代碼素?cái)?shù)的篩選#include #include #define N 1000 int main(int argc, char* argv)int i,j,numN;for(i=0;iN;i+) numi=i+1;for(i=1;i(int)sqrt(double)N);i+) if(numi)for(j=i+1;jN;j+)if(numj)if(numj%numi=0) numj=0;for(i=1;iN;i+) if(numi) printf( %d,numi);return 0; 輸出時(shí)分秒 #include main()int x,s,t,d;scanf(%d,&

2、x);s=x%60;t=(x/60)%60;d=(x/60)/60;prin tf(%d時(shí) 小分 %d秒 ,d,t,s); 三個(gè)數(shù)的交換 #include void main() int a,b,c,t;scanf(%d%d%d,&a,&b,&c);printf(The original number:a=%d,b=%d,c=%dn,a,b,c);t=a,a=b,b=t;t=a,a=c,c=t;printf(after swap:a=%d,b=%d,c=%d,a,b,c);switch 語句的運(yùn)用#include main()int a,m;printf(please input a numb

3、er:);scanf(%d,&a);switch(a/10)case 10:case 9: m=5; break;case 8: m=4; break;case 7:m=3; break;case 6:m=2; break;default:m=1;printf(m=%d,m); 判斷奇數(shù)和偶數(shù) #include main()int x;printf(Please input a number: );scanf(%d,&x);if(x%2=0)printf(this is a oushu!);elseprintf(this is a jishu!); 小寫改大寫,若是大寫則不變 include

4、main()char c1,c2;printf(Input a lettern);scanf(%c,&c1);c2=(a=c1&c1=z)?(c1-a+A):c1;printf(%c,c2);getchar();return 0; 輸入一個(gè)數(shù),輸出這個(gè)數(shù)是幾位數(shù) #include main()int x,n;printf(please input a number:); scanf(%d,&x);n=0;while(x!=0)x=x/10;n+;printf(%d,n);輸入一個(gè)數(shù),判斷它是幾位數(shù),然后逆序輸出 #includeint main()int num;int len = 0;int

5、 temp =0;printf(Please input a number:n); scanf(%d,&num);while(num % 10)!=0)temp = temp*10+(num%10);len+;num = num/10;printf(length = %dn,len); printf(Reverse is %d, temp);打印水仙花數(shù)#include main()int x, a,b,c;for(x=100;x=999;x+)a=x%10;b=(x/10)%10;c=x/100; if(x=a*a*a+b*b*b+c*c*c)printf(%5d,x);printf(n);

6、 判斷閏年 #include stdio.h main() int k,flag,n=0;for(k=1980;k=2880;k+)OU&LUCLTOP4SV pnous 迺星5廠+66.|)0303(0只寸|6舉-S 齊0_4OU&LUCLTOP4SV pnous 矣瘁u 4|錢 宀_En5p%HLUns=)匕 d 宀._*4+LunsHLUnsVJL(0+H-LOLHvo.ll)04HJL HHLUns -i-rTuuns ju 一int n,i,fz,fm;s=0;fz=2;fm=1;printf(Enter n:);scanf(%d,&n);for(i=1;i=n;i+)t=(1.0*

7、fz)/fm;s=s+t;fz=fz+fm;fm=fz-fm;printf(s=%fn,s);開發(fā)一個(gè)程序求全班學(xué)生某門課的平均成績, 要求程序每次運(yùn)行時(shí)班 級人數(shù)可為任意/* 用標(biāo)記值來指示數(shù)據(jù)輸入的結(jié)束 */#include main()float average,total;int counter,score;total=0.0;counter=0;printf( 輸入成績 ,-1 表示結(jié)束 :);scanf(%d,&score);while(score!=-1) /* 用-1 表示輸入的結(jié)束 , 同時(shí)循環(huán) 輸入分?jǐn)?shù) */total=total+score;counter+;printf

8、( 輸入下一個(gè)成績 ,-1 表示輸入結(jié)束 :); scanf(%d,&score);if(counter!=0)average=total/counter;printf( 全班平均成績?yōu)椋?%.2fn,average);elseprintf( 沒有輸入成績 n);return 0;求二元一次方程的根#include #include main()float a=4,b=-40,c=91;float x1,x2;float delta;delta=sqrt(b*b-4.0*a*c);x1=(-b+delta)/(2.0*a);x2=(-b-delta)/(2.0*a);printf(x1=%f

9、x2=%fn,x1,x2);if 語句的運(yùn)用#include main()int a,b,c,s,w,t;s=w=t=0;a=-1;b=c=3;if(c0)s=a+b;if(a0) if(c0) w=a-b;elset=c;printf(s=%d, w=%d,t=%d,s,w,t);輸入學(xué)生的生日,輸出他的實(shí)足年齡#include main()int y0,m0,d0,y1,m1,d1,age;printf(nplease input birthday (yyyy/mm/dd):);scanf(%d/%d/%d ,&y0,&m0,&d0);printf(nplease input curren

10、t day(yyyy/mm/dd):);scanf(%d/%d/%d ,&y1,&m1,&d1);if(m1m0) | (m1=m0 & d1=d0) age=y1-y0;elseage=y1-y0-1;printf(age=%d n,age);指針變量作為函數(shù)參數(shù),實(shí)現(xiàn)兩數(shù)的交換#include void sswap(int *x,int *y);main()int a=10,b=20;printf(before swap:a=%d,b=%dn,a,b);sswap(&a,&b);printf(after swap:a=%d,b=%dn,a,b);void sswap(int *x,int

11、*y)int t;t=*x;*x=*y;*y=t;通過函數(shù)調(diào)用輸出兩個(gè)實(shí)型數(shù)中最大的一個(gè)#include float *f(float *px,float *py)if(*px*py)return px;return py;main()float x=10.0,y=20.0,*a=&x,*b=&y; printf(*a=%f,*b=%f,max=%fn,*a,*b,*f(a,b);計(jì)算并輸出一個(gè)數(shù)組中所有元素的和,最大值,最小值,值為奇數(shù)的 元素個(gè)數(shù)編寫一個(gè)函數(shù),將一個(gè)字符串中的字符全部轉(zhuǎn)成大寫#include void Lower2Upper(char *str)char *p=str;wh

12、ile(*p!=0)if(*p=a)*p=*p-32;p+;main()char s10=Hello!;int i;Lower2Upper(s);for(i=0;i10;i+)printf(%c,si); 計(jì)算一個(gè)字符在一個(gè)字符串中出現(xiàn)的次數(shù) #include int count(char ch,char *str)char *p=str;int num=0;while(*p!=0)if(*p+=ch)num+;return num;main()char ah=a;char s=haowareayou;printf(the number is:);printf(%dn,count(a,s);刪

13、除一維數(shù)組中所有相同的數(shù)#include void unique(int *a,int *n)int k=1;int i,m;m=a0;for(i=0;im) ak+=ai; m=ai;*n=k;main()int a10;int n=10,i;printf(input a list number:); for(i=0;in;i+) scanf(%d,&ai); printf(Before delete:.n); for(i=0;in;i+) printf(%d,ai);unique(a,&n); printf(after deleten);for(i=0;in;i+) printf(%d,a

14、i);printf(n);將字符串按字典順序輸出#include #include #define N 5void sort(char *name,int n);void prn(char *name,int n); void main()char*name=China,America,Australia,France,German; sort(name,N);prn(name,N);void sort(char *name,int n)char *pt;int i,j,k;for(i=0;in-1;i+)k=i;for(j=i+1;j0)k=j; if(k!=i) pt=namei; nam

15、ei=namek; namek=pt;void prn(char *name,int n)int i;for(i=0;in;i+)printf(%sn,namei);有n個(gè)學(xué)生,每個(gè)學(xué)生考m門課程,要求編一程序,能檢查n個(gè)學(xué)生 有無不及格的課程,如果有某一學(xué)生有一門或一門以上課程不及格, 就輸出該學(xué)生的號碼和其全部成績#include #define N 5#define M 4,int col);void prt(float *s,int no,int len);main()floataNM=78,99,89,87,67,59,87,77,86,53,59,95,77,88,5 7,95,78,75,87,95;score(a,N,M);void score(float (*p)M,int row,int col)int i,j;int pass

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論