我有用C编写的代码,但它返回运行时错误,显示STANDARD INPUT MISSING,而且它显示变量x,y,z不能接受整数值,因为它们需要加倍。这是我的代码:
#include <stdio.h>;
#include<stdlib.h>;
int main()
{
double r,H;
int i;
int a=5;
int b=10;
int t=5;
int I=10;
int y;
double x,z;
FILE *fp;
fp=fopen("Graph.xlsx" ,"w");
for(i=0;i<=1000;i++){
r=(.015*i);
if(r>=0 && r<5){
H=(I*r)/(2*3.14*a*a);
fprintf(fp,"%f",r);
fprintf(fp,"\t");
fprintf(fp,"%f",H);
fprintf(fp,"\n");
}
else if(r>=5 && r<10){
H=I/(2*3.14*r);
fprintf(fp,"%f",r);
fprintf(fp,"\t");
fprintf(fp,"%f",H);
fprintf(fp,"\n");
}
else if(r>=10 && r<15){
x=(r*r- b*b);
y=(t*t+2*b*t);
z= 1 - x / y;
H=z*I/(2*3.14*r);
fprintf(fp,"%f",r);
fprintf(fp,"\t");
fprintf(fp,"%f",H);
fprintf(fp,"\n");
}
else
H=0;
}
fclose(fp);
return 0;
}
答案 0 :(得分:1)
根本的错误是认为MS Excel可以打开文本文件并将其作为格式正确的.xlsx
文件。 Excel接受的文本格式通常称为.csv
或“逗号分隔变量”文件。所以我用逗号替换了你的tab
字符(并简化了每个打印)。当我双击Graph.csv
时,它会正确运行带有数据的Excel。
#include <stdio.h> // removed trailing ;
#include <stdlib.h> // removed trailing ;
int main()
{
double r,H;
int i;
int a=5;
int b=10;
int t=5;
int I=10;
int y;
double x,z;
FILE *fp;
fp=fopen("Graph.csv" ,"wt"); // open a a CSV file in text mode
if(fp == NULL) {
exit(1); // added error checking
}
for(i=0;i<=1000;i++){
r = 0.015 * i;
if(r>=0 && r<5){
H=(I*r)/(2*3.14*a*a);
fprintf(fp,"%f,%f\n", r, H); // simplified to a single statement ...
}
else if(r>=5 && r<10){
H=I/(2*3.14*r);
fprintf(fp,"%f,%f\n", r, H); // ... with a comma instead of a tab ...
}
else if(r>=10 && r<15){
x=(r*r- b*b);
y=(t*t+2*b*t);
z= 1 - x / y;
H=z*I/(2*3.14*r);
fprintf(fp,"%f,%f\n", r, H); // ... same thing here too
}
else
H=0;
}
fclose(fp);
return 0;
}