第一次作业
一、实验目的
设计一个可实现加减乘除的计算机软件
二、实验环境
1.操作系统:Windows11
2.开发工具:Dev-C++
三、完整代码:
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>
#include <conio.h> 
void display_calculator();  // 显示计算器界面
void input_expression(char* expression, int size);  // 获取用户输入的表达式,直接在应该显示表达式的位置显示
void gotoxy(int x, int y);  // 设置光标的位置
void display_ending();  // 显示结束界面
void zhuanhuan(char g[], char e[]);  // 处理输入的表达式,去除空格等
int is_operation(char op);  // 判断字符是否为运算符
int priority(char op);  // 获取运算符的优先级
void postfix(char e[], char f[]);  // 将处理后的表达式转换为后缀表达式
double readnum(char f[], int* i);  // 从字符串中读取一个数字
double evalpost(char f[]);  // 计算后缀表达式的值
int is_valid_expression(const char* expression);  // 检查表达式是否合法
void gotoxy(int x, int y) {
    COORD coord = { x, y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void display_calculator() {
    gotoxy(10, 1);
    printf("__________________________________");
    gotoxy(10, 2);
    printf("| |                             | |");
    gotoxy(10, 3);
    printf("|      欢迎使用多功能计算器       |");
    gotoxy(10, 4);
    printf("| |_____________________________| |");
    gotoxy(10, 5);
    printf("| |                     学号姓名| |");
    gotoxy(10, 6);
    printf("| |_____________________________| |");
    gotoxy(10, 7);
    printf("|                                 |");
    gotoxy(10, 8);
    printf("|___ ___ ___ ___ ___ ___ ___ ___ _|");
    gotoxy(10, 9);
    printf("| _____   _____   _____   _____   |");
    gotoxy(10, 10);
    printf("|| ⑨ |  | ⑧ |  | ⑦ |  |  *  |  |");
    gotoxy(10, 11);
    printf("| _____   _____   _____   _____   |");
    gotoxy(10, 12);
    printf("| _____   _____   _____   _____   |");
    gotoxy(10, 13);
    printf("|| ⑥ |  | ⑤ |  | ④ |  |  -  |  |");
    gotoxy(10, 14);
    printf("| _____   _____   _____   _____   |");
    gotoxy(10, 15);
printf("| _____   _____   _____   _____   |");
    gotoxy(10, 16);
    printf("|| ③ |  | ② |  | ① |  |  +  |  |");
    gotoxy(10, 17);
    printf("| _____   _____   _____   _____   |");
    gotoxy(10, 18);
    printf("| _____   _____   _____   _____   |");
    gotoxy(10, 19);
    printf("||    |  |  =  |  |    |  |  /  | |");
    gotoxy(10, 20);
    printf("| _____   _____   _____   _____   |");
    gotoxy(10, 21);
    printf("__________________________________");
}
void input_expression(char* expression, int size) {
    char c;
    int index = 0;
gotoxy(12, 7);
    while (1) {
        c = _getch(); // 逐字符读取输入
        if (c == '\r' || c == '\n') { // 回车或换行表示输入结束
            expression[index] = '\0';
            break;
        }
        else if (c == '\b' && index > 0) { // 处理退格
            index--;
            gotoxy(25 + index, 7);
            printf(" ");
            gotoxy(25 + index, 7);
        }
        else if (isprint(c) &&index < size - 1) { // 只接受可打印字符
            expression[index++] = c;
            printf("%c", c);
        }
    }
}
void zhuanhuan(char g[], char e[]) {
    int i, j;
    for (i = 0, j = 0; g[i] != '\0'; i++) {
        if (g[i] == '-' && (i == 0 || g[i - 1] == '(')) {
            e[j++] = '0';
        }
   while (top >= 0) {
        f[j++] = stack[top--];
    }
    f[j] = '\0';
}
double readnum(char f[], int* i) {
    double num = 0.0;
    double decimal = 0.1;
    int is_decimal = 0;
    while (isdigit(f[*i]) || f[*i] == '.') {
        if (f[*i] == '.') {
            is_decimal = 1;
        }
        else {
            if (is_decimal) {
                num += (f[*i] - '0') * decimal;
                decimal *= 0.1;
            }
            else {
                num = num * 10 + (f[*i] - '0');
            }
        }
        (*i)++;
    }
    return num;
}
double evalpost(char f[]) {
    double stack[100];
    int top = -1;
    double num1, num2;
    for (int i = 0; f[i] != '\0'; i++) {
        if (isdigit(f[i]) || f[i] == '.') {
            stack[++top] = readnum(f, &i);
        }
        else if (f[i] == ' ') {
            continue;
        }
        else if (is_operation(f[i])) {
            num2 = stack[top--];
            num1 = stack[top--];
            switch (f[i]) {
            case '+':
                stack[++top] = num1 + num2;
                break;
            case '-':
                stack[++top] = num1 - num2;
                break;
            case '*':
                stack[++top] = num1 * num2;
                break;
            case '/':
                stack[++top] = num1 / num2;
                break;
            }
        }
    }
    return stack[top];
}
int is_valid_expression(const char* expression) {
    int parentheses_count = 0;
    int last_char_is_operator = 0;
    for (int i = 0; expression[i] != '\0'; i++) {
        if (isdigit(expression[i]) || expression[i] == '.') {
            last_char_is_operator = 0;
        }
        else if (is_operation(expression[i])) {
            if (last_char_is_operator) {
                return 0;
            }
            last_char_is_operator = 1;
        }
        else if (expression[i] == '(') {
            parentheses_count++;
            last_char_is_operator = 0;
        }
        else if (expression[i] == ')') {
            parentheses_count--;
            if (parentheses_count < 0) {
                return 0;
            }
            last_char_is_operator = 0;
        }
        else {
            return 0;
        }
    }
    if (parentheses_count != 0) {
        return 0;
    }
    return 1;
}
void display_ending() {
    system("cls");
    printf("##########################\n");
    printf("#                        #\n");
    printf("#       谢谢使用         #\n");
    printf("#                        #\n");
    printf("##########################\n");
}
int main() {
    char input_expr[100]; // 用于存储用户输入的表达式
    char processed_expression[100]; // 用于存储处理后的表达式(去除空格等)
    char postfix_expression[100]; // 用于存储后缀表达式
    double result; // 用于存储计算结果
    int continue_calculating = 1; // 标志位,用于判断是否继续计算
    int user_choice; // 用户选择:继续计算或退出
display_calculator(); // 显示计算器界面
    while (continue_calculating) {
        input_expression(input_expr, sizeof(input_expr));
        if (!is_valid_expression(input_expr)) {
            gotoxy(12, 7);
            printf("输入不合法,请按回车重新输入\n");
            while (getchar() != '\n');  // 等待用户按回车
            display_calculator();  // 显示新的计算器界面
            continue;  // 跳过本次循环的剩余部分
        }
        zhuanhuan(input_expr, processed_expression);
        postfix(processed_expression, postfix_expression);
        result = evalpost(postfix_expression);
        gotoxy(12 + strlen(input_expr) + 1, 7);
        printf(" = %.2f\n", result);
        gotoxy(50, 15);
        printf("继续计算/退出:1/0: ");
        scanf("%d", &user_choice);
        while (getchar() != '\n');  // 清空输入缓冲区,防止干扰下次输入
        if (user_choice == 0) {
            continue_calculating = 0;
        }
        else {
            // 清除输入表达式行
            gotoxy(12, 7);
            for (int i = 0; i < strlen(input_expr) + 100; i++) {
                printf(" ");
            }
            gotoxy(12, 7);
        }
    }
    display_ending();
    system("pause");
    return 0;
}
测试结果如下




四、程序流程图如下:




五、开方运算代码如下
#include<stdio.h>
#include<math.h>
double kaifang(int a){
 if(a<0){
  printf("负数无法开方");
  return 65535.0f;
 }
 double x=1.0f,y=0.0f,k=0.0f;
 y=x*x-a;
 while(abs(y)>=0.001f){
  k=2*x;
  x=x-(y*1.0f)/k;
  y=x*x-a;
 }
 return x;
}
int main(){
 printf("请输入一个非负整数");
 int a=0;
 scanf("%d",&a);
 double c=kaifang(a);
 if(c==65535.0f){
  printf("输入有误"); 
 }else{
  printf("a的算术平方根是%f和-%f",c,c);
 }
 return 0;
}
测试结果如下

 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号