满意答案
zxuu11
2013.09.15
采纳率:44% 等级:8
已帮助:2365人
//从键盘上任意输入一个主析取范式,输出与之等值的主合取范式。┐∧∨
# include
# include
# include
using namespace std ;
//跳出函数
bool jump(const char ch){
char c = cin.peek() ;
if (ch == ')' && c != '+' && c != '*')
return 1 ;
return 0 ;
}
//将字母转化为二进制
int change(char ch){
int k = ch-97 ;
return pow(2,k) ;
}
//将转化后的和取公式输出
void output(int k, int n , int left){
char ch = 96;
int i ;
cout << '(' ;
for (i=0 ; i
ch++ ;
switch (k%2){
case 1:
cout << ch << '*' ;
k/=2 ;
break ;
case 0 :
cout << '!' << ch << '*' ;
k/=2 ;
break ;
}
}
if (left == 0)
cout << '\b' << ')' ;
else
cout << '\b' << ')' << '+' ;
}
//公式输入 and 公式输出....
void funtion(){
cout << "输入变量数:" ;
int var_n=0 , k ;
cin >> var_n ;
if (var_n>26 || var_n<1){
cout << "enter error!" << endl ;
MessageBox(NULL, "输入错误!", "error", MB_OK);
exit (0) ;
}
k = pow(2, var_n) ;
int *p = new int[k] ;
for (int i=0; i
p[i] = 0 ;
char ch ;
int num = 1 , count = 0 ;
cout << "----------------------------------------------------" << endl ;
cout << "输入析取公式: " ;
while (1){
cin >> ch ;
if(jump(ch))
break ;
if (ch == '*'){
p[count] = 1 ;
num++ ;
count = 0 ;
cin >> ch ;
}
if (ch == '(' || ch == ')' || ch == '+')
cin >> ch ;
if (ch == '!')
cin >> ch >> ch;
if(jump(ch))
break ;
if (ch != '*' && ch != '!' && ch != '(' && ch != ')' && ch != '+'){
count += change(ch) ;
}
if (ch == '*'){
p[count] = 1 ;
num++ ;
count = 0 ;
cin >> ch ;
}
}
p[count] = 1 ;
cout << "变项数有: " << num << endl ;
cout << "----------------------------------------------------" << endl ;
cout << "和取公式:" ;
int left ;
left = k - num ;
for (i=0 ; i
if (p[i] == 0){
left-- ;
output(i, var_n, left) ;
}
cout << "\n变项数有:" << k - num << endl ;
cout << "\n----------------------------------------------------" << endl ;
}
main(){
funtion();
return 0 ;
}
00分享举报