gpt4 book ai didi

c - 包含 Math.h 会导致 cmath 出现错误

转载 作者:行者123 更新时间:2023-11-30 20:08:30 26 4
gpt4 key购买 nike

在我的包含中,我使用了 #include ,但是当我尝试编译和运行该程序时,它给了我一长串语法错误。 Visual Studio 告诉我错误位于文件 cmath 中,我没有在程序中包含或使用该文件

程序在 C++ 中运行良好,但在 C 中运行不佳

这是我的全部内容:

#include <iostream>
#include <stdio.h>
#include <math.h>

这是我使用 math.h 中任何类型的数学的唯一地方:

double compoundinterest(double principle, double interest, double years) {
float a = 1 + (interest);
float b = pow(a, years);
return principle * b;

错误输出日志:

1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\cstdlib(20,51): error C2061:  syntax error: identifier 'noexcept'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\cstdlib(20,51): error C2059: syntax error: ';'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\cstdlib(20,60): error C2449: found '{' at file scope (missing function header?)

(编辑:删除了大部分错误,因为它们非常相似并且占用了太多空间)

整个程序:

#include <iostream>
#include <stdio.h>
#include <math.h>

int leapyears(int year);
int numberofdays(int month, int day, int year);
int monthlength(int month, int year);
double compoundinterest(double principle, double interest, double years);
double simpleinterest(double principle, double interest, double years);

int main(void) {
int option = 5;
int month, day, year;
printf("option 0: exit program\n");
printf("option 1: Calculate and display the day of the week of a given date\n");
printf("option 2: Calculate and display the principal on a savings account after a given number of years for compounded interest.\n");
printf("option 3: Calculate and display the principal on a savings account after a given number of years for simple interest\n");
while (option != 0) {
int days, week;
double p, i, y;
printf("enter an option: ");
scanf("%d", &option);
switch (option) {
case 0:
printf("goodbye\n");
break;
case 1:
printf("enter the date in the format month day year in numerical form: \n");
scanf(" %d %d %d", &month, &day, &year);
days = numberofdays(month, day, year);
week = days % 7;
switch (week) {
case 1:
printf("tuesday\n");
break;
case 2:
printf("wednesday\n");
break;
case 3:
printf("thursday\n");
break;
case 4:
printf("friday\n");
break;
case 5:
printf("saturday\n");
break;
case 6:
printf("sunday\n");
break;
default:
printf("monday\n");
break;
}

break;
case 2:
printf("Enter the initial principle, interest rate(as a decimal), and number of years for compound interest: \n");
scanf(" %lf%lf%lf", &p, &i, &y);
printf("Your principle would be valued at %lf\n", compoundinterest(p, i, y));
break;
case 3:
printf("Enter the initial principle, interest rate(as a decimal), and number of years for simple interest: \n");
scanf(" %lf%lf%lf", &p, &i, &y);
printf("Your principle would be valued at %lf\n", simpleinterest(p, i, y));
break;
default:
printf("invalid entry\n");
break;
}
}

return 0;
}

int leapyears(int year) {
if ((year % 4) == 0) {
if ((year % 100) != 0) return 1;
if ((year % 100) == 0) {
if ((year % 400) == 0) {
return 1;
}
else return 0;
}
}
else return 0;
}

int monthlength(int month, int year) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31;
if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
if (leapyears(year) == 1 && month == 2) {
return 29;
}
if (leapyears(year) == 0 && month == 2) {
return 28;
}
}

int numberofdays(int month, int day, int year) {
day -= 1;
while (year != 0001) {
if (leapyears(year) == 1) {
day += 366;
}
else{
day += 365;
}
year -= 1;
}
while (month != 1) {
month -= 1;
day += monthlength(month, year);
}
return day;
}

double compoundinterest(double principle, double interest, double years) {
float a = 1 + (interest);
float b = pow(a, years);
return principle * b;
}
double simpleinterest(double principle, double interest, double years) {
return principle * (1 + (years * interest));
}

最佳答案

您正在 C 文件中包含 C++ header 。如果删除 #include <iostream>行,它应该修复这些错误。

关于c - 包含 Math.h 会导致 cmath 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com