注册 登录
编程论坛 C++教室

c++程序 如何编译随机算数式

紅苏酒 发布于 2013-04-26 20:04, 453 次点击
如题 c++语言如何编译一个 随机的算数式 求解
1 回复
#2
邓士林2013-04-26 21:17
#include<iostream>
#include<ctime>
using namespace std;
#define N 3
//最大操作数
void symbol(const int &x)
{ switch(x)
{ case 0:cout<<"+";break;
case 1:cout<<"-";break;
case 2:cout<<"*";break;
 case 3:cout<<"/";break;
 default: ; }}
void main()
{ int i,j;
unsigned int r;
srand((unsigned)time(NULL));
for(i=0;i<100;i++)
{  cout<<rand()%100;
 r=rand()%N;  
 for(j=0;j<r;j++)  
 {   symbol(rand()%4);   
 cout<<rand()%100;  
}  
 cout<<"="<<endl;
}
}
这是一个模板,你修改下
1