用c++编写程序,打印输出9种基本逻辑运算符(与、或、非、条件、双条件、异或、与非、或非、条件否定)的运算表到字符文件logic.txt中。
时间: 2023-05-17 14:06:23 浏览: 107
好的,以下是代码:
```c
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("logic.txt", "w");
if (fp == NULL) {
printf("Failed to open file!");
return 1;
}
fprintf(fp, "Logic Table:\n\n");
fprintf(fp, "AND\tOR\tNOT\tCONDITIONAL\tBICONDITIONAL\tXOR\tNAND\tNOR\tCONDITIONAL NOT\n");
for (int a = 0; a <= 1; a++) {
for (int b = 0; b <= 1; b++) {
int and = a && b;
int or = a || b;
int not = !a;
int conditional = (!a || b);
int biconditional = ((a && b) || (!a && !b));
int xor = a ^ b;
int nand = !(a && b);
int nor = !(a || b);
int conditional_not = (a && !b);
fprintf(fp, "%d\t%d\t%d\t%d\t\t%d\t\t%d\t%d\t%d\t%d\n", and, or, not, conditional, biconditional, xor, nand, nor, conditional_not);
}
}
fclose(fp);
printf("Logic table generated successfully!");
return 0;
}
```
这个程序会生成一个名为logic.txt的文件,里面包含了9种基本逻辑运算符的运算表。