包含 Math.h 会导致 cmath 出错
Including Math.h results in errors in cmath
在我的包含中,我使用了 #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 Studio19\Community\VC\Tools\MSVC.21.27702\include\cstdlib(20,51): error C2061: syntax error: identifier 'noexcept'
1>C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\cstdlib(20,51): error C2059: syntax error: ';'
1>C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\cstdlib(20,60): error C2449: found '{' at file scope (missing function header?)
(编辑:删除了大部分错误,因为它们非常相似并且占用了太多space)
整个程序:
#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++ 头文件。如果删除 #include <iostream>
行,它应该会修复这些错误。
在我的包含中,我使用了 #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 Studio19\Community\VC\Tools\MSVC.21.27702\include\cstdlib(20,51): error C2061: syntax error: identifier 'noexcept'
1>C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\cstdlib(20,51): error C2059: syntax error: ';'
1>C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\cstdlib(20,60): error C2449: found '{' at file scope (missing function header?)
(编辑:删除了大部分错误,因为它们非常相似并且占用了太多space)
整个程序:
#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++ 头文件。如果删除 #include <iostream>
行,它应该会修复这些错误。