#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <cstdlib>
using namespace std;
#include "bcio2.h"
int error, x;
char totalimpulse[80], averageimpulse[80], ejection[50], emptymass[50], enginemass[50], fuelmass[50];
char launch[50];
void validate_number();
int main(void)
{
clrscr();
do{
printf("\nTotal Impulse delivered: ");
gets(totalimpulse);
validate_number();
} while (error != 0);
do{
printf("\nAverage Impulse delivered: ");
gets(averageimpulse);
validate_number();
} while (error != 0);
do{
printf("\nTime that ejection charge fires: ");
gets(ejection);
validate_number();
} while (error != 0);
do{
printf("\nThe mass of the empty vehicle: ");
gets(emptymass);
validate_number();
} while (error != 0);
do{
printf("\nThe mass of the engine: ");
gets(enginemass);
validate_number();
} while (error != 0);
do{
printf("\nThe mass of fuel: ");
gets(fuelmass);
validate_number();
} while (error != 0);
printf("\nRocket parameters entered: \n");
printf("\nTotal Impulse delivered: %s\n", totalimpulse);
printf("Average Impulse delivered: %s\n", averageimpulse);
printf("Time that ejection charge fires: %s\n", ejection);
printf("The mass of the empty vehicle: %sg\n", emptymass);
printf("The mass of the engine: %sg\n", enginemass);
printf("The mass of fuel: %sg\n", fuelmass);
char ans;
do
{
cout<< "\nLaunch simulation? (Y/N) : \n";
cout<< "You must type a 'Y' or an 'N'.\n";
cin >> ans;
}
while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));
if (ans == 'N')
{
getchar();
return 0;
}
else if (ans == 'Y')
{
// do something, calculations display page
return 0;
}
}
/************ Test all input in range 0 to 9 ****************/
void validate_number()
{
int errange = 0, numonly = 0, errlength = 0;
/********* validate numbers ********/
error = 0;
for (x = 0; x<strlen(totalimpulse); x++){
if (totalimpulse[x] >= '0' && totalimpulse[x] <= '9'); else{
numonly++;
error++;
}
for (x = 0; x<strlen(averageimpulse); x++){
if (averageimpulse[x] >= '0' && averageimpulse[x] <= '9'); else{
numonly++;
error++;
}
for (x = 0; x<strlen(ejection); x++){
if (ejection[x] >= '0' && ejection[x] <= '9'); else{
numonly++;
error++;
}
for (x = 0; x<strlen(emptymass); x++){
if (emptymass[x] >= '0' && emptymass[x] <= '9'); else{
numonly++;
error++;
}
for (x = 0; x<strlen(enginemass); x++){
if (enginemass[x] >= '0' && enginemass[x] <= '9'); else{
numonly++;
error++;
}
for (x = 0; x<strlen(fuelmass); x++){
if (fuelmass[x] >= '0' && fuelmass[x] <= '9'); else{
numonly++;
error++;
}
}
/********* validate range ********/
if (strlen(totalimpulse) <= 3){
x = atoi(totalimpulse);
if (x >= 0 && x <= 256)
;
else{
errange++;
error++;
}
}
else{
errlength++;
error++;
}
/**************** Report errors ******************/
if (error != 0){
if (numonly>0)
printf("\nOnly values in the range 0 to 9 are valid ");
if (errange>0)
printf("\nValue must be in the range 0 to 255");
if (errlength>0)
printf("\nMore than three values were entered");
}
我已经计算了21个开口矫正器和21个闭合矫正器,它们似乎都正确定位但我仍然得到错误,任何一双新眼睛可以帮助我吗?我已经多次计算括号,因为我疯了。
这是基本问题的基本错误,但我对于要更改或添加的内容一无所知。
答案 0 :(得分:1)
代码中有27个开括号。
[11:05am][wlynch@watermelon ~] fgrep -o '{' /tmp/asd.cc | wc -l
27
[11:05am][wlynch@watermelon ~] fgrep -o '}' /tmp/asd.cc | wc -l
21
值得注意的是,这段代码中有两个开括号,但只有一个右括号:
for (x = 0; x<strlen(totalimpulse); x++){
if (totalimpulse[x] >= '0' && totalimpulse[x] <= '9'); else{
numonly++;
error++;
}
此外,这不是无效的,但写得非常好:
if (averageimpulse[x] >= '0' && averageimpulse[x] <= '9'); else{
请将其写为:
if (not (averageimpulse[x] >= '0' && averageimpulse[x] <= '9')) {
或:
if (averageimpulse[x] < '0' || averageimpulse[x] > '9')) {