我上课C语言的第6节的笔记 :
C语言真的需要多做多练多理解,不然真的学不懂.记不住呀。
第一题:睇图片解题。
main()
{
int x,y;
scanf("%d",&x);
if(x<0)
printf("y=%d",0);
if(x>=0&&x<5)
printf("y=%d",x+2);
if(x>=5&&x<10)
printf("y=%d",x*x-3);
if(x>=10)
printf("y=%d",10);
getch();
}
第二题:先定义两个变量的值,然后输入 + - * / 自动计算..
#include <stdio.h>
int main()
{
float a=5.0,b=2.0,c=3.0;
char sym = '\0';
printf("Please choose \n");
printf("+ : addition\n");
printf("- : subtraction\n");
printf("* : multiplication\n");
printf("/ : divsion\n");
sym=getchar();
printf("%f%c%f=",a,sym,b);
switch(sym)
{
case '+':c=a+b; break;
case '-':c=a-b; break;
case '*':c=a*b; break;
case '/':c=a/b; break;
}
printf("%f\n",c);
getch();
}
第三题:从键盘上输入一个整数n(1-7)表示今天星期几,输入另一个整数m,过m天后,打印输出是星期几?(提示用“%”模除取余(m+n) %7)
int main()
{
int n,m,c;
scanf("%d%d",&n,&m);
c = (n+m)%7;
if(c==0)
printf("today is monday!");
else if(c==1)
printf("today is tuesday!");
else if(c==3)
printf("today is Wednesday!");
else if(c==4)
printf("today is thursday!");
else if(c==5)
printf("today is Firday!");
else if(c==6)
printf("today is Saturday!");
else
printf("today is Sunday!");
getch();
}
第四题:编写程序从键盘上输入一个字符,如果是小写字母转成大写输出,如果是大写字母转成小写输出,如果是数字的直接输出,其他的输出一个‘?’。
#include <stdio.h>
main()
{
int d = 32;
char ch1;
printf("please input number:");
ch1 = getchar();
if (ch1>64&&ch1<91)
ch1 = ch1 + 32;
else if (ch1>96&&ch1<123)
ch1 = ch1 - 32;
else if (ch1>48&&ch1<58)
ch1 = ch1 ;
else
ch1 = '?';
putchar(ch1);
getch();
}
第五题:麻将牌游戏开始需要掷两个骰子计点数,请你利用计算机随机函数产生1-6的随机数模拟,编程序每次运行打印出掷两个骰子的随机数。(随机函数参考P87面公式。提示: srand(time()) ; i = rand()%6+1产生)
#include <stdio.h>
main()
{
int a,b;
srand(time(0));
a=rand()%(6-1+1)+1;
b=rand()%(6-1+1)+1;
printf("a=%d,b=%d",a,b);
getch();
}
第六题:计算机利用随机函数和switch语句随机产生一张扑克(2-10JQKA)程序如下,现在你输入花色和值,如何比较人机两张牌输赢呢?(提示: i = rand()%13+1产生2-10AJQK字符 ,a =rand()%4产生桃、杏、梅、方,最后打印输出一张牌。)
#include "stdio.h"
#include "stdlib.h"
void printpoker(int i,char a)
{
printf("%c",a) ;
switch(i)
{ case 1:
printf("%c",'A');
break;
case 10: printf("%c%c",'1','0') ;
break;
case 11:
printf("%c",'J');
break;
case 12:
printf("%c",'Q'); ;
break;
case 13:
printf("%c",'K');;
break;
default:
printf("%c",'0'+i);
}
printf("\n");
}
main()
{
int com_inf,com_v,man_inf,man_v;
char com_f,man_f;
printf("\n\t1.%c\t2.%c\n",6,3) ;
printf("\n\t3.%c\t4.%c\n",5,4) ;
printf("please input 1-4 a poker flower:\n" );
scanf("%d",&man_inf) ;
if(man_inf>=1&& man_inf<=4 )
{
if (man_inf==1) man_f=6 ;
if (man_inf==2) man_f=3 ;
if (man_inf==3) man_f=5 ;
if (man_inf==4) man_f=4 ;
}
else
printf("input 1-4 flower error:\n" );
printf("please input 1-13 sele a poker value:\n" );
scanf("%d",&man_v) ;
if(!(man_v>=1&& man_v<=13) )
printf("input 1-13 poker value error:\n" );
printf("\n man input a poker :" );
printpoker(man_v,man_f);
srand(time()) ;
com_inf=rand()%4;
if (com_inf==1) com_f=6 ;
if (com_inf==2) com_f=3 ;
if (com_inf==3) com_f=5 ;
if (com_inf==4) com_f=4 ;
com_v=rand()%13+1;
printf("\ncomputer make a poker :" );
printpoker(com_v,com_f);
if (man_v==1)man_v=man_f+13;
if (com_v==1)com_v=com_v+13;
if (man_v!=com_v)
if (man_inf>com_inf)
printf("\nyou win!!!");
else
printf("\ncomputer win!!!");
else
if (man_inf<com_inf)
printf("\nyou flower win!!!");
else
printf("\ncomputer flower win!!!");
getch();
}
转载于:https://blog.51cto.com/1120173856/719635