实验1 分支结构
【实验目的】
通过本实验,了解C语言的分支结构程序设计的方法及语法规则的正确使用。
【实验内容】
利用随机函数出个位数加/减法算术题
#include<cstdlib>
#include<ctime>
#include<iostream>
using namespace std;
int main(){
int a,b,c,s,x,t;
char fh;
srand((unsigned)time(NULL));
a=(int)rand()%10;
b=(int)rand()%10;
c=(int)rand()%10;
switch(c%2){
case 0:fh='+'; s=a+b; break;
case 1:fh='-'; if(a<b) {t=a;a=b;b=t;} s=a-b; break;
}
cout<<a<<' '<<fh<<' '<<b<<" = "; cin>>x;
if(s==x) cout<<"/nOK!"<<endl;
else cout<<"/nNO!"<<endl;
return 0;
}