我正在復制粘貼代碼,由于某種原因,此代碼在復制粘貼后不起作用?在最后一個輸入而不是給陣列的值上,它會更改 for 回圈的值。它似乎只有在包含所有其余代碼時才有效,但這對我來說毫無意義。幫助!
#include <stdio.h>
#include <math.h>
int main(void){
int verts[7];
int shapesquare;
int shapequad;
float AB;
float BC;
float CD;
float DA;
float area;
float angleABC;
float angleBCD;
float angleCDA;
float angleDAB;
int i;
char Yes;
start:
printf("Shape checker\n");
for (i = 0; i < 8; i ){
if(i == 0){
printf("\nWhat is the x cord of vertice A?\n");
}
if(i == 1){
printf("\nWhat is the y cord of vertice A?\n");
}
if(i == 2){
printf("\nWhat is the x cord of vertice B?\n");
}
if(i == 3){
printf("\nWhat is the y cord of vertice B?\n");
}
if(i == 4){
printf("\nWhat is the x cord of vertice C?\n");
}
if(i == 5){
printf("\nWhat is the y cord of vertice C?\n");
}
if(i == 6){
printf("\nWhat is the x cord of vertice D?\n");
}
if(i == 7){
printf("\nWhat is the y cord of vertice D?\n");
}
scanf( "%d", &verts[i]);
printf("%d",i);
}
AB = sqrt(pow(verts[2]-verts[0],2) pow(verts[3]-verts[1],2));
BC = sqrt(pow(verts[4]-verts[2],2) pow(verts[5]-verts[3],2));
CD = sqrt(pow(verts[6]-verts[4],2) pow(verts[7]-verts[5],2));
DA = sqrt(pow(verts[0]-verts[6],2) pow(verts[1]-verts[7],2));
angleABC = acos((((verts[0]-verts[2])*(verts[4]-verts[2])) ((verts[1]-verts[3])*(verts[5]-verts[3])))/(AB*BC));
angleBCD = acos((((verts[2]-verts[4])*(verts[6]-verts[4])) ((verts[3]-verts[5])*(verts[7]-verts[5])))/(BC*CD));
angleCDA = acos((((verts[4]-verts[6])*(verts[0]-verts[6])) ((verts[5]-verts[7])*(verts[1]-verts[7])))/(CD*DA));
angleDAB = acos((((verts[6]-verts[0])*(verts[2]-verts[0])) ((verts[7]-verts[1])*(verts[3]-verts[1])))/(AB*DA));
if((ceil(10000*angleABC) == 15708)&&(ceil(10000*angleBCD) == 15708)&&(ceil(10000*angleCDA) == 15708)&&(ceil(10000*angleDAB) == 15708)){
shapesquare = 1;
shapequad = 0 ;
}else{
shapequad = 1;
shapesquare = 0;
}
if(shapesquare == 1 && AB == BC && BC == CD && DA == AB){
printf("Your shape is a square\n");
}else{if(shapequad == 0){
printf("Your shape is a rectangle\n");
}
}
if(shapequad == 1 && ceil(10000*angleABC) == ceil(10000*angleCDA) && ceil(10000*angleBCD) == ceil(10000*angleDAB)){
if(AB == BC && BC == CD && DA == AB){
printf("Your shape is a diamond\n");
}else{
printf("Your shape is a parallelogram\n");
}
}else{if(shapesquare == 0){
printf("Your shape is a quadrilateral\n");
}
}
if(shapesquare == 1){
area = AB * BC;
printf("The area is %f \n", area);
}
printf("\n\nWould you like to do another calculation? \n Y for continue \n");
scanf(" %s", &Yes);
if(Yes == 'Y'){
goto start;
}
printf("goodbye!");
return 0;
}
輸出 形狀檢查器
頂點 A 的 x 線是什么?3 0
頂點 A 的 y 線是多少?3 1
頂點 B 的 x 線是多少?3 2
頂點 B 的 y 線是多少?3 3
頂點 C 的 x 線是什么?3 4
頂點 C 的 y 線是多少?3 5
頂點 D 的 x 線是多少?3 6
頂點 D 的 y 線是多少?3 3
頂點 C 的 x 線是什么?3 4
頂點 C 的 y 線是多少?3 5
頂點 D 的 x 線是多少?3 6
頂點 D 的 y 線是多少?3 3
頂點 C 的 x 線是什么?3
uj5u.com熱心網友回復:
你有
int verts[7];
和
for (i = 0; i < 8; i ){
....
scanf( "%d", &verts[i]);
您不能在 7 個條目的陣列中容納 8 個數字。更改陣列大小或 for 回圈數
verts[7]創建一個陣列,該陣列中有七個插槽。他們是
verts[0]
verts[1]
verts[2]
verts[3]
verts[4]
verts[5]
verts[6]
數一數,有 7 個插槽,編號為 0 到 6。您的回圈嘗試分配給 verts[7]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457188.html
標籤:C
下一篇:CSV檔案中用于初始化結構的字串
