實驗4

#1:星號金字塔函數

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5 - i; j++)
		{
			printf(" ");
		}
		for (int j = 0; j < 2 * i + 1; j++)
		{
			printf("*");
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

#2星號的金字塔和倒金字塔code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5 - i; j++)
		{
			printf(" ");
		}
		for (int j = 0; j < 2 * i + 1; j++)
		{
			printf("*");
		}
		printf("\n");
	}
	for (int i = 5; i < 9; i++)
	{
		for (int j = 0; j < i-3; j++)
		{
			printf(" ");
		}
		for (int j = 0; j < 17-2 * i ; j++)
		{
			printf("*");
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

#3:星號出現位移的用法遊戲

#include<stdio.h>
#include<stdlib.h>
int main()
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 5 - i; j++)
		{
			printf(" ");
		}
		for(int j=0;j<6;j++)
		{
			printf("*");
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

#4:找最值get

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int a, b, c, d;
	printf("請輸入數值:");
	scanf("%d", &a);
	printf("請輸入數值:");
	scanf("%d", &b);
	printf("請輸入數值:");
	scanf("%d", &c);
	printf("請輸入數值:");
	scanf("%d", &d);
	if (a > b && a > c && a > d)
		printf("Largest:%d", a);
	else if (b > a && b > c && b > d)
		printf("Largest:%d", b);
	else if (c > b && c > a && c > d)
		printf("Largest:%d", c);
	else 
		printf("Largest:%d", d);
	if (a < b && a < b && a < d)
		printf("Smallest:%d", a);
	else if (b < a && b < c && b < d)
		printf("Smallest:%d", b);
	else if (c < a && c < b && c < d)
		printf("Smallest:%d", c);
	else
		printf("Smallest:%d", d);
	system("pause");
	return 0;
}

#5:買賣貨物io

#include<stdio.h>
#include<stdlib.h>
int main()
{
	float x, y,z;
	while (1)
	{
		printf("請輸入價格:");
		scanf("%f", &x);
		printf("請輸入顧客付款:");
		scanf("%f", &y);
		z = y - x;
		getchar();
		while (z < 0)
		{
			printf("交易失敗\n");
			printf("請輸入價格:");
			scanf("%f", &x);
			printf("請輸入顧客付款:");
			scanf("%f", &y);
		}
		if (z == 0)
		{
			printf("交易成功!");
		}
		else if (z > 0)
			printf("找給顧客%f元,交易成功!", z);
		break;
	}
	system("pause");
	return 0;
}

#6:寫函數class

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	float x, a, b, c, d,y;
	printf("請輸入一個x值:");
	scanf("%f", &x);
	a = sin(x);
	b =log(x + 1);
	c = exp(2);//exp函數爲e的表達函數//
	d = fabs(cos(x));//fabs函數爲絕對值函數//
	y = (a + b )/(c + d);
	if (x > -1)
		printf("得出的值爲%f", y);
	else
		printf("得出的值不存在");
	system("pause");
	return 0;
}

#7:寫一個數的以前偶數平方float

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	int N,y,j;
	printf("請輸入一個數值N:");
	scanf("%d", &N);
	y=sqrt(N);
	for (int i = 1; i <= y/2; i++)
	{
		j = 2 * i*i*2;
		printf("%d\n",j);
	}
	system("pause");
	return 0;
}

#8:公約數最大im

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	int x1,x2, y1,y2, z,o,p;
	char sel;
	while (1)
	{
		printf("請輸入一個實數:");
		scanf("%d", &x1);
		printf("請輸入另外一個實數:");
		scanf("%d", &y1);
		x2 = x1;
		y2 = y1;
		if (x1 < y1)
		{
			o = x1;
			x1 = y1;
			y1 = 0;
		}
		z = x1 % y1;
		while (z != 0) {
			x1 = y1;
			y1 = z;
			z = x1 % y1;
		}
		p = x2 * y2 / y1;
		printf("最小公倍數爲%d\n", p);
		printf("最大公約數爲%d", y1);
	}
	system("pause");
	return 0;
}

#9:猜數遊戲di

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	int x,y,z,count=0;
	srand((unsigned)time(NULL));
	y = rand();
	z = y % 10;
	while (count<=5)
	{
		printf("請輸入一個0到9的數:");
		scanf("%d", &x);
		if (x < z)
		{
			printf("你輸入的值太小,請重輸:");
			scanf("%d",&x);
		}
		if (x > z)
		{
			printf("你輸入的值過大,請重輸:");
			scanf("%d", &x);
		}
		if (x == z)
		{
			printf("恭喜你輸入正確");
			break;
		}
		count++;
		if (count == 5)
		{
			printf("你已猜完5次,歡迎下次再玩:");
			break;
		}
		break;
	}
	system("pause");
	return 0;
}

#10:對比數字while

#include<stdio.h>
#include<stdlib.h>
int main()
{
	float x,i=0;
	printf("Enter a number:");
	scanf("%f", &x);
	while (x > 0)
	{
		printf("Enter a number:");
		scanf("%f", &x);
		i = (x > i) ? x : i;
	}
	printf("the largest number is %f", i);
	system("pause");
	return 0;
}
相關文章
相關標籤/搜索