c++编程:从键盘上任意输入一个主析取范式,输出与之等值的主合取范式
展开全部
//从键盘上任意输入一个主析取范式,输出与之等值的主合取范式。┐∧∨
# include <iostream>
# include <cmath>
# include <windows.h>
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<n ; 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<k; 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<k ; i++)
if (p[i] == 0){
left-- ;
output(i, var_n, left) ;
}
cout << "\n变项数有:" << k - num << endl ;
cout << "\n----------------------------------------------------" << endl ;
}
main(){
funtion();
return 0 ;
}
# include <iostream>
# include <cmath>
# include <windows.h>
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<n ; 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<k; 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<k ; i++)
if (p[i] == 0){
left-- ;
output(i, var_n, left) ;
}
cout << "\n变项数有:" << k - num << endl ;
cout << "\n----------------------------------------------------" << endl ;
}
main(){
funtion();
return 0 ;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询