- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图创建一个输入函数,它接受用逗号分隔的内容并将每个内容放入它自己的数组中。我想我几乎已经弄清楚了这个开关,但它卡在了第二个语句中。我不知道为什么。
/*************************************************************************
3/25/2015
This program takes in a file of the format
PART,2.000,-1,0.050,V
PART,0.975,-1,0.025,V
PART,3.000,+1,0.010,F
GAP,0.000,0.080
does the tolerance analysis
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define BUFFER_SIZE 1024
#define ARRAYSIZE 100
void input(char *gapPart, float *nom,float *tollerance,int *SIGNS,char *V_F,float Spec_Minnimum,float Spec_Maximum);
void toleracningPt1(int size, char gapPart[], float nom[],float tollerance[],int SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum);
int main(){
/**Decs**/
float nom[ARRAYSIZE]; //holds the nominal values (2.00, .975 ect)
float tollerance[ARRAYSIZE]; //holds the third value (.05, .025, ect)
int SIGNS[ARRAYSIZE]; // signifies if the value goes up or down
char gapPart[ARRAYSIZE];// holds the value if it's a gap or part
char V_F[ARRAYSIZE]; // F things cannot be changed, V things can be
int size=0;
float Spec_Minnimum=0, Spec_Maximum=0;
/**custom functions**/
input(gapPart, nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
toleracningPt1(size, gapPart,nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
return 0;
}
/***********************************************************************************************************/
void input(char *gapPart,float *nom,float *tollerance,int *SIGNS,char *V_F,float Spec_Minnimum,float Spec_Maximum){
const char *delimiterCharacters = " ";
const char *delimiterCharacters2 = ",";
const char *filename = "tin.txt";
FILE *inputFile = fopen( filename, "r" );
char buffer[ BUFFER_SIZE ];
char *lastToken;
int i=1, step;
printf("File Data\n");
/* usual error check*/
if(inputFile == NULL ){
fprintf( stderr, "Unable to open file %s\n", filename );
}else{
/**Prints out contents of the file **/
while( fgets(buffer, BUFFER_SIZE,inputFile) != NULL ){// while there is stuff to do this with
lastToken = strtok( buffer, delimiterCharacters );
while( lastToken != NULL ){//same song..
printf( "%s\n", lastToken );
lastToken = strtok( NULL, delimiterCharacters );// clear out lastToken
}
}
rewind(inputFile);
while( fgets(buffer, BUFFER_SIZE,inputFile) != NULL ){// while there is stuff to do this with
lastToken = strtok( buffer, delimiterCharacters2 );
while( lastToken != NULL ){//same song..
//strtok into seperate arrays
while(i=1,i<5,++i){
switch(i){
case 1:
fscanf(inputFile,"%s\n", &gapPart[i]);
printf("debug1");
++i;
break;
case 2:
printf("debug2");
fscanf(inputFile,"%f\n", &nom[i]);
++i;
break;
case 3:
printf("debug3");
fscanf(inputFile,"%d\n", &SIGNS[i]);
++i;
break;
case 4:
printf("debug4");
fscanf(inputFile,"%f\n", &tollerance[i]);
break;
case 5:
printf("debug5");
fscanf(inputFile,"%c\n", &V_F[i]);
break;
default:
printf("Error");
}
}
}
lastToken = strtok( NULL, delimiterCharacters2 );// clear out lastToken
}
}
fclose(inputFile );
}
/*****************************************************************************************************************/
void toleracningPt1(int size, char gapPart[], float nom[],float tollerance[],int SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum)
{
int x;
float Act_Gap, Act_Tollerance, Maximum_Gap = 0.0, Minnimum_Gap = 0.0;
for ( x=0, Act_Gap = 0; x<size; x++){ //does tolerance math
Act_Gap = Act_Gap + (nom[x]*SIGNS[x]);
}
for ( x=0, Act_Tollerance = 0; x<size; x++){
Act_Tollerance = Act_Tollerance + (tollerance[x]);
}
for (x= 0, Maximum_Gap = 0; x<size; x++){
Maximum_Gap = (nom[x]*SIGNS[x]+tollerance[x])+Maximum_Gap;
Minnimum_Gap = (nom[x]*SIGNS[x]-tollerance[x])+Minnimum_Gap;
}
printf("Actual Gap Mean: %.3f\"\n", Act_Gap); //printing
printf("Actual Gap Tolerance: %.3f\"\n", Act_Tollerance);
if (Maximum_Gap > Spec_Maximum){
printf("The maximum gap (%.3f\") is (Greater) than specified (%.3f\")\n", Maximum_Gap, Spec_Maximum);
}
if (Maximum_Gap < Spec_Maximum){
printf("The maximum gap (%.3f\") is (Less) than specified (%.3f\")\n", Maximum_Gap, Spec_Maximum);
}
if (Minnimum_Gap > Spec_Minnimum){
printf("The minimum gap (%.3f\") is (Greater) than specified (%.3f\")\n", Minnimum_Gap, Spec_Minnimum);
}
if (Minnimum_Gap < Spec_Minnimum){
printf("The minimum gap (%.3f\") is (Less) than specified (%.3f\")\n", Minnimum_Gap, Spec_Minnimum);
}
}
最佳答案
您的问题是由于使用
while(i=1,i<5,++i)
而不是
for(i=1;i<5;++i)
它的工作原理如下:
for()
循环中,初始化只进入一次 => i=1
仅发生一次。i>=5
表示退出循环;每次都会检查这一点。++i
在每次迭代执行循环体之后发生。当你做 while(i=1,i<5,++i)
时, while()
内的所有 3 个操作在循环的每次迭代中按顺序发生,并且在开始时发生。
编辑:
移动线
lastToken = strtok(NULL, delimiterCharacters2);// clear out lastToken
当前位置上方有一个括号。
由于lastToken永远不会改变,因此while循环不会退出,因此会一次又一次地循环内容。
解决了代码中的错误并添加了以下代码。请使用像 winmerge 这样的 diff 实用程序来了解每个更改:
/*************************************************************************
3/25/2015
This program takes in a file of the format
PART,2.000,-1,0.050,V
PART,0.975,-1,0.025,V
PART,3.000,+1,0.010,F
GAP,0.000,0.080
does the tolerance analysis
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
#define BUFFER_SIZE 1024
#define ARRAYSIZE 100
int input(char *gapPart, float *nom, float *tollerance, int *SIGNS, char *V_F, float *Spec_Minnimum, float *Spec_Maximum);
void toleracningPt1(int size, char gapPart[], float nom[], float tollerance[], int SIGNS[], char V_F[], float *Spec_Minnimum, float *Spec_Maximum);
char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;/* Count how many elements will be extracted. */
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token.*/
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller knows where the list of returned strings ends.*/
count++;
result = (char **)malloc(sizeof(char*)* count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = _strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
int main(){
float nom[ARRAYSIZE]; //holds the nominal values (2.00, .975 ect)
float tollerance[ARRAYSIZE]; //holds the third value (.05, .025, ect)
int SIGNS[ARRAYSIZE]; // signifies if the value goes up or down
char gapPart[ARRAYSIZE];// holds the value if it's a gap or part
char V_F[ARRAYSIZE]; // F things cannot be changed, V things can be
int size = 0;
float Spec_Minnimum = 0, Spec_Maximum = 0;
size = input(gapPart, nom, tollerance, SIGNS, V_F, &Spec_Minnimum, &Spec_Maximum);
toleracningPt1(size, gapPart, nom, tollerance, SIGNS, V_F, &Spec_Minnimum, &Spec_Maximum);
getchar();
return 0;
}
void printAllTokens(char** tokens){
int j;
for (j = 0; *(tokens + j); j++){
printf("token=[%s]\n", *(tokens + j));
}
}
void freeTokens(char** tokens){
int j;
for (j = 0; *(tokens + j); j++){
free(*(tokens + j));
}
}
int input(char *gapPart, float *nom, float *tollerance, int *SIGNS, char *V_F, float *Spec_Minnimum, float *Spec_Maximum)
{
char delimiter = ',';
const char *filename = "tin.txt";
char buffer[BUFFER_SIZE];
char** tokens;
int i = 0, j = 0;
FILE *inputFile = fopen(filename, "r");
printf("File Data\n");
/* usual error check*/
if (inputFile == NULL){
fprintf(stderr, "Unable to open file %s\n", filename);
}
else{
while (fgets(buffer, BUFFER_SIZE, inputFile) != NULL){// while there is stuff to do this with
strtok(buffer, "\n");
tokens = str_split(buffer, delimiter);
printAllTokens(tokens);
if (strstr(buffer, "GAP") != NULL) {
*Spec_Minnimum = strtof(*(tokens + 1),NULL);
*Spec_Maximum = strtof(*(tokens + 2), NULL);
}
else{
if (tokens){
for (j = 0; *(tokens + j); j++){
switch (j){
case 0: strcpy(*(tokens + j), &gapPart[i]); break;
case 1: nom[i] = strtof(*(tokens + j), NULL); break;
case 2: SIGNS[i] = atoi(*(tokens + j)); break;
case 3: tollerance[i] = strtof(*(tokens + j), NULL);break;
case 4: V_F[i] = (char)*(tokens + j); break;
default: printf("Error");
}
}
i++;
}
}
printf("\n");
freeTokens(tokens);
free(tokens);
}
}
fclose(inputFile);
return i;
}
void toleracningPt1(int size, char gapPart[], float nom[], float tollerance[], int SIGNS[], char V_F[], float *Spec_Minnimum, float *Spec_Maximum)
{
int x;
float Act_Gap = 0, Act_Tollerance = 0, Maximum_Gap = 0.0, Minnimum_Gap = 0.0;
for (x = 0, Act_Gap = 0; x<size; x++){
Act_Gap = Act_Gap + (nom[x] * SIGNS[x]);
Act_Tollerance = Act_Tollerance + (tollerance[x]);
Maximum_Gap = (nom[x] * SIGNS[x] + tollerance[x]) + Maximum_Gap;
Minnimum_Gap = (nom[x] * SIGNS[x] - tollerance[x]) + Minnimum_Gap;
}
printf("\nActual Gap Mean: %.3f\"\n", Act_Gap);
printf("Actual Gap Tolerance: %.3f\"\n", Act_Tollerance);
printf("The maximum gap (%.3f\") is (", Maximum_Gap);
printf(Maximum_Gap > *Spec_Maximum ? "Greater than" : (Maximum_Gap < *Spec_Maximum ? "Less than" : "Equal to"));
printf(") than specified (%.3f\")\n", *Spec_Maximum);
printf("The minimum gap (%.3f\") is (", Minnimum_Gap);
printf(Minnimum_Gap > *Spec_Minnimum ? "Greater than" : (Minnimum_Gap < *Spec_Minnimum ? "Less than" : "Equal to"));
printf(") than specified (%.3f\")\n", *Spec_Minnimum);
}
间隙计算的输出:
关于c - 我的开关盒出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29337675/
我想要 angularJs 中的一个按钮,当我按下它时会调用一个函数,当我再次按下它时它会执行另一个功能,比如 ON-OFF 开关。 我有这个: 我会在单击一次时调用另一个函数。 最佳答案 您可
我有一个相当大的 switch 语句(或同样大的 if 语句),我想在 Doxygen 中记录一些方法,可能是调用者图,或者可能是某种流程图格式。 我怎样才能做到这一点? 谢谢 ! 最佳答案 通常 d
public int[] Level1Items(int floor) { switch (floor) { case 0: case 1:
我有一些R代码看起来基本上是这样的: compute.quantiles <- function(mu, type) { ## 'mu' and 'type' are vectors of the
嗨,我正在尝试处理 ajax json 响应 这是我的代码 success: function (j) { switch(true) { case (j.cho
我在尝试在 Javascript 中进行切换时遇到问题,当切换激活时,根据情况,程序将显示许多新按钮,您可以单击这些按钮并播放声音。 这是我的 HTML 和 Javascript,带有第一组按钮: f
static double rSetzen(){ double r; System.out.println("Sind sie männlich oder weiblich?");
作为我用 Haskell 编写的迷你解释器的一部分,我正在编写一个执行以下操作的函数:如果是 eval (App e1 e2) ,我想递归评估 e1 ( eval e1 ),将结果设置为 v1 .然后
对于C语言,我知道我很接近,但是对于输入的任何字符,打印输出都是“辅音”。我的 switch case 语句有什么问题。我需要 if 语句吗? #include #include int main
我有以下方法,它以类的类型作为参数: public void test(Type proType){ } 我目前有一个很大的 if else 看起来像: if(proType == typeof(Cl
我正在编写带有开关功能的代码。我希望我的默认代码打印“错误”并使程序停止运行。我应该在默认值末尾添加 return 1; 吗? 如果是这样,这样对吗? switch (course3) { c
VB.NET,但 C# 也可以。 我有一个 MustInherit 基类和 170 个基于它的继承类。为什么这么多?因为每个继承的类在 Sub New() 中做了不同的事情。继承的类型不添加任何新属性
在我的开关中,我希望案例从 0 变为 (number_of_cases-1),而无需自己编写数字。因此,如果我在中间删除一个 case block ,则以下 case 将重新编号(减 1),以便再次从
我想在我的应用中使用精确的 Material 开关。正是谷歌设计指南中的那些。 https://www.google.com/design/spec/components/selection-cont
有没有比我使用的更好的方法来实现切换/开关。它有效,但对我来说似乎很笨拙。 var foo = true; $(document).on("click","$element",function(){
我尝试将全局变量与 switch 语句一起使用,但变量的值没有改变。也许是范围问题? 我需要一个 switch 语句来逐一循环遍历每种情况,但每次我在控制台中查看时,“i”变量都会重置为 1。 为什么
我最近在一个网站上工作,在本地主机上一切正常,但是当我将它移到服务器上时,更改 div 函数开始加载怪异,我不知道为什么。当您循环浏览投资组合按钮和联系人按钮时,该错误会更加明显。 该网站仅针对 Fi
我一直在开发一个使用 switch() 函数的项目,如下所示: switch (selectedMenu) { case 1: switch (selectedIndex) {
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Variable declaration in c# switch statement 我一直想知道: 当我
我有一个奇怪的问题,我似乎无法解决。我有相当复杂的代码,但我已经简化了它,问题仍然存在。 请参阅以下内容: 'correct'); switch (true) { case empty($m
我是一名优秀的程序员,十分优秀!