下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)用順序結(jié)構(gòu)程序設(shè)計(jì)=1已知三角形的三邊長(zhǎng)為a, b, c,計(jì)算三角形面積的公式為:1 ,.、area = 4s(s a)(s b)(s c) , s= (a b c)2要求編寫程序,從鍵盤輸入a, b, c的值,計(jì)算并輸出三角形的面積。#include<stdio.h> #include<math.h> void main()float a,b,c,s,area;scanf("%f,%f,%f",&a,&b,&c);s=1.0/2*(a+b+c);area=sqrt(s*(s-a)*(s-b)*(s-c);printf(&q
2、uot;a=%7.2f, b=%7.2f, c=%7.2f, s=%7.2fn",a,b,c,s);printf("area=%7.2fn",area);2編程從鍵盤輸入圓的半徑r,計(jì)算并輸出圓的周長(zhǎng)和面積。#include "stdio.h"#define Pi 3.14 void main() float r,c,area;printf("請(qǐng)輸入圓的半徑:");scanf("%f",&r);c=2*Pi*r;area=Pi*r*r;printf("該圓的周長(zhǎng)是 %.2f,面積是 %.
3、2fn",c,area);二、選擇結(jié)構(gòu)程序設(shè)計(jì)=1從鍵盤任意輸入一個(gè)年號(hào),判斷它是否是閏年。 若是閏年,輸出“Yes”,否則輸出“No”。已知符合下列條件之一者是閏年:能被4整除,但不能被100整除。能被400整除。#include <stdio.h>main()int year, flag;printf("Enter year:");scanf("%d",&year );if (year % 4 = 0 && year % 100 != 0) | (year % 400 = 0)flag = 1;/*如果y
4、ear是閏年,則標(biāo)志變量 flag置1*/else flag = 0;/*否則,標(biāo)志變量 flag置0*/if (flag) printf("%d is a leap year!n",year); /* 打印“是閏年"*/else printf("%d is not a leap year!n",year); /* 打印"不是閏年" */2通過(guò)鍵盤輸入一個(gè)字符,判斷該字符是數(shù)字字符、大寫字母、小寫字母、空格還是其他 字符。#include <stdio.h>main()char ch;scanf("%c
5、",ch);if (ch >= 97 && ch <= 122) | (ch >= 65 && ch <= 98)printf("It is an English character。");else if (ch <= 57 && ch >= 48) printf("It is a digit character!n");else if (ch = 95) printf("It is a space character!n"); elsepr
6、intf("It is other character!n");3華氏和攝氏溫度的轉(zhuǎn)換公式為C=5/9 X(F-32)。其中,C表示攝氏溫度,F(xiàn)表示華氏溫度。要求:華氏0丁300下,每隔20 丁輸出一個(gè)華氏溫度對(duì)應(yīng)的攝氏溫度值。#include <math.h>#include <stdio.h>main()int upper,step;float fahr = 0,celsius;upper = 300;step = 20;while ( fahr< upper)celsius = 5.0 / 9 * (fahr - 32);printf(&
7、quot;%4.0ft%6.1fn", fahr, celsius);fahr = fahr + step;4編程判斷輸入整數(shù)的正負(fù)性和奇偶性。#include <stdio.h>main()printf("Input m:"); scanf("%d", &m); if (m > 0) if (m%2 = 0)int m;/*輸入一個(gè)整數(shù)*/*是否為正數(shù)*/*是正數(shù),且能被 2整除,則是正偶數(shù)*/printf("%d is a positive evenn", m);else/*不能被2整除,則是正
8、奇數(shù)*/printf("%d is a positive oddn", m);else if (m < 0)/*判斷是否為負(fù)數(shù)*/if (m % 2 = 0) printf("%d is a negative evenn", m);/* 是負(fù)偶數(shù) */ else printf("%d is a negative oddn", m);/* 是負(fù)奇數(shù) */else printf("%d is zero. It is an even'n", m);/* 是 0*/5編程計(jì)算分段函數(shù)exx0y1x0exx0輸
9、入x,打印出y值。流程圖如圖1-2所示。#include <stdio.h>#include <math.h>main()int x;double y;printf("Input x:");scanf("%d", &x);/*輸入一個(gè)整數(shù)*/if (x > 0)y = exp(-x);/*如果大于0,計(jì)算y=exp(-x)的值*/else if (x = 0)y = 1;/*x=0 ,貝 U y=1*/elsey = -exp(x);/*x<0 ,貝U y=-exp(x)*/printf("y=%fn
10、", y);6輸入三角形的三條邊a, b, c,判斷它們能否構(gòu)成三角形。若能構(gòu)成三角形,指出是何種三角形(等腰三角形、直角三角形、一般三角形) 。#include <stdio.h> #include <math.h>#define LIMIT 1e-1 main()float a, b, c;int flag = 1;printf("Input the three edge length:");scanf("%f, %f, %f", &a, &b , &c);/* 輸入三角形的三條邊 */*三角
11、形的基本條件*/if (a + b) > c && (b + c) > a) && (a + c) > b)if (fabs(a-b) <= LIMIT | fabs(b-c) <= LIMIT| fabs(c-a) <= LIMIT)/*等腰三角形的條件 */printf("等腰");flag = 0;if (fabs(a * a + b * b - c * c) <= LIMIT| fabs(a * a + c * c - b * b) <= LIMIT| fabs(c * c + b *
12、b - a * a) <= LIMIT) /* 直角三角形的條件 */ printf("直角");flag = 0; if (flag)printf(" 一般");printf("三角形 n");elseprintf("不是三角形n");7在屏幕上顯示一張如下所示的時(shí)間表:*Time*文案1 morning2 afternoon3 nightPlease enter your choice:操作人員根據(jù)提示進(jìn)行選擇,程序根據(jù)輸入的時(shí)間序號(hào)顯示相應(yīng)的問候信息,選擇1時(shí)顯示"Good morning&q
13、uot;, 選擇 2 時(shí)顯示"Good afternoon", 選擇 3 時(shí)顯示"Good night", 對(duì)于其他選擇顯示"Selection error!",用switch語(yǔ)句編程實(shí)現(xiàn)。#include <stdio.h>main()char c;printf("*Time*n");printf("1 morning n");printf("2 afternoon 'n");/*建立相應(yīng)的菜單 */printf("3 night n"
14、;);printf("please enter your choice");c = getchar();/* 輸入選項(xiàng) */switch (c)/* 通過(guò) switch 選擇 */ case 1: printf("Good morning n"); break; case 2:printf("Good afternoon n"); break; case 3:printf("Good night'n"); break; default: printf("Selection error!n"
15、;); 8讀入一個(gè)年份和月份,打印出該月有多少天(考慮閏年) ,用switch語(yǔ)句編程。#include <stdio.h> main()int year, month;printf("Input year,month:");/*輸入相應(yīng)白年和月*/scanf("%d, %d", &year, &month); switch (month)case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf("31 daysn");break;case 2:if
16、(year % 4= 0 && year % 100 != 0)|(year % 400 = 0) (printf("29 daysn");/* 閏年的 2 月有 29 天 */) else (printf("28 daysn"); /*平年的 2 月有 28 天 */ ) break;case 4:case 6:case 9:case 11:printf("30 daysn");break;default:printf("Input error!n");)三、循環(huán)結(jié)構(gòu)程序設(shè)計(jì)=1 編程計(jì)算1!+2
17、!+3!+4!+10!的值。#include <stdio.h>main()(long term = 1,sum = 0;int i;for (i = 1; i <= 10; i+) (term = term * i;sum = sum + term;printf("1!+2!+10! = %ld n", sum); 2 編程計(jì)算 a+aa+aaa+aaa (n個(gè)a)的值,n和a的值由鍵盤輸入。#include <stdio.h> main()long term = 0,sum = 0;int a , i, n;printf("Inp
18、ut a,n:"); scanf("%d,%d", &a, &n);for (i = 1; i <= n; i+)term = term * 10 + a;sum = sum + term;printf("sum=%ldn",sum);/*輸入a, n的值*/*求出累加項(xiàng)*/*進(jìn)行累加*/一 224466 一3利用=2 f f 4 ? 乂二:的前100項(xiàng)之積計(jì)算的值。2 133557#include <stdio.h>main()double term, result = 1;/* 累乘項(xiàng)初值應(yīng)為 1*/int
19、 n;for (n = 2; n <= 100; n = n + 2)term = (double)( n * n)/( n - 1) * ( n + 1);/*計(jì)算累乘項(xiàng) */result = result * term;printf("result = %fn", 2*result);111114計(jì)算111,直到最后一項(xiàng)的絕對(duì)值小于10-4為止。23499100#include <stdio.h>#include <math.h>main()int n = 1;float term = 1.0, sign = 1,sum = 0;/*判斷末
20、項(xiàng)大小*/*求出累加項(xiàng)*/*累加*/*改變項(xiàng)的符號(hào)*/*分母加1*/while (fabs(term) >= 1e-4) term = sign / n;sum = sum + term;sign = -sign;n+;printf("sum = %fn", sum);35795利用泰勒級(jí)數(shù) sin(x)=x ,計(jì)算sin(x)的值。要求最后一項(xiàng)的絕對(duì)3!5!7!9!值小于10-5,并統(tǒng)計(jì)出此時(shí)累加了多少項(xiàng)。#include <math.h>#include <stdio.h> main()int n = 1,count = 1;float x
21、;double sum , term;/*因?yàn)槲粩?shù)多,所以定義為雙精度*/printf("Input x:");scanf("%f", &x);sum = x;term = x;/* 賦初值 */doterm = -term * x * x / (n + 1) * (n + 2); sum = sum + term;/*累力口 */n = n + 2;count+;while (fabs(term) >= 1e-5);printf("sin(x) = %f, count = %dn", sum, count);6打印所有
22、的“水仙花數(shù)”。所謂“水仙花數(shù)”,是指一個(gè)三位數(shù),其各位數(shù)字的立方和等于該數(shù)本身。例如,153是“水仙花數(shù)”,因?yàn)?53=1 3+33+53。#include <stdio.h>main()int i, j, k, n;printf("result is:");for (n = 100; n < 1000; n+)i = n / 100;/*分出百位*/j = (n - i * 100) / 10;/* 分出十位 */k = n % 10;/*分出個(gè)位*/if (i*100 + j*10 + k = i*i*i + j*j*j + k*k*k)printf
23、("%dt ",n);/* 輸出結(jié)果 */printf("n");7從鍵盤任意輸入一個(gè)4位數(shù)x,編程計(jì)算x的每一位數(shù)字相加之和(忽略整數(shù)前的正負(fù)號(hào))。例如,輸入x為1234,則由1234分離出其千位1、百位2、十位3、個(gè)位4,然 后計(jì)算1+2+3+4=10,并輸出10。#include <stdio.h>#include <math.h>main()int i1, i2, i3, i4, k, n;printf("Input data is:");/*取絕值*/*分離出千位*/*分離出百位*/分離出十位*/*分
24、離出個(gè)位*/scanf("%d", &n);k = fabs(n);i1 = k / 1000;i2 = (k - i1 * 1000) / 100;i3 = (k - i1 * 1000 - i2 * 100) / 10; /*i4 = k % 10;printf("The sum of the total bit is %dn”, i1+i2+i3+i4);8三色球問題。若一個(gè)口袋中放有12個(gè)球,其中有3個(gè)紅色的,3個(gè)白色的,6個(gè)黑色的, 從中任取8個(gè)球,問共有多少種不同的顏色搭配?#include <stdio.h> main()int
25、i, j, k;for (i = 0; i <= 3; i+)for (j = 0; j <= 3; j+)for (k = 0; k <= 6; k+)if (i + j + k = 8)printf("i=%d, j=%d, k=%dn", i, j, k);9編程打印以下圖案。(1)*#include <stdio.h>main()int i, j, k;for (i = 1; i<= 4; i+)for (j = 1; j <= 4-i; j+)printf("");for (k = 1; k <=
26、 6; k+)printf("*");printf("n"); *(3)*/*i控制行數(shù)*/*隨行數(shù)的增加,輸出遞減數(shù)目的空格 */*每行輸出6個(gè)*字符*/*將光標(biāo)移到下一行起始位置處*/10按如下格式打印100以內(nèi)整數(shù)的平方根表。012345678900.0001.0001.4141.7322.0002.2362.4492.6462.8283.00013.1623.3173.4643.6063.7423.8734.0004.1234.2434.35924.4724.5834.6904.7964.8995.0005.0995.1965.2925.3853
27、5.4775.5685.6575.7455.8315.9166.0006.0836.1646.24546.3256.4036.4816.5576.6336.7086.7826.8566.9287.00057.0717.1417.2117.2807.3487.4167.4837.5507.6167.68167.7467.8107.8747.9378.0008.0628.1248.1858.2468.30778.3678.4268.4858.5448.6028.6608.7188.7758.8328.88888.9449.0009.0559.1109.1659.2209.2749.3279.381
28、9.43499.4879.5399.5929.6449.6959.7479.7989.8499.8999.950#include <stdio.h>#include <math.h> main()int m, n, i;for (m = 0; m < 10; m+)printf("%7d", m);printf("n");for (n = 0; n < 10; n+)printf("%d", n);for (m = 0; m < 10; m+)/*打印表頭*/*乘數(shù)n從1變化到9*/*輸出每行
29、的開頭數(shù)字*/*被乘數(shù)m從1變化到9*/printf(" %4.3f ", sqrt(n * 10 + m);/*輸出第m行n列中的值*/printf("n");/*輸出換行符,準(zhǔn)備打印下一行*/四、一維數(shù)組=1 下面程序模擬了骰子的 6000次投擲,用rand函數(shù)產(chǎn)生16之間的隨機(jī)數(shù)face,然后統(tǒng) 計(jì)16每一面出現(xiàn)的機(jī)會(huì)(概率)存放到數(shù)組frequency中。#include <stdlib.h>#include <time.h>#include <stdio.h>main()int face, roll, fre
30、quency。= 0;srand(time (NULL);for (roll=1; roll<=6000; roll+)face = rand()%6 + 1;+frequencyface;printf("%4s%17sn", "Face", "Frequency");for (face=1; face<=6; face+) printf("%4d%17dn", face, frequencyface);2從鍵盤輸入10個(gè)整型數(shù)據(jù),放入數(shù)組a中,求其最大值、最小值及其所在元素的下標(biāo)位置,并輸出。#inc
31、lude <stdio.h>main()int a10, n, max, min, maxPos, minPos;for (n=0; n<10; n+)scanf("%d”,&an);max = min = a0;maxPos = minPos = 0; for (n=0; n<10; n+) if (an > max) max = an;maxPos = n;else if (an < min)min = an;minPos = n;printf("max=%d, pos=%dn",max, maxPos); prin
32、tf("min=%d, pos=%dn",min, minPos); 3編程實(shí)現(xiàn)從鍵盤任意輸入 20個(gè)整數(shù),統(tǒng)計(jì)非負(fù)數(shù)個(gè)數(shù),并計(jì)算非負(fù)數(shù)之和。#include <stdio.h>main()int i, n, sum = 0, counter = 0;printf("Input 20 Numbers:n");for (i=0; i < 20; i+)scanf("%d", &n);if (n >= 0)/*判斷是否為非負(fù)數(shù)*/sum += n;/*非負(fù)數(shù)求和*/counter+;/*非負(fù)數(shù)個(gè)數(shù)計(jì)數(shù)*/p
33、rintf("sum=%d,counter=%dn", sum,counter);4從鍵盤任意輸入10個(gè)整數(shù),用函數(shù)編程實(shí)現(xiàn)將其中最大數(shù)與最小數(shù)的位置對(duì)換后,再輸出調(diào)整后的數(shù)組。#include <stdio.h>#define ARR_SIZE 10/*函數(shù)功能: 找出n個(gè)數(shù)中的最大數(shù)與最小數(shù)并將其位置對(duì)換函數(shù)參數(shù):整型數(shù)組a,存放待處理數(shù)據(jù)整型變量n,為數(shù)據(jù)個(gè)數(shù)返回值:無(wú)*/void MaxMinExchang(int a口,int n)int maxValue = a0, minV alue = a0, maxPos = 0, minPos = 0; in
34、t i, temp;for (i=1; i<n; i+)if (ai > maxValue)maxValue = ai;maxPos = i;if (ai < minValue)minValue = ai;minPos = i;temp = amaxPos;amaxPos = aminPos;aminPos = temp;main()int aARR_SIZE, i, n;printf("Input n(n<=10):");scanf("%d", &n);printf("Input %d Numbers:n&qu
35、ot;, n);for (i=0; i<n; i+)scanf("%d”, &ai);MaxMinExchang(a, n);printf("After MaxMinExchange:n");for (i=0; i<n; i+) printf("%4d", ai);printf("n");五、 二維數(shù)組=1輸入5 >5階的矩陣,編程實(shí)現(xiàn):(1)求兩條對(duì)角線上的各元素之和。(2)求兩條對(duì)角線上行、列下標(biāo)均為偶數(shù)的各元素之積。#include <stdio.h>#define ARR_SIZ
36、E 10 main()int aARR_SIZEARR_SIZE, i, j, n, sum = 0;long product = 1;printf("Input n:");scanf("%d", &n);printf("Input %d*%d matrix:n", n, n);for (i=0; i<n; i+)for (j=0; j<n; j+)scanf("%d",&aij);for (i=0; i<n; i+)for (j=0; j<n; j+)if (i = j |
37、 i+j = n-1) sum += aij;if (i = j | i+j = n-1) && i%2 = 0 && j%2 = 0) product *= aij;printf("sum = %dnproduct = %ldn", sum, product);2編程打印如下形式的楊輝三角形。11 11 2113311464115101051#include<stdio.h>#define ARR_SIZE 11 void YHTriangle(int aARR_SIZE, int n);void PrintYHTriangle
38、(int aARR_SIZE, int n);main()int aARR_SIZEARR_SIZE, n;printf("input n (n<=10):");scanf("%d",&n);/*根據(jù)要求輸入楊輝三角形的行數(shù)*/YHTriangle(a,n);PrintYHTriangle(a,n);/*函數(shù)功能:計(jì)算楊輝三角形中各元素?cái)?shù)值函數(shù)參數(shù):整型數(shù)組a,存放計(jì)算得到的楊輝三角形數(shù)據(jù)整型變量n,代表?xiàng)钶x三角形的行數(shù)函數(shù)返回值:無(wú)*/void YHTriangle(int aARR_SIZE, int n)int i, j ;for (
39、i=1; i<=n; i+)ai1 = 1;a皿i = 1;for (i=3; i<=n; i+)for (j=2; j<=i-1; j+)aij = ai-1j-1 + ai-1j/*函數(shù)功能:輸出楊輝三角形函數(shù)參數(shù):整型數(shù)組a,存放楊輝三角形數(shù)據(jù)整型變量n,代表?xiàng)钶x三角形的行數(shù)函數(shù)返回值:無(wú)*/void PrintYHTriangle(int a口ARR_SIZE, int n)int i , j ;for (i=1; i<=n; i+)for (j=1; j<=i; j+)printf("%4d", a皿);printf("n&
40、quot;);3利用公式Q=aij+bij計(jì)算mxn階矩陣A和mxn階矩陣B之和。已知aj為矩陣A的元素, bij為矩陣B的元素,cj為矩陣C的元素(i=1, 2,,m; j=1, 2,,n)。#include<stdio.h>#define ROW 2#define COL 3/*函數(shù)功能:輸入矩陣元素,存于數(shù)組a中函數(shù)參數(shù):整型數(shù)組a,存放矩陣元素 返回值:無(wú)*/void InputMatrix(int aROWCOL)int i , j ;for (i=0; i<ROW; i+) for (j=0; j<COL; j+) scanf("%d”, &
41、;aij); 計(jì)算矩陣之和,即計(jì)算數(shù)組 a、b對(duì)應(yīng)位置數(shù)據(jù)相加之和,結(jié)果存于數(shù)組 c中 /* 函數(shù)功能:函數(shù)參數(shù):整型數(shù)組a、b,分別存放兩個(gè)待求和的矩陣元素整型數(shù)組c,存放矩陣求和結(jié)果返回值:無(wú)*/void AddMatrix(int aROWCOL, int bROWCOL, int cROWCOL) int i , j ;for (i=0; i<ROW; i+)for (j=0; j<COL; j+)cij = a皿+ bij;/*函數(shù)功能:輸出矩陣a中的元素函數(shù)參數(shù):整型數(shù)組a,存放矩陣元素返回值:無(wú)*/void PrintMatrix(int aROWCOL)int i
42、, j ;for (i=0; i<ROW; i+)for (j=0; j<COL; j+)printf("%6d", a皿); printf("n"); main()int aROWCOL, bROWCOL, cROWCOL;printf("Input 2*3 matrix a:n");InputMatrix(a);printf("Input 2*3 matrix b:n");InputMatrix(b);AddMatrix(a, b, c);printf("Results:n");
43、 PrintMatrix(c);n4* 利用公式cij=aik*bkj計(jì)算矩陣A和矩陣B之積。已知aj為mxn階矩陣A的元素(i=1,k 12,,m;j=1, 2,,n), bij 為 n>m 階矩陣 B 的元素(i=1 , 2,,n; j=1, 2, m), cij 為 mxm 階矩陣 C 的元素(i=1, 2,,m; j=1, 2,,m)。#include<stdio.h>#define ROW 2#define COL 3/*函數(shù)功能:計(jì)算矩陣相乘之積,結(jié)果存于數(shù)組c中函數(shù)參數(shù):整型數(shù)組a、b,分別存放兩個(gè)待求乘積的矩陣元素整型數(shù)組c,存放矩陣相乘的結(jié)果返回值:無(wú)*/M
44、ultiplyMatrix(int aROWCOL, int bCOLROW, int cROWROW)int i, j, k;for (i=0; i<ROW; i+)for (j=0; j<ROW; j+)cij = 0;for (k=0; k<COL; k+)ci朋=cij + aik * bkj;/*函數(shù)功能:輸出矢I陣a中的元素函數(shù)參數(shù):整型數(shù)組a,存放矩陣元素返回值:無(wú)*/void PrintMatrix(int aROWROW)int i , j ;for (i=0; i<ROW; i+)for (j=0; j<ROW; j+)printf("
45、;%6d", a皿);printf("n");main()int aROWCOL, bCOLROW, cROWROW, i, j;printf("Input 2*3 matrix a:n");for (i=0; i<ROW ;i+)for (j=0; j<COL; j+) scanf("%d”, &aij);printf("Input 3*2 matrix b:n");for (i=0; i<COL; i+)for (j=0; j<ROW; j+) scanf("%d”,
46、&bij);MultiplyMatrix(a, b, c);printf("Results:n");PrintMatrix(c);六、字符數(shù)組=1輸入一行字符,統(tǒng)計(jì)其中的英文字符、數(shù)字字符、空格和其他字符的個(gè)數(shù)。#include <stdio.h>#include <string.h>#define ARR_SIZE 80 main()char strARR_SIZE;int len, i, letter = 0, digit = 0, space = 0, others = 0;printf("Please input a str
47、ing:"); gets(str);for (i=0; stri!='0' i+) if (stri>='a' && stri<='z' | stri>='A' && stri<='Z')letter +;/*統(tǒng)計(jì)英文字符*/else if (stri >= '0' && stri <= '9')digit +;/*統(tǒng)計(jì)數(shù)字字符*/else if (stri='')space
48、 +;/*統(tǒng)計(jì)空格*/elseothers +;/*統(tǒng)計(jì)其他字符的個(gè)數(shù)*/printf("English character: %dn", letter); printf("digit character: %dn", digit); printf("space: %dn", space);printf("other character: %dn", others);2 編寫一個(gè)程序,實(shí)現(xiàn)將字符數(shù)組中的字符串逆序存放的功能。#include <stdio.h>#include <string.h&
49、gt;#define ARR_SIZE 80void Inverse(char str口,char ptr);main()char aARR_SIZE, bARR_SIZE;printf("Please enter a string:");gets(a);Inverse(a, b);printf("The inversed string is:");puts(b);/*函數(shù)功能:實(shí)現(xiàn)將字符數(shù)組中的字符串逆序存放函數(shù)參數(shù):字符數(shù)組a,存放源字符串字符數(shù)組b,存放逆序字符串函數(shù)返回值:無(wú)*/void Inverse(char str口,char ptr)in
50、t i = 0, j;j = strlen(str) - 1;while (stri != '0')ptrj = stri;i+;j-;ptri='0'3從鍵盤輸入一行字符,統(tǒng)計(jì)其中有多少單詞。假設(shè)單詞之間以空格分開。#include <stdio.h>main()char str20;int i, num;gets(str);if (str0 !='')num = 1;elsenum = 0;for (i=1; stri!='0' i+)if (stri != ' ' && stri-
51、1='')num+;printf("num=%dn", num);4編寫一個(gè)程序,其功能是刪除字符串s中所出現(xiàn)的與變量c相同的字符。#include <stdio.h>voidSqueeze(char s口,char c);main()char str20,c;printf("Input string:");gets(str);printf("Input character:");c = getchar();Squeeze(str,c);puts(str);voidSqueeze(char s口,char
52、c)int i,j;for (i=j=0; si != '0' i+)if (si != c)sj = si;j+;sj = '0'5 不用函數(shù)strcat(),編程實(shí)現(xiàn)字符串連接函數(shù)strcat()的功能,將字符串srcStr連接到字符串dstStr的尾部。#include <stdio.h>#include <string.h>#define ARR_SIZE 80void MyStrcat(char dstStr口,char srcStr);main()char sARR_SIZE, tARR_SIZE;printf("P
53、lease enter source string:");gets(s);printf("Please enter destination string:");gets(t);MyStrcat(s,t);printf("The concatenate string is:");puts(s);/*函數(shù)功能:將源字符串srcStr中的字符串連接到目的字符串dstStr之后函數(shù)參數(shù):字符數(shù)組srcStr,存放源字符串字符數(shù)組dstStr,存放目的字符串源字符串srcStr和目的字符串dstStr連接后的字符串也存于此數(shù)組中 函數(shù)返回值:無(wú)*/voi
54、d MyStrcat(char dstStr口, char srcStr口)unsigned int i, j;i = strlen(dstStr);/*將下標(biāo)移動(dòng)到目的字符串末尾 */for (j=0; j<=strlen(srcStr); j+, i+)(dstStri = srcStrj;七、函 數(shù)=1設(shè)計(jì)一個(gè)函數(shù),判斷一個(gè)整數(shù)是否為素?cái)?shù)。#include <math.h>main()(int n, ret;printf("Input n:");scanf("%d",&n);ret = IsPrimeNumber(n);i
55、f (ret != 0)(printf("%d is a prime numbern",n);else(printf("%d is not a prime numbern",n);/*函數(shù)功能:判斷參數(shù)是否是素?cái)?shù)函數(shù)入口參數(shù):number為整型數(shù),要求為正整數(shù)函數(shù)返回值: 非0值表示是素?cái)?shù),否則不是素?cái)?shù)*/int IsPrimeNumber(int number)int i;*/if (number <= 1)/*負(fù)數(shù)、0和1都不是素?cái)?shù)return 0;for (i=2; i<=sqrt(number); i+)if (number % i) = 0)/* 被整除,不是素?cái)?shù)*/return 0;return 1;m!2編程計(jì)算組合數(shù)p cm 的值。k!(m k)!#include <stdio.h>unsigned long Factorial(unsigned int number);main()unsigned int m, k;double p;doprintf("Please input m,
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024餐飲業(yè)務(wù)合作協(xié)議格式
- 單位員工固定期限勞動(dòng)協(xié)議樣例
- 2024年航空航天:《最佳路徑》課件下的飛行路徑規(guī)劃
- 2024年協(xié)議條款參考模板系列一
- SaaS服務(wù)定制協(xié)議模板2024
- 化產(chǎn)品售后維護(hù)服務(wù)協(xié)議范本
- 職業(yè)規(guī)劃結(jié)束
- 2024教師招聘教師資格考試面試說(shuō)課稿小學(xué)體育單杠
- 寫秋天的早晨的句子
- 2024年度正式員工加入?yún)f(xié)議樣本
- 江蘇開放大學(xué)答案 第2次作業(yè)(單元4)
- 一年級(jí)數(shù)學(xué)專項(xiàng)練習(xí)(大括號(hào)問題、求總數(shù)、求部分?jǐn)?shù)、一圖四式)
- 第二節(jié)、復(fù)韻母
- 幼兒園優(yōu)質(zhì)公開課:小班語(yǔ)言《甜甜的,酸酸的》課件
- 混凝土有限公司財(cái)務(wù)管理制度
- 感動(dòng)中國(guó)十大人物顧方舟事跡ppt(思修課堂展示or愛國(guó)主題演講)
- 《正確認(rèn)識(shí)緩解焦慮》調(diào)節(jié)考試焦慮正確迎考主題班會(huì)課件
- 合同交底記錄表
- 左室流出道狹窄疏通術(shù)后護(hù)理查房
- 中國(guó)特色社會(huì)主義思想概論 課件 第四章 堅(jiān)持以人民為中心
- 重大事故隱患整改驗(yàn)收?qǐng)?bào)告
評(píng)論
0/150
提交評(píng)論