C语言练习
Posted 新起点1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言练习相关的知识,希望对你有一定的参考价值。
#include <stdio.h> int main(void ) { double number1=0.0; double number2=0.0; char operation =0; printf ("\nEnter the calculation \n"); scanf("%lf %c %lf ",&number1,operation,&number2 ); switch (operation) { case ‘+‘: printf("=%lf",number1+number2); break; case‘-‘: printf("=%lf",number1-number2); break; case‘*‘: printf("=%lf",number1*number2); break; case ‘/‘: if (number2!=0) printf("=%lf",number1/number2); else printf("wrong number2"); break ; case ‘%‘: if ((long )number2==0) printf("Error"); else ; printf("=%lf",(long)number1%(long)number2); break; default: printf("aiiiiiiiii"); break; } return 0; }
机房的系统执行不了,也是很无语。。。。
#include <stdio.h> int main() { int i=1; printf("********************"); for (;i<12;i++) printf("\n* *"); printf("\n********************"); return 0; }
打印小方格
#include <stdio.h> int main() { for(int k=1,j=2;k<=5;++k,j=j+2) printf("\n%-5d",k*j); return 0; }打印几个数相乘
#include <stdio.h> int main() { long sum =0l; int count = 0; printf("\nEnter the number of integers you want to summed"); scanf("%d",&count ); for (int i=1;i<=count;i++) sum+=i;
上述代码可以替换为
for (int i=1;i<=count;sum+=i++);//循环语句为空,其次可以发现for循环的范围是管到第一个分号就结束了
上述代码可以替换为
for (int i=count;i>=1;sum+=i--);//逆向运算
printf("%ld",sum); return 0; }
>>6
返回6+5+4+3+2+1
一个无限循环只有输入n 才跳出 #include <stdio.h> #include <ctype.h>//切记,tolower()函数必须要有 int main() { char a=0; for (;;) { printf(" y/n ?"); scanf("%c",&a); if(tolower(a)==‘n‘) break; } return 0; }
#include <stdio.h> #include <ctype.h> int main() { char a=0; double total=0.0; double value=0.0; int count =0; printf("\nthis program ...."); for(;;) { printf("\nEnter a value :"); scanf ("%lf",&value); total+=value; ++count; printf("Do you want another value?"); scanf("%c",&a); if (tolower(a)==‘q‘) break; } printf("%-2lf",total/count); return 0; }//字母和那个数字他如何识的前后?
#include <stdio.h> #include <ctype.h> int main() { int chosen =12; int guess=0; int count=3; for (;count>=1;count--) { printf("\nEnter the number you guess"); scanf("%d",&guess); if (guess==chosen) {printf("Congratulation"); goto m;} if((guess>15)||(guess<1)) printf("Error"); } printf("\n\nshibai"); m : printf("\nyes"); }//goto m;多此一举。直接可以用return 0;
跳出所有直接结束,更不必用break
// 或 ||
且 &&
莫搞错
以上是关于C语言练习的主要内容,如果未能解决你的问题,请参考以下文章