//================================第二章 变量和数据类型================================== ------------------------------------------------------------------------------------------
/*把输入的小写字母变成大写的形式,然后显示在屏幕上输出*/
#include <stdio.h>
void main()
{
char c;
c=getchar();
if(c>='a'&&c<='z')
{
c=c-32;
putchar(c);
}
else
{
putchar(c);
}
}
-----------------------------------------------------------------------------------------
/*输入一个字符,然后在屏幕上输出*/
#include <stdio.h>
main()
{
char c;
c=getchar();
putchar(c);
}
------------------------------------------------------------------------------------------
输入一个字符串,然后在屏幕上显示
#include <stdio.h>
main()
{
char m[100];
printf("Please input String:\n");
gets(m);
puts(m);
}
-----------------------------------------------------------------------------------------
//================================第三章 运算符========================================== -----------------------------------------------------------------------------------------
/*用户从键盘输入一行字符,分别统计其中的英文字母,空格,数字占字符总数的百分数*/
#include <stdio.h>
void main()
{
int c1,c2,c3,total;
char c;
c1=0;
c2=0;
c3=0;
while((c=getchar())!='\n')
{
total++;
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
{ c1++;}
if(c==' ')
{ c2++;}
if(c>='0'&& c<='9')
{ c3++;}
}
total=c1+c2+c3;
printf("英文字母占:%d%%\n",100*c1/total);
printf("空格占:%d%%\n",100*c2/total);
printf("数字占:%d%%\n",100*c3/total);
}
--------------------------------------------------------------------------------------------/*1.输入四个互不相等的实型数,输出其中最大的一个。要求(1)整数的允许范围为-106~106,若输入有越界,则给出出错信息
2.程序要有连续处理的能力,当且仅当输入的四个数全为0的时候,程序完成*/
#include <stdio.h>
main()
{//程序错误,没有注意“实型数”这几个字。
long z1,z2,z3,z4,temp;
while(1==1)
{
scanf("%ld%ld%ld%ld",&z1,&z2,&z3,&z4);
if((z1<-1e6||z1>1e6)||(z2<-1e6||z2>1e6)||(z3<-1e6||z3>1e6)||(z4<-1e6||z4>1e6))
{
printf("error\n");
break;
}
else
{
if(z1<z2)
{temp=z1,z1=z2,z2=temp;}
if(z1<z3)
{temp=z1,z1=z3,z3=temp;}
if(z1<z4)
{temp=z1,z1=z4,z4=temp;}
}
printf("The max one is:%ld\n",z1);if((z1==0)&&(z2==0)&&(z3==0)&&(z4==0))
break;
}
}
-----------------------------------------------------------------------------------------------/*接受用户输入的一串字符,其中的大写字母不变动,小写字母转换为大写字母,其余类型的字符一律忽略,然后按输入顺序的逆序输出*/
#include<stdio.h>
main()
{
char ch;
char c[100];
int i=0;
int k;
while((ch=getchar())!='\n')
{
if(ch>='a'&&ch<='z')
{ch=ch-32;} c[i]=ch;
i++;
}for(k=i-1;k>=0;k--)
printf("%c",c[k]);
}
------------------------------------------------------------------------------------------- //================================第四章 条件结构========================================/*编写一个程序,如果含有大写字母,将其转化为小写字母,如果有小写字母,将其转化为大写字母*/
#include <stdio.h>
main()
{
char c;
while((c=getchar())!=EOF)
{
if(c>='a'&&c<='z')
{c=c-32;putchar(c);}
else if(c>='A'&&c<='Z')
{c=c+32;putchar(c);}
else
{putchar(c);}
}
}
--------------------------------------------------------------------------------------------
/*求三个数x,y,z(0-9)满足方程xyz+zyx=1231. )其中xyz指由xyz三个数字组成的三位数*/
#include <stdio.h>
main()
{
int x,y,z; for(x=1;x<=9;x++)
for(y=0;y<=9;y++)
for(z=1;z<=9;z++)
if((100*x+10*y+z+100*z+10*y+x)==1231)
printf("the value of x,y,z:%d-%d-%d\n",x,y,z);
}
---------------------------------------------------------------------------------------------
//================================第五章 循环结构-1=================================== ---------------------------------------------------------------------------------------
/*用for循环结构编程解决1+2+3+…+100。*/
#include <stdio.h>
main()
{
int i,sum;
sum=0;
for(i=1;i<=100;i++)
{sum+=i;}
printf("The sum is:%d\n",sum);
}
----------------------------------------------------------------------------------------
/*用do-while循环结构编程解决1+2+3+…+100。*/
#include <stdio.h>
main()
{
int i;
int sum;
i=0;
sum=0;
do{
sum+=i;
i++;
}while(i<=100);
printf("The sum is:%d\n",sum);
}
---------------------------------------------------------------------------------------
/*用while循环结构编程解决1+2+3+…+100。*/
#include <stdio.h>
main()
{
int i;
int sum;
i=0;
sum=0;
while(i<=100)
{
sum+=i;
i++;
}
printf("The sum is:%d\n",sum);
}
---------------------------------------------------------------------------------------
/*判断某字符串是否是前后对称的。*/
#include<stdio.h>
main()
{
char ch;
char c[100];
int i=0;
int j;
int k;
int flag;
while((ch=getchar())!='\n')
{
if(ch>='a'&&ch<='z')
{ch=ch-32;} c[i]=ch;
i++;
}
k=i-1;
for(j=0;j<=k;j++,k--)
if(c[j]!=c[k])
{
flag=0;
break;
}if(flag)
printf("对称");
else
printf("不对称");
}
------------------------------------------------------------------------------------------
/*求2个数的最小公倍数。*/
#include <stdio.h>
main()
{
int num1,num2;
int i;
int min,max;
printf("please input two numbers num1 num2:\n");
scanf("%d%d",&num1,&num2); min=num1>num2?num2:num1;
max=num1>num2?num1:num2;for(i=min;i<=max*min;i++)
if(i%min==0&&i%max==0)
break;printf("num1和num2的最小公倍数是:%d\n",i);
}
--------------------------------------------------------------------------------------
/*猜数源程序do-while*/
#include<stdio.h>
main()
{
int number=5,guess;
printf ("猜一个介于 1 与 10 之间的数\n");
do
{
printf("请输入您猜测的数:");
scanf("%d",&guess);
if (guess > number)
printf("太大\n");
else if (guess < number)
printf("太小\n");
} while (guess != number);
printf("您猜中了! 答案为 %d\n",number);
}
------------
/*猜数修改后的程序while*/
#include<stdio.h>
main()
{
int number=5,guess;
printf ("猜一个介于 1 与 10 之间的数\n");
while(guess != number)
{
printf("请输入您猜测的数:");
scanf("%d",&guess);
if (guess > number)
printf("太大\n");
else if (guess < number)
printf("太小\n");
}
printf("您猜中了! 答案为 %d\n",number);
}
--------------------------------------------------------------------------------------------
/*用“*”打印一个直角三角形图案程序while*/
#include<stdio.h>
void main()
{
int nstars=1,stars;
while(nstars <= 10)
{
stars=1;
while (stars <= nstars)
{
printf("*");
stars++;
}
printf("\n");
nstars++;
}
}
------------------
/*用“*”打印一个直角三角形图案修改后的程序do-while*/
#include<stdio.h>
void main()
{
int nstars=1,stars;
do
{
stars=1;
do
{
printf("*");
stars++;
}while (stars <= nstars);
printf("\n");
nstars++;
}while(nstars <= 10);
}
----------------------------------------------------------------------------------------------